LLVM API Documentation

InstCombine.h
Go to the documentation of this file.
00001 //===- InstCombine.h - Main InstCombine pass definition ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef INSTCOMBINE_INSTCOMBINE_H
00011 #define INSTCOMBINE_INSTCOMBINE_H
00012 
00013 #include "InstCombineWorklist.h"
00014 #include "llvm/Analysis/ValueTracking.h"
00015 #include "llvm/IR/IRBuilder.h"
00016 #include "llvm/IR/IntrinsicInst.h"
00017 #include "llvm/IR/Operator.h"
00018 #include "llvm/InstVisitor.h"
00019 #include "llvm/Pass.h"
00020 #include "llvm/Support/TargetFolder.h"
00021 #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
00022 
00023 namespace llvm {
00024   class CallSite;
00025   class DataLayout;
00026   class TargetLibraryInfo;
00027   class DbgDeclareInst;
00028   class MemIntrinsic;
00029   class MemSetInst;
00030 
00031 /// SelectPatternFlavor - We can match a variety of different patterns for
00032 /// select operations.
00033 enum SelectPatternFlavor {
00034   SPF_UNKNOWN = 0,
00035   SPF_SMIN, SPF_UMIN,
00036   SPF_SMAX, SPF_UMAX
00037   //SPF_ABS - TODO.
00038 };
00039 
00040 /// getComplexity:  Assign a complexity or rank value to LLVM Values...
00041 ///   0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
00042 static inline unsigned getComplexity(Value *V) {
00043   if (isa<Instruction>(V)) {
00044     if (BinaryOperator::isNeg(V) ||
00045         BinaryOperator::isFNeg(V) ||
00046         BinaryOperator::isNot(V))
00047       return 3;
00048     return 4;
00049   }
00050   if (isa<Argument>(V)) return 3;
00051   return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
00052 }
00053 
00054 
00055 /// InstCombineIRInserter - This is an IRBuilder insertion helper that works
00056 /// just like the normal insertion helper, but also adds any new instructions
00057 /// to the instcombine worklist.
00058 class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
00059     : public IRBuilderDefaultInserter<true> {
00060   InstCombineWorklist &Worklist;
00061 public:
00062   InstCombineIRInserter(InstCombineWorklist &WL) : Worklist(WL) {}
00063 
00064   void InsertHelper(Instruction *I, const Twine &Name,
00065                     BasicBlock *BB, BasicBlock::iterator InsertPt) const {
00066     IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
00067     Worklist.Add(I);
00068   }
00069 };
00070 
00071 /// InstCombiner - The -instcombine pass.
00072 class LLVM_LIBRARY_VISIBILITY InstCombiner
00073                              : public FunctionPass,
00074                                public InstVisitor<InstCombiner, Instruction*> {
00075   DataLayout *TD;
00076   TargetLibraryInfo *TLI;
00077   bool MadeIRChange;
00078   LibCallSimplifier *Simplifier;
00079   bool MinimizeSize;
00080 public:
00081   /// Worklist - All of the instructions that need to be simplified.
00082   InstCombineWorklist Worklist;
00083 
00084   /// Builder - This is an IRBuilder that automatically inserts new
00085   /// instructions into the worklist when they are created.
00086   typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
00087   BuilderTy *Builder;
00088 
00089   static char ID; // Pass identification, replacement for typeid
00090   InstCombiner() : FunctionPass(ID), TD(0), Builder(0) {
00091     MinimizeSize = false;
00092     initializeInstCombinerPass(*PassRegistry::getPassRegistry());
00093   }
00094 
00095 public:
00096   virtual bool runOnFunction(Function &F);
00097 
00098   bool DoOneIteration(Function &F, unsigned ItNum);
00099 
00100   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
00101 
00102   DataLayout *getDataLayout() const { return TD; }
00103 
00104   TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
00105 
00106   // Visitation implementation - Implement instruction combining for different
00107   // instruction types.  The semantics are as follows:
00108   // Return Value:
00109   //    null        - No change was made
00110   //     I          - Change was made, I is still valid, I may be dead though
00111   //   otherwise    - Change was made, replace I with returned instruction
00112   //
00113   Instruction *visitAdd(BinaryOperator &I);
00114   Instruction *visitFAdd(BinaryOperator &I);
00115   Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
00116   Instruction *visitSub(BinaryOperator &I);
00117   Instruction *visitFSub(BinaryOperator &I);
00118   Instruction *visitMul(BinaryOperator &I);
00119   Value *foldFMulConst(Instruction *FMulOrDiv, ConstantFP *C,
00120                        Instruction *InsertBefore);
00121   Instruction *visitFMul(BinaryOperator &I);
00122   Instruction *visitURem(BinaryOperator &I);
00123   Instruction *visitSRem(BinaryOperator &I);
00124   Instruction *visitFRem(BinaryOperator &I);
00125   bool SimplifyDivRemOfSelect(BinaryOperator &I);
00126   Instruction *commonRemTransforms(BinaryOperator &I);
00127   Instruction *commonIRemTransforms(BinaryOperator &I);
00128   Instruction *commonDivTransforms(BinaryOperator &I);
00129   Instruction *commonIDivTransforms(BinaryOperator &I);
00130   Instruction *visitUDiv(BinaryOperator &I);
00131   Instruction *visitSDiv(BinaryOperator &I);
00132   Instruction *visitFDiv(BinaryOperator &I);
00133   Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
00134   Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
00135   Instruction *visitAnd(BinaryOperator &I);
00136   Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS);
00137   Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
00138   Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op,
00139                                    Value *A, Value *B, Value *C);
00140   Instruction *visitOr (BinaryOperator &I);
00141   Instruction *visitXor(BinaryOperator &I);
00142   Instruction *visitShl(BinaryOperator &I);
00143   Instruction *visitAShr(BinaryOperator &I);
00144   Instruction *visitLShr(BinaryOperator &I);
00145   Instruction *commonShiftTransforms(BinaryOperator &I);
00146   Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
00147                                     Constant *RHSC);
00148   Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
00149                                             GlobalVariable *GV, CmpInst &ICI,
00150                                             ConstantInt *AndCst = 0);
00151   Instruction *visitFCmpInst(FCmpInst &I);
00152   Instruction *visitICmpInst(ICmpInst &I);
00153   Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
00154   Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
00155                                               Instruction *LHS,
00156                                               ConstantInt *RHS);
00157   Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
00158                               ConstantInt *DivRHS);
00159   Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
00160                               ConstantInt *DivRHS);
00161   Instruction *FoldICmpAddOpCst(ICmpInst &ICI, Value *X, ConstantInt *CI,
00162                                 ICmpInst::Predicate Pred, Value *TheAdd);
00163   Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
00164                            ICmpInst::Predicate Cond, Instruction &I);
00165   Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
00166                                    BinaryOperator &I);
00167   Instruction *commonCastTransforms(CastInst &CI);
00168   Instruction *commonPointerCastTransforms(CastInst &CI);
00169   Instruction *visitTrunc(TruncInst &CI);
00170   Instruction *visitZExt(ZExtInst &CI);
00171   Instruction *visitSExt(SExtInst &CI);
00172   Instruction *visitFPTrunc(FPTruncInst &CI);
00173   Instruction *visitFPExt(CastInst &CI);
00174   Instruction *visitFPToUI(FPToUIInst &FI);
00175   Instruction *visitFPToSI(FPToSIInst &FI);
00176   Instruction *visitUIToFP(CastInst &CI);
00177   Instruction *visitSIToFP(CastInst &CI);
00178   Instruction *visitPtrToInt(PtrToIntInst &CI);
00179   Instruction *visitIntToPtr(IntToPtrInst &CI);
00180   Instruction *visitBitCast(BitCastInst &CI);
00181   Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
00182                               Instruction *FI);
00183   Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*);
00184   Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
00185                             Value *A, Value *B, Instruction &Outer,
00186                             SelectPatternFlavor SPF2, Value *C);
00187   Instruction *visitSelectInst(SelectInst &SI);
00188   Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
00189   Instruction *visitCallInst(CallInst &CI);
00190   Instruction *visitInvokeInst(InvokeInst &II);
00191 
00192   Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
00193   Instruction *visitPHINode(PHINode &PN);
00194   Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
00195   Instruction *visitAllocaInst(AllocaInst &AI);
00196   Instruction *visitAllocSite(Instruction &FI);
00197   Instruction *visitFree(CallInst &FI);
00198   Instruction *visitLoadInst(LoadInst &LI);
00199   Instruction *visitStoreInst(StoreInst &SI);
00200   Instruction *visitBranchInst(BranchInst &BI);
00201   Instruction *visitSwitchInst(SwitchInst &SI);
00202   Instruction *visitInsertElementInst(InsertElementInst &IE);
00203   Instruction *visitExtractElementInst(ExtractElementInst &EI);
00204   Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
00205   Instruction *visitExtractValueInst(ExtractValueInst &EV);
00206   Instruction *visitLandingPadInst(LandingPadInst &LI);
00207 
00208   // visitInstruction - Specify what to return for unhandled instructions...
00209   Instruction *visitInstruction(Instruction &I) { return 0; }
00210 
00211 private:
00212   bool ShouldChangeType(Type *From, Type *To) const;
00213   Value *dyn_castNegVal(Value *V) const;
00214   Value *dyn_castFNegVal(Value *V, bool NoSignedZero=false) const;
00215   Type *FindElementAtOffset(Type *Ty, int64_t Offset,
00216                                   SmallVectorImpl<Value*> &NewIndices);
00217   Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
00218 
00219   /// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually
00220   /// results in any code being generated and is interesting to optimize out. If
00221   /// the cast can be eliminated by some other simple transformation, we prefer
00222   /// to do the simplification first.
00223   bool ShouldOptimizeCast(Instruction::CastOps opcode,const Value *V,
00224                           Type *Ty);
00225 
00226   Instruction *visitCallSite(CallSite CS);
00227   Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *TD);
00228   bool transformConstExprCastCall(CallSite CS);
00229   Instruction *transformCallThroughTrampoline(CallSite CS,
00230                                               IntrinsicInst *Tramp);
00231   Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
00232                                  bool DoXform = true);
00233   Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
00234   bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
00235   Value *EmitGEPOffset(User *GEP);
00236   Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
00237 
00238 public:
00239   // InsertNewInstBefore - insert an instruction New before instruction Old
00240   // in the program.  Add the new instruction to the worklist.
00241   //
00242   Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
00243     assert(New && New->getParent() == 0 &&
00244            "New instruction already inserted into a basic block!");
00245     BasicBlock *BB = Old.getParent();
00246     BB->getInstList().insert(&Old, New);  // Insert inst
00247     Worklist.Add(New);
00248     return New;
00249   }
00250 
00251   // InsertNewInstWith - same as InsertNewInstBefore, but also sets the
00252   // debug loc.
00253   //
00254   Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
00255     New->setDebugLoc(Old.getDebugLoc());
00256     return InsertNewInstBefore(New, Old);
00257   }
00258 
00259   // ReplaceInstUsesWith - This method is to be used when an instruction is
00260   // found to be dead, replacable with another preexisting expression.  Here
00261   // we add all uses of I to the worklist, replace all uses of I with the new
00262   // value, then return I, so that the inst combiner will know that I was
00263   // modified.
00264   //
00265   Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
00266     Worklist.AddUsersToWorkList(I);   // Add all modified instrs to worklist.
00267 
00268     // If we are replacing the instruction with itself, this must be in a
00269     // segment of unreachable code, so just clobber the instruction.
00270     if (&I == V)
00271       V = UndefValue::get(I.getType());
00272 
00273     DEBUG(errs() << "IC: Replacing " << I << "\n"
00274                     "    with " << *V << '\n');
00275 
00276     I.replaceAllUsesWith(V);
00277     return &I;
00278   }
00279 
00280   // EraseInstFromFunction - When dealing with an instruction that has side
00281   // effects or produces a void value, we can't rely on DCE to delete the
00282   // instruction.  Instead, visit methods should return the value returned by
00283   // this function.
00284   Instruction *EraseInstFromFunction(Instruction &I) {
00285     DEBUG(errs() << "IC: ERASE " << I << '\n');
00286 
00287     assert(I.use_empty() && "Cannot erase instruction that is used!");
00288     // Make sure that we reprocess all operands now that we reduced their
00289     // use counts.
00290     if (I.getNumOperands() < 8) {
00291       for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
00292         if (Instruction *Op = dyn_cast<Instruction>(*i))
00293           Worklist.Add(Op);
00294     }
00295     Worklist.Remove(&I);
00296     I.eraseFromParent();
00297     MadeIRChange = true;
00298     return 0;  // Don't do anything with FI
00299   }
00300 
00301   void ComputeMaskedBits(Value *V, APInt &KnownZero,
00302                          APInt &KnownOne, unsigned Depth = 0) const {
00303     return llvm::ComputeMaskedBits(V, KnownZero, KnownOne, TD, Depth);
00304   }
00305 
00306   bool MaskedValueIsZero(Value *V, const APInt &Mask,
00307                          unsigned Depth = 0) const {
00308     return llvm::MaskedValueIsZero(V, Mask, TD, Depth);
00309   }
00310   unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const {
00311     return llvm::ComputeNumSignBits(Op, TD, Depth);
00312   }
00313 
00314 private:
00315 
00316   /// SimplifyAssociativeOrCommutative - This performs a few simplifications for
00317   /// operators which are associative or commutative.
00318   bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
00319 
00320   /// SimplifyUsingDistributiveLaws - This tries to simplify binary operations
00321   /// which some other binary operation distributes over either by factorizing
00322   /// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this
00323   /// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is
00324   /// a win).  Returns the simplified value, or null if it didn't simplify.
00325   Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
00326 
00327   /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value
00328   /// based on the demanded bits.
00329   Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
00330                                  APInt& KnownZero, APInt& KnownOne,
00331                                  unsigned Depth);
00332   bool SimplifyDemandedBits(Use &U, APInt DemandedMask,
00333                             APInt& KnownZero, APInt& KnownOne,
00334                             unsigned Depth=0);
00335   /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
00336   /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
00337   Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
00338                                     APInt DemandedMask, APInt &KnownZero,
00339                                     APInt &KnownOne);
00340 
00341   /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
00342   /// SimplifyDemandedBits knows about.  See if the instruction has any
00343   /// properties that allow us to simplify its operands.
00344   bool SimplifyDemandedInstructionBits(Instruction &Inst);
00345 
00346   Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
00347                                     APInt& UndefElts, unsigned Depth = 0);
00348 
00349   // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
00350   // which has a PHI node as operand #0, see if we can fold the instruction
00351   // into the PHI (which is only possible if all operands to the PHI are
00352   // constants).
00353   //
00354   Instruction *FoldOpIntoPhi(Instruction &I);
00355 
00356   // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
00357   // operator and they all are only used by the PHI, PHI together their
00358   // inputs, and do the operation once, to the result of the PHI.
00359   Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
00360   Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
00361   Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
00362   Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
00363 
00364 
00365   Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
00366                         ConstantInt *AndRHS, BinaryOperator &TheAnd);
00367 
00368   Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
00369                             bool isSub, Instruction &I);
00370   Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
00371                          bool isSigned, bool Inside);
00372   Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
00373   Instruction *MatchBSwap(BinaryOperator &I);
00374   bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
00375   Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
00376   Instruction *SimplifyMemSet(MemSetInst *MI);
00377 
00378 
00379   Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
00380 
00381   /// Descale - Return a value X such that Val = X * Scale, or null if none.  If
00382   /// the multiplication is known not to overflow then NoSignedWrap is set.
00383   Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
00384 };
00385 
00386 
00387 
00388 } // end namespace llvm.
00389 
00390 #endif