Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/IR/ConstantFolder.h
Line
Count
Source (jump to first uncovered line)
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/IR/Constants.h"
21
#include "llvm/IR/InstrTypes.h"
22
#include "llvm/IR/Instruction.h"
23
24
namespace llvm {
25
26
/// ConstantFolder - Create constants with minimum, target independent, folding.
27
class ConstantFolder {
28
public:
29
  explicit ConstantFolder() = default;
30
31
  //===--------------------------------------------------------------------===//
32
  // Binary Operators
33
  //===--------------------------------------------------------------------===//
34
35
  Constant *CreateAdd(Constant *LHS, Constant *RHS,
36
54.9k
                      bool HasNUW = false, bool HasNSW = false) const {
37
54.9k
    return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
38
54.9k
  }
39
40
0
  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
41
0
    return ConstantExpr::getFAdd(LHS, RHS);
42
0
  }
43
44
  Constant *CreateSub(Constant *LHS, Constant *RHS,
45
124k
                      bool HasNUW = false, bool HasNSW = false) const {
46
124k
    return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
47
124k
  }
48
49
0
  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
50
0
    return ConstantExpr::getFSub(LHS, RHS);
51
0
  }
52
53
  Constant *CreateMul(Constant *LHS, Constant *RHS,
54
49.2k
                      bool HasNUW = false, bool HasNSW = false) const {
55
49.2k
    return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
56
49.2k
  }
57
58
0
  Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
59
0
    return ConstantExpr::getFMul(LHS, RHS);
60
0
  }
61
62
  Constant *CreateUDiv(Constant *LHS, Constant *RHS,
63
3.56k
                       bool isExact = false) const {
64
3.56k
    return ConstantExpr::getUDiv(LHS, RHS, isExact);
65
3.56k
  }
66
67
  Constant *CreateSDiv(Constant *LHS, Constant *RHS,
68
25.0k
                       bool isExact = false) const {
69
25.0k
    return ConstantExpr::getSDiv(LHS, RHS, isExact);
70
25.0k
  }
71
72
0
  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
73
0
    return ConstantExpr::getFDiv(LHS, RHS);
74
0
  }
75
76
0
  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
77
0
    return ConstantExpr::getURem(LHS, RHS);
78
0
  }
79
80
0
  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
81
0
    return ConstantExpr::getSRem(LHS, RHS);
82
0
  }
83
84
0
  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
85
0
    return ConstantExpr::getFRem(LHS, RHS);
86
0
  }
87
88
  Constant *CreateShl(Constant *LHS, Constant *RHS,
89
23.2k
                      bool HasNUW = false, bool HasNSW = false) const {
90
23.2k
    return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
91
23.2k
  }
92
93
  Constant *CreateLShr(Constant *LHS, Constant *RHS,
94
1.01k
                       bool isExact = false) const {
95
1.01k
    return ConstantExpr::getLShr(LHS, RHS, isExact);
96
1.01k
  }
97
98
  Constant *CreateAShr(Constant *LHS, Constant *RHS,
99
1.73k
                       bool isExact = false) const {
100
1.73k
    return ConstantExpr::getAShr(LHS, RHS, isExact);
101
1.73k
  }
102
103
2.74k
  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
104
2.74k
    return ConstantExpr::getAnd(LHS, RHS);
105
2.74k
  }
106
107
3.56k
  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
108
3.56k
    return ConstantExpr::getOr(LHS, RHS);
109
3.56k
  }
110
111
0
  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
112
0
    return ConstantExpr::getXor(LHS, RHS);
113
0
  }
114
115
  Constant *CreateBinOp(Instruction::BinaryOps Opc,
116
89.0k
                        Constant *LHS, Constant *RHS) const {
117
89.0k
    return ConstantExpr::get(Opc, LHS, RHS);
118
89.0k
  }
119
120
  //===--------------------------------------------------------------------===//
121
  // Unary Operators
122
  //===--------------------------------------------------------------------===//
123
124
  Constant *CreateNeg(Constant *C,
125
2.41k
                      bool HasNUW = false, bool HasNSW = false) const {
126
2.41k
    return ConstantExpr::getNeg(C, HasNUW, HasNSW);
127
2.41k
  }
128
129
15
  Constant *CreateFNeg(Constant *C) const {
130
15
    return ConstantExpr::getFNeg(C);
131
15
  }
132
133
3.82k
  Constant *CreateNot(Constant *C) const {
134
3.82k
    return ConstantExpr::getNot(C);
135
3.82k
  }
136
137
0
  Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
138
0
    return ConstantExpr::get(Opc, C);
139
0
  }
140
141
  //===--------------------------------------------------------------------===//
142
  // Memory Instructions
143
  //===--------------------------------------------------------------------===//
