Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/IR/NoFolder.h
Line
Count
Source (jump to first uncovered line)
1
//===- NoFolder.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 NoFolder class, a helper for IRBuilder.  It provides
10
// IRBuilder with a set of methods for creating unfolded constants.  This is
11
// useful for learners trying to understand how LLVM IR works, and who don't
12
// want details to be hidden by the constant folder.  For general constant
13
// creation and folding, use ConstantExpr and the routines in
14
// llvm/Analysis/ConstantFolding.h.
15
//
16
// Note: since it is not actually possible to create unfolded constants, this
17
// class returns instructions rather than constants.
18
//
19
//===----------------------------------------------------------------------===//
20
21
#ifndef LLVM_IR_NOFOLDER_H
22
#define LLVM_IR_NOFOLDER_H
23
24
#include "llvm/ADT/ArrayRef.h"
25
#include "llvm/IR/Constants.h"
26
#include "llvm/IR/InstrTypes.h"
27
#include "llvm/IR/Instruction.h"
28
#include "llvm/IR/Instructions.h"
29
30
namespace llvm {
31
32
/// NoFolder - Create "constants" (actually, instructions) with no folding.
33
class NoFolder {
34
public:
35
  explicit NoFolder() = default;
36
37
  //===--------------------------------------------------------------------===//
38
  // Binary Operators
39
  //===--------------------------------------------------------------------===//
40
41
  Instruction *CreateAdd(Constant *LHS, Constant *RHS,
42
                         bool HasNUW = false, bool HasNSW = false) const {
43
    BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS);
44
    if (HasNUW) BO->setHasNoUnsignedWrap();
45
    if (HasNSW) BO->setHasNoSignedWrap();
46
    return BO;
47
  }
48
49
0
  Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
50
0
    return BinaryOperator::CreateNSWAdd(LHS, RHS);
51
0
  }
52
53
0
  Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
54
0
    return BinaryOperator::CreateNUWAdd(LHS, RHS);
55
0
  }
56
57
0
  Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
58
0
    return BinaryOperator::CreateFAdd(LHS, RHS);
59
0
  }
60
61
  Instruction *CreateSub(Constant *LHS, Constant *RHS,
62
                         bool HasNUW = false, bool HasNSW = false) const {
63
    BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
64
    if (HasNUW) BO->setHasNoUnsignedWrap();
65
    if (HasNSW) BO->setHasNoSignedWrap();
66
    return BO;
67
  }
68
69
0
  Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
70
0
    return BinaryOperator::CreateNSWSub(LHS, RHS);
71
0
  }
72
73
0
  Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
74
0
    return BinaryOperator::CreateNUWSub(LHS, RHS);
75
0
  }
76
77
0
  Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
78
0
    return BinaryOperator::CreateFSub(LHS, RHS);
79
0
  }
80
81
  Instruction *CreateMul(Constant *LHS, Constant *RHS,
82
                         bool HasNUW = false, bool HasNSW = false) const {
83
    BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS);
84
    if (HasNUW) BO->setHasNoUnsignedWrap();
85
    if (HasNSW) BO->setHasNoSignedWrap();
86
    return BO;
87
  }
88
89
0
  Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
90
0
    return BinaryOperator::CreateNSWMul(LHS, RHS);
91
0
  }
92
93
0
  Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
94
0
    return BinaryOperator::CreateNUWMul(LHS, RHS);
95
0
  }
96
97
0
  Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
98
0
    return BinaryOperator::CreateFMul(LHS, RHS);
99
0
  }
100
101
  Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
102
0
                          bool isExact = false) const {
103
0
    if (!isExact)
104
0
      return BinaryOperator::CreateUDiv(LHS, RHS);
105
0
    return BinaryOperator::CreateExactUDiv(LHS, RHS);
106
0
  }
107
108
0
  Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
109
0
    return BinaryOperator::CreateExactUDiv(LHS, RHS);
110
0
  }
111
112
  Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
113
0
                          bool isExact = false) const {
114
0
    if (!isExact)
115
0
      return BinaryOperator::CreateSDiv(LHS, RHS);
116
0
    return BinaryOperator::CreateExactSDiv(LHS, RHS);
117
0
  }
