LLVM 17.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"
26
27namespace llvm {
28
29class Constant;
30class DataLayout;
31class Type;
32
33/// TargetFolder - Create constants with target dependent folding.
34class TargetFolder final : public IRBuilderFolder {
35 const DataLayout &DL;
36
37 /// Fold - Fold the constant using target specific information.
38 Constant *Fold(Constant *C) const {
39 return ConstantFoldConstant(C, DL);
40 }
41
42 virtual void anchor();
43
44public:
45 explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
46
47 //===--------------------------------------------------------------------===//
48 // Value-based folders.
49 //
50 // Return an existing value or a constant if the operation can be simplified.
51 // Otherwise return nullptr.
52 //===--------------------------------------------------------------------===//
53
55 Value *RHS) const override {
56 auto *LC = dyn_cast<Constant>(LHS);
57 auto *RC = dyn_cast<Constant>(RHS);
58 if (LC && RC) {
60 return Fold(ConstantExpr::get(Opc, LC, RC));
61 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
62 }
63 return nullptr;
64 }
65
67 bool IsExact) const override {
68 auto *LC = dyn_cast<Constant>(LHS);
69 auto *RC = dyn_cast<Constant>(RHS);
70 if (LC && RC) {
72 return Fold(ConstantExpr::get(
73 Opc, LC, RC, IsExact ? PossiblyExactOperator::IsExact : 0));
74 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
75 }
76 return nullptr;
77 }
78
80 bool HasNUW, bool HasNSW) const override {
81 auto *LC = dyn_cast<Constant>(LHS);
82 auto *RC = dyn_cast<Constant>(RHS);
83 if (LC && RC) {
85 unsigned Flags = 0;
86 if (HasNUW)
88 if (HasNSW)
90 return Fold(ConstantExpr::get(Opc, LC, RC, Flags));
91 }
92 return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
93 }
94 return nullptr;
95 }
96
98 FastMathFlags FMF) const override {
99 return FoldBinOp(Opc, LHS, RHS);
100 }
101
103 auto *LC = dyn_cast<Constant>(LHS);
104 auto *RC = dyn_cast<Constant>(RHS);
105 if (LC && RC)
106 return Fold(ConstantExpr::getCompare(P, LC, RC));
107 return nullptr;
108 }
109
111 FastMathFlags FMF) const override {
112 if (Constant *C = dyn_cast<Constant>(V))
113 return ConstantFoldUnaryOpOperand(Opc, C, DL);
114 return nullptr;
115 }
116
118 bool IsInBounds = false) const override {
120 return nullptr;
121
122 if (auto *PC = dyn_cast<Constant>(Ptr)) {
123 // Every index must be constant.
124 if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
125 return nullptr;
126 if (IsInBounds)
127 return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, PC, IdxList));
128 else
129 return Fold(ConstantExpr::getGetElementPtr(Ty, PC, IdxList));
130 }
131 return nullptr;
132 }
133
134 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
135 auto *CC = dyn_cast<Constant>(C);
136 auto *TC = dyn_cast<Constant>(True);
137 auto *FC = dyn_cast<Constant>(False);
138 if (CC && TC && FC)
139 return ConstantFoldSelectInstruction(CC, TC, FC);
140
141 return nullptr;
142 }
143
145 ArrayRef<unsigned> IdxList) const override {
146 if (auto *CAgg = dyn_cast<Constant>(Agg))
147 return ConstantFoldExtractValueInstruction(CAgg, IdxList);
148 return nullptr;
149 };
150
152 ArrayRef<unsigned> IdxList) const override {
153 auto *CAgg = dyn_cast<Constant>(Agg);
154 auto *CVal = dyn_cast<Constant>(Val);
155 if (CAgg && CVal)
156 return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList);
157 return nullptr;
158 }
159
160 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
161 auto *CVec = dyn_cast<Constant>(Vec);
162 auto *CIdx = dyn_cast<Constant>(Idx);
163 if (CVec && CIdx)
164 return Fold(ConstantExpr::getExtractElement(CVec, CIdx));
165 return nullptr;
166 }
167
169 Value *Idx) const override {
170 auto *CVec = dyn_cast<Constant>(Vec);
171 auto *CNewElt = dyn_cast<Constant>(NewElt);
172 auto *CIdx = dyn_cast<Constant>(Idx);
173 if (CVec && CNewElt && CIdx)
174 return Fold(ConstantExpr::getInsertElement(CVec, CNewElt, CIdx));
175 return nullptr;
176 }
177
179 ArrayRef<int> Mask) const override {
180 auto *C1 = dyn_cast<Constant>(V1);
181 auto *C2 = dyn_cast<Constant>(V2);
182 if (C1 && C2)
183 return Fold(ConstantExpr::getShuffleVector(C1, C2, Mask));
184 return nullptr;
185 }
186
187 //===--------------------------------------------------------------------===//
188 // Cast/Conversion Operators
189 //===--------------------------------------------------------------------===//
190
192 Type *DestTy) const override {
193 if (C->getType() == DestTy)
194 return C; // avoid calling Fold
195 return Fold(ConstantExpr::getCast(Op, C, DestTy));
196 }
198 bool isSigned) const override {
199 if (C->getType() == DestTy)
200 return C; // avoid calling Fold
201 return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
202 }
203 Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
204 if (C->getType() == DestTy)
205 return C; // avoid calling Fold
206 return Fold(ConstantExpr::getPointerCast(C, DestTy));
207 }
208 Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
209 if (C->getType() == DestTy)
210 return C; // avoid calling Fold
211 return Fold(ConstantExpr::getFPCast(C, DestTy));
212 }
213 Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
214 return CreateCast(Instruction::BitCast, C, DestTy);
215 }
216 Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
217 return CreateCast(Instruction::IntToPtr, C, DestTy);
218 }
219 Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
220 return CreateCast(Instruction::PtrToInt, C, DestTy);
221 }
222 Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
223 if (C->getType() == DestTy)
224 return C; // avoid calling Fold
225 return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
226 }
227 Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
228 if (C->getType() == DestTy)
229 return C; // avoid calling Fold
230 return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
231 }
232 Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
233 if (C->getType() == DestTy)
234 return C; // avoid calling Fold
235 return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
236 }
237
239 Type *DestTy) const override {
240 if (C->getType() == DestTy)
241 return C; // avoid calling Fold
243 }
244
245 //===--------------------------------------------------------------------===//
246 // Compare Instructions
247 //===--------------------------------------------------------------------===//
248
250 Constant *RHS) const override {
251 return Fold(ConstantExpr::getCompare(P, LHS, RHS));
252 }
253};
254
255}
256
257#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
RelocType Type
Definition: COFFYAML.cpp:390
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)
@ Flags
Definition: TextStubV5.cpp:93
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:2009
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2530
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1258
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:2032
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2015
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2047
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1964
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2552
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2575
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
Definition: Constants.h:1342
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:1232
static Constant * getIntegerCast(Constant *C, Type *Ty, bool IsSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:2058
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2003
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:2254
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
Definition: Constants.cpp:2315
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:2070
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:2392
This is an important base class in LLVM.
Definition: Constant.h:41
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:21
IRBuilderFolder - Interface for constant folding in IRBuilder.
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:34
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Definition: TargetFolder.h:102
Constant * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:203
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
Definition: TargetFolder.h:178
TargetFolder(const DataLayout &DL)
Definition: TargetFolder.h:45
Value * FoldInsertElement(Value *Vec, Value *NewElt, Value *Idx) const override
Definition: TargetFolder.h:168
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Definition: TargetFolder.h:134
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:249
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:216
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Definition: TargetFolder.h:144
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Definition: TargetFolder.h:66
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:238
Value * FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override
Definition: TargetFolder.h:110
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:222
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Definition: TargetFolder.h:79
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:191
Value * FoldBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS) const override
Definition: TargetFolder.h:54
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:232
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:227
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Definition: TargetFolder.h:97
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: TargetFolder.h:197
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) const override
Definition: TargetFolder.h:117
Constant * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:208
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Definition: TargetFolder.h:151
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:219
Value * FoldExtractElement(Value *Vec, Value *Idx) const override
Definition: TargetFolder.h:160
Constant * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:213
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 * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction 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 * 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:1826
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...