LLVM  6.0.0svn
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 ImmutableCallSite;
35 class TargetLibraryInfo;
36 class Type;
37 
38 /// If this constant is a constant offset from a global, return the global and
39 /// the constant. Because of constantexprs, this function is recursive.
40 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
41  const DataLayout &DL);
42 
43 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
44 /// If successful, the constant result is returned, if not, null is returned.
45 /// Note that this fails if not all of the operands are constant. Otherwise,
46 /// this function can only fail when attempting to fold instructions like loads
47 /// and stores, which have no constant expression form.
48 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
49  const TargetLibraryInfo *TLI = nullptr);
50 
51 /// ConstantFoldConstant - Attempt to fold the constant using the
52 /// specified DataLayout.
53 /// If successful, the constant result is returned, if not, null is returned.
54 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
55  const TargetLibraryInfo *TLI = nullptr);
56 
57 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
58 /// specified operands. If successful, the constant result is returned, if not,
59 /// null is returned. Note that this function can fail when attempting to
60 /// fold instructions like loads and stores, which have no constant expression
61 /// form.
62 ///
63 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
64  const DataLayout &DL,
65  const TargetLibraryInfo *TLI = nullptr);
66 
67 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
68 /// instruction (icmp/fcmp) with the specified operands. If it fails, it
69 /// returns a constant expression of the specified operands.
70 ///
71 Constant *
73  Constant *RHS, const DataLayout &DL,
74  const TargetLibraryInfo *TLI = nullptr);
75 
76 /// \brief Attempt to constant fold a binary operation with the specified
77 /// operands. If it fails, it returns a constant expression of the specified
78 /// operands.
79 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
80  Constant *RHS, const DataLayout &DL);
81 
82 /// \brief Attempt to constant fold a select instruction with the specified
83 /// operands. The constant result is returned if successful; if not, null is
84 /// returned.
86  Constant *V2);
87 
88 /// \brief Attempt to constant fold a cast with the specified operand. If it
89 /// fails, it returns a constant expression of the specified operand.
90 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
91  const DataLayout &DL);
92 
93 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
94 /// instruction with the specified operands and indices. The constant result is
95 /// returned if successful; if not, null is returned.
97  ArrayRef<unsigned> Idxs);
98 
99 /// \brief Attempt to constant fold an extractvalue instruction with the
100 /// specified operands and indices. The constant result is returned if
101 /// successful; if not, null is returned.
103  ArrayRef<unsigned> Idxs);
104 
105 /// \brief Attempt to constant fold an insertelement instruction with the
106 /// specified operands and indices. The constant result is returned if
107 /// successful; if not, null is returned.
109  Constant *Elt,
110  Constant *Idx);
111 
112 /// \brief Attempt to constant fold an extractelement instruction with the
113 /// specified operands and indices. The constant result is returned if
114 /// successful; if not, null is returned.
116 
117 /// \brief Attempt to constant fold a shufflevector instruction with the
118 /// specified operands and indices. The constant result is returned if
119 /// successful; if not, null is returned.
121  Constant *Mask);
122 
123 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
124 /// produce if it is constant and determinable. If this is not determinable,
125 /// return null.
126 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
127 
128 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
129 /// getelementptr constantexpr, return the constant value being addressed by the
130 /// constant expression, or null if something is funny and we can't decide.
132 
133 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
134 /// indices (with an *implied* zero pointer index that is not in the list),
135 /// return the constant value being addressed by a virtual load, or null if
136 /// something is funny and we can't decide.
138  ArrayRef<Constant *> Indices);
139 
140 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
141 /// the specified function.
142 bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F);
143 
144 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
145 /// with the specified arguments, returning null if unsuccessful.
146 Constant *ConstantFoldCall(ImmutableCallSite CS, Function *F,
147  ArrayRef<Constant *> Operands,
148  const TargetLibraryInfo *TLI = nullptr);
149 
150 /// \brief Check whether the given call has no side-effects.
151 /// Specifically checks for math routimes which sometimes set errno.
152 bool isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI);
153 }
154 
155 #endif
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
uint64_t CallInst * C
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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&#39;t decide.
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.
F(f)
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
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 * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, Constant *Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and indices...
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
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 * ConstantFoldLoadThroughGEPIndices(Constant *C, ArrayRef< Constant *> Indices)
ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr indices (with an implied zero ...
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...
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Definition: PPCPredicates.h:27
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
#define I(x, y, z)
Definition: MD5.cpp:58
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices...
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...
bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function...
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices...
Constant * ConstantFoldCall(ImmutableCallSite CS, 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.