118
119
0
  Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
120
0
    return BinaryOperator::CreateExactSDiv(LHS, RHS);
121
0
  }
122
123
0
  Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
124
0
    return BinaryOperator::CreateFDiv(LHS, RHS);
125
0
  }
126
127
0
  Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
128
0
    return BinaryOperator::CreateURem(LHS, RHS);
129
0
  }
130
131
0
  Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
132
0
    return BinaryOperator::CreateSRem(LHS, RHS);
133
0
  }
134
135
0
  Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
136
0
    return BinaryOperator::CreateFRem(LHS, RHS);
137
0
  }
138
139
  Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
140
                         bool HasNSW = false) const {
141
    BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
142
    if (HasNUW) BO->setHasNoUnsignedWrap();
143
    if (HasNSW) BO->setHasNoSignedWrap();
144
    return BO;
145
  }
146
147
  Instruction *CreateLShr(Constant *LHS, Constant *RHS,
148
0
                          bool isExact = false) const {
149
0
    if (!isExact)
150
0
      return BinaryOperator::CreateLShr(LHS, RHS);
151
0
    return BinaryOperator::CreateExactLShr(LHS, RHS);
152
0
  }
153
154
  Instruction *CreateAShr(Constant *LHS, Constant *RHS,
155
0
                          bool isExact = false) const {
156
0
    if (!isExact)
157
0
      return BinaryOperator::CreateAShr(LHS, RHS);
158
0
    return BinaryOperator::CreateExactAShr(LHS, RHS);
159
0
  }
160
161
  Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
162
    return BinaryOperator::CreateAnd(LHS, RHS);
163
  }
164
165
0
  Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
166
0
    return BinaryOperator::CreateOr(LHS, RHS);
167
0
  }
168
169
0
  Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
170
0
    return BinaryOperator::CreateXor(LHS, RHS);
171
0
  }
172
173
  Instruction *CreateBinOp(Instruction::BinaryOps Opc,
174
5
                           Constant *LHS, Constant *RHS) const {
175
5
    return BinaryOperator::Create(Opc, LHS, RHS);
176
5
  }
177
178
  //===--------------------------------------------------------------------===//
179
  // Unary Operators
180
  //===--------------------------------------------------------------------===//
181
182
  Instruction *CreateNeg(Constant *C,
183
0
                         bool HasNUW = false, bool HasNSW = false) const {
184
0
    BinaryOperator *BO = BinaryOperator::CreateNeg(C);
185
0
    if (HasNUW) BO->setHasNoUnsignedWrap();
186
0
    if (HasNSW) BO->setHasNoSignedWrap();
187
0
    return BO;
188
0
  }
189
190
0
  Instruction *CreateNSWNeg(Constant *C) const {
191
0
    return BinaryOperator::CreateNSWNeg(C);
192
0
  }
193
194
0
  Instruction *CreateNUWNeg(Constant *C) const {
195
0
    return BinaryOperator::CreateNUWNeg(C);
196
0
  }
197
198
  Instruction *CreateFNeg(Constant *C) const {
199
    return BinaryOperator::CreateFNeg(C);
200
  }
201
202
16
  Instruction *CreateNot(Constant *C) const {
203
16
    return BinaryOperator::CreateNot(C);
204
16
  }
205
206
0
  Instruction *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
207
0
    return UnaryOperator::Create(Opc, C);
208
0
  }
209
210
  //===--------------------------------------------------------------------===//
211
  // Memory Instructions
212
  //===--------------------------------------------------------------------===//
213
214
  Constant *CreateGetElementPtr(Type *Ty, Constant *C,
215
0
                                ArrayRef<Constant *> IdxList) const {
216
0
    return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
217
0
  }
218
219
0
  Constant *CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const {
220
0
    // This form of the function only exists to avoid ambiguous overload
221
0
    // warnings about whether to convert Idx to ArrayRef<Constant *> or
222
0
    // ArrayRef<Value *>.
223
0
    return ConstantExpr::getGetElementPtr(Ty, C, Idx);
224
0
  }
225
226
  Instruction *CreateGetElementPtr(Type *Ty, Constant *C,
227
25
                                   ArrayRef<Value *> IdxList) const {
228
25
    return GetElementPtrInst::Create(Ty, C, IdxList);
229
25
  }
