LLVM  4.0.0
ConstantFolding.h
Go to the documentation of this file.
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 *
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.
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.
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.
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.
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.
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
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE)
ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a getelementptr constantexpr, return the constant value being addressed by the constant expression, or null if something is funny and we can't decide.
bool canConstantFoldCallTo(const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function...
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices...
bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL)
If this constant is a constant offset from a global, return the global and the constant.
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
#define F(x, y, z)
Definition: MD5.cpp:51
bool isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
uint32_t Offset
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCompareInstOperands - Attempt to constant fold a compare instruction (icmp/fcmp) with the...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
#define I(x, y, z)
Definition: MD5.cpp:54
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands...
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices...
Constant * ConstantFoldLoadThroughGEPIndices(Constant *C, ArrayRef< Constant * > Indices)
ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr indices (with an implied zero ...
Constant * ConstantFoldCall(Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.