LLVM 18.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 {
25class APInt;
26template <typename T> class ArrayRef;
27class CallBase;
28class Constant;
29class DSOLocalEquivalent;
30class DataLayout;
31class Function;
32class GlobalValue;
33class GlobalVariable;
34class Instruction;
35class TargetLibraryInfo;
36class 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/// If the global is part of a dso_local_equivalent constant, return it through
41/// `Equiv` if it is provided.
42bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
43 const DataLayout &DL,
44 DSOLocalEquivalent **DSOEquiv = nullptr);
45
46/// ConstantFoldInstruction - Try to constant fold the specified instruction.
47/// If successful, the constant result is returned, if not, null is returned.
48/// Note that this fails if not all of the operands are constant. Otherwise,
49/// this function can only fail when attempting to fold instructions like loads
50/// and stores, which have no constant expression form.
51Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
52 const TargetLibraryInfo *TLI = nullptr);
53
54/// ConstantFoldConstant - Fold the constant using the specified DataLayout.
55/// This function always returns a non-null constant: Either the folding result,
56/// or the original constant if further folding is not possible.
57Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
58 const TargetLibraryInfo *TLI = nullptr);
59
60/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
61/// specified operands. If successful, the constant result is returned, if not,
62/// null is returned. Note that this function can fail when attempting to
63/// fold instructions like loads and stores, which have no constant expression
64/// form.
65///
66Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
67 const DataLayout &DL,
68 const TargetLibraryInfo *TLI = nullptr);
69
70/// Attempt to constant fold a compare instruction (icmp/fcmp) with the
71/// specified operands. Returns null or a constant expression of the specified
72/// operands on failure.
73/// Denormal inputs may be flushed based on the denormal handling mode.
75 unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL,
76 const TargetLibraryInfo *TLI = nullptr, const Instruction *I = nullptr);
77
78/// Attempt to constant fold a unary operation with the specified operand.
79/// Returns null on failure.
80Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
81 const DataLayout &DL);
82
83/// Attempt to constant fold a binary operation with the specified operands.
84/// Returns null or a constant expression of the specified operands on failure.
85Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
86 Constant *RHS, const DataLayout &DL);
87
88/// Attempt to constant fold a floating point binary operation with the
89/// specified operands, applying the denormal handling mod to the operands.
90/// Returns null or a constant expression of the specified operands on failure.
91Constant *ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS,
92 Constant *RHS, const DataLayout &DL,
93 const Instruction *I);
94
95/// Attempt to flush float point constant according to denormal mode set in the
96/// instruction's parent function attributes. If so, return a zero with the
97/// correct sign, otherwise return the original constant. Inputs and outputs to
98/// floating point instructions can have their mode set separately, so the
99/// direction is also needed.
100///
101/// If the calling function's "denormal-fp-math" input mode is "dynamic" for the
102/// floating-point type, returns nullptr for denormal inputs.
103Constant *FlushFPConstant(Constant *Operand, const Instruction *I,
104 bool IsOutput);
105
106/// Attempt to constant fold a select instruction with the specified
107/// operands. The constant result is returned if successful; if not, null is
108/// returned.
109Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
110 Constant *V2);
111
112/// Attempt to constant fold a cast with the specified operand. If it
113/// fails, it returns a constant expression of the specified operand.
114Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
115 const DataLayout &DL);
116
117/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
118/// instruction with the specified operands and indices. The constant result is
119/// returned if successful; if not, null is returned.
120Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
121 ArrayRef<unsigned> Idxs);
122
123/// Attempt to constant fold an extractvalue instruction with the
124/// specified operands and indices. The constant result is returned if
125/// successful; if not, null is returned.
127 ArrayRef<unsigned> Idxs);
128
129/// Attempt to constant fold an insertelement instruction with the
130/// specified operands and indices. The constant result is returned if
131/// successful; if not, null is returned.
133 Constant *Elt,
134 Constant *Idx);
135
136/// Attempt to constant fold an extractelement instruction with the
137/// specified operands and indices. The constant result is returned if
138/// successful; if not, null is returned.
139Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
140
141/// Attempt to constant fold a shufflevector instruction with the
142/// specified operands and mask. See class ShuffleVectorInst for a description
143/// of the mask representation. The constant result is returned if successful;
144/// if not, null is returned.
145Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
146 ArrayRef<int> Mask);
147
148/// Extract value of C at the given Offset reinterpreted as Ty. If bits past
149/// the end of C are accessed, they are assumed to be poison.
150Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset,
151 const DataLayout &DL);
152
153/// Extract value of C reinterpreted as Ty. Same as previous API with zero
154/// offset.
156 const DataLayout &DL);
157
158/// Return the value that a load from C with offset Offset would produce if it
159/// is constant and determinable. If this is not determinable, return null.
160Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset,
161 const DataLayout &DL);
162
163/// Return the value that a load from C would produce if it is constant and
164/// determinable. If this is not determinable, return null.
166 const DataLayout &DL);
167
168/// If C is a uniform value where all bits are the same (either all zero, all
169/// ones, all undef or all poison), return the corresponding uniform value in
170/// the new type. If the value is not uniform or the result cannot be
171/// represented, return null.
173
174/// canConstantFoldCallTo - Return true if its even possible to fold a call to
175/// the specified function.
176bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
177
178/// ConstantFoldCall - Attempt to constant fold a call to the specified function
179/// with the specified arguments, returning null if unsuccessful.
180Constant *ConstantFoldCall(const CallBase *Call, Function *F,
181 ArrayRef<Constant *> Operands,
182 const TargetLibraryInfo *TLI = nullptr);
183
184/// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
185/// returning null if unsuccessful. Can cast pointer to pointer or pointer to
186/// integer and vice versa if their sizes are equal.
188 const DataLayout &DL);
189
190/// Check whether the given call has no side-effects.
191/// Specifically checks for math routimes which sometimes set errno.
192bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
193
194Constant *ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset);
195}
196
197#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
@ 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:440
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.
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I)
Attempt to constant fold a floating point binary operation with the specified operands,...
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 * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
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 * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
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 * 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 * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
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 * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
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.