LLVM 19.0.0git
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#include <stdint.h>
23
24namespace llvm {
25
26namespace Intrinsic {
27using ID = unsigned;
28}
29
30class APInt;
31template <typename T> class ArrayRef;
32class CallBase;
33class Constant;
34class DSOLocalEquivalent;
35class DataLayout;
36class Function;
37class GlobalValue;
38class GlobalVariable;
39class Instruction;
40class TargetLibraryInfo;
41class Type;
42
43/// If this constant is a constant offset from a global, return the global and
44/// the constant. Because of constantexprs, this function is recursive.
45/// If the global is part of a dso_local_equivalent constant, return it through
46/// `Equiv` if it is provided.
47bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
48 const DataLayout &DL,
49 DSOLocalEquivalent **DSOEquiv = nullptr);
50
51/// ConstantFoldInstruction - Try to constant fold the specified instruction.
52/// If successful, the constant result is returned, if not, null is returned.
53/// Note that this fails if not all of the operands are constant. Otherwise,
54/// this function can only fail when attempting to fold instructions like loads
55/// and stores, which have no constant expression form.
56Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
57 const TargetLibraryInfo *TLI = nullptr);
58
59/// ConstantFoldConstant - Fold the constant using the specified DataLayout.
60/// This function always returns a non-null constant: Either the folding result,
61/// or the original constant if further folding is not possible.
62Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
63 const TargetLibraryInfo *TLI = nullptr);
64
65/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
66/// specified operands. If successful, the constant result is returned, if not,
67/// null is returned. Note that this function can fail when attempting to
68/// fold instructions like loads and stores, which have no constant expression
69/// form.
70///
71/// In some cases, constant folding may return one value chosen from a set of
72/// multiple legal return values. For example, the exact bit pattern of NaN
73/// results is not guaranteed. Using such a result is usually only valid if
74/// all uses of the original operation are replaced by the constant-folded
75/// result. The \p AllowNonDeterministic parameter controls whether this is
76/// allowed.
77Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
78 const DataLayout &DL,
79 const TargetLibraryInfo *TLI = nullptr,
80 bool AllowNonDeterministic = true);
81
82/// Attempt to constant fold a compare instruction (icmp/fcmp) with the
83/// specified operands. Returns null or a constant expression of the specified
84/// operands on failure.
85/// Denormal inputs may be flushed based on the denormal handling mode.
87 unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL,
88 const TargetLibraryInfo *TLI = nullptr, const Instruction *I = nullptr);
89
90/// Attempt to constant fold a unary operation with the specified operand.
91/// Returns null on failure.
92Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
93 const DataLayout &DL);
94
95/// Attempt to constant fold a binary operation with the specified operands.
96/// Returns null or a constant expression of the specified operands on failure.
97Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
98 Constant *RHS, const DataLayout &DL);
99
100/// Attempt to constant fold a floating point binary operation with the
101/// specified operands, applying the denormal handling mod to the operands.
102/// Returns null or a constant expression of the specified operands on failure.
103Constant *ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS,
104 Constant *RHS, const DataLayout &DL,
105 const Instruction *I,
106 bool AllowNonDeterministic = true);
107
108/// Attempt to flush float point constant according to denormal mode set in the
109/// instruction's parent function attributes. If so, return a zero with the
110/// correct sign, otherwise return the original constant. Inputs and outputs to
111/// floating point instructions can have their mode set separately, so the
112/// direction is also needed.
113///
114/// If the calling function's "denormal-fp-math" input mode is "dynamic" for the
115/// floating-point type, returns nullptr for denormal inputs.
116Constant *FlushFPConstant(Constant *Operand, const Instruction *I,
117 bool IsOutput);
118
119/// Attempt to constant fold a select instruction with the specified
120/// operands. The constant result is returned if successful; if not, null is
121/// returned.
122Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
123 Constant *V2);
124
125/// Attempt to constant fold a cast with the specified operand. If it
126/// fails, it returns a constant expression of the specified operand.
127Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
128 const DataLayout &DL);
129
130/// Constant fold a zext, sext or trunc, depending on IsSigned and whether the
131/// DestTy is wider or narrower than C. Returns nullptr on failure.
132Constant *ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned,
133 const DataLayout &DL);
134
135/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
136/// instruction with the specified operands and indices. The constant result is
137/// returned if successful; if not, null is returned.
138Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
139 ArrayRef<unsigned> Idxs);
140
141/// Attempt to constant fold an extractvalue instruction with the
142/// specified operands and indices. The constant result is returned if
143/// successful; if not, null is returned.
145 ArrayRef<unsigned> Idxs);
146
147/// Attempt to constant fold an insertelement instruction with the
148/// specified operands and indices. The constant result is returned if
149/// successful; if not, null is returned.
151 Constant *Elt,
152 Constant *Idx);
153
154/// Attempt to constant fold an extractelement instruction with the
155/// specified operands and indices. The constant result is returned if
156/// successful; if not, null is returned.
157Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
158
159/// Attempt to constant fold a shufflevector instruction with the
160/// specified operands and mask. See class ShuffleVectorInst for a description
161/// of the mask representation. The constant result is returned if successful;
162/// if not, null is returned.
163Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
164 ArrayRef<int> Mask);
165
166/// Extract value of C at the given Offset reinterpreted as Ty. If bits past
167/// the end of C are accessed, they are assumed to be poison.
168Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset,
169 const DataLayout &DL);
170
171/// Extract value of C reinterpreted as Ty. Same as previous API with zero
172/// offset.
174 const DataLayout &DL);
175
176/// Return the value that a load from C with offset Offset would produce if it
177/// is constant and determinable. If this is not determinable, return null.
178Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset,
179 const DataLayout &DL);
180
181/// Return the value that a load from C would produce if it is constant and
182/// determinable. If this is not determinable, return null.
184 const DataLayout &DL);
185
186/// If C is a uniform value where all bits are the same (either all zero, all
187/// ones, all undef or all poison), return the corresponding uniform value in
188/// the new type. If the value is not uniform or the result cannot be
189/// represented, return null.
191 const DataLayout &DL);
192
193/// canConstantFoldCallTo - Return true if its even possible to fold a call to
194/// the specified function.
195bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
196
197/// ConstantFoldCall - Attempt to constant fold a call to the specified function
198/// with the specified arguments, returning null if unsuccessful.
199Constant *ConstantFoldCall(const CallBase *Call, Function *F,
200 ArrayRef<Constant *> Operands,
201 const TargetLibraryInfo *TLI = nullptr,
202 bool AllowNonDeterministic = true);
203
205 Constant *RHS, Type *Ty,
206 Instruction *FMFSource);
207
208/// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
209/// returning null if unsuccessful. Can cast pointer to pointer or pointer to
210/// integer and vice versa if their sizes are equal.
212 const DataLayout &DL);
213
214/// Check whether the given call has no side-effects.
215/// Specifically checks for math routimes which sometimes set errno.
216bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
217
218Constant *ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset);
219}
220
221#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
RelocType Type
Definition: COFFYAML.cpp:391
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
const SmallVectorImpl< MachineOperand > & Cond
Value * RHS
Value * LHS
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
Constant * ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, Constant *RHS, Type *Ty, Instruction *FMFSource)
Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I, bool AllowNonDeterministic=true)
Attempt to constant fold a floating point binary operation with the specified operands,...
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
Constant * FlushFPConstant(Constant *Operand, const Instruction *I, bool IsOutput)
Attempt to flush float point constant according to denormal mode set in the instruction's parent func...
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, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
DWARFExpression::Operation Op
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
ArrayRef(const T &OneElt) -> ArrayRef< T >
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...