230
231
  Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
232
0
                                        ArrayRef<Constant *> IdxList) const {
233
0
    return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
234
0
  }
235
236
  Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
237
0
                                        Constant *Idx) const {
238
0
    // This form of the function only exists to avoid ambiguous overload
239
0
    // warnings about whether to convert Idx to ArrayRef<Constant *> or
240
0
    // ArrayRef<Value *>.
241
0
    return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
242
0
  }
243
244
  Instruction *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
245
8
                                           ArrayRef<Value *> IdxList) const {
246
8
    return GetElementPtrInst::CreateInBounds(Ty, C, IdxList);
247
8
  }
248
249
  //===--------------------------------------------------------------------===//
250
  // Cast/Conversion Operators
251
  //===--------------------------------------------------------------------===//
252
253
  Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
254
2
                    Type *DestTy) const {
255
2
    return CastInst::Create(Op, C, DestTy);
256
2
  }
257
258
0
  Instruction *CreatePointerCast(Constant *C, Type *DestTy) const {
259
0
    return CastInst::CreatePointerCast(C, DestTy);
260
0
  }
261
262
  Instruction *CreateIntCast(Constant *C, Type *DestTy,
263
0
                       bool isSigned) const {
264
0
    return CastInst::CreateIntegerCast(C, DestTy, isSigned);
265
0
  }
266
267
0
  Instruction *CreateFPCast(Constant *C, Type *DestTy) const {
268
0
    return CastInst::CreateFPCast(C, DestTy);
269
0
  }
270
271
0
  Instruction *CreateBitCast(Constant *C, Type *DestTy) const {
272
0
    return CreateCast(Instruction::BitCast, C, DestTy);
273
0
  }
274
275
0
  Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const {
276
0
    return CreateCast(Instruction::IntToPtr, C, DestTy);
277
0
  }
278
279
0
  Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const {
280
0
    return CreateCast(Instruction::PtrToInt, C, DestTy);
281
0
  }
282
283
0
  Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
284
0
    return CastInst::CreateZExtOrBitCast(C, DestTy);
285
0
  }
286
287
0
  Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
288
0
    return CastInst::CreateSExtOrBitCast(C, DestTy);
289
0
  }
290
291
0
  Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
292
0
    return CastInst::CreateTruncOrBitCast(C, DestTy);
293
0
  }
294
295
  //===--------------------------------------------------------------------===//
296
  // Compare Instructions
297
  //===--------------------------------------------------------------------===//
298
299
  Instruction *CreateICmp(CmpInst::Predicate P,
300
                          Constant *LHS, Constant *RHS) const {
301
    return new ICmpInst(P, LHS, RHS);
302
  }
303
304
  Instruction *CreateFCmp(CmpInst::Predicate P,
305
                          Constant *LHS, Constant *RHS) const {
306
    return new FCmpInst(P, LHS, RHS);
307
  }
308
309
  //===--------------------------------------------------------------------===//
310
  // Other Instructions
311
  //===--------------------------------------------------------------------===//
312
313
  Instruction *CreateSelect(Constant *C,
314
7
                            Constant *True, Constant *False) const {
315
7
    return SelectInst::Create(C, True, False);
316
7
  }
317
318
  Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
319
    return ExtractElementInst::Create(Vec, Idx);
320
  }
321
322
  Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
323
                                   Constant *Idx) const {
324
    return InsertElementInst::Create(Vec, NewElt, Idx);
325
  }
326
327
  Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
328
0
                                   Constant *Mask) const {
329
0
    return new ShuffleVectorInst(V1, V2, Mask);
330
0
  }
331
332
  Instruction *CreateExtractValue(Constant *Agg,
333
0
                                  ArrayRef<unsigned> IdxList) const {
334
0
    return ExtractValueInst::Create(Agg, IdxList);
335
0
  }
336
337
  Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
338
0
                                 ArrayRef<unsigned> IdxList) const {
339
0
    return InsertValueInst::Create(Agg, Val, IdxList);
340
0
  }
341
};
342
343
} // end namespace llvm
344
345
#endif // LLVM_IR_NOFOLDER_H