144
145
  Constant *CreateGetElementPtr(Type *Ty, Constant *C,
146
0
                                ArrayRef<Constant *> IdxList) const {
147
0
    return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
148
0
  }
149
150
265
  Constant *CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const {
151
265
    // This form of the function only exists to avoid ambiguous overload
152
265
    // warnings about whether to convert Idx to ArrayRef<Constant *> or
153
265
    // ArrayRef<Value *>.
154
265
    return ConstantExpr::getGetElementPtr(Ty, C, Idx);
155
265
  }
156
157
  Constant *CreateGetElementPtr(Type *Ty, Constant *C,
158
590
                                ArrayRef<Value *> IdxList) const {
159
590
    return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
160
590
  }
161
162
  Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
163
0
                                        ArrayRef<Constant *> IdxList) const {
164
0
    return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
165
0
  }
166
167
  Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
168
1
                                        Constant *Idx) const {
169
1
    // This form of the function only exists to avoid ambiguous overload
170
1
    // warnings about whether to convert Idx to ArrayRef<Constant *> or
171
1
    // ArrayRef<Value *>.
172
1
    return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
173
1
  }
174
175
  Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
176
178k
                                        ArrayRef<Value *> IdxList) const {
177
178k
    return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
178
178k
  }
179
180
  //===--------------------------------------------------------------------===//
181
  // Cast/Conversion Operators
182
  //===--------------------------------------------------------------------===//
183
184
  Constant *CreateCast(Instruction::CastOps Op, Constant *C,
185
121k
                       Type *DestTy) const {
186
121k
    return ConstantExpr::getCast(Op, C, DestTy);
187
121k
  }
188
189
11.2k
  Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
190
11.2k
    return ConstantExpr::getPointerCast(C, DestTy);
191
11.2k
  }
192
193
  Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
194
1.19k
                                                Type *DestTy) const {
195
1.19k
    return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
196
1.19k
  }
197
198
  Constant *CreateIntCast(Constant *C, Type *DestTy,
199
280k
                          bool isSigned) const {
200
280k
    return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
201
280k
  }
202
203
0
  Constant *CreateFPCast(Constant *C, Type *DestTy) const {
204
0
    return ConstantExpr::getFPCast(C, DestTy);
205
0
  }
206
207
1
  Constant *CreateBitCast(Constant *C, Type *DestTy) const {
208
1
    return CreateCast(Instruction::BitCast, C, DestTy);
209
1
  }
210
211
0
  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
212
0
    return CreateCast(Instruction::IntToPtr, C, DestTy);
213
0
  }
214
215
0
  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
216
0
    return CreateCast(Instruction::PtrToInt, C, DestTy);
217
0
  }
218
219
8.34k
  Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
220
8.34k
    return ConstantExpr::getZExtOrBitCast(C, DestTy);
221
8.34k
  }
222
223
0
  Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
224
0
    return ConstantExpr::getSExtOrBitCast(C, DestTy);
225
0
  }
226
227
448
  Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
228
448
    return ConstantExpr::getTruncOrBitCast(C, DestTy);
229
448
  }
230
231
  //===--------------------------------------------------------------------===//
232
  // Compare Instructions
233
  //===--------------------------------------------------------------------===//
234
235
  Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
236
21.8k
                       Constant *RHS) const {
237
21.8k
    return ConstantExpr::getCompare(P, LHS, RHS);
238
21.8k
  }
239
240
  Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
241
46
                       Constant *RHS) const {
242
46
    return ConstantExpr::getCompare(P, LHS, RHS);
243
46
  }
244
245
  //===--------------------------------------------------------------------===//
246
  // Other Instructions
247
  //===--------------------------------------------------------------------===//
248
249
124
  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
250
124
    return ConstantExpr::getSelect(C, True, False);
251
124
  }
252
253
537
  Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
254
537
    return ConstantExpr::getExtractElement(Vec, Idx);
255
537
  }
256
257
  Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
258
197k
                                Constant *Idx) const {
259
197k
    return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
260
197k
  }
261
262
  Constant *CreateShuffleVector(Constant *V1, Constant *V2,
263
72.9k
                                Constant *Mask) const {
264
72.9k
    return ConstantExpr::getShuffleVector(V1, V2, Mask);
265
72.9k
  }
266
267
  Constant *CreateExtractValue(Constant *Agg,
268
465
                               ArrayRef<unsigned> IdxList) const {
269
465
    return ConstantExpr::getExtractValue(Agg, IdxList);
270
465
  }
271
272
  Constant *CreateInsertValue(Constant *Agg, Constant *Val,
273
50
                              ArrayRef<unsigned> IdxList) const {
274
50
    return ConstantExpr::getInsertValue(Agg, Val, IdxList);
275
50
  }
276
};
277
278
} // end namespace llvm
279
280
#endif // LLVM_IR_CONSTANTFOLDER_H