LLVM API Documentation

ConstantFolding.h
Go to the documentation of this file.
00001 //===-- ConstantFolding.h - Fold instructions into constants ----*- 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 // This file declares routines for folding instructions into constants when all
00011 // operands are constants, for example "sub i32 1, 0" -> "1".
00012 //
00013 // Also, to supplement the basic VMCore ConstantExpr simplifications,
00014 // this file declares some additional folding routines that can make use of
00015 // DataLayout information. These functions cannot go in VMCore due to library
00016 // dependency issues.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
00021 #define LLVM_ANALYSIS_CONSTANTFOLDING_H
00022 
00023 namespace llvm {
00024   class Constant;
00025   class ConstantExpr;
00026   class Instruction;
00027   class DataLayout;
00028   class TargetLibraryInfo;
00029   class Function;
00030   class Type;
00031   template<typename T>
00032   class ArrayRef;
00033 
00034 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
00035 /// If successful, the constant result is returned, if not, null is returned.
00036 /// Note that this fails if not all of the operands are constant.  Otherwise,
00037 /// this function can only fail when attempting to fold instructions like loads
00038 /// and stores, which have no constant expression form.
00039 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout *TD = 0,
00040                                   const TargetLibraryInfo *TLI = 0);
00041 
00042 /// ConstantFoldConstantExpression - Attempt to fold the constant expression
00043 /// using the specified DataLayout.  If successful, the constant result is
00044 /// result is returned, if not, null is returned.
00045 Constant *ConstantFoldConstantExpression(const ConstantExpr *CE,
00046                                          const DataLayout *TD = 0,
00047                                          const TargetLibraryInfo *TLI = 0);
00048 
00049 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
00050 /// specified operands.  If successful, the constant result is returned, if not,
00051 /// null is returned.  Note that this function can fail when attempting to
00052 /// fold instructions like loads and stores, which have no constant expression
00053 /// form.
00054 ///
00055 Constant *ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
00056                                    ArrayRef<Constant *> Ops,
00057                                    const DataLayout *TD = 0,
00058                                    const TargetLibraryInfo *TLI = 0);
00059 
00060 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
00061 /// instruction (icmp/fcmp) with the specified operands.  If it fails, it
00062 /// returns a constant expression of the specified operands.
00063 ///
00064 Constant *ConstantFoldCompareInstOperands(unsigned Predicate,
00065                                           Constant *LHS, Constant *RHS,
00066                                           const DataLayout *TD = 0,
00067                                           const TargetLibraryInfo *TLI = 0);
00068 
00069 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
00070 /// instruction with the specified operands and indices.  The constant result is
00071 /// returned if successful; if not, null is returned.
00072 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
00073                                              ArrayRef<unsigned> Idxs);
00074 
00075 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
00076 /// produce if it is constant and determinable.  If this is not determinable,
00077 /// return null.
00078 Constant *ConstantFoldLoadFromConstPtr(Constant *C, const DataLayout *TD = 0);
00079 
00080 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
00081 /// getelementptr constantexpr, return the constant value being addressed by the
00082 /// constant expression, or null if something is funny and we can't decide.
00083 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
00084 
00085 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
00086 /// indices (with an *implied* zero pointer index that is not in the list),
00087 /// return the constant value being addressed by a virtual load, or null if
00088 /// something is funny and we can't decide.
00089 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
00090                                             ArrayRef<Constant*> Indices);
00091 
00092 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
00093 /// the specified function.
00094 bool canConstantFoldCallTo(const Function *F);
00095 
00096 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
00097 /// with the specified arguments, returning null if unsuccessful.
00098 Constant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
00099                            const TargetLibraryInfo *TLI = 0);
00100 }
00101 
00102 #endif