LLVM 17.0.0git
ConstantFolder.h
Go to the documentation of this file.
1//===- ConstantFolder.h - Constant folding helper ---------------*- 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 defines the ConstantFolder class, a helper for IRBuilder.
10// It provides IRBuilder with a set of methods for creating constants
11// with minimal folding. For general constant creation and folding,
12// use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_CONSTANTFOLDER_H
17#define LLVM_IR_CONSTANTFOLDER_H
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/IR/Constants.h"
24#include "llvm/IR/Instruction.h"
25#include "llvm/IR/Operator.h"
26
27namespace llvm {
28
29/// ConstantFolder - Create constants with minimum, target independent, folding.
30class ConstantFolder final : public IRBuilderFolder {
31 virtual void anchor();
32
33public:
34 explicit ConstantFolder() = default;
35
36 //===--------------------------------------------------------------------===//
37 // Value-based folders.
38 //
39 // Return an existing value or a constant if the operation can be simplified.
40 // Otherwise return nullptr.
41 //===--------------------------------------------------------------------===//
42
44 Value *RHS) const override {
45 auto *LC = dyn_cast<Constant>(LHS);
46 auto *RC = dyn_cast<Constant>(RHS);
47 if (LC && RC) {
49 return ConstantExpr::get(Opc, LC, RC);
50 return ConstantFoldBinaryInstruction(Opc, LC, RC);
51 }
52 return nullptr;
53 }
54
56 bool IsExact) const override {
57 auto *LC = dyn_cast<Constant>(LHS);
58 auto *RC = dyn_cast<Constant>(RHS);
59 if (LC && RC) {
61 return ConstantExpr::get(Opc, LC, RC,
62 IsExact ? PossiblyExactOperator::IsExact : 0);
63 return ConstantFoldBinaryInstruction(Opc, LC, RC);
64 }
65 return nullptr;
66 }
67
69 bool HasNUW, bool HasNSW) const override {
70 auto *LC = dyn_cast<Constant>(LHS);
71 auto *RC = dyn_cast<Constant>(RHS);
72 if (LC && RC) {
74 unsigned Flags = 0;
75 if (HasNUW)
77 if (HasNSW)
79 return ConstantExpr::get(Opc, LC, RC, Flags);
80 }
81 return ConstantFoldBinaryInstruction(Opc, LC, RC);
82 }
83 return nullptr;
84 }
85
87 FastMathFlags FMF) const override {
88 return FoldBinOp(Opc, LHS, RHS);
89 }
90
92 FastMathFlags FMF) const override {
93 if (Constant *C = dyn_cast<Constant>(V))
95 return nullptr;
96 }
97
99 auto *LC = dyn_cast<Constant>(LHS);
100 auto *RC = dyn_cast<Constant>(RHS);
101 if (LC && RC)
102 return ConstantExpr::getCompare(P, LC, RC);
103 return nullptr;
104 }
105
107 bool IsInBounds = false) const override {
108 if (auto *PC = dyn_cast<Constant>(Ptr)) {
109 // Every index must be constant.
110 if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
111 return nullptr;
112
113 if (IsInBounds)
114 return ConstantExpr::getInBoundsGetElementPtr(Ty, PC, IdxList);
115 else
116 return ConstantExpr::getGetElementPtr(Ty, PC, IdxList);
117 }
118 return nullptr;
119 }
120
121 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
122 auto *CC = dyn_cast<Constant>(C);
123 auto *TC = dyn_cast<Constant>(True);
124 auto *FC = dyn_cast<Constant>(False);
125 if (CC && TC && FC)
126 return ConstantExpr::getSelect(CC, TC, FC);
127 return nullptr;
128 }
129
131 ArrayRef<unsigned> IdxList) const override {
132 if (auto *CAgg = dyn_cast<Constant>(Agg))
133 return ConstantFoldExtractValueInstruction(CAgg, IdxList);
134 return nullptr;
135 };
136
138 ArrayRef<unsigned> IdxList) const override {
139 auto *CAgg = dyn_cast<Constant>(Agg);
140 auto *CVal = dyn_cast<Constant>(Val);
141 if (CAgg && CVal)
142 return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList);
143 return nullptr;
144 }
145
146 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
147 auto *CVec = dyn_cast<Constant>(Vec);
148 auto *CIdx = dyn_cast<Constant>(Idx);
149 if (CVec && CIdx)
150 return ConstantExpr::getExtractElement(CVec, CIdx);
151 return nullptr;
152 }
153
155 Value *Idx) const override {
156 auto *CVec = dyn_cast<Constant>(Vec);
157 auto *CNewElt = dyn_cast<Constant>(NewElt);
158 auto *CIdx = dyn_cast<Constant>(Idx);
159 if (CVec && CNewElt && CIdx)
160 return ConstantExpr::getInsertElement(CVec, CNewElt, CIdx);
161 return nullptr;
162 }
163
165 ArrayRef<int> Mask) const override {
166 auto *C1 = dyn_cast<Constant>(V1);
167 auto *C2 = dyn_cast<Constant>(V2);
168 if (C1 && C2)
169 return ConstantExpr::getShuffleVector(C1, C2, Mask);
170 return nullptr;
171 }
172
173 //===--------------------------------------------------------------------===//
174 // Cast/Conversion Operators
175 //===--------------------------------------------------------------------===//
176
178 Type *DestTy) const override {
179 return ConstantExpr::getCast(Op, C, DestTy);
180 }
181
182 Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
183 return ConstantExpr::getPointerCast(C, DestTy);
184 }
185
187 Type *DestTy) const override {
189 }
190
192 bool isSigned) const override {
193 return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
194 }
195
196 Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
197 return ConstantExpr::getFPCast(C, DestTy);
198 }
199
200 Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
201 return CreateCast(Instruction::BitCast, C, DestTy);
202 }
203
204 Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
205 return CreateCast(Instruction::IntToPtr, C, DestTy);
206 }
207
208 Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
209 return CreateCast(Instruction::PtrToInt, C, DestTy);
210 }
211
212 Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
213 return ConstantExpr::getZExtOrBitCast(C, DestTy);
214 }
215
216 Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
217 return ConstantExpr::getSExtOrBitCast(C, DestTy);
218 }
219
220 Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
221 return ConstantExpr::getTruncOrBitCast(C, DestTy);
222 }
223
224 //===--------------------------------------------------------------------===//
225 // Compare Instructions
226 //===--------------------------------------------------------------------===//
227
229 Constant *RHS) const override {
231 }
232};
233
234} // end namespace llvm
235
236#endif // LLVM_IR_CONSTANTFOLDER_H
This file contains the declarations for the subclasses of Constant, which represent the different fla...
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
static bool isSigned(unsigned int Opcode)
#define P(N)
This file contains some templates that are useful if you are working with the STL at all.
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:718
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2018
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2573
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1271
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:2041
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2024
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2056
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1973
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2595
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2618
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, std::optional< unsigned > InRangeIndex=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1245
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:2441
static Constant * getIntegerCast(Constant *C, Type *Ty, bool IsSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:2067
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2012
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
Definition: Constants.cpp:2263
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
Definition: Constants.cpp:2324
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:2079
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:2419
ConstantFolder - Create constants with minimum, target independent, folding.
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) const override
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const override
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Value * FoldExtractElement(Value *Vec, Value *Idx) const override
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Constant * CreateBitCast(Constant *C, Type *DestTy) const override
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const override
Constant * CreateFPCast(Constant *C, Type *DestTy) const override
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Constant * CreatePointerCast(Constant *C, Type *DestTy) const override
Value * FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override
Value * FoldBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS) const override
Value * FoldInsertElement(Value *Vec, Value *NewElt, Value *Idx) const override
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
ConstantFolder()=default
Value * FoldSelect(Value *C, Value *True, Value *False) const override
This is an important base class in LLVM.
Definition: Constant.h:41
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:21
IRBuilderFolder - Interface for constant folding in IRBuilder.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
@ 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
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1742
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)