]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/ConstantFolding.h
MFV r318947: 7578 Fix/improve some aspects of ZIL writing.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / ConstantFolding.h
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
23 namespace llvm {
24 class APInt;
25 template <typename T> class ArrayRef;
26 class CallSite;
27 class Constant;
28 class ConstantExpr;
29 class ConstantVector;
30 class DataLayout;
31 class Function;
32 class GlobalValue;
33 class Instruction;
34 class TargetLibraryInfo;
35 class Type;
36
37 /// If this constant is a constant offset from a global, return the global and
38 /// the constant. Because of constantexprs, this function is recursive.
39 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
40                                 const DataLayout &DL);
41
42 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
43 /// If successful, the constant result is returned, if not, null is returned.
44 /// Note that this fails if not all of the operands are constant.  Otherwise,
45 /// this function can only fail when attempting to fold instructions like loads
46 /// and stores, which have no constant expression form.
47 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
48                                   const TargetLibraryInfo *TLI = nullptr);
49
50 /// ConstantFoldConstant - Attempt to fold the constant using the
51 /// specified DataLayout.
52 /// If successful, the constant result is returned, if not, null is returned.
53 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
54                                const TargetLibraryInfo *TLI = nullptr);
55
56 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
57 /// specified operands.  If successful, the constant result is returned, if not,
58 /// null is returned.  Note that this function can fail when attempting to
59 /// fold instructions like loads and stores, which have no constant expression
60 /// form.
61 ///
62 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
63                                    const DataLayout &DL,
64                                    const TargetLibraryInfo *TLI = nullptr);
65
66 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
67 /// instruction (icmp/fcmp) with the specified operands.  If it fails, it
68 /// returns a constant expression of the specified operands.
69 ///
70 Constant *
71 ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
72                                 Constant *RHS, const DataLayout &DL,
73                                 const TargetLibraryInfo *TLI = nullptr);
74
75 /// \brief Attempt to constant fold a binary operation with the specified
76 /// operands.  If it fails, it returns a constant expression of the specified
77 /// operands.
78 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
79                                        Constant *RHS, const DataLayout &DL);
80
81 /// \brief Attempt to constant fold a cast with the specified operand.  If it
82 /// fails, it returns a constant expression of the specified operand.
83 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
84                                   const DataLayout &DL);
85
86 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
87 /// instruction with the specified operands and indices.  The constant result is
88 /// returned if successful; if not, null is returned.
89 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
90                                              ArrayRef<unsigned> Idxs);
91
92 /// \brief Attempt to constant fold an extractvalue instruction with the
93 /// specified operands and indices.  The constant result is returned if
94 /// successful; if not, null is returned.
95 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
96                                               ArrayRef<unsigned> Idxs);
97
98 /// \brief Attempt to constant fold an extractelement instruction with the
99 /// specified operands and indices.  The constant result is returned if
100 /// successful; if not, null is returned.
101 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
102
103 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
104 /// produce if it is constant and determinable.  If this is not determinable,
105 /// return null.
106 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
107
108 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
109 /// getelementptr constantexpr, return the constant value being addressed by the
110 /// constant expression, or null if something is funny and we can't decide.
111 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
112
113 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
114 /// indices (with an *implied* zero pointer index that is not in the list),
115 /// return the constant value being addressed by a virtual load, or null if
116 /// something is funny and we can't decide.
117 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
118                                             ArrayRef<Constant *> Indices);
119
120 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
121 /// the specified function.
122 bool canConstantFoldCallTo(const Function *F);
123
124 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
125 /// with the specified arguments, returning null if unsuccessful.
126 Constant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
127                            const TargetLibraryInfo *TLI = nullptr);
128
129 /// \brief Check whether the given call has no side-effects.
130 /// Specifically checks for math routimes which sometimes set errno.
131 bool isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI);
132 }
133
134 #endif