Coverage Report

Created: 2019-02-20 07:29

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