LLVM  12.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"
24 #include "llvm/IR/InstrTypes.h"
26 
27 namespace llvm {
28 
29 class DataLayout;
30 
31 /// TargetFolder - Create constants with target dependent folding.
32 class TargetFolder final : public IRBuilderFolder {
33  const DataLayout &DL;
34 
35  /// Fold - Fold the constant using target specific information.
36  Constant *Fold(Constant *C) const {
37  return ConstantFoldConstant(C, DL);
38  }
39 
40  virtual void anchor();
41 
42 public:
43  explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
44 
45  //===--------------------------------------------------------------------===//
46  // Binary Operators
47  //===--------------------------------------------------------------------===//
48 
50  bool HasNUW = false, bool HasNSW = false) const override {
51  return Fold(ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW));
52  }
53  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const override {
54  return Fold(ConstantExpr::getFAdd(LHS, RHS));
55  }
57  bool HasNUW = false, bool HasNSW = false) const override {
58  return Fold(ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW));
59  }
60  Constant *CreateFSub(Constant *LHS, Constant *RHS) const override {
61  return Fold(ConstantExpr::getFSub(LHS, RHS));
62  }
64  bool HasNUW = false, bool HasNSW = false) const override {
65  return Fold(ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW));
66  }
67  Constant *CreateFMul(Constant *LHS, Constant *RHS) const override {
68  return Fold(ConstantExpr::getFMul(LHS, RHS));
69  }
71  bool isExact = false) const override {
72  return Fold(ConstantExpr::getUDiv(LHS, RHS, isExact));
73  }
75  bool isExact = false) const override {
76  return Fold(ConstantExpr::getSDiv(LHS, RHS, isExact));
77  }
78  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const override {
79  return Fold(ConstantExpr::getFDiv(LHS, RHS));
80  }
81  Constant *CreateURem(Constant *LHS, Constant *RHS) const override {
82  return Fold(ConstantExpr::getURem(LHS, RHS));
83  }
84  Constant *CreateSRem(Constant *LHS, Constant *RHS) const override {
85  return Fold(ConstantExpr::getSRem(LHS, RHS));
86  }
87  Constant *CreateFRem(Constant *LHS, Constant *RHS) const override {
88  return Fold(ConstantExpr::getFRem(LHS, RHS));
89  }
91  bool HasNUW = false, bool HasNSW = false) const override {
92  return Fold(ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW));
93  }
95  bool isExact = false) const override {
96  return Fold(ConstantExpr::getLShr(LHS, RHS, isExact));
97  }
99  bool isExact = false) const override {
100  return Fold(ConstantExpr::getAShr(LHS, RHS, isExact));
101  }
102  Constant *CreateAnd(Constant *LHS, Constant *RHS) const override {
103  return Fold(ConstantExpr::getAnd(LHS, RHS));
104  }
105  Constant *CreateOr(Constant *LHS, Constant *RHS) const override {
106  return Fold(ConstantExpr::getOr(LHS, RHS));
107  }
108  Constant *CreateXor(Constant *LHS, Constant *RHS) const override {
109  return Fold(ConstantExpr::getXor(LHS, RHS));
110  }
111 
113  Constant *LHS, Constant *RHS) const override {
114  return Fold(ConstantExpr::get(Opc, LHS, RHS));
115  }
116 
117  //===--------------------------------------------------------------------===//
118  // Unary Operators
119  //===--------------------------------------------------------------------===//
120 
122  bool HasNUW = false, bool HasNSW = false) const override {
123  return Fold(ConstantExpr::getNeg(C, HasNUW, HasNSW));
124  }
125  Constant *CreateFNeg(Constant *C) const override {
126  return Fold(ConstantExpr::getFNeg(C));
127  }
128  Constant *CreateNot(Constant *C) const override {
129  return Fold(ConstantExpr::getNot(C));
130  }
131 
133  return Fold(ConstantExpr::get(Opc, C));
134  }
135 
136  //===--------------------------------------------------------------------===//
137  // Memory Instructions
138  //===--------------------------------------------------------------------===//
139 
141  ArrayRef<Constant *> IdxList) const override {
142  return Fold(ConstantExpr::getGetElementPtr(Ty, C, IdxList));
143  }
145  Constant *Idx) const override {
146  // This form of the function only exists to avoid ambiguous overload
147  // warnings about whether to convert Idx to ArrayRef<Constant *> or
148  // ArrayRef<Value *>.
149  return Fold(ConstantExpr::getGetElementPtr(Ty, C, Idx));
150  }
152  ArrayRef<Value *> IdxList) const override {
153  return Fold(ConstantExpr::getGetElementPtr(Ty, C, IdxList));
154  }
155 
157  Type *Ty, Constant *C, ArrayRef<Constant *> IdxList) const override {
158  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList));
159  }
161  Constant *Idx) const override {
162  // This form of the function only exists to avoid ambiguous overload
163  // warnings about whether to convert Idx to ArrayRef<Constant *> or
164  // ArrayRef<Value *>.
165  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx));
166  }
168  Type *Ty, Constant *C, ArrayRef<Value *> IdxList) const override {
169  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList));
170  }
171 
172  //===--------------------------------------------------------------------===//
173  // Cast/Conversion Operators
174  //===--------------------------------------------------------------------===//
175 
177  Type *DestTy) const override {
178  if (C->getType() == DestTy)
179  return C; // avoid calling Fold
180  return Fold(ConstantExpr::getCast(Op, C, DestTy));
181  }
183  bool isSigned) const override {
184  if (C->getType() == DestTy)
185  return C; // avoid calling Fold
186  return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
187  }
188  Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
189  if (C->getType() == DestTy)
190  return C; // avoid calling Fold
191  return Fold(ConstantExpr::getPointerCast(C, DestTy));
192  }
193  Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
194  if (C->getType() == DestTy)
195  return C; // avoid calling Fold
196  return Fold(ConstantExpr::getFPCast(C, DestTy));
197  }
198  Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
199  return CreateCast(Instruction::BitCast, C, DestTy);
200  }
201  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
202  return CreateCast(Instruction::IntToPtr, C, DestTy);
203  }
204  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
205  return CreateCast(Instruction::PtrToInt, C, DestTy);
206  }
207  Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
208  if (C->getType() == DestTy)
209  return C; // avoid calling Fold
210  return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
211  }
212  Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
213  if (C->getType() == DestTy)
214  return C; // avoid calling Fold
215  return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
216  }
217  Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
218  if (C->getType() == DestTy)
219  return C; // avoid calling Fold
220  return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
221  }
222 
224  Type *DestTy) const override {
225  if (C->getType() == DestTy)
226  return C; // avoid calling Fold
227  return Fold(ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy));
228  }
229 
230  //===--------------------------------------------------------------------===//
231  // Compare Instructions
232  //===--------------------------------------------------------------------===//
233 
235  Constant *RHS) const override {
236  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
237  }
239  Constant *RHS) const override {
240  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
241  }
242 
243  //===--------------------------------------------------------------------===//
244  // Other Instructions
245  //===--------------------------------------------------------------------===//
246 
248  Constant *False) const override {
249  return Fold(ConstantExpr::getSelect(C, True, False));
250  }
251 
252  Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const override {
253  return Fold(ConstantExpr::getExtractElement(Vec, Idx));
254  }
255 
257  Constant *Idx) const override {
258  return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
259  }
260 
262  ArrayRef<int> Mask) const override {
263  return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
264  }
265 
267  ArrayRef<unsigned> IdxList) const override {
268  return Fold(ConstantExpr::getExtractValue(Agg, IdxList));
269  }
270 
272  ArrayRef<unsigned> IdxList) const override {
273  return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList));
274  }
275 };
276 
277 }
278 
279 #endif
uint64_t CallInst * C
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2488
Constant * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: TargetFolder.h:94
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:1852
Constant * CreateFMul(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:67
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1166
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const override
Definition: TargetFolder.h:167
Constant * CreateFDiv(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:78
Constant * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: TargetFolder.h:49
Constant * CreateURem(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:81
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2343
Constant * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: TargetFolder.h:90
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:2189
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2492
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:725
Constant * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: TargetFolder.h:63
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2365
Constant * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const override
Definition: TargetFolder.h:271
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2481
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2510
Constant * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:112
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const override
Definition: TargetFolder.h:156
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1863
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2559
Constant * CreateFAdd(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:53
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2388
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:238
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:176
Constant * CreateNot(Constant *C) const override
Definition: TargetFolder.h:128
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
Constant * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: TargetFolder.h:56
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const override
Definition: TargetFolder.h:144
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:1875
Constant * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: TargetFolder.h:74
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2564
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:2211
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:32
Constant * CreateSRem(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:84
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2514
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2524
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:223
Constant * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:193
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:201
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2414
#define P(N)
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2469
static Constant * getFRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2536
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:207
This is an important base class in LLVM.
Definition: Constant.h:41
Constant * CreateOr(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:105
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2540
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1825
Constant * CreateAnd(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:102
Constant * CreateFRem(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:87
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const override
Definition: TargetFolder.h:140
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2475
Constant * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: TargetFolder.h:98
Constant * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: TargetFolder.h:70
Constant * CreateFNeg(Constant *C) const override
Definition: TargetFolder.h:125
IRBuilderFolder - Interface for constant folding in IRBuilder.
Constant * CreateExtractElement(Constant *Vec, Constant *Idx) const override
Definition: TargetFolder.h:252
Constant * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const override
Definition: TargetFolder.h:121
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1837
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:217
Constant * CreateShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask) const override
Definition: TargetFolder.h:261
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2519
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const override
Definition: TargetFolder.h:160
Constant * CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override
Definition: TargetFolder.h:132
Constant * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const override
Definition: TargetFolder.h:256
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1780
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1819
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:212
static Constant * getFSub(Constant *C1, Constant *C2)
Definition: Constants.cpp:2499
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1831
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2462
Constant * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:198
Constant * CreateSelect(Constant *C, Constant *True, Constant *False) const override
Definition: TargetFolder.h:247
Constant * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:234
Constant * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const override
Definition: TargetFolder.h:266
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:204
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1193
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2544
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: TargetFolder.h:182
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2552
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const override
Definition: TargetFolder.h:151
Constant * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:188
static Constant * getSRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2532
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2528
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2438
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2503
Constant * CreateFSub(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:60
TargetFolder(const DataLayout &DL)
Definition: TargetFolder.h:43
Constant * CreateXor(Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:108
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2548
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.
Definition: Constants.cpp:2058