ConstantFolding.h revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
1//===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===//
2//
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// This file declares routines for folding instructions into constants when all
11// operands are constants, for example "sub i32 1, 0" -> "1".
12//
13// Also, to supplement the basic VMCore ConstantExpr simplifications,
14// this file declares some additional folding routines that can make use of
15// DataLayout information. These functions cannot go in VMCore due to library
16// dependency issues.
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
21#define LLVM_ANALYSIS_CONSTANTFOLDING_H
22
23namespace llvm {
24  class Constant;
25  class ConstantExpr;
26  class Instruction;
27  class DataLayout;
28  class TargetLibraryInfo;
29  class Function;
30  class Type;
31  template<typename T>
32  class ArrayRef;
33
34/// ConstantFoldInstruction - Try to constant fold the specified instruction.
35/// If successful, the constant result is returned, if not, null is returned.
36/// Note that this fails if not all of the operands are constant.  Otherwise,
37/// this function can only fail when attempting to fold instructions like loads
38/// and stores, which have no constant expression form.
39Constant *ConstantFoldInstruction(Instruction *I,
40                                  const DataLayout *TD = nullptr,
41                                  const TargetLibraryInfo *TLI = nullptr);
42
43/// ConstantFoldConstantExpression - Attempt to fold the constant expression
44/// using the specified DataLayout.  If successful, the constant result is
45/// result is returned, if not, null is returned.
46Constant *ConstantFoldConstantExpression(const ConstantExpr *CE,
47                                         const DataLayout *TD = nullptr,
48                                         const TargetLibraryInfo *TLI =nullptr);
49
50/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
51/// specified operands.  If successful, the constant result is returned, if not,
52/// null is returned.  Note that this function can fail when attempting to
53/// fold instructions like loads and stores, which have no constant expression
54/// form.
55///
56Constant *ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
57                                   ArrayRef<Constant *> Ops,
58                                   const DataLayout *TD = nullptr,
59                                   const TargetLibraryInfo *TLI = nullptr);
60
61/// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
62/// instruction (icmp/fcmp) with the specified operands.  If it fails, it
63/// returns a constant expression of the specified operands.
64///
65Constant *ConstantFoldCompareInstOperands(unsigned Predicate,
66                                          Constant *LHS, Constant *RHS,
67                                          const DataLayout *TD = nullptr,
68                                          const TargetLibraryInfo *TLI=nullptr);
69
70/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
71/// instruction with the specified operands and indices.  The constant result is
72/// returned if successful; if not, null is returned.
73Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
74                                             ArrayRef<unsigned> Idxs);
75
76/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
77/// produce if it is constant and determinable.  If this is not determinable,
78/// return null.
79Constant *ConstantFoldLoadFromConstPtr(Constant *C,
80                                       const DataLayout *TD = nullptr);
81
82/// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
83/// getelementptr constantexpr, return the constant value being addressed by the
84/// constant expression, or null if something is funny and we can't decide.
85Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
86
87/// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
88/// indices (with an *implied* zero pointer index that is not in the list),
89/// return the constant value being addressed by a virtual load, or null if
90/// something is funny and we can't decide.
91Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
92                                            ArrayRef<Constant*> Indices);
93
94/// canConstantFoldCallTo - Return true if its even possible to fold a call to
95/// the specified function.
96bool canConstantFoldCallTo(const Function *F);
97
98/// ConstantFoldCall - Attempt to constant fold a call to the specified function
99/// with the specified arguments, returning null if unsuccessful.
100Constant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
101                           const TargetLibraryInfo *TLI = nullptr);
102}
103
104#endif
105