LLVM API Documentation
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, 00040 const DataLayout *TD = nullptr, 00041 const TargetLibraryInfo *TLI = nullptr); 00042 00043 /// ConstantFoldConstantExpression - Attempt to fold the constant expression 00044 /// using the specified DataLayout. If successful, the constant result is 00045 /// result is returned, if not, null is returned. 00046 Constant *ConstantFoldConstantExpression(const ConstantExpr *CE, 00047 const DataLayout *TD = nullptr, 00048 const TargetLibraryInfo *TLI =nullptr); 00049 00050 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the 00051 /// specified operands. If successful, the constant result is returned, if not, 00052 /// null is returned. Note that this function can fail when attempting to 00053 /// fold instructions like loads and stores, which have no constant expression 00054 /// form. 00055 /// 00056 Constant *ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, 00057 ArrayRef<Constant *> Ops, 00058 const DataLayout *TD = nullptr, 00059 const TargetLibraryInfo *TLI = nullptr); 00060 00061 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare 00062 /// instruction (icmp/fcmp) with the specified operands. If it fails, it 00063 /// returns a constant expression of the specified operands. 00064 /// 00065 Constant *ConstantFoldCompareInstOperands(unsigned Predicate, 00066 Constant *LHS, Constant *RHS, 00067 const DataLayout *TD = nullptr, 00068 const TargetLibraryInfo *TLI=nullptr); 00069 00070 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue 00071 /// instruction with the specified operands and indices. The constant result is 00072 /// returned if successful; if not, null is returned. 00073 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, 00074 ArrayRef<unsigned> Idxs); 00075 00076 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would 00077 /// produce if it is constant and determinable. If this is not determinable, 00078 /// return null. 00079 Constant *ConstantFoldLoadFromConstPtr(Constant *C, 00080 const DataLayout *TD = nullptr); 00081 00082 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a 00083 /// getelementptr constantexpr, return the constant value being addressed by the 00084 /// constant expression, or null if something is funny and we can't decide. 00085 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); 00086 00087 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr 00088 /// indices (with an *implied* zero pointer index that is not in the list), 00089 /// return the constant value being addressed by a virtual load, or null if 00090 /// something is funny and we can't decide. 00091 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, 00092 ArrayRef<Constant*> Indices); 00093 00094 /// canConstantFoldCallTo - Return true if its even possible to fold a call to 00095 /// the specified function. 00096 bool canConstantFoldCallTo(const Function *F); 00097 00098 /// ConstantFoldCall - Attempt to constant fold a call to the specified function 00099 /// with the specified arguments, returning null if unsuccessful. 00100 Constant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands, 00101 const TargetLibraryInfo *TLI = nullptr); 00102 } 00103 00104 #endif