LLVM 22.0.0git
TargetFolder.h
Go to the documentation of this file.
1//====- TargetFolder.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 TargetFolder class, a helper for IRBuilder.
10// It provides IRBuilder with a set of methods for creating constants with
11// target dependent folding, in addition to the same target-independent
12// folding that the ConstantFolder class provides. For general constant
13// creation and folding, use ConstantExpr and the routines in
14// llvm/Analysis/ConstantFolding.h.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_ANALYSIS_TARGETFOLDER_H
19#define LLVM_ANALYSIS_TARGETFOLDER_H
20
21#include "llvm/ADT/ArrayRef.h"
23#include "llvm/IR/Constants.h"
25#include "llvm/IR/Operator.h"
27
28namespace llvm {
29
30class Constant;
31class DataLayout;
32class Type;
33
34/// TargetFolder - Create constants with target dependent folding.
36 const DataLayout &DL;
37
38 /// Fold - Fold the constant using target specific information.
39 Constant *Fold(Constant *C) const {
40 return ConstantFoldConstant(C, DL);
41 }
42
44
45public:
46 explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
47
48 //===--------------------------------------------------------------------===//
49 // Value-based folders.
50 //
51 // Return an existing value or a constant if the operation can be simplified.
52 // Otherwise return nullptr.
53 //===--------------------------------------------------------------------===//
54
56 Value *RHS) const override {
57 auto *LC = dyn_cast<Constant>(LHS);
58 auto *RC = dyn_cast<Constant>(RHS);
59 if (LC && RC) {
61 return Fold(ConstantExpr::get(Opc, LC, RC));
62 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
63 }
64 return nullptr;
65 }
66
68 bool IsExact) const override {
69 auto *LC = dyn_cast<Constant>(LHS);
70 auto *RC = dyn_cast<Constant>(RHS);
71 if (LC && RC) {
73 return Fold(ConstantExpr::get(
74 Opc, LC, RC, IsExact ? PossiblyExactOperator::IsExact : 0));
75 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
76 }
77 return nullptr;
78 }
79
81 bool HasNUW, bool HasNSW) const override {
82 auto *LC = dyn_cast<Constant>(LHS);
83 auto *RC = dyn_cast<Constant>(RHS);
84 if (LC && RC) {
86 unsigned Flags = 0;
87 if (HasNUW)
89 if (HasNSW)
91 return Fold(ConstantExpr::get(Opc, LC, RC, Flags));
92 }
93 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
94 }
95 return nullptr;
96 }
97
99 FastMathFlags FMF) const override {
100 return FoldBinOp(Opc, LHS, RHS);
101 }
102
104 auto *LC = dyn_cast<Constant>(LHS);
105 auto *RC = dyn_cast<Constant>(RHS);
106 if (LC && RC)
107 return ConstantFoldCompareInstOperands(P, LC, RC, DL);
108 return nullptr;
109 }
110
112 FastMathFlags FMF) const override {
113 if (Constant *C = dyn_cast<Constant>(V))
114 return ConstantFoldUnaryOpOperand(Opc, C, DL);
115 return nullptr;
116 }
117
119 GEPNoWrapFlags NW) const override {
121 return nullptr;
122
123 if (auto *PC = dyn_cast<Constant>(Ptr)) {
124 // Every index must be constant.
125 if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
126 return nullptr;
127 return Fold(ConstantExpr::getGetElementPtr(Ty, PC, IdxList, NW));
128 }
129 return nullptr;
130 }
131
132 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
133 auto *CC = dyn_cast<Constant>(C);
134 auto *TC = dyn_cast<Constant>(True);
135 auto *FC = dyn_cast<Constant>(False);
136 if (CC && TC && FC)
137 return ConstantFoldSelectInstruction(CC, TC, FC);
138
139 return nullptr;
140 }
141
143 ArrayRef<unsigned> IdxList) const override {
144 if (auto *CAgg = dyn_cast<Constant>(Agg))
145 return ConstantFoldExtractValueInstruction(CAgg, IdxList);
146 return nullptr;
147 };
148
150 ArrayRef<unsigned> IdxList) const override {
151 auto *CAgg = dyn_cast<Constant>(Agg);
152 auto *CVal = dyn_cast<Constant>(Val);
153 if (CAgg && CVal)
154 return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList);
155 return nullptr;
156 }
157
158 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
159 auto *CVec = dyn_cast<Constant>(Vec);
160 auto *CIdx = dyn_cast<Constant>(Idx);
161 if (CVec && CIdx)
162 return Fold(ConstantExpr::getExtractElement(CVec, CIdx));
163 return nullptr;
164 }
165
167 Value *Idx) const override {
168 auto *CVec = dyn_cast<Constant>(Vec);
169 auto *CNewElt = dyn_cast<Constant>(NewElt);
170 auto *CIdx = dyn_cast<Constant>(Idx);
171 if (CVec && CNewElt && CIdx)
172 return Fold(ConstantExpr::getInsertElement(CVec, CNewElt, CIdx));
173 return nullptr;
174 }
175
177 ArrayRef<int> Mask) const override {
178 auto *C1 = dyn_cast<Constant>(V1);
179 auto *C2 = dyn_cast<Constant>(V2);
180 if (C1 && C2)
181 return Fold(ConstantExpr::getShuffleVector(C1, C2, Mask));
182 return nullptr;
183 }
184
186 Type *DestTy) const override {
187 if (auto *C = dyn_cast<Constant>(V))
188 return ConstantFoldCastOperand(Op, C, DestTy, DL);
189 return nullptr;
190 }
191
193 Instruction *FMFSource) const override {
194 auto *C1 = dyn_cast<Constant>(LHS);
195 auto *C2 = dyn_cast<Constant>(RHS);
196 if (C1 && C2)
197 return ConstantFoldBinaryIntrinsic(ID, C1, C2, Ty, FMFSource);
198 return nullptr;
199 }
200
201 //===--------------------------------------------------------------------===//
202 // Cast/Conversion Operators
203 //===--------------------------------------------------------------------===//
204
205 Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
206 if (C->getType() == DestTy)
207 return C; // avoid calling Fold
208 return Fold(ConstantExpr::getPointerCast(C, DestTy));
209 }
210
212 Type *DestTy) const override {
213 if (C->getType() == DestTy)
214 return C; // avoid calling Fold
216 }
217};
218}
219
220#endif
#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:737
#define LLVM_ABI
Definition Compiler.h:213
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define P(N)
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
static LLVM_ABI Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_ABI Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
static LLVM_ABI Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
static LLVM_ABI Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_ABI Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
Definition Constants.h:1387
static LLVM_ABI 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.
static LLVM_ABI bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition Constants.h:1274
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
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.
Constant * CreatePointerCast(Constant *C, Type *DestTy) const override
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
TargetFolder(const DataLayout &DL)
Value * FoldInsertElement(Value *Vec, Value *NewElt, Value *Idx) const override
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Value * FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override
Value * FoldCmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, GEPNoWrapFlags NW) const override
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Value * FoldBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS) const override
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Value * FoldExtractElement(Value *Vec, Value *Idx) const override
Value * FoldCast(Instruction::CastOps Op, Value *V, Type *DestTy) const override
Value * FoldBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Type *Ty, Instruction *FMFSource) const override
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
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.
LLVM_ABI Constant * ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, Constant *RHS, Type *Ty, Instruction *FMFSource)
LLVM_ABI Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
LLVM_ABI 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.
LLVM_ABI Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
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:1714
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
DWARFExpression::Operation Op
LLVM_ABI Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...