LLVM  10.0.0svn
ConstantFolding.h
Go to the documentation of this file.
1 //===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares routines for folding instructions into constants when all
10 // operands are constants, for example "sub i32 1, 0" -> "1".
11 //
12 // Also, to supplement the basic VMCore ConstantExpr simplifications,
13 // this file declares some additional folding routines that can make use of
14 // DataLayout information. These functions cannot go in VMCore due to library
15 // dependency issues.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
20 #define LLVM_ANALYSIS_CONSTANTFOLDING_H
21 
22 namespace llvm {
23 class APInt;
24 template <typename T> class ArrayRef;
25 class CallBase;
26 class Constant;
27 class ConstantExpr;
28 class ConstantVector;
29 class DataLayout;
30 class Function;
31 class GlobalValue;
32 class Instruction;
33 class TargetLibraryInfo;
34 class Type;
35 
36 /// If this constant is a constant offset from a global, return the global and
37 /// the constant. Because of constantexprs, this function is recursive.
38 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
39  const DataLayout &DL);
40 
41 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
42 /// If successful, the constant result is returned, if not, null is returned.
43 /// Note that this fails if not all of the operands are constant. Otherwise,
44 /// this function can only fail when attempting to fold instructions like loads
45 /// and stores, which have no constant expression form.
46 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
47  const TargetLibraryInfo *TLI = nullptr);
48 
49 /// ConstantFoldConstant - Attempt to fold the constant using the
50 /// specified DataLayout.
51 /// If successful, the constant result is returned, if not, null is returned.
52 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
53  const TargetLibraryInfo *TLI = nullptr);
54 
55 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
56 /// specified operands. If successful, the constant result is returned, if not,
57 /// null is returned. Note that this function can fail when attempting to
58 /// fold instructions like loads and stores, which have no constant expression
59 /// form.
60 ///
61 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
62  const DataLayout &DL,
63  const TargetLibraryInfo *TLI = nullptr);
64 
65 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
66 /// instruction (icmp/fcmp) with the specified operands. If it fails, it
67 /// returns a constant expression of the specified operands.
68 ///
69 Constant *
71  Constant *RHS, const DataLayout &DL,
72  const TargetLibraryInfo *TLI = nullptr);
73 
74 /// Attempt to constant fold a unary operation with the specified
75 /// operand. If it fails, it returns a constant expression of the specified
76 /// operands.
78  const DataLayout &DL);
79 
80 /// Attempt to constant fold a binary operation with the specified
81 /// operands. If it fails, it returns a constant expression of the specified
82 /// operands.
83 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
84  Constant *RHS, const DataLayout &DL);
85 
86 /// Attempt to constant fold a select instruction with the specified
87 /// operands. The constant result is returned if successful; if not, null is
88 /// returned.
90  Constant *V2);
91 
92 /// Attempt to constant fold a cast with the specified operand. If it
93 /// fails, it returns a constant expression of the specified operand.
94 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
95  const DataLayout &DL);
96 
97 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
98 /// instruction with the specified operands and indices. The constant result is
99 /// returned if successful; if not, null is returned.
101  ArrayRef<unsigned> Idxs);
102 
103 /// Attempt to constant fold an extractvalue instruction with the
104 /// specified operands and indices. The constant result is returned if
105 /// successful; if not, null is returned.
107  ArrayRef<unsigned> Idxs);
108 
109 /// Attempt to constant fold an insertelement instruction with the
110 /// specified operands and indices. The constant result is returned if
111 /// successful; if not, null is returned.
113  Constant *Elt,
114  Constant *Idx);
115 
116 /// Attempt to constant fold an extractelement instruction with the
117 /// specified operands and indices. The constant result is returned if
118 /// successful; if not, null is returned.
120 
121 /// Attempt to constant fold a shufflevector instruction with the
122 /// specified operands and indices. The constant result is returned if
123 /// successful; if not, null is returned.
125  Constant *Mask);
126 
127 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
128 /// produce if it is constant and determinable. If this is not determinable,
129 /// return null.
130 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
131 
132 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
133 /// getelementptr constantexpr, return the constant value being addressed by the
134 /// constant expression, or null if something is funny and we can't decide.
136 
137 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
138 /// indices (with an *implied* zero pointer index that is not in the list),
139 /// return the constant value being addressed by a virtual load, or null if
140 /// something is funny and we can't decide.
142  ArrayRef<Constant *> Indices);
143 
144 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
145 /// the specified function.
146 bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
147 
148 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
149 /// with the specified arguments, returning null if unsuccessful.
150 Constant *ConstantFoldCall(const CallBase *Call, Function *F,
151  ArrayRef<Constant *> Operands,
152  const TargetLibraryInfo *TLI = nullptr);
153 
154 /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
155 /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
156 /// integer and vice versa if their sizes are equal.
158  const DataLayout &DL);
159 
160 /// Check whether the given call has no side-effects.
161 /// Specifically checks for math routimes which sometimes set errno.
162 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
163 }
164 
165 #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
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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 * ConstantFoldCall(const CallBase *Call, 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 * 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)
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
mir Rename Register Operands
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
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...
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function...
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Definition: PPCPredicates.h:26
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 * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
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...
Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
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:80
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices...
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.