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"
24#include "llvm/IR/Constants.h"
26#include "llvm/IR/Operator.h"
28
29namespace llvm {
30
31class Constant;
32class DataLayout;
33class Type;
34
35/// TargetFolder - Create constants with target dependent folding.
37 const DataLayout &DL;
38
39 /// Fold - Fold the constant using target specific information.
40 Constant *Fold(Constant *C) const {
41 return ConstantFoldConstant(C, DL);
42 }
43
45
46public:
47 explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
48
49 //===--------------------------------------------------------------------===//
50 // Value-based folders.
51 //
52 // Return an existing value or a constant if the operation can be simplified.
53 // Otherwise return nullptr.
54 //===--------------------------------------------------------------------===//
55
57 Value *RHS) const override {
58 auto *LC = dyn_cast<Constant>(LHS);
59 auto *RC = dyn_cast<Constant>(RHS);
60 if (LC && RC) {
62 return Fold(ConstantExpr::get(Opc, LC, RC));
63 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
64 }
65 return nullptr;
66 }
67
69 bool IsExact) const override {
70 auto *LC = dyn_cast<Constant>(LHS);
71 auto *RC = dyn_cast<Constant>(RHS);
72 if (LC && RC) {
74 return Fold(ConstantExpr::get(
75 Opc, LC, RC, IsExact ? PossiblyExactOperator::IsExact : 0));
76 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
77 }
78 return nullptr;
79 }
80
82 bool HasNUW, bool HasNSW) const override {
83 auto *LC = dyn_cast<Constant>(LHS);
84 auto *RC = dyn_cast<Constant>(RHS);
85 if (LC && RC) {
87 unsigned Flags = 0;
88 if (HasNUW)
90 if (HasNSW)
92 return Fold(ConstantExpr::get(Opc, LC, RC, Flags));
93 }
94 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
95 }
96 return nullptr;
97 }
98
100 FastMathFlags FMF) const override {
101 return FoldBinOp(Opc, LHS, RHS);
102 }
103
105 auto *LC = dyn_cast<Constant>(LHS);
106 auto *RC = dyn_cast<Constant>(RHS);
107 if (LC && RC)
108 return ConstantFoldCompareInstOperands(P, LC, RC, DL);
109 return nullptr;
110 }
111
113 FastMathFlags FMF) const override {
114 if (Constant *C = dyn_cast<Constant>(V))
115 return ConstantFoldUnaryOpOperand(Opc, C, DL);
116 return nullptr;
117 }
118
120 GEPNoWrapFlags NW) const override {
122 return nullptr;
123
124 if (auto *PC = dyn_cast<Constant>(Ptr)) {
125 // Every index must be constant.
126 if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
127 return nullptr;
128 return Fold(ConstantExpr::getGetElementPtr(Ty, PC, IdxList, NW));
129 }
130 return nullptr;
131 }
132
133 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
134 auto *CC = dyn_cast<Constant>(C);
135 auto *TC = dyn_cast<Constant>(True);
136 auto *FC = dyn_cast<Constant>(False);
137 if (CC && TC && FC)
138 return ConstantFoldSelectInstruction(CC, TC, FC);
139
140 return nullptr;
141 }
142
144 ArrayRef<unsigned> IdxList) const override {
145 if (auto *CAgg = dyn_cast<Constant>(Agg))
146 return ConstantFoldExtractValueInstruction(CAgg, IdxList);
147 return nullptr;
148 };
149
151 ArrayRef<unsigned> IdxList) const override {
152 auto *CAgg = dyn_cast<Constant>(Agg);
153 auto *CVal = dyn_cast<Constant>(Val);
154 if (CAgg && CVal)
155 return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList);
156 return nullptr;
157 }
158
159 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
160 auto *CVec = dyn_cast<Constant>(Vec);
161 auto *CIdx = dyn_cast<Constant>(Idx);
162 if (CVec && CIdx)
163 return Fold(ConstantExpr::getExtractElement(CVec, CIdx));
164 return nullptr;
165 }
166
168 Value *Idx) const override {
169 auto *CVec = dyn_cast<Constant>(Vec);
170 auto *CNewElt = dyn_cast<Constant>(NewElt);
171 auto *CIdx = dyn_cast<Constant>(Idx);
172 if (CVec && CNewElt && CIdx)
173 return Fold(ConstantExpr::getInsertElement(CVec, CNewElt, CIdx));
174 return nullptr;
175 }
176
178 ArrayRef<int> Mask) const override {
179 auto *C1 = dyn_cast<Constant>(V1);
180 auto *C2 = dyn_cast<Constant>(V2);
181 if (C1 && C2)
182 return Fold(ConstantExpr::getShuffleVector(C1, C2, Mask));
183 return nullptr;
184 }
185
187 Type *DestTy) const override {
188 if (auto *C = dyn_cast<Constant>(V))
189 return ConstantFoldCastOperand(Op, C, DestTy, DL);
190 return nullptr;
191 }
192
194 Instruction *FMFSource) const override {
195 auto *C1 = dyn_cast<Constant>(LHS);
196 auto *C2 = dyn_cast<Constant>(RHS);
197 if (C1 && C2)
198 return ConstantFoldBinaryIntrinsic(ID, C1, C2, Ty, FMFSource);
199 return nullptr;
200 }
201
202 //===--------------------------------------------------------------------===//
203 // Cast/Conversion Operators
204 //===--------------------------------------------------------------------===//
205
206 Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
207 if (C->getType() == DestTy)
208 return C; // avoid calling Fold
209 return Fold(ConstantExpr::getPointerCast(C, DestTy));
210 }
211
213 Type *DestTy) const override {
214 if (C->getType() == DestTy)
215 return C; // avoid calling Fold
217 }
218};
219}
220
221#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:676
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:643
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:1732
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:547
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)
Attempt to constant fold an insertvalue instruction with the specified operands and indices.