LLVM 22.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"
22#include "llvm/IR/Constants.h"
24#include "llvm/IR/Instruction.h"
25#include "llvm/IR/Operator.h"
27
28namespace llvm {
29
30/// ConstantFolder - Create constants with minimum, target independent, folding.
33
34public:
35 explicit ConstantFolder() = default;
36
37 //===--------------------------------------------------------------------===//
38 // Value-based folders.
39 //
40 // Return an existing value or a constant if the operation can be simplified.
41 // Otherwise return nullptr.
42 //===--------------------------------------------------------------------===//
43
45 Value *RHS) const override {
46 auto *LC = dyn_cast<Constant>(LHS);
47 auto *RC = dyn_cast<Constant>(RHS);
48 if (LC && RC) {
49 if (ConstantExpr::isDesirableBinOp(Opc))
50 return ConstantExpr::get(Opc, LC, RC);
51 return ConstantFoldBinaryInstruction(Opc, LC, RC);
52 }
53 return nullptr;
54 }
55
57 bool IsExact) const override {
58 auto *LC = dyn_cast<Constant>(LHS);
59 auto *RC = dyn_cast<Constant>(RHS);
60 if (LC && RC) {
61 if (ConstantExpr::isDesirableBinOp(Opc))
62 return ConstantExpr::get(Opc, LC, RC,
63 IsExact ? PossiblyExactOperator::IsExact : 0);
64 return ConstantFoldBinaryInstruction(Opc, LC, RC);
65 }
66 return nullptr;
67 }
68
70 bool HasNUW, bool HasNSW) const override {
71 auto *LC = dyn_cast<Constant>(LHS);
72 auto *RC = dyn_cast<Constant>(RHS);
73 if (LC && RC) {
74 if (ConstantExpr::isDesirableBinOp(Opc)) {
75 unsigned Flags = 0;
76 if (HasNUW)
77 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
78 if (HasNSW)
79 Flags |= OverflowingBinaryOperator::NoSignedWrap;
80 return ConstantExpr::get(Opc, LC, RC, Flags);
81 }
82 return ConstantFoldBinaryInstruction(Opc, LC, RC);
83 }
84 return nullptr;
85 }
86
88 FastMathFlags FMF) const override {
89 return FoldBinOp(Opc, LHS, RHS);
90 }
91
93 FastMathFlags FMF) const override {
94 if (Constant *C = dyn_cast<Constant>(V))
96 return nullptr;
97 }
98
100 auto *LC = dyn_cast<Constant>(LHS);
101 auto *RC = dyn_cast<Constant>(RHS);
102 if (LC && RC)
103 return ConstantFoldCompareInstruction(P, LC, RC);
104 return nullptr;
105 }
106
108 GEPNoWrapFlags NW) const override {
109 if (!ConstantExpr::isSupportedGetElementPtr(Ty))
110 return nullptr;
111
112 if (auto *PC = dyn_cast<Constant>(Ptr)) {
113 // Every index must be constant.
114 if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
115 return nullptr;
116
117 return ConstantExpr::getGetElementPtr(Ty, PC, IdxList, NW);
118 }
119 return nullptr;
120 }
121
122 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
123 auto *CC = dyn_cast<Constant>(C);
124 auto *TC = dyn_cast<Constant>(True);
125 auto *FC = dyn_cast<Constant>(False);
126 if (CC && TC && FC)
127 return ConstantFoldSelectInstruction(CC, TC, FC);
128 return nullptr;
129 }
130
132 ArrayRef<unsigned> IdxList) const override {
133 if (auto *CAgg = dyn_cast<Constant>(Agg))
134 return ConstantFoldExtractValueInstruction(CAgg, IdxList);
135 return nullptr;
136 };
137
139 ArrayRef<unsigned> IdxList) const override {
140 auto *CAgg = dyn_cast<Constant>(Agg);
141 auto *CVal = dyn_cast<Constant>(Val);
142 if (CAgg && CVal)
143 return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList);
144 return nullptr;
145 }
146
147 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
148 auto *CVec = dyn_cast<Constant>(Vec);
149 auto *CIdx = dyn_cast<Constant>(Idx);
150 if (CVec && CIdx)
151 return ConstantExpr::getExtractElement(CVec, CIdx);
152 return nullptr;
153 }
154
156 Value *Idx) const override {
157 auto *CVec = dyn_cast<Constant>(Vec);
158 auto *CNewElt = dyn_cast<Constant>(NewElt);
159 auto *CIdx = dyn_cast<Constant>(Idx);
160 if (CVec && CNewElt && CIdx)
161 return ConstantExpr::getInsertElement(CVec, CNewElt, CIdx);
162 return nullptr;
163 }
164
166 ArrayRef<int> Mask) const override {
167 auto *C1 = dyn_cast<Constant>(V1);
168 auto *C2 = dyn_cast<Constant>(V2);
169 if (C1 && C2)
170 return ConstantExpr::getShuffleVector(C1, C2, Mask);
171 return nullptr;
172 }
173
175 Type *DestTy) const override {
176 if (auto *C = dyn_cast<Constant>(V)) {
177 if (ConstantExpr::isDesirableCastOp(Op))
178 return ConstantExpr::getCast(Op, C, DestTy);
179 return ConstantFoldCastInstruction(Op, C, DestTy);
180 }
181 return nullptr;
182 }
183
185 Instruction *FMFSource) const override {
186 // Use TargetFolder or InstSimplifyFolder instead.
187 return nullptr;
188 }
189
190 //===--------------------------------------------------------------------===//
191 // Cast/Conversion Operators
192 //===--------------------------------------------------------------------===//
193
194 Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
195 return ConstantExpr::getPointerCast(C, DestTy);
196 }
197
199 Type *DestTy) const override {
200 return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
201 }
202};
203
204} // end namespace llvm
205
206#endif // LLVM_IR_CONSTANTFOLDER_H
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION()
\macro LLVM_VIRTUAL_ANCHOR_FUNCTION This macro is used to adhere to LLVM's policy that each class wit...
Definition: Compiler.h:728
#define LLVM_ABI
Definition: Compiler.h:213
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
#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:678
ConstantFolder - Create constants with minimum, target independent, folding.
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Value * FoldCast(Instruction::CastOps Op, Value *V, Type *DestTy) const override
Value * FoldCmp(CmpInst::Predicate P, Value *LHS, Value *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
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, GEPNoWrapFlags NW) 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
Value * FoldBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Type *Ty, Instruction *FMFSource) 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:43
This class represents an Operation in the Expression.
This provides a helper for copying FMF from an instruction or setting specified flags.
Definition: IRBuilder.h:93
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:22
Represents flags for the getelementptr instruction/expression.
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:75
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
LLVM_ABI Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
LLVM_ABI Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
LLVM_ABI 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:1751
LLVM_ABI Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
LLVM_ABI Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
LLVM_ABI Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)