/src/llvm-project/llvm/lib/IR/Constants.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
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 implements the Constant* classes. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "llvm/IR/Constants.h" |
14 | | #include "LLVMContextImpl.h" |
15 | | #include "llvm/ADT/STLExtras.h" |
16 | | #include "llvm/ADT/SmallVector.h" |
17 | | #include "llvm/ADT/StringMap.h" |
18 | | #include "llvm/IR/BasicBlock.h" |
19 | | #include "llvm/IR/ConstantFold.h" |
20 | | #include "llvm/IR/DerivedTypes.h" |
21 | | #include "llvm/IR/Function.h" |
22 | | #include "llvm/IR/GetElementPtrTypeIterator.h" |
23 | | #include "llvm/IR/GlobalAlias.h" |
24 | | #include "llvm/IR/GlobalIFunc.h" |
25 | | #include "llvm/IR/GlobalValue.h" |
26 | | #include "llvm/IR/GlobalVariable.h" |
27 | | #include "llvm/IR/Instructions.h" |
28 | | #include "llvm/IR/Operator.h" |
29 | | #include "llvm/IR/PatternMatch.h" |
30 | | #include "llvm/Support/ErrorHandling.h" |
31 | | #include "llvm/Support/MathExtras.h" |
32 | | #include "llvm/Support/raw_ostream.h" |
33 | | #include <algorithm> |
34 | | |
35 | | using namespace llvm; |
36 | | using namespace PatternMatch; |
37 | | |
38 | | //===----------------------------------------------------------------------===// |
39 | | // Constant Class |
40 | | //===----------------------------------------------------------------------===// |
41 | | |
42 | 0 | bool Constant::isNegativeZeroValue() const { |
43 | | // Floating point values have an explicit -0.0 value. |
44 | 0 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
45 | 0 | return CFP->isZero() && CFP->isNegative(); |
46 | | |
47 | | // Equivalent for a vector of -0.0's. |
48 | 0 | if (getType()->isVectorTy()) |
49 | 0 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue())) |
50 | 0 | return SplatCFP->isNegativeZeroValue(); |
51 | | |
52 | | // We've already handled true FP case; any other FP vectors can't represent -0.0. |
53 | 0 | if (getType()->isFPOrFPVectorTy()) |
54 | 0 | return false; |
55 | | |
56 | | // Otherwise, just use +0.0. |
57 | 0 | return isNullValue(); |
58 | 0 | } |
59 | | |
60 | | // Return true iff this constant is positive zero (floating point), negative |
61 | | // zero (floating point), or a null value. |
62 | 66.6k | bool Constant::isZeroValue() const { |
63 | | // Floating point values have an explicit -0.0 value. |
64 | 66.6k | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
65 | 5.60k | return CFP->isZero(); |
66 | | |
67 | | // Check for constant splat vectors of 1 values. |
68 | 61.0k | if (getType()->isVectorTy()) |
69 | 3.15k | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue())) |
70 | 121 | return SplatCFP->isZero(); |
71 | | |
72 | | // Otherwise, just use +0.0. |
73 | 60.9k | return isNullValue(); |
74 | 61.0k | } |
75 | | |
76 | 6.26M | bool Constant::isNullValue() const { |
77 | | // 0 is null. |
78 | 6.26M | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
79 | 5.47M | return CI->isZero(); |
80 | | |
81 | | // +0.0 is null. |
82 | 791k | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
83 | | // ppc_fp128 determine isZero using high order double only |
84 | | // Should check the bitwise value to make sure all bits are zero. |
85 | 139k | return CFP->isExactlyValue(+0.0); |
86 | | |
87 | | // constant zero is zero for aggregates, cpnull is null for pointers, none for |
88 | | // tokens. |
89 | 652k | return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) || |
90 | 652k | isa<ConstantTokenNone>(this) || isa<ConstantTargetNone>(this); |
91 | 791k | } |
92 | | |
93 | 70.0k | bool Constant::isAllOnesValue() const { |
94 | | // Check for -1 integers |
95 | 70.0k | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
96 | 47.2k | return CI->isMinusOne(); |
97 | | |
98 | | // Check for FP which are bitcasted from -1 integers |
99 | 22.8k | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
100 | 1.79k | return CFP->getValueAPF().bitcastToAPInt().isAllOnes(); |
101 | | |
102 | | // Check for constant splat vectors of 1 values. |
103 | 21.0k | if (getType()->isVectorTy()) |
104 | 8.05k | if (const auto *SplatVal = getSplatValue()) |
105 | 2.19k | return SplatVal->isAllOnesValue(); |
106 | | |
107 | 18.8k | return false; |
108 | 21.0k | } |
109 | | |
110 | 22.0k | bool Constant::isOneValue() const { |
111 | | // Check for 1 integers |
112 | 22.0k | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
113 | 20.8k | return CI->isOne(); |
114 | | |
115 | | // Check for FP which are bitcasted from 1 integers |
116 | 1.16k | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
117 | 0 | return CFP->getValueAPF().bitcastToAPInt().isOne(); |
118 | | |
119 | | // Check for constant splat vectors of 1 values. |
120 | 1.16k | if (getType()->isVectorTy()) |
121 | 954 | if (const auto *SplatVal = getSplatValue()) |
122 | 184 | return SplatVal->isOneValue(); |
123 | | |
124 | 983 | return false; |
125 | 1.16k | } |
126 | | |
127 | 181 | bool Constant::isNotOneValue() const { |
128 | | // Check for 1 integers |
129 | 181 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
130 | 134 | return !CI->isOneValue(); |
131 | | |
132 | | // Check for FP which are bitcasted from 1 integers |
133 | 47 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
134 | 0 | return !CFP->getValueAPF().bitcastToAPInt().isOne(); |
135 | | |
136 | | // Check that vectors don't contain 1 |
137 | 47 | if (auto *VTy = dyn_cast<FixedVectorType>(getType())) { |
138 | 111 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
139 | 78 | Constant *Elt = getAggregateElement(I); |
140 | 78 | if (!Elt || !Elt->isNotOneValue()) |
141 | 6 | return false; |
142 | 78 | } |
143 | 33 | return true; |
144 | 39 | } |
145 | | |
146 | | // Check for splats that don't contain 1 |
147 | 8 | if (getType()->isVectorTy()) |
148 | 8 | if (const auto *SplatVal = getSplatValue()) |
149 | 8 | return SplatVal->isNotOneValue(); |
150 | | |
151 | | // It *may* contain 1, we can't tell. |
152 | 0 | return false; |
153 | 8 | } |
154 | | |
155 | 0 | bool Constant::isMinSignedValue() const { |
156 | | // Check for INT_MIN integers |
157 | 0 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
158 | 0 | return CI->isMinValue(/*isSigned=*/true); |
159 | | |
160 | | // Check for FP which are bitcasted from INT_MIN integers |
161 | 0 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
162 | 0 | return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue(); |
163 | | |
164 | | // Check for splats of INT_MIN values. |
165 | 0 | if (getType()->isVectorTy()) |
166 | 0 | if (const auto *SplatVal = getSplatValue()) |
167 | 0 | return SplatVal->isMinSignedValue(); |
168 | | |
169 | 0 | return false; |
170 | 0 | } |
171 | | |
172 | 2.75k | bool Constant::isNotMinSignedValue() const { |
173 | | // Check for INT_MIN integers |
174 | 2.75k | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
175 | 2.46k | return !CI->isMinValue(/*isSigned=*/true); |
176 | | |
177 | | // Check for FP which are bitcasted from INT_MIN integers |
178 | 290 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
179 | 0 | return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue(); |
180 | | |
181 | | // Check that vectors don't contain INT_MIN |
182 | 290 | if (auto *VTy = dyn_cast<FixedVectorType>(getType())) { |
183 | 760 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
184 | 569 | Constant *Elt = getAggregateElement(I); |
185 | 569 | if (!Elt || !Elt->isNotMinSignedValue()) |
186 | 68 | return false; |
187 | 569 | } |
188 | 191 | return true; |
189 | 259 | } |
190 | | |
191 | | // Check for splats that aren't INT_MIN |
192 | 31 | if (getType()->isVectorTy()) |
193 | 9 | if (const auto *SplatVal = getSplatValue()) |
194 | 9 | return SplatVal->isNotMinSignedValue(); |
195 | | |
196 | | // It *may* contain INT_MIN, we can't tell. |
197 | 22 | return false; |
198 | 31 | } |
199 | | |
200 | 780 | bool Constant::isFiniteNonZeroFP() const { |
201 | 780 | if (auto *CFP = dyn_cast<ConstantFP>(this)) |
202 | 514 | return CFP->getValueAPF().isFiniteNonZero(); |
203 | | |
204 | 266 | if (auto *VTy = dyn_cast<FixedVectorType>(getType())) { |
205 | 1.11k | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
206 | 860 | auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I)); |
207 | 860 | if (!CFP || !CFP->getValueAPF().isFiniteNonZero()) |
208 | 6 | return false; |
209 | 860 | } |
210 | 257 | return true; |
211 | 263 | } |
212 | | |
213 | 3 | if (getType()->isVectorTy()) |
214 | 3 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue())) |
215 | 3 | return SplatCFP->isFiniteNonZeroFP(); |
216 | | |
217 | | // It *may* contain finite non-zero, we can't tell. |
218 | 0 | return false; |
219 | 3 | } |
220 | | |
221 | 127 | bool Constant::isNormalFP() const { |
222 | 127 | if (auto *CFP = dyn_cast<ConstantFP>(this)) |
223 | 108 | return CFP->getValueAPF().isNormal(); |
224 | | |
225 | 19 | if (auto *VTy = dyn_cast<FixedVectorType>(getType())) { |
226 | 55 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
227 | 39 | auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I)); |
228 | 39 | if (!CFP || !CFP->getValueAPF().isNormal()) |
229 | 2 | return false; |
230 | 39 | } |
231 | 16 | return true; |
232 | 18 | } |
233 | | |
234 | 1 | if (getType()->isVectorTy()) |
235 | 1 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue())) |
236 | 1 | return SplatCFP->isNormalFP(); |
237 | | |
238 | | // It *may* contain a normal fp value, we can't tell. |
239 | 0 | return false; |
240 | 1 | } |
241 | | |
242 | 719 | bool Constant::hasExactInverseFP() const { |
243 | 719 | if (auto *CFP = dyn_cast<ConstantFP>(this)) |
244 | 564 | return CFP->getValueAPF().getExactInverse(nullptr); |
245 | | |
246 | 155 | if (auto *VTy = dyn_cast<FixedVectorType>(getType())) { |
247 | 175 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
248 | 173 | auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I)); |
249 | 173 | if (!CFP || !CFP->getValueAPF().getExactInverse(nullptr)) |
250 | 152 | return false; |
251 | 173 | } |
252 | 2 | return true; |
253 | 154 | } |
254 | | |
255 | 1 | if (getType()->isVectorTy()) |
256 | 1 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue())) |
257 | 1 | return SplatCFP->hasExactInverseFP(); |
258 | | |
259 | | // It *may* have an exact inverse fp value, we can't tell. |
260 | 0 | return false; |
261 | 1 | } |
262 | | |
263 | 1.99k | bool Constant::isNaN() const { |
264 | 1.99k | if (auto *CFP = dyn_cast<ConstantFP>(this)) |
265 | 1.97k | return CFP->isNaN(); |
266 | | |
267 | 20 | if (auto *VTy = dyn_cast<FixedVectorType>(getType())) { |
268 | 0 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
269 | 0 | auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I)); |
270 | 0 | if (!CFP || !CFP->isNaN()) |
271 | 0 | return false; |
272 | 0 | } |
273 | 0 | return true; |
274 | 0 | } |
275 | | |
276 | 20 | if (getType()->isVectorTy()) |
277 | 2 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue())) |
278 | 2 | return SplatCFP->isNaN(); |
279 | | |
280 | | // It *may* be NaN, we can't tell. |
281 | 18 | return false; |
282 | 20 | } |
283 | | |
284 | 7.37k | bool Constant::isElementWiseEqual(Value *Y) const { |
285 | | // Are they fully identical? |
286 | 7.37k | if (this == Y) |
287 | 1.07k | return true; |
288 | | |
289 | | // The input value must be a vector constant with the same type. |
290 | 6.29k | auto *VTy = dyn_cast<VectorType>(getType()); |
291 | 6.29k | if (!isa<Constant>(Y) || !VTy || VTy != Y->getType()) |
292 | 6.04k | return false; |
293 | | |
294 | | // TODO: Compare pointer constants? |
295 | 251 | if (!(VTy->getElementType()->isIntegerTy() || |
296 | 251 | VTy->getElementType()->isFloatingPointTy())) |
297 | 0 | return false; |
298 | | |
299 | | // They may still be identical element-wise (if they have `undef`s). |
300 | | // Bitcast to integer to allow exact bitwise comparison for all types. |
301 | 251 | Type *IntTy = VectorType::getInteger(VTy); |
302 | 251 | Constant *C0 = ConstantExpr::getBitCast(const_cast<Constant *>(this), IntTy); |
303 | 251 | Constant *C1 = ConstantExpr::getBitCast(cast<Constant>(Y), IntTy); |
304 | 251 | Constant *CmpEq = ConstantExpr::getICmp(ICmpInst::ICMP_EQ, C0, C1); |
305 | 251 | return isa<UndefValue>(CmpEq) || match(CmpEq, m_One()); |
306 | 251 | } |
307 | | |
308 | | static bool |
309 | | containsUndefinedElement(const Constant *C, |
310 | 17.6k | function_ref<bool(const Constant *)> HasFn) { |
311 | 17.6k | if (auto *VTy = dyn_cast<VectorType>(C->getType())) { |
312 | 2.44k | if (HasFn(C)) |
313 | 0 | return true; |
314 | 2.44k | if (isa<ConstantAggregateZero>(C)) |
315 | 655 | return false; |
316 | 1.78k | if (isa<ScalableVectorType>(C->getType())) |
317 | 8 | return false; |
318 | | |
319 | 1.78k | for (unsigned i = 0, e = cast<FixedVectorType>(VTy)->getNumElements(); |
320 | 9.67k | i != e; ++i) { |
321 | 8.40k | if (Constant *Elem = C->getAggregateElement(i)) |
322 | 8.40k | if (HasFn(Elem)) |
323 | 506 | return true; |
324 | 8.40k | } |
325 | 1.78k | } |
326 | | |
327 | 16.4k | return false; |
328 | 17.6k | } |
329 | | |
330 | 17.1k | bool Constant::containsUndefOrPoisonElement() const { |
331 | 17.1k | return containsUndefinedElement( |
332 | 17.1k | this, [&](const auto *C) { return isa<UndefValue>(C); }); |
333 | 17.1k | } |
334 | | |
335 | 503 | bool Constant::containsPoisonElement() const { |
336 | 503 | return containsUndefinedElement( |
337 | 1.57k | this, [&](const auto *C) { return isa<PoisonValue>(C); }); |
338 | 503 | } |
339 | | |
340 | 0 | bool Constant::containsUndefElement() const { |
341 | 0 | return containsUndefinedElement(this, [&](const auto *C) { |
342 | 0 | return isa<UndefValue>(C) && !isa<PoisonValue>(C); |
343 | 0 | }); |
344 | 0 | } |
345 | | |
346 | 65.3k | bool Constant::containsConstantExpression() const { |
347 | 65.3k | if (auto *VTy = dyn_cast<FixedVectorType>(getType())) { |
348 | 41.3k | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) |
349 | 32.2k | if (isa<ConstantExpr>(getAggregateElement(i))) |
350 | 16 | return true; |
351 | 9.14k | } |
352 | 65.3k | return false; |
353 | 65.3k | } |
354 | | |
355 | | /// Constructor to create a '0' constant of arbitrary type. |
356 | 2.11M | Constant *Constant::getNullValue(Type *Ty) { |
357 | 2.11M | switch (Ty->getTypeID()) { |
358 | 1.90M | case Type::IntegerTyID: |
359 | 1.90M | return ConstantInt::get(Ty, 0); |
360 | 4.08k | case Type::HalfTyID: |
361 | 4.17k | case Type::BFloatTyID: |
362 | 75.6k | case Type::FloatTyID: |
363 | 135k | case Type::DoubleTyID: |
364 | 135k | case Type::X86_FP80TyID: |
365 | 136k | case Type::FP128TyID: |
366 | 137k | case Type::PPC_FP128TyID: |
367 | 137k | return ConstantFP::get(Ty->getContext(), |
368 | 137k | APFloat::getZero(Ty->getFltSemantics())); |
369 | 14.9k | case Type::PointerTyID: |
370 | 14.9k | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
371 | 792 | case Type::StructTyID: |
372 | 6.31k | case Type::ArrayTyID: |
373 | 51.7k | case Type::FixedVectorTyID: |
374 | 58.5k | case Type::ScalableVectorTyID: |
375 | 58.5k | return ConstantAggregateZero::get(Ty); |
376 | 1.29k | case Type::TokenTyID: |
377 | 1.29k | return ConstantTokenNone::get(Ty->getContext()); |
378 | 0 | case Type::TargetExtTyID: |
379 | 0 | return ConstantTargetNone::get(cast<TargetExtType>(Ty)); |
380 | 0 | default: |
381 | | // Function, Label, or Opaque type? |
382 | 0 | llvm_unreachable("Cannot create a null constant of that type!"); |
383 | 2.11M | } |
384 | 2.11M | } |
385 | | |
386 | 1.23k | Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) { |
387 | 1.23k | Type *ScalarTy = Ty->getScalarType(); |
388 | | |
389 | | // Create the base integer constant. |
390 | 1.23k | Constant *C = ConstantInt::get(Ty->getContext(), V); |
391 | | |
392 | | // Convert an integer to a pointer, if necessary. |
393 | 1.23k | if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy)) |
394 | 5 | C = ConstantExpr::getIntToPtr(C, PTy); |
395 | | |
396 | | // Broadcast a scalar to a vector, if necessary. |
397 | 1.23k | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
398 | 299 | C = ConstantVector::getSplat(VTy->getElementCount(), C); |
399 | | |
400 | 1.23k | return C; |
401 | 1.23k | } |
402 | | |
403 | 242k | Constant *Constant::getAllOnesValue(Type *Ty) { |
404 | 242k | if (IntegerType *ITy = dyn_cast<IntegerType>(Ty)) |
405 | 235k | return ConstantInt::get(Ty->getContext(), |
406 | 235k | APInt::getAllOnes(ITy->getBitWidth())); |
407 | | |
408 | 6.95k | if (Ty->isFloatingPointTy()) { |
409 | 76 | APFloat FL = APFloat::getAllOnesValue(Ty->getFltSemantics()); |
410 | 76 | return ConstantFP::get(Ty->getContext(), FL); |
411 | 76 | } |
412 | | |
413 | 6.87k | VectorType *VTy = cast<VectorType>(Ty); |
414 | 6.87k | return ConstantVector::getSplat(VTy->getElementCount(), |
415 | 6.87k | getAllOnesValue(VTy->getElementType())); |
416 | 6.95k | } |
417 | | |
418 | 2.07M | Constant *Constant::getAggregateElement(unsigned Elt) const { |
419 | 2.07M | assert((getType()->isAggregateType() || getType()->isVectorTy()) && |
420 | 2.07M | "Must be an aggregate/vector constant"); |
421 | | |
422 | 2.07M | if (const auto *CC = dyn_cast<ConstantAggregate>(this)) |
423 | 350k | return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr; |
424 | | |
425 | 1.72M | if (const auto *CAZ = dyn_cast<ConstantAggregateZero>(this)) |
426 | 42.5k | return Elt < CAZ->getElementCount().getKnownMinValue() |
427 | 42.5k | ? CAZ->getElementValue(Elt) |
428 | 42.5k | : nullptr; |
429 | | |
430 | | // FIXME: getNumElements() will fail for non-fixed vector types. |
431 | 1.67M | if (isa<ScalableVectorType>(getType())) |
432 | 65 | return nullptr; |
433 | | |
434 | 1.67M | if (const auto *PV = dyn_cast<PoisonValue>(this)) |
435 | 184k | return Elt < PV->getNumElements() ? PV->getElementValue(Elt) : nullptr; |
436 | | |
437 | 1.49M | if (const auto *UV = dyn_cast<UndefValue>(this)) |
438 | 1.17M | return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr; |
439 | | |
440 | 321k | if (const auto *CDS = dyn_cast<ConstantDataSequential>(this)) |
441 | 309k | return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt) |
442 | 309k | : nullptr; |
443 | | |
444 | 12.2k | return nullptr; |
445 | 321k | } |
446 | | |
447 | 174k | Constant *Constant::getAggregateElement(Constant *Elt) const { |
448 | 174k | assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer"); |
449 | 174k | if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) { |
450 | | // Check if the constant fits into an uint64_t. |
451 | 174k | if (CI->getValue().getActiveBits() > 64) |
452 | 0 | return nullptr; |
453 | 174k | return getAggregateElement(CI->getZExtValue()); |
454 | 174k | } |
455 | 0 | return nullptr; |
456 | 174k | } |
457 | | |
458 | 117k | void Constant::destroyConstant() { |
459 | | /// First call destroyConstantImpl on the subclass. This gives the subclass |
460 | | /// a chance to remove the constant from any maps/pools it's contained in. |
461 | 117k | switch (getValueID()) { |
462 | 0 | default: |
463 | 0 | llvm_unreachable("Not a constant!"); |
464 | 0 | #define HANDLE_CONSTANT(Name) \ |
465 | 117k | case Value::Name##Val: \ |
466 | 117k | cast<Name>(this)->destroyConstantImpl(); \ |
467 | 117k | break; |
468 | 117k | #include "llvm/IR/Value.def" |
469 | 117k | } |
470 | | |
471 | | // When a Constant is destroyed, there may be lingering |
472 | | // references to the constant by other constants in the constant pool. These |
473 | | // constants are implicitly dependent on the module that is being deleted, |
474 | | // but they don't know that. Because we only find out when the CPV is |
475 | | // deleted, we must now notify all of our users (that should only be |
476 | | // Constants) that they are, in fact, invalid now and should be deleted. |
477 | | // |
478 | 117k | while (!use_empty()) { |
479 | 0 | Value *V = user_back(); |
480 | 0 | #ifndef NDEBUG // Only in -g mode... |
481 | 0 | if (!isa<Constant>(V)) { |
482 | 0 | dbgs() << "While deleting: " << *this |
483 | 0 | << "\n\nUse still stuck around after Def is destroyed: " << *V |
484 | 0 | << "\n\n"; |
485 | 0 | } |
486 | 0 | #endif |
487 | 0 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
488 | 0 | cast<Constant>(V)->destroyConstant(); |
489 | | |
490 | | // The constant should remove itself from our use list... |
491 | 0 | assert((use_empty() || user_back() != V) && "Constant not removed!"); |
492 | 0 | } |
493 | | |
494 | | // Value has no outstanding references it is safe to delete it now... |
495 | 117k | deleteConstant(this); |
496 | 117k | } |
497 | | |
498 | 239k | void llvm::deleteConstant(Constant *C) { |
499 | 239k | switch (C->getValueID()) { |
500 | 0 | case Constant::ConstantIntVal: |
501 | 0 | delete static_cast<ConstantInt *>(C); |
502 | 0 | break; |
503 | 0 | case Constant::ConstantFPVal: |
504 | 0 | delete static_cast<ConstantFP *>(C); |
505 | 0 | break; |
506 | 0 | case Constant::ConstantAggregateZeroVal: |
507 | 0 | delete static_cast<ConstantAggregateZero *>(C); |
508 | 0 | break; |
509 | 2.82k | case Constant::ConstantArrayVal: |
510 | 2.82k | delete static_cast<ConstantArray *>(C); |
511 | 2.82k | break; |
512 | 2.77k | case Constant::ConstantStructVal: |
513 | 2.77k | delete static_cast<ConstantStruct *>(C); |
514 | 2.77k | break; |
515 | 57.0k | case Constant::ConstantVectorVal: |
516 | 57.0k | delete static_cast<ConstantVector *>(C); |
517 | 57.0k | break; |
518 | 0 | case Constant::ConstantPointerNullVal: |
519 | 0 | delete static_cast<ConstantPointerNull *>(C); |
520 | 0 | break; |
521 | 0 | case Constant::ConstantDataArrayVal: |
522 | 0 | delete static_cast<ConstantDataArray *>(C); |
523 | 0 | break; |
524 | 0 | case Constant::ConstantDataVectorVal: |
525 | 0 | delete static_cast<ConstantDataVector *>(C); |
526 | 0 | break; |
527 | 0 | case Constant::ConstantTokenNoneVal: |
528 | 0 | delete static_cast<ConstantTokenNone *>(C); |
529 | 0 | break; |
530 | 11.7k | case Constant::BlockAddressVal: |
531 | 11.7k | delete static_cast<BlockAddress *>(C); |
532 | 11.7k | break; |
533 | 0 | case Constant::DSOLocalEquivalentVal: |
534 | 0 | delete static_cast<DSOLocalEquivalent *>(C); |
535 | 0 | break; |
536 | 974 | case Constant::NoCFIValueVal: |
537 | 974 | delete static_cast<NoCFIValue *>(C); |
538 | 974 | break; |
539 | 0 | case Constant::UndefValueVal: |
540 | 0 | delete static_cast<UndefValue *>(C); |
541 | 0 | break; |
542 | 0 | case Constant::PoisonValueVal: |
543 | 0 | delete static_cast<PoisonValue *>(C); |
544 | 0 | break; |
545 | 163k | case Constant::ConstantExprVal: |
546 | 163k | if (isa<CastConstantExpr>(C)) |
547 | 32.1k | delete static_cast<CastConstantExpr *>(C); |
548 | 131k | else if (isa<BinaryConstantExpr>(C)) |
549 | 19.6k | delete static_cast<BinaryConstantExpr *>(C); |
550 | 112k | else if (isa<ExtractElementConstantExpr>(C)) |
551 | 9.93k | delete static_cast<ExtractElementConstantExpr *>(C); |
552 | 102k | else if (isa<InsertElementConstantExpr>(C)) |
553 | 3.15k | delete static_cast<InsertElementConstantExpr *>(C); |
554 | 99.0k | else if (isa<ShuffleVectorConstantExpr>(C)) |
555 | 2.82k | delete static_cast<ShuffleVectorConstantExpr *>(C); |
556 | 96.1k | else if (isa<GetElementPtrConstantExpr>(C)) |
557 | 85.3k | delete static_cast<GetElementPtrConstantExpr *>(C); |
558 | 10.7k | else if (isa<CompareConstantExpr>(C)) |
559 | 10.7k | delete static_cast<CompareConstantExpr *>(C); |
560 | 0 | else |
561 | 0 | llvm_unreachable("Unexpected constant expr"); |
562 | 163k | break; |
563 | 0 | default: |
564 | 0 | llvm_unreachable("Unexpected constant"); |
565 | 239k | } |
566 | 239k | } |
567 | | |
568 | | /// Check if C contains a GlobalValue for which Predicate is true. |
569 | | static bool |
570 | | ConstHasGlobalValuePredicate(const Constant *C, |
571 | 25.9k | bool (*Predicate)(const GlobalValue *)) { |
572 | 25.9k | SmallPtrSet<const Constant *, 8> Visited; |
573 | 25.9k | SmallVector<const Constant *, 8> WorkList; |
574 | 25.9k | WorkList.push_back(C); |
575 | 25.9k | Visited.insert(C); |
576 | | |
577 | 52.4k | while (!WorkList.empty()) { |
578 | 26.6k | const Constant *WorkItem = WorkList.pop_back_val(); |
579 | 26.6k | if (const auto *GV = dyn_cast<GlobalValue>(WorkItem)) |
580 | 1.16k | if (Predicate(GV)) |
581 | 206 | return true; |
582 | 26.4k | for (const Value *Op : WorkItem->operands()) { |
583 | 692 | const Constant *ConstOp = dyn_cast<Constant>(Op); |
584 | 692 | if (!ConstOp) |
585 | 0 | continue; |
586 | 692 | if (Visited.insert(ConstOp).second) |
587 | 692 | WorkList.push_back(ConstOp); |
588 | 692 | } |
589 | 26.4k | } |
590 | 25.7k | return false; |
591 | 25.9k | } |
592 | | |
593 | 13.0k | bool Constant::isThreadDependent() const { |
594 | 13.0k | auto DLLImportPredicate = [](const GlobalValue *GV) { |
595 | 638 | return GV->isThreadLocal(); |
596 | 638 | }; |
597 | 13.0k | return ConstHasGlobalValuePredicate(this, DLLImportPredicate); |
598 | 13.0k | } |
599 | | |
600 | 12.9k | bool Constant::isDLLImportDependent() const { |
601 | 12.9k | auto DLLImportPredicate = [](const GlobalValue *GV) { |
602 | 525 | return GV->hasDLLImportStorageClass(); |
603 | 525 | }; |
604 | 12.9k | return ConstHasGlobalValuePredicate(this, DLLImportPredicate); |
605 | 12.9k | } |
606 | | |
607 | 45 | bool Constant::isConstantUsed() const { |
608 | 45 | for (const User *U : users()) { |
609 | 45 | const Constant *UC = dyn_cast<Constant>(U); |
610 | 45 | if (!UC || isa<GlobalValue>(UC)) |
611 | 26 | return true; |
612 | | |
613 | 19 | if (UC->isConstantUsed()) |
614 | 19 | return true; |
615 | 19 | } |
616 | 0 | return false; |
617 | 45 | } |
618 | | |
619 | 17.2k | bool Constant::needsDynamicRelocation() const { |
620 | 17.2k | return getRelocationInfo() == GlobalRelocation; |
621 | 17.2k | } |
622 | | |
623 | 1.73k | bool Constant::needsRelocation() const { |
624 | 1.73k | return getRelocationInfo() != NoRelocation; |
625 | 1.73k | } |
626 | | |
627 | 37.6k | Constant::PossibleRelocationsTy Constant::getRelocationInfo() const { |
628 | 37.6k | if (isa<GlobalValue>(this)) |
629 | 3.32k | return GlobalRelocation; // Global reference. |
630 | | |
631 | 34.3k | if (const BlockAddress *BA = dyn_cast<BlockAddress>(this)) |
632 | 3.21k | return BA->getFunction()->getRelocationInfo(); |
633 | | |
634 | 31.1k | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) { |
635 | 562 | if (CE->getOpcode() == Instruction::Sub) { |
636 | 0 | ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0)); |
637 | 0 | ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1)); |
638 | 0 | if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt && |
639 | 0 | RHS->getOpcode() == Instruction::PtrToInt) { |
640 | 0 | Constant *LHSOp0 = LHS->getOperand(0); |
641 | 0 | Constant *RHSOp0 = RHS->getOperand(0); |
642 | | |
643 | | // While raw uses of blockaddress need to be relocated, differences |
644 | | // between two of them don't when they are for labels in the same |
645 | | // function. This is a common idiom when creating a table for the |
646 | | // indirect goto extension, so we handle it efficiently here. |
647 | 0 | if (isa<BlockAddress>(LHSOp0) && isa<BlockAddress>(RHSOp0) && |
648 | 0 | cast<BlockAddress>(LHSOp0)->getFunction() == |
649 | 0 | cast<BlockAddress>(RHSOp0)->getFunction()) |
650 | 0 | return NoRelocation; |
651 | | |
652 | | // Relative pointers do not need to be dynamically relocated. |
653 | 0 | if (auto *RHSGV = |
654 | 0 | dyn_cast<GlobalValue>(RHSOp0->stripInBoundsConstantOffsets())) { |
655 | 0 | auto *LHS = LHSOp0->stripInBoundsConstantOffsets(); |
656 | 0 | if (auto *LHSGV = dyn_cast<GlobalValue>(LHS)) { |
657 | 0 | if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal()) |
658 | 0 | return LocalRelocation; |
659 | 0 | } else if (isa<DSOLocalEquivalent>(LHS)) { |
660 | 0 | if (RHSGV->isDSOLocal()) |
661 | 0 | return LocalRelocation; |
662 | 0 | } |
663 | 0 | } |
664 | 0 | } |
665 | 0 | } |
666 | 562 | } |
667 | | |
668 | 31.1k | PossibleRelocationsTy Result = NoRelocation; |
669 | 46.6k | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
670 | 15.4k | Result = |
671 | 15.4k | std::max(cast<Constant>(getOperand(i))->getRelocationInfo(), Result); |
672 | | |
673 | 31.1k | return Result; |
674 | 31.1k | } |
675 | | |
676 | | /// Return true if the specified constantexpr is dead. This involves |
677 | | /// recursively traversing users of the constantexpr. |
678 | | /// If RemoveDeadUsers is true, also remove dead users at the same time. |
679 | 106k | static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) { |
680 | 106k | if (isa<GlobalValue>(C)) return false; // Cannot remove this |
681 | | |
682 | 106k | Value::const_user_iterator I = C->user_begin(), E = C->user_end(); |
683 | 137k | while (I != E) { |
684 | 31.0k | const Constant *User = dyn_cast<Constant>(*I); |
685 | 31.0k | if (!User) return false; // Non-constant usage; |
686 | 31.0k | if (!constantIsDead(User, RemoveDeadUsers)) |
687 | 0 | return false; // Constant wasn't dead |
688 | | |
689 | | // Just removed User, so the iterator was invalidated. |
690 | | // Since we return immediately upon finding a live user, we can always |
691 | | // restart from user_begin(). |
692 | 31.0k | if (RemoveDeadUsers) |
693 | 31.0k | I = C->user_begin(); |
694 | 0 | else |
695 | 0 | ++I; |
696 | 31.0k | } |
697 | | |
698 | 106k | if (RemoveDeadUsers) { |
699 | | // If C is only used by metadata, it should not be preserved but should |
700 | | // have its uses replaced. |
701 | 106k | ReplaceableMetadataImpl::SalvageDebugInfo(*C); |
702 | 106k | const_cast<Constant *>(C)->destroyConstant(); |
703 | 106k | } |
704 | | |
705 | 106k | return true; |
706 | 106k | } |
707 | | |
708 | 2.22M | void Constant::removeDeadConstantUsers() const { |
709 | 2.22M | Value::const_user_iterator I = user_begin(), E = user_end(); |
710 | 2.22M | Value::const_user_iterator LastNonDeadUser = E; |
711 | 2.30M | while (I != E) { |
712 | 75.4k | const Constant *User = dyn_cast<Constant>(*I); |
713 | 75.4k | if (!User) { |
714 | 0 | LastNonDeadUser = I; |
715 | 0 | ++I; |
716 | 0 | continue; |
717 | 0 | } |
718 | | |
719 | 75.4k | if (!constantIsDead(User, /* RemoveDeadUsers= */ true)) { |
720 | | // If the constant wasn't dead, remember that this was the last live use |
721 | | // and move on to the next constant. |
722 | 0 | LastNonDeadUser = I; |
723 | 0 | ++I; |
724 | 0 | continue; |
725 | 0 | } |
726 | | |
727 | | // If the constant was dead, then the iterator is invalidated. |
728 | 75.4k | if (LastNonDeadUser == E) |
729 | 75.4k | I = user_begin(); |
730 | 0 | else |
731 | 0 | I = std::next(LastNonDeadUser); |
732 | 75.4k | } |
733 | 2.22M | } |
734 | | |
735 | 1 | bool Constant::hasOneLiveUse() const { return hasNLiveUses(1); } |
736 | | |
737 | 0 | bool Constant::hasZeroLiveUses() const { return hasNLiveUses(0); } |
738 | | |
739 | 1 | bool Constant::hasNLiveUses(unsigned N) const { |
740 | 1 | unsigned NumUses = 0; |
741 | 1 | for (const Use &U : uses()) { |
742 | 1 | const Constant *User = dyn_cast<Constant>(U.getUser()); |
743 | 1 | if (!User || !constantIsDead(User, /* RemoveDeadUsers= */ false)) { |
744 | 1 | ++NumUses; |
745 | | |
746 | 1 | if (NumUses > N) |
747 | 0 | return false; |
748 | 1 | } |
749 | 1 | } |
750 | 1 | return NumUses == N; |
751 | 1 | } |
752 | | |
753 | 249 | Constant *Constant::replaceUndefsWith(Constant *C, Constant *Replacement) { |
754 | 249 | assert(C && Replacement && "Expected non-nullptr constant arguments"); |
755 | 0 | Type *Ty = C->getType(); |
756 | 249 | if (match(C, m_Undef())) { |
757 | 0 | assert(Ty == Replacement->getType() && "Expected matching types"); |
758 | 0 | return Replacement; |
759 | 0 | } |
760 | | |
761 | | // Don't know how to deal with this constant. |
762 | 249 | auto *VTy = dyn_cast<FixedVectorType>(Ty); |
763 | 249 | if (!VTy) |
764 | 108 | return C; |
765 | | |
766 | 141 | unsigned NumElts = VTy->getNumElements(); |
767 | 141 | SmallVector<Constant *, 32> NewC(NumElts); |
768 | 739 | for (unsigned i = 0; i != NumElts; ++i) { |
769 | 598 | Constant *EltC = C->getAggregateElement(i); |
770 | 598 | assert((!EltC || EltC->getType() == Replacement->getType()) && |
771 | 598 | "Expected matching types"); |
772 | 598 | NewC[i] = EltC && match(EltC, m_Undef()) ? Replacement : EltC; |
773 | 598 | } |
774 | 141 | return ConstantVector::get(NewC); |
775 | 249 | } |
776 | | |
777 | 232 | Constant *Constant::mergeUndefsWith(Constant *C, Constant *Other) { |
778 | 232 | assert(C && Other && "Expected non-nullptr constant arguments"); |
779 | 232 | if (match(C, m_Undef())) |
780 | 0 | return C; |
781 | | |
782 | 232 | Type *Ty = C->getType(); |
783 | 232 | if (match(Other, m_Undef())) |
784 | 0 | return UndefValue::get(Ty); |
785 | | |
786 | 232 | auto *VTy = dyn_cast<FixedVectorType>(Ty); |
787 | 232 | if (!VTy) |
788 | 152 | return C; |
789 | | |
790 | 80 | Type *EltTy = VTy->getElementType(); |
791 | 80 | unsigned NumElts = VTy->getNumElements(); |
792 | 80 | assert(isa<FixedVectorType>(Other->getType()) && |
793 | 80 | cast<FixedVectorType>(Other->getType())->getNumElements() == NumElts && |
794 | 80 | "Type mismatch"); |
795 | | |
796 | 0 | bool FoundExtraUndef = false; |
797 | 80 | SmallVector<Constant *, 32> NewC(NumElts); |
798 | 249 | for (unsigned I = 0; I != NumElts; ++I) { |
799 | 169 | NewC[I] = C->getAggregateElement(I); |
800 | 169 | Constant *OtherEltC = Other->getAggregateElement(I); |
801 | 169 | assert(NewC[I] && OtherEltC && "Unknown vector element"); |
802 | 169 | if (!match(NewC[I], m_Undef()) && match(OtherEltC, m_Undef())) { |
803 | 17 | NewC[I] = UndefValue::get(EltTy); |
804 | 17 | FoundExtraUndef = true; |
805 | 17 | } |
806 | 169 | } |
807 | 80 | if (FoundExtraUndef) |
808 | 17 | return ConstantVector::get(NewC); |
809 | 63 | return C; |
810 | 80 | } |
811 | | |
812 | 0 | bool Constant::isManifestConstant() const { |
813 | 0 | if (isa<ConstantData>(this)) |
814 | 0 | return true; |
815 | 0 | if (isa<ConstantAggregate>(this) || isa<ConstantExpr>(this)) { |
816 | 0 | for (const Value *Op : operand_values()) |
817 | 0 | if (!cast<Constant>(Op)->isManifestConstant()) |
818 | 0 | return false; |
819 | 0 | return true; |
820 | 0 | } |
821 | 0 | return false; |
822 | 0 | } |
823 | | |
824 | | //===----------------------------------------------------------------------===// |
825 | | // ConstantInt |
826 | | //===----------------------------------------------------------------------===// |
827 | | |
828 | | ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V) |
829 | 6.13M | : ConstantData(Ty, ConstantIntVal), Val(V) { |
830 | 6.13M | assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); |
831 | 6.13M | } |
832 | | |
833 | 939k | ConstantInt *ConstantInt::getTrue(LLVMContext &Context) { |
834 | 939k | LLVMContextImpl *pImpl = Context.pImpl; |
835 | 939k | if (!pImpl->TheTrueVal) |
836 | 36.6k | pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1); |
837 | 939k | return pImpl->TheTrueVal; |
838 | 939k | } |
839 | | |
840 | 621k | ConstantInt *ConstantInt::getFalse(LLVMContext &Context) { |
841 | 621k | LLVMContextImpl *pImpl = Context.pImpl; |
842 | 621k | if (!pImpl->TheFalseVal) |
843 | 32.6k | pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0); |
844 | 621k | return pImpl->TheFalseVal; |
845 | 621k | } |
846 | | |
847 | 1.02M | ConstantInt *ConstantInt::getBool(LLVMContext &Context, bool V) { |
848 | 1.02M | return V ? getTrue(Context) : getFalse(Context); |
849 | 1.02M | } |
850 | | |
851 | 67.3k | Constant *ConstantInt::getTrue(Type *Ty) { |
852 | 67.3k | assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1."); |
853 | 0 | ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext()); |
854 | 67.3k | if (auto *VTy = dyn_cast<VectorType>(Ty)) |
855 | 7.05k | return ConstantVector::getSplat(VTy->getElementCount(), TrueC); |
856 | 60.3k | return TrueC; |
857 | 67.3k | } |
858 | | |
859 | 71.6k | Constant *ConstantInt::getFalse(Type *Ty) { |
860 | 71.6k | assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1."); |
861 | 0 | ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext()); |
862 | 71.6k | if (auto *VTy = dyn_cast<VectorType>(Ty)) |
863 | 6.35k | return ConstantVector::getSplat(VTy->getElementCount(), FalseC); |
864 | 65.3k | return FalseC; |
865 | 71.6k | } |
866 | | |
867 | 882 | Constant *ConstantInt::getBool(Type *Ty, bool V) { |
868 | 882 | return V ? getTrue(Ty) : getFalse(Ty); |
869 | 882 | } |
870 | | |
871 | | // Get a ConstantInt from an APInt. |
872 | 75.7M | ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { |
873 | | // get an existing value or the insertion position |
874 | 75.7M | LLVMContextImpl *pImpl = Context.pImpl; |
875 | 75.7M | std::unique_ptr<ConstantInt> &Slot = |
876 | 75.7M | V.isZero() ? pImpl->IntZeroConstants[V.getBitWidth()] |
877 | 75.7M | : V.isOne() ? pImpl->IntOneConstants[V.getBitWidth()] |
878 | 53.8M | : pImpl->IntConstants[V]; |
879 | 75.7M | if (!Slot) { |
880 | | // Get the corresponding integer type for the bit width of the value. |
881 | 6.13M | IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); |
882 | 6.13M | Slot.reset(new ConstantInt(ITy, V)); |
883 | 6.13M | } |
884 | 75.7M | assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth())); |
885 | 0 | return Slot.get(); |
886 | 75.7M | } |
887 | | |
888 | 7.74M | Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) { |
889 | 7.74M | Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned); |
890 | | |
891 | | // For vectors, broadcast the value. |
892 | 7.74M | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
893 | 9.47k | return ConstantVector::getSplat(VTy->getElementCount(), C); |
894 | | |
895 | 7.73M | return C; |
896 | 7.74M | } |
897 | | |
898 | 28.6M | ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) { |
899 | 28.6M | return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned)); |
900 | 28.6M | } |
901 | | |
902 | 405k | Constant *ConstantInt::get(Type *Ty, const APInt& V) { |
903 | 405k | ConstantInt *C = get(Ty->getContext(), V); |
904 | 405k | assert(C->getType() == Ty->getScalarType() && |
905 | 405k | "ConstantInt type doesn't match the type implied by its value!"); |
906 | | |
907 | | // For vectors, broadcast the value. |
908 | 405k | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
909 | 7.63k | return ConstantVector::getSplat(VTy->getElementCount(), C); |
910 | | |
911 | 398k | return C; |
912 | 405k | } |
913 | | |
914 | 0 | ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) { |
915 | 0 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix)); |
916 | 0 | } |
917 | | |
918 | | /// Remove the constant from the constant table. |
919 | 0 | void ConstantInt::destroyConstantImpl() { |
920 | 0 | llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!"); |
921 | 0 | } |
922 | | |
923 | | //===----------------------------------------------------------------------===// |
924 | | // ConstantFP |
925 | | //===----------------------------------------------------------------------===// |
926 | | |
927 | 33.3k | Constant *ConstantFP::get(Type *Ty, double V) { |
928 | 33.3k | LLVMContext &Context = Ty->getContext(); |
929 | | |
930 | 33.3k | APFloat FV(V); |
931 | 33.3k | bool ignored; |
932 | 33.3k | FV.convert(Ty->getScalarType()->getFltSemantics(), |
933 | 33.3k | APFloat::rmNearestTiesToEven, &ignored); |
934 | 33.3k | Constant *C = get(Context, FV); |
935 | | |
936 | | // For vectors, broadcast the value. |
937 | 33.3k | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
938 | 290 | return ConstantVector::getSplat(VTy->getElementCount(), C); |
939 | | |
940 | 33.1k | return C; |
941 | 33.3k | } |
942 | | |
943 | 2.14k | Constant *ConstantFP::get(Type *Ty, const APFloat &V) { |
944 | 2.14k | ConstantFP *C = get(Ty->getContext(), V); |
945 | 2.14k | assert(C->getType() == Ty->getScalarType() && |
946 | 2.14k | "ConstantFP type doesn't match the type implied by its value!"); |
947 | | |
948 | | // For vectors, broadcast the value. |
949 | 2.14k | if (auto *VTy = dyn_cast<VectorType>(Ty)) |
950 | 16 | return ConstantVector::getSplat(VTy->getElementCount(), C); |
951 | | |
952 | 2.13k | return C; |
953 | 2.14k | } |
954 | | |
955 | 0 | Constant *ConstantFP::get(Type *Ty, StringRef Str) { |
956 | 0 | LLVMContext &Context = Ty->getContext(); |
957 | |
|
958 | 0 | APFloat FV(Ty->getScalarType()->getFltSemantics(), Str); |
959 | 0 | Constant *C = get(Context, FV); |
960 | | |
961 | | // For vectors, broadcast the value. |
962 | 0 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
963 | 0 | return ConstantVector::getSplat(VTy->getElementCount(), C); |
964 | | |
965 | 0 | return C; |
966 | 0 | } |
967 | | |
968 | 1.64k | Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) { |
969 | 1.64k | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
970 | 1.64k | APFloat NaN = APFloat::getNaN(Semantics, Negative, Payload); |
971 | 1.64k | Constant *C = get(Ty->getContext(), NaN); |
972 | | |
973 | 1.64k | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
974 | 93 | return ConstantVector::getSplat(VTy->getElementCount(), C); |
975 | | |
976 | 1.55k | return C; |
977 | 1.64k | } |
978 | | |
979 | 0 | Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) { |
980 | 0 | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
981 | 0 | APFloat NaN = APFloat::getQNaN(Semantics, Negative, Payload); |
982 | 0 | Constant *C = get(Ty->getContext(), NaN); |
983 | |
|
984 | 0 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
985 | 0 | return ConstantVector::getSplat(VTy->getElementCount(), C); |
986 | | |
987 | 0 | return C; |
988 | 0 | } |
989 | | |
990 | 0 | Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) { |
991 | 0 | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
992 | 0 | APFloat NaN = APFloat::getSNaN(Semantics, Negative, Payload); |
993 | 0 | Constant *C = get(Ty->getContext(), NaN); |
994 | |
|
995 | 0 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
996 | 0 | return ConstantVector::getSplat(VTy->getElementCount(), C); |
997 | | |
998 | 0 | return C; |
999 | 0 | } |
1000 | | |
1001 | 46.2k | Constant *ConstantFP::getZero(Type *Ty, bool Negative) { |
1002 | 46.2k | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
1003 | 46.2k | APFloat NegZero = APFloat::getZero(Semantics, Negative); |
1004 | 46.2k | Constant *C = get(Ty->getContext(), NegZero); |
1005 | | |
1006 | 46.2k | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
1007 | 320 | return ConstantVector::getSplat(VTy->getElementCount(), C); |
1008 | | |
1009 | 45.9k | return C; |
1010 | 46.2k | } |
1011 | | |
1012 | | |
1013 | | // ConstantFP accessors. |
1014 | 938k | ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { |
1015 | 938k | LLVMContextImpl* pImpl = Context.pImpl; |
1016 | | |
1017 | 938k | std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V]; |
1018 | | |
1019 | 938k | if (!Slot) { |
1020 | 499k | Type *Ty = Type::getFloatingPointTy(Context, V.getSemantics()); |
1021 | 499k | Slot.reset(new ConstantFP(Ty, V)); |
1022 | 499k | } |
1023 | | |
1024 | 938k | return Slot.get(); |
1025 | 938k | } |
1026 | | |
1027 | 107 | Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) { |
1028 | 107 | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
1029 | 107 | Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative)); |
1030 | | |
1031 | 107 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
1032 | 44 | return ConstantVector::getSplat(VTy->getElementCount(), C); |
1033 | | |
1034 | 63 | return C; |
1035 | 107 | } |
1036 | | |
1037 | | ConstantFP::ConstantFP(Type *Ty, const APFloat &V) |
1038 | 499k | : ConstantData(Ty, ConstantFPVal), Val(V) { |
1039 | 499k | assert(&V.getSemantics() == &Ty->getFltSemantics() && |
1040 | 499k | "FP type Mismatch"); |
1041 | 499k | } |
1042 | | |
1043 | 152k | bool ConstantFP::isExactlyValue(const APFloat &V) const { |
1044 | 152k | return Val.bitwiseIsEqual(V); |
1045 | 152k | } |
1046 | | |
1047 | | /// Remove the constant from the constant table. |
1048 | 0 | void ConstantFP::destroyConstantImpl() { |
1049 | 0 | llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!"); |
1050 | 0 | } |
1051 | | |
1052 | | //===----------------------------------------------------------------------===// |
1053 | | // ConstantAggregateZero Implementation |
1054 | | //===----------------------------------------------------------------------===// |
1055 | | |
1056 | 39.3k | Constant *ConstantAggregateZero::getSequentialElement() const { |
1057 | 39.3k | if (auto *AT = dyn_cast<ArrayType>(getType())) |
1058 | 8 | return Constant::getNullValue(AT->getElementType()); |
1059 | 39.3k | return Constant::getNullValue(cast<VectorType>(getType())->getElementType()); |
1060 | 39.3k | } |
1061 | | |
1062 | 7.17k | Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const { |
1063 | 7.17k | return Constant::getNullValue(getType()->getStructElementType(Elt)); |
1064 | 7.17k | } |
1065 | | |
1066 | 0 | Constant *ConstantAggregateZero::getElementValue(Constant *C) const { |
1067 | 0 | if (isa<ArrayType>(getType()) || isa<VectorType>(getType())) |
1068 | 0 | return getSequentialElement(); |
1069 | 0 | return getStructElement(cast<ConstantInt>(C)->getZExtValue()); |
1070 | 0 | } |
1071 | | |
1072 | 46.5k | Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { |
1073 | 46.5k | if (isa<ArrayType>(getType()) || isa<VectorType>(getType())) |
1074 | 39.3k | return getSequentialElement(); |
1075 | 7.17k | return getStructElement(Idx); |
1076 | 46.5k | } |
1077 | | |
1078 | 42.8k | ElementCount ConstantAggregateZero::getElementCount() const { |
1079 | 42.8k | Type *Ty = getType(); |
1080 | 42.8k | if (auto *AT = dyn_cast<ArrayType>(Ty)) |
1081 | 8 | return ElementCount::getFixed(AT->getNumElements()); |
1082 | 42.8k | if (auto *VT = dyn_cast<VectorType>(Ty)) |
1083 | 35.6k | return VT->getElementCount(); |
1084 | 7.17k | return ElementCount::getFixed(Ty->getStructNumElements()); |
1085 | 42.8k | } |
1086 | | |
1087 | | //===----------------------------------------------------------------------===// |
1088 | | // UndefValue Implementation |
1089 | | //===----------------------------------------------------------------------===// |
1090 | | |
1091 | 1.16M | UndefValue *UndefValue::getSequentialElement() const { |
1092 | 1.16M | if (ArrayType *ATy = dyn_cast<ArrayType>(getType())) |
1093 | 1.02M | return UndefValue::get(ATy->getElementType()); |
1094 | 146k | return UndefValue::get(cast<VectorType>(getType())->getElementType()); |
1095 | 1.16M | } |
1096 | | |
1097 | 3.05k | UndefValue *UndefValue::getStructElement(unsigned Elt) const { |
1098 | 3.05k | return UndefValue::get(getType()->getStructElementType(Elt)); |
1099 | 3.05k | } |
1100 | | |
1101 | 0 | UndefValue *UndefValue::getElementValue(Constant *C) const { |
1102 | 0 | if (isa<ArrayType>(getType()) || isa<VectorType>(getType())) |
1103 | 0 | return getSequentialElement(); |
1104 | 0 | return getStructElement(cast<ConstantInt>(C)->getZExtValue()); |
1105 | 0 | } |
1106 | | |
1107 | 1.17M | UndefValue *UndefValue::getElementValue(unsigned Idx) const { |
1108 | 1.17M | if (isa<ArrayType>(getType()) || isa<VectorType>(getType())) |
1109 | 1.16M | return getSequentialElement(); |
1110 | 3.05k | return getStructElement(Idx); |
1111 | 1.17M | } |
1112 | | |
1113 | 1.35M | unsigned UndefValue::getNumElements() const { |
1114 | 1.35M | Type *Ty = getType(); |
1115 | 1.35M | if (auto *AT = dyn_cast<ArrayType>(Ty)) |
1116 | 1.03M | return AT->getNumElements(); |
1117 | 317k | if (auto *VT = dyn_cast<VectorType>(Ty)) |
1118 | 312k | return cast<FixedVectorType>(VT)->getNumElements(); |
1119 | 4.95k | return Ty->getStructNumElements(); |
1120 | 317k | } |
1121 | | |
1122 | | //===----------------------------------------------------------------------===// |
1123 | | // PoisonValue Implementation |
1124 | | //===----------------------------------------------------------------------===// |
1125 | | |
1126 | 182k | PoisonValue *PoisonValue::getSequentialElement() const { |
1127 | 182k | if (ArrayType *ATy = dyn_cast<ArrayType>(getType())) |
1128 | 16.2k | return PoisonValue::get(ATy->getElementType()); |
1129 | 166k | return PoisonValue::get(cast<VectorType>(getType())->getElementType()); |
1130 | 182k | } |
1131 | | |
1132 | 1.68k | PoisonValue *PoisonValue::getStructElement(unsigned Elt) const { |
1133 | 1.68k | return PoisonValue::get(getType()->getStructElementType(Elt)); |
1134 | 1.68k | } |
1135 | | |
1136 | 0 | PoisonValue *PoisonValue::getElementValue(Constant *C) const { |
1137 | 0 | if (isa<ArrayType>(getType()) || isa<VectorType>(getType())) |
1138 | 0 | return getSequentialElement(); |
1139 | 0 | return getStructElement(cast<ConstantInt>(C)->getZExtValue()); |
1140 | 0 | } |
1141 | | |
1142 | 184k | PoisonValue *PoisonValue::getElementValue(unsigned Idx) const { |
1143 | 184k | if (isa<ArrayType>(getType()) || isa<VectorType>(getType())) |
1144 | 182k | return getSequentialElement(); |
1145 | 1.68k | return getStructElement(Idx); |
1146 | 184k | } |
1147 | | |
1148 | | //===----------------------------------------------------------------------===// |
1149 | | // ConstantXXX Classes |
1150 | | //===----------------------------------------------------------------------===// |
1151 | | |
1152 | | template <typename ItTy, typename EltTy> |
1153 | 922 | static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) { |
1154 | 832k | for (; Start != End; ++Start) |
1155 | 832k | if (*Start != Elt) |
1156 | 697 | return false; |
1157 | 225 | return true; |
1158 | 922 | } |
1159 | | |
1160 | | template <typename SequentialTy, typename ElementTy> |
1161 | 91.0k | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { |
1162 | 91.0k | assert(!V.empty() && "Cannot get empty int sequence."); |
1163 | | |
1164 | 0 | SmallVector<ElementTy, 16> Elts; |
1165 | 91.0k | for (Constant *C : V) |
1166 | 365k | if (auto *CI = dyn_cast<ConstantInt>(C)) |
1167 | 324k | Elts.push_back(CI->getZExtValue()); |
1168 | 41.3k | else |
1169 | 41.3k | return nullptr; |
1170 | 49.6k | return SequentialTy::get(V[0]->getContext(), Elts); |
1171 | 91.0k | } Constants.cpp:llvm::Constant* getIntSequenceIfElementsMatch<llvm::ConstantDataArray, unsigned char>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1161 | 18 | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1162 | 18 | assert(!V.empty() && "Cannot get empty int sequence."); | 1163 | | | 1164 | 0 | SmallVector<ElementTy, 16> Elts; | 1165 | 18 | for (Constant *C : V) | 1166 | 43 | if (auto *CI = dyn_cast<ConstantInt>(C)) | 1167 | 26 | Elts.push_back(CI->getZExtValue()); | 1168 | 17 | else | 1169 | 17 | return nullptr; | 1170 | 1 | return SequentialTy::get(V[0]->getContext(), Elts); | 1171 | 18 | } |
Constants.cpp:llvm::Constant* getIntSequenceIfElementsMatch<llvm::ConstantDataArray, unsigned short>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1161 | 7 | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1162 | 7 | assert(!V.empty() && "Cannot get empty int sequence."); | 1163 | | | 1164 | 0 | SmallVector<ElementTy, 16> Elts; | 1165 | 7 | for (Constant *C : V) | 1166 | 13 | if (auto *CI = dyn_cast<ConstantInt>(C)) | 1167 | 7 | Elts.push_back(CI->getZExtValue()); | 1168 | 6 | else | 1169 | 6 | return nullptr; | 1170 | 1 | return SequentialTy::get(V[0]->getContext(), Elts); | 1171 | 7 | } |
Constants.cpp:llvm::Constant* getIntSequenceIfElementsMatch<llvm::ConstantDataArray, unsigned int>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1161 | 199 | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1162 | 199 | assert(!V.empty() && "Cannot get empty int sequence."); | 1163 | | | 1164 | 0 | SmallVector<ElementTy, 16> Elts; | 1165 | 199 | for (Constant *C : V) | 1166 | 1.14k | if (auto *CI = dyn_cast<ConstantInt>(C)) | 1167 | 1.07k | Elts.push_back(CI->getZExtValue()); | 1168 | 73 | else | 1169 | 73 | return nullptr; | 1170 | 126 | return SequentialTy::get(V[0]->getContext(), Elts); | 1171 | 199 | } |
Constants.cpp:llvm::Constant* getIntSequenceIfElementsMatch<llvm::ConstantDataArray, unsigned long>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1161 | 152 | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1162 | 152 | assert(!V.empty() && "Cannot get empty int sequence."); | 1163 | | | 1164 | 0 | SmallVector<ElementTy, 16> Elts; | 1165 | 152 | for (Constant *C : V) | 1166 | 237 | if (auto *CI = dyn_cast<ConstantInt>(C)) | 1167 | 209 | Elts.push_back(CI->getZExtValue()); | 1168 | 28 | else | 1169 | 28 | return nullptr; | 1170 | 124 | return SequentialTy::get(V[0]->getContext(), Elts); | 1171 | 152 | } |
Constants.cpp:llvm::Constant* getIntSequenceIfElementsMatch<llvm::ConstantDataVector, unsigned char>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1161 | 4.35k | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1162 | 4.35k | assert(!V.empty() && "Cannot get empty int sequence."); | 1163 | | | 1164 | 0 | SmallVector<ElementTy, 16> Elts; | 1165 | 4.35k | for (Constant *C : V) | 1166 | 38.1k | if (auto *CI = dyn_cast<ConstantInt>(C)) | 1167 | 35.7k | Elts.push_back(CI->getZExtValue()); | 1168 | 2.42k | else | 1169 | 2.42k | return nullptr; | 1170 | 1.93k | return SequentialTy::get(V[0]->getContext(), Elts); | 1171 | 4.35k | } |
Constants.cpp:llvm::Constant* getIntSequenceIfElementsMatch<llvm::ConstantDataVector, unsigned short>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1161 | 5.98k | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1162 | 5.98k | assert(!V.empty() && "Cannot get empty int sequence."); | 1163 | | | 1164 | 0 | SmallVector<ElementTy, 16> Elts; | 1165 | 5.98k | for (Constant *C : V) | 1166 | 28.3k | if (auto *CI = dyn_cast<ConstantInt>(C)) | 1167 | 24.1k | Elts.push_back(CI->getZExtValue()); | 1168 | 4.14k | else | 1169 | 4.14k | return nullptr; | 1170 | 1.84k | return SequentialTy::get(V[0]->getContext(), Elts); | 1171 | 5.98k | } |
Constants.cpp:llvm::Constant* getIntSequenceIfElementsMatch<llvm::ConstantDataVector, unsigned int>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1161 | 73.8k | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1162 | 73.8k | assert(!V.empty() && "Cannot get empty int sequence."); | 1163 | | | 1164 | 0 | SmallVector<ElementTy, 16> Elts; | 1165 | 73.8k | for (Constant *C : V) | 1166 | 279k | if (auto *CI = dyn_cast<ConstantInt>(C)) | 1167 | 248k | Elts.push_back(CI->getZExtValue()); | 1168 | 30.1k | else | 1169 | 30.1k | return nullptr; | 1170 | 43.6k | return SequentialTy::get(V[0]->getContext(), Elts); | 1171 | 73.8k | } |
Constants.cpp:llvm::Constant* getIntSequenceIfElementsMatch<llvm::ConstantDataVector, unsigned long>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1161 | 6.49k | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1162 | 6.49k | assert(!V.empty() && "Cannot get empty int sequence."); | 1163 | | | 1164 | 0 | SmallVector<ElementTy, 16> Elts; | 1165 | 6.49k | for (Constant *C : V) | 1166 | 18.3k | if (auto *CI = dyn_cast<ConstantInt>(C)) | 1167 | 13.8k | Elts.push_back(CI->getZExtValue()); | 1168 | 4.47k | else | 1169 | 4.47k | return nullptr; | 1170 | 2.02k | return SequentialTy::get(V[0]->getContext(), Elts); | 1171 | 6.49k | } |
|
1172 | | |
1173 | | template <typename SequentialTy, typename ElementTy> |
1174 | 5.66k | static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) { |
1175 | 5.66k | assert(!V.empty() && "Cannot get empty FP sequence."); |
1176 | | |
1177 | 0 | SmallVector<ElementTy, 16> Elts; |
1178 | 5.66k | for (Constant *C : V) |
1179 | 15.3k | if (auto *CFP = dyn_cast<ConstantFP>(C)) |
1180 | 11.3k | Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
1181 | 3.96k | else |
1182 | 3.96k | return nullptr; |
1183 | 1.70k | return SequentialTy::getFP(V[0]->getType(), Elts); |
1184 | 5.66k | } Unexecuted instantiation: Constants.cpp:llvm::Constant* getFPSequenceIfElementsMatch<llvm::ConstantDataArray, unsigned short>(llvm::ArrayRef<llvm::Constant*>) Constants.cpp:llvm::Constant* getFPSequenceIfElementsMatch<llvm::ConstantDataArray, unsigned int>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1174 | 106 | static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1175 | 106 | assert(!V.empty() && "Cannot get empty FP sequence."); | 1176 | | | 1177 | 0 | SmallVector<ElementTy, 16> Elts; | 1178 | 106 | for (Constant *C : V) | 1179 | 260 | if (auto *CFP = dyn_cast<ConstantFP>(C)) | 1180 | 205 | Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); | 1181 | 55 | else | 1182 | 55 | return nullptr; | 1183 | 51 | return SequentialTy::getFP(V[0]->getType(), Elts); | 1184 | 106 | } |
Constants.cpp:llvm::Constant* getFPSequenceIfElementsMatch<llvm::ConstantDataArray, unsigned long>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1174 | 92 | static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1175 | 92 | assert(!V.empty() && "Cannot get empty FP sequence."); | 1176 | | | 1177 | 0 | SmallVector<ElementTy, 16> Elts; | 1178 | 92 | for (Constant *C : V) | 1179 | 184 | if (auto *CFP = dyn_cast<ConstantFP>(C)) | 1180 | 141 | Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); | 1181 | 43 | else | 1182 | 43 | return nullptr; | 1183 | 49 | return SequentialTy::getFP(V[0]->getType(), Elts); | 1184 | 92 | } |
Constants.cpp:llvm::Constant* getFPSequenceIfElementsMatch<llvm::ConstantDataVector, unsigned short>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1174 | 139 | static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1175 | 139 | assert(!V.empty() && "Cannot get empty FP sequence."); | 1176 | | | 1177 | 0 | SmallVector<ElementTy, 16> Elts; | 1178 | 139 | for (Constant *C : V) | 1179 | 499 | if (auto *CFP = dyn_cast<ConstantFP>(C)) | 1180 | 389 | Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); | 1181 | 110 | else | 1182 | 110 | return nullptr; | 1183 | 29 | return SequentialTy::getFP(V[0]->getType(), Elts); | 1184 | 139 | } |
Constants.cpp:llvm::Constant* getFPSequenceIfElementsMatch<llvm::ConstantDataVector, unsigned int>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1174 | 3.75k | static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1175 | 3.75k | assert(!V.empty() && "Cannot get empty FP sequence."); | 1176 | | | 1177 | 0 | SmallVector<ElementTy, 16> Elts; | 1178 | 3.75k | for (Constant *C : V) | 1179 | 10.2k | if (auto *CFP = dyn_cast<ConstantFP>(C)) | 1180 | 7.65k | Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); | 1181 | 2.58k | else | 1182 | 2.58k | return nullptr; | 1183 | 1.17k | return SequentialTy::getFP(V[0]->getType(), Elts); | 1184 | 3.75k | } |
Constants.cpp:llvm::Constant* getFPSequenceIfElementsMatch<llvm::ConstantDataVector, unsigned long>(llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1174 | 1.57k | static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) { | 1175 | 1.57k | assert(!V.empty() && "Cannot get empty FP sequence."); | 1176 | | | 1177 | 0 | SmallVector<ElementTy, 16> Elts; | 1178 | 1.57k | for (Constant *C : V) | 1179 | 4.12k | if (auto *CFP = dyn_cast<ConstantFP>(C)) | 1180 | 2.95k | Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); | 1181 | 1.16k | else | 1182 | 1.16k | return nullptr; | 1183 | 401 | return SequentialTy::getFP(V[0]->getType(), Elts); | 1184 | 1.57k | } |
|
1185 | | |
1186 | | template <typename SequenceTy> |
1187 | | static Constant *getSequenceIfElementsMatch(Constant *C, |
1188 | 141k | ArrayRef<Constant *> V) { |
1189 | | // We speculatively build the elements here even if it turns out that there is |
1190 | | // a constantexpr or something else weird, since it is so uncommon for that to |
1191 | | // happen. |
1192 | 141k | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { |
1193 | 91.0k | if (CI->getType()->isIntegerTy(8)) |
1194 | 4.37k | return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V); |
1195 | 86.6k | else if (CI->getType()->isIntegerTy(16)) |
1196 | 5.99k | return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V); |
1197 | 80.6k | else if (CI->getType()->isIntegerTy(32)) |
1198 | 74.0k | return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V); |
1199 | 6.64k | else if (CI->getType()->isIntegerTy(64)) |
1200 | 6.64k | return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V); |
1201 | 91.0k | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
1202 | 5.66k | if (CFP->getType()->isHalfTy() || CFP->getType()->isBFloatTy()) |
1203 | 139 | return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V); |
1204 | 5.52k | else if (CFP->getType()->isFloatTy()) |
1205 | 3.86k | return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V); |
1206 | 1.66k | else if (CFP->getType()->isDoubleTy()) |
1207 | 1.66k | return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V); |
1208 | 5.66k | } |
1209 | | |
1210 | 44.6k | return nullptr; |
1211 | 141k | } Constants.cpp:llvm::Constant* getSequenceIfElementsMatch<llvm::ConstantDataArray>(llvm::Constant*, llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1188 | 765 | ArrayRef<Constant *> V) { | 1189 | | // We speculatively build the elements here even if it turns out that there is | 1190 | | // a constantexpr or something else weird, since it is so uncommon for that to | 1191 | | // happen. | 1192 | 765 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { | 1193 | 376 | if (CI->getType()->isIntegerTy(8)) | 1194 | 18 | return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V); | 1195 | 358 | else if (CI->getType()->isIntegerTy(16)) | 1196 | 7 | return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V); | 1197 | 351 | else if (CI->getType()->isIntegerTy(32)) | 1198 | 199 | return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V); | 1199 | 152 | else if (CI->getType()->isIntegerTy(64)) | 1200 | 152 | return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V); | 1201 | 389 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { | 1202 | 198 | if (CFP->getType()->isHalfTy() || CFP->getType()->isBFloatTy()) | 1203 | 0 | return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V); | 1204 | 198 | else if (CFP->getType()->isFloatTy()) | 1205 | 106 | return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V); | 1206 | 92 | else if (CFP->getType()->isDoubleTy()) | 1207 | 92 | return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V); | 1208 | 198 | } | 1209 | | | 1210 | 191 | return nullptr; | 1211 | 765 | } |
Constants.cpp:llvm::Constant* getSequenceIfElementsMatch<llvm::ConstantDataVector>(llvm::Constant*, llvm::ArrayRef<llvm::Constant*>) Line | Count | Source | 1188 | 140k | ArrayRef<Constant *> V) { | 1189 | | // We speculatively build the elements here even if it turns out that there is | 1190 | | // a constantexpr or something else weird, since it is so uncommon for that to | 1191 | | // happen. | 1192 | 140k | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { | 1193 | 90.6k | if (CI->getType()->isIntegerTy(8)) | 1194 | 4.35k | return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V); | 1195 | 86.2k | else if (CI->getType()->isIntegerTy(16)) | 1196 | 5.98k | return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V); | 1197 | 80.3k | else if (CI->getType()->isIntegerTy(32)) | 1198 | 73.8k | return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V); | 1199 | 6.49k | else if (CI->getType()->isIntegerTy(64)) | 1200 | 6.49k | return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V); | 1201 | 90.6k | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { | 1202 | 5.46k | if (CFP->getType()->isHalfTy() || CFP->getType()->isBFloatTy()) | 1203 | 139 | return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V); | 1204 | 5.32k | else if (CFP->getType()->isFloatTy()) | 1205 | 3.75k | return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V); | 1206 | 1.57k | else if (CFP->getType()->isDoubleTy()) | 1207 | 1.57k | return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V); | 1208 | 5.46k | } | 1209 | | | 1210 | 44.4k | return nullptr; | 1211 | 140k | } |
|
1212 | | |
1213 | | ConstantAggregate::ConstantAggregate(Type *T, ValueTy VT, |
1214 | | ArrayRef<Constant *> V) |
1215 | | : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(), |
1216 | 62.5k | V.size()) { |
1217 | 62.5k | llvm::copy(V, op_begin()); |
1218 | | |
1219 | | // Check that types match, unless this is an opaque struct. |
1220 | 62.5k | if (auto *ST = dyn_cast<StructType>(T)) { |
1221 | 2.77k | if (ST->isOpaque()) |
1222 | 0 | return; |
1223 | 9.68k | for (unsigned I = 0, E = V.size(); I != E; ++I) |
1224 | 6.91k | assert(V[I]->getType() == ST->getTypeAtIndex(I) && |
1225 | 2.77k | "Initializer for struct element doesn't match!"); |
1226 | 2.77k | } |
1227 | 62.5k | } |
1228 | | |
1229 | | ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V) |
1230 | 2.82k | : ConstantAggregate(T, ConstantArrayVal, V) { |
1231 | 2.82k | assert(V.size() == T->getNumElements() && |
1232 | 2.82k | "Invalid initializer for constant array"); |
1233 | 2.82k | } |
1234 | | |
1235 | 3.63k | Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { |
1236 | 3.63k | if (Constant *C = getImpl(Ty, V)) |
1237 | 577 | return C; |
1238 | 3.06k | return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V); |
1239 | 3.63k | } |
1240 | | |
1241 | 13.2k | Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) { |
1242 | | // Empty arrays are canonicalized to ConstantAggregateZero. |
1243 | 13.2k | if (V.empty()) |
1244 | 0 | return ConstantAggregateZero::get(Ty); |
1245 | | |
1246 | 1.16M | for (Constant *C : V) { |
1247 | 1.16M | assert(C->getType() == Ty->getElementType() && |
1248 | 1.16M | "Wrong type in array element initializer"); |
1249 | 0 | (void)C; |
1250 | 1.16M | } |
1251 | | |
1252 | | // If this is an all-zero array, return a ConstantAggregateZero object. If |
1253 | | // all undef, return an UndefValue, if "all simple", then return a |
1254 | | // ConstantDataArray. |
1255 | 13.2k | Constant *C = V[0]; |
1256 | 13.2k | if (isa<PoisonValue>(C) && rangeOnlyContains(V.begin(), V.end(), C)) |
1257 | 170 | return PoisonValue::get(Ty); |
1258 | | |
1259 | 13.0k | if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C)) |
1260 | 35 | return UndefValue::get(Ty); |
1261 | | |
1262 | 13.0k | if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C)) |
1263 | 20 | return ConstantAggregateZero::get(Ty); |
1264 | | |
1265 | | // Check to see if all of the elements are ConstantFP or ConstantInt and if |
1266 | | // the element type is compatible with ConstantDataVector. If so, use it. |
1267 | 12.9k | if (ConstantDataSequential::isElementTypeCompatible(C->getType())) |
1268 | 765 | return getSequenceIfElementsMatch<ConstantDataArray>(C, V); |
1269 | | |
1270 | | // Otherwise, we really do want to create a ConstantArray. |
1271 | 12.2k | return nullptr; |
1272 | 12.9k | } |
1273 | | |
1274 | | StructType *ConstantStruct::getTypeForElements(LLVMContext &Context, |
1275 | | ArrayRef<Constant*> V, |
1276 | 56 | bool Packed) { |
1277 | 56 | unsigned VecSize = V.size(); |
1278 | 56 | SmallVector<Type*, 16> EltTypes(VecSize); |
1279 | 226 | for (unsigned i = 0; i != VecSize; ++i) |
1280 | 170 | EltTypes[i] = V[i]->getType(); |
1281 | | |
1282 | 56 | return StructType::get(Context, EltTypes, Packed); |
1283 | 56 | } |
1284 | | |
1285 | | |
1286 | | StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V, |
1287 | 56 | bool Packed) { |
1288 | 56 | assert(!V.empty() && |
1289 | 56 | "ConstantStruct::getTypeForElements cannot be called on empty list"); |
1290 | 0 | return getTypeForElements(V[0]->getContext(), V, Packed); |
1291 | 56 | } |
1292 | | |
1293 | | ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V) |
1294 | 2.77k | : ConstantAggregate(T, ConstantStructVal, V) { |
1295 | 2.77k | assert((T->isOpaque() || V.size() == T->getNumElements()) && |
1296 | 2.77k | "Invalid initializer for constant struct"); |
1297 | 2.77k | } |
1298 | | |
1299 | | // ConstantStruct accessors. |
1300 | 23.0k | Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) { |
1301 | 23.0k | assert((ST->isOpaque() || ST->getNumElements() == V.size()) && |
1302 | 23.0k | "Incorrect # elements specified to ConstantStruct::get"); |
1303 | | |
1304 | | // Create a ConstantAggregateZero value if all elements are zeros. |
1305 | 0 | bool isZero = true; |
1306 | 23.0k | bool isUndef = false; |
1307 | 23.0k | bool isPoison = false; |
1308 | | |
1309 | 23.0k | if (!V.empty()) { |
1310 | 3.58k | isUndef = isa<UndefValue>(V[0]); |
1311 | 3.58k | isPoison = isa<PoisonValue>(V[0]); |
1312 | 3.58k | isZero = V[0]->isNullValue(); |
1313 | | // PoisonValue inherits UndefValue, so its check is not necessary. |
1314 | 3.58k | if (isUndef || isZero) { |
1315 | 4.46k | for (Constant *C : V) { |
1316 | 4.46k | if (!C->isNullValue()) |
1317 | 3.09k | isZero = false; |
1318 | 4.46k | if (!isa<PoisonValue>(C)) |
1319 | 4.09k | isPoison = false; |
1320 | 4.46k | if (isa<PoisonValue>(C) || !isa<UndefValue>(C)) |
1321 | 2.56k | isUndef = false; |
1322 | 4.46k | } |
1323 | 1.60k | } |
1324 | 3.58k | } |
1325 | 23.0k | if (isZero) |
1326 | 19.6k | return ConstantAggregateZero::get(ST); |
1327 | 3.40k | if (isPoison) |
1328 | 17 | return PoisonValue::get(ST); |
1329 | 3.38k | if (isUndef) |
1330 | 232 | return UndefValue::get(ST); |
1331 | | |
1332 | 3.15k | return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V); |
1333 | 3.38k | } |
1334 | | |
1335 | | ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V) |
1336 | 57.0k | : ConstantAggregate(T, ConstantVectorVal, V) { |
1337 | 57.0k | assert(V.size() == cast<FixedVectorType>(T)->getNumElements() && |
1338 | 57.0k | "Invalid initializer for constant vector"); |
1339 | 57.0k | } |
1340 | | |
1341 | | // ConstantVector accessors. |
1342 | 228k | Constant *ConstantVector::get(ArrayRef<Constant*> V) { |
1343 | 228k | if (Constant *C = getImpl(V)) |
1344 | 101k | return C; |
1345 | 126k | auto *Ty = FixedVectorType::get(V.front()->getType(), V.size()); |
1346 | 126k | return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V); |
1347 | 228k | } |
1348 | | |
1349 | 228k | Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) { |
1350 | 228k | assert(!V.empty() && "Vectors can't be empty"); |
1351 | 0 | auto *T = FixedVectorType::get(V.front()->getType(), V.size()); |
1352 | | |
1353 | | // If this is an all-undef or all-zero vector, return a |
1354 | | // ConstantAggregateZero or UndefValue. |
1355 | 228k | Constant *C = V[0]; |
1356 | 228k | bool isZero = C->isNullValue(); |
1357 | 228k | bool isUndef = isa<UndefValue>(C); |
1358 | 228k | bool isPoison = isa<PoisonValue>(C); |
1359 | | |
1360 | 228k | if (isZero || isUndef) { |
1361 | 570k | for (unsigned i = 1, e = V.size(); i != e; ++i) |
1362 | 519k | if (V[i] != C) { |
1363 | 98.9k | isZero = isUndef = isPoison = false; |
1364 | 98.9k | break; |
1365 | 98.9k | } |
1366 | 149k | } |
1367 | | |
1368 | 228k | if (isZero) |
1369 | 21.7k | return ConstantAggregateZero::get(T); |
1370 | 207k | if (isPoison) |
1371 | 27.8k | return PoisonValue::get(T); |
1372 | 179k | if (isUndef) |
1373 | 1.14k | return UndefValue::get(T); |
1374 | | |
1375 | | // Check to see if all of the elements are ConstantFP or ConstantInt and if |
1376 | | // the element type is compatible with ConstantDataVector. If so, use it. |
1377 | 178k | if (ConstantDataSequential::isElementTypeCompatible(C->getType())) |
1378 | 140k | return getSequenceIfElementsMatch<ConstantDataVector>(C, V); |
1379 | | |
1380 | | // Otherwise, the element type isn't compatible with ConstantDataVector, or |
1381 | | // the operand list contains a ConstantExpr or something else strange. |
1382 | 37.5k | return nullptr; |
1383 | 178k | } |
1384 | | |
1385 | 55.7k | Constant *ConstantVector::getSplat(ElementCount EC, Constant *V) { |
1386 | 55.7k | if (!EC.isScalable()) { |
1387 | | // If this splat is compatible with ConstantDataVector, use it instead of |
1388 | | // ConstantVector. |
1389 | 55.0k | if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) && |
1390 | 55.0k | ConstantDataSequential::isElementTypeCompatible(V->getType())) |
1391 | 33.7k | return ConstantDataVector::getSplat(EC.getKnownMinValue(), V); |
1392 | | |
1393 | 21.2k | SmallVector<Constant *, 32> Elts(EC.getKnownMinValue(), V); |
1394 | 21.2k | return get(Elts); |
1395 | 55.0k | } |
1396 | | |
1397 | 792 | Type *VTy = VectorType::get(V->getType(), EC); |
1398 | | |
1399 | 792 | if (V->isNullValue()) |
1400 | 243 | return ConstantAggregateZero::get(VTy); |
1401 | 549 | else if (isa<UndefValue>(V)) |
1402 | 4 | return UndefValue::get(VTy); |
1403 | | |
1404 | 545 | Type *IdxTy = Type::getInt64Ty(VTy->getContext()); |
1405 | | |
1406 | | // Move scalar into vector. |
1407 | 545 | Constant *PoisonV = PoisonValue::get(VTy); |
1408 | 545 | V = ConstantExpr::getInsertElement(PoisonV, V, ConstantInt::get(IdxTy, 0)); |
1409 | | // Build shuffle mask to perform the splat. |
1410 | 545 | SmallVector<int, 8> Zeros(EC.getKnownMinValue(), 0); |
1411 | | // Splat. |
1412 | 545 | return ConstantExpr::getShuffleVector(V, PoisonV, Zeros); |
1413 | 792 | } |
1414 | | |
1415 | 4.65k | ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) { |
1416 | 4.65k | LLVMContextImpl *pImpl = Context.pImpl; |
1417 | 4.65k | if (!pImpl->TheNoneToken) |
1418 | 614 | pImpl->TheNoneToken.reset(new ConstantTokenNone(Context)); |
1419 | 4.65k | return pImpl->TheNoneToken.get(); |
1420 | 4.65k | } |
1421 | | |
1422 | | /// Remove the constant from the constant table. |
1423 | 0 | void ConstantTokenNone::destroyConstantImpl() { |
1424 | 0 | llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!"); |
1425 | 0 | } |
1426 | | |
1427 | | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
1428 | | // can't be inline because we don't want to #include Instruction.h into |
1429 | | // Constant.h |
1430 | 34.7k | bool ConstantExpr::isCast() const { |
1431 | 34.7k | return Instruction::isCast(getOpcode()); |
1432 | 34.7k | } |
1433 | | |
1434 | 684k | bool ConstantExpr::isCompare() const { |
1435 | 684k | return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp; |
1436 | 684k | } |
1437 | | |
1438 | 39.1k | unsigned ConstantExpr::getPredicate() const { |
1439 | 39.1k | return cast<CompareConstantExpr>(this)->predicate; |
1440 | 39.1k | } |
1441 | | |
1442 | 5.31k | ArrayRef<int> ConstantExpr::getShuffleMask() const { |
1443 | 5.31k | return cast<ShuffleVectorConstantExpr>(this)->ShuffleMask; |
1444 | 5.31k | } |
1445 | | |
1446 | 0 | Constant *ConstantExpr::getShuffleMaskForBitcode() const { |
1447 | 0 | return cast<ShuffleVectorConstantExpr>(this)->ShuffleMaskForBitcode; |
1448 | 0 | } |
1449 | | |
1450 | | Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty, |
1451 | 4.08k | bool OnlyIfReduced, Type *SrcTy) const { |
1452 | 4.08k | assert(Ops.size() == getNumOperands() && "Operand count mismatch!"); |
1453 | | |
1454 | | // If no operands changed return self. |
1455 | 4.08k | if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin())) |
1456 | 2.17k | return const_cast<ConstantExpr*>(this); |
1457 | | |
1458 | 1.91k | Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr; |
1459 | 1.91k | switch (getOpcode()) { |
1460 | 0 | case Instruction::Trunc: |
1461 | 0 | case Instruction::ZExt: |
1462 | 0 | case Instruction::SExt: |
1463 | 0 | case Instruction::FPTrunc: |
1464 | 0 | case Instruction::FPExt: |
1465 | 0 | case Instruction::UIToFP: |
1466 | 0 | case Instruction::SIToFP: |
1467 | 0 | case Instruction::FPToUI: |
1468 | 0 | case Instruction::FPToSI: |
1469 | 12 | case Instruction::PtrToInt: |
1470 | 12 | case Instruction::IntToPtr: |
1471 | 12 | case Instruction::BitCast: |
1472 | 12 | case Instruction::AddrSpaceCast: |
1473 | 12 | return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced); |
1474 | 323 | case Instruction::InsertElement: |
1475 | 323 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2], |
1476 | 323 | OnlyIfReducedTy); |
1477 | 861 | case Instruction::ExtractElement: |
1478 | 861 | return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy); |
1479 | 0 | case Instruction::ShuffleVector: |
1480 | 0 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], getShuffleMask(), |
1481 | 0 | OnlyIfReducedTy); |
1482 | 707 | case Instruction::GetElementPtr: { |
1483 | 707 | auto *GEPO = cast<GEPOperator>(this); |
1484 | 707 | assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType())); |
1485 | 0 | return ConstantExpr::getGetElementPtr( |
1486 | 707 | SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1), |
1487 | 707 | GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy); |
1488 | 12 | } |
1489 | 13 | case Instruction::ICmp: |
1490 | 13 | case Instruction::FCmp: |
1491 | 13 | return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1], |
1492 | 13 | OnlyIfReducedTy); |
1493 | 0 | default: |
1494 | 0 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
1495 | 0 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData, |
1496 | 0 | OnlyIfReducedTy); |
1497 | 1.91k | } |
1498 | 1.91k | } |
1499 | | |
1500 | | |
1501 | | //===----------------------------------------------------------------------===// |
1502 | | // isValueValidForType implementations |
1503 | | |
1504 | 0 | bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) { |
1505 | 0 | unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay |
1506 | 0 | if (Ty->isIntegerTy(1)) |
1507 | 0 | return Val == 0 || Val == 1; |
1508 | 0 | return isUIntN(NumBits, Val); |
1509 | 0 | } |
1510 | | |
1511 | 1.16M | bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) { |
1512 | 1.16M | unsigned NumBits = Ty->getIntegerBitWidth(); |
1513 | 1.16M | if (Ty->isIntegerTy(1)) |
1514 | 0 | return Val == 0 || Val == 1 || Val == -1; |
1515 | 1.16M | return isIntN(NumBits, Val); |
1516 | 1.16M | } |
1517 | | |
1518 | 21.9k | bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) { |
1519 | | // convert modifies in place, so make a copy. |
1520 | 21.9k | APFloat Val2 = APFloat(Val); |
1521 | 21.9k | bool losesInfo; |
1522 | 21.9k | switch (Ty->getTypeID()) { |
1523 | 0 | default: |
1524 | 0 | return false; // These can't be represented as floating point! |
1525 | | |
1526 | | // FIXME rounding mode needs to be more flexible |
1527 | 21.9k | case Type::HalfTyID: { |
1528 | 21.9k | if (&Val2.getSemantics() == &APFloat::IEEEhalf()) |
1529 | 1 | return true; |
1530 | 21.9k | Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo); |
1531 | 21.9k | return !losesInfo; |
1532 | 21.9k | } |
1533 | 1 | case Type::BFloatTyID: { |
1534 | 1 | if (&Val2.getSemantics() == &APFloat::BFloat()) |
1535 | 0 | return true; |
1536 | 1 | Val2.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven, &losesInfo); |
1537 | 1 | return !losesInfo; |
1538 | 1 | } |
1539 | 2 | case Type::FloatTyID: { |
1540 | 2 | if (&Val2.getSemantics() == &APFloat::IEEEsingle()) |
1541 | 0 | return true; |
1542 | 2 | Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo); |
1543 | 2 | return !losesInfo; |
1544 | 2 | } |
1545 | 1 | case Type::DoubleTyID: { |
1546 | 1 | if (&Val2.getSemantics() == &APFloat::IEEEhalf() || |
1547 | 1 | &Val2.getSemantics() == &APFloat::BFloat() || |
1548 | 1 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
1549 | 1 | &Val2.getSemantics() == &APFloat::IEEEdouble()) |
1550 | 1 | return true; |
1551 | 0 | Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo); |
1552 | 0 | return !losesInfo; |
1553 | 1 | } |
1554 | 1 | case Type::X86_FP80TyID: |
1555 | 1 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
1556 | 1 | &Val2.getSemantics() == &APFloat::BFloat() || |
1557 | 1 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
1558 | 1 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
1559 | 1 | &Val2.getSemantics() == &APFloat::x87DoubleExtended(); |
1560 | 2 | case Type::FP128TyID: |
1561 | 2 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
1562 | 2 | &Val2.getSemantics() == &APFloat::BFloat() || |
1563 | 2 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
1564 | 2 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
1565 | 2 | &Val2.getSemantics() == &APFloat::IEEEquad(); |
1566 | 0 | case Type::PPC_FP128TyID: |
1567 | 0 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
1568 | 0 | &Val2.getSemantics() == &APFloat::BFloat() || |
1569 | 0 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
1570 | 0 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
1571 | 0 | &Val2.getSemantics() == &APFloat::PPCDoubleDouble(); |
1572 | 21.9k | } |
1573 | 21.9k | } |
1574 | | |
1575 | | |
1576 | | //===----------------------------------------------------------------------===// |
1577 | | // Factory Function Implementation |
1578 | | |
1579 | 110k | ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) { |
1580 | 110k | assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) && |
1581 | 110k | "Cannot create an aggregate zero of non-aggregate type!"); |
1582 | | |
1583 | 0 | std::unique_ptr<ConstantAggregateZero> &Entry = |
1584 | 110k | Ty->getContext().pImpl->CAZConstants[Ty]; |
1585 | 110k | if (!Entry) |
1586 | 37.4k | Entry.reset(new ConstantAggregateZero(Ty)); |
1587 | | |
1588 | 110k | return Entry.get(); |
1589 | 110k | } |
1590 | | |
1591 | | /// Remove the constant from the constant table. |
1592 | 0 | void ConstantAggregateZero::destroyConstantImpl() { |
1593 | 0 | getContext().pImpl->CAZConstants.erase(getType()); |
1594 | 0 | } |
1595 | | |
1596 | | /// Remove the constant from the constant table. |
1597 | 493 | void ConstantArray::destroyConstantImpl() { |
1598 | 493 | getType()->getContext().pImpl->ArrayConstants.remove(this); |
1599 | 493 | } |
1600 | | |
1601 | | |
1602 | | //---- ConstantStruct::get() implementation... |
1603 | | // |
1604 | | |
1605 | | /// Remove the constant from the constant table. |
1606 | 266 | void ConstantStruct::destroyConstantImpl() { |
1607 | 266 | getType()->getContext().pImpl->StructConstants.remove(this); |
1608 | 266 | } |
1609 | | |
1610 | | /// Remove the constant from the constant table. |
1611 | 414 | void ConstantVector::destroyConstantImpl() { |
1612 | 414 | getType()->getContext().pImpl->VectorConstants.remove(this); |
1613 | 414 | } |
1614 | | |
1615 | 556k | Constant *Constant::getSplatValue(bool AllowUndefs) const { |
1616 | 556k | assert(this->getType()->isVectorTy() && "Only valid for vectors!"); |
1617 | 556k | if (isa<ConstantAggregateZero>(this)) |
1618 | 50.1k | return getNullValue(cast<VectorType>(getType())->getElementType()); |
1619 | 506k | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
1620 | 341k | return CV->getSplatValue(); |
1621 | 164k | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
1622 | 139k | return CV->getSplatValue(AllowUndefs); |
1623 | | |
1624 | | // Check if this is a constant expression splat of the form returned by |
1625 | | // ConstantVector::getSplat() |
1626 | 24.7k | const auto *Shuf = dyn_cast<ConstantExpr>(this); |
1627 | 24.7k | if (Shuf && Shuf->getOpcode() == Instruction::ShuffleVector && |
1628 | 24.7k | isa<UndefValue>(Shuf->getOperand(1))) { |
1629 | | |
1630 | 4.70k | const auto *IElt = dyn_cast<ConstantExpr>(Shuf->getOperand(0)); |
1631 | 4.70k | if (IElt && IElt->getOpcode() == Instruction::InsertElement && |
1632 | 4.70k | isa<UndefValue>(IElt->getOperand(0))) { |
1633 | | |
1634 | 4.69k | ArrayRef<int> Mask = Shuf->getShuffleMask(); |
1635 | 4.69k | Constant *SplatVal = IElt->getOperand(1); |
1636 | 4.69k | ConstantInt *Index = dyn_cast<ConstantInt>(IElt->getOperand(2)); |
1637 | | |
1638 | 4.69k | if (Index && Index->getValue() == 0 && |
1639 | 13.9k | llvm::all_of(Mask, [](int I) { return I == 0; })) |
1640 | 4.69k | return SplatVal; |
1641 | 4.69k | } |
1642 | 4.70k | } |
1643 | | |
1644 | 20.0k | return nullptr; |
1645 | 24.7k | } |
1646 | | |
1647 | 139k | Constant *ConstantVector::getSplatValue(bool AllowUndefs) const { |
1648 | | // Check out first element. |
1649 | 139k | Constant *Elt = getOperand(0); |
1650 | | // Then make sure all remaining elements point to the same value. |
1651 | 383k | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) { |
1652 | 326k | Constant *OpC = getOperand(I); |
1653 | 326k | if (OpC == Elt) |
1654 | 233k | continue; |
1655 | | |
1656 | | // Strict mode: any mismatch is not a splat. |
1657 | 92.4k | if (!AllowUndefs) |
1658 | 80.3k | return nullptr; |
1659 | | |
1660 | | // Allow undefs mode: ignore undefined elements. |
1661 | 12.0k | if (isa<UndefValue>(OpC)) |
1662 | 9.84k | continue; |
1663 | | |
1664 | | // If we do not have a defined element yet, use the current operand. |
1665 | 2.22k | if (isa<UndefValue>(Elt)) |
1666 | 661 | Elt = OpC; |
1667 | | |
1668 | 2.22k | if (OpC != Elt) |
1669 | 1.56k | return nullptr; |
1670 | 2.22k | } |
1671 | 57.7k | return Elt; |
1672 | 139k | } |
1673 | | |
1674 | 258k | const APInt &Constant::getUniqueInteger() const { |
1675 | 258k | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
1676 | 257k | return CI->getValue(); |
1677 | | // Scalable vectors can use a ConstantExpr to build a splat. |
1678 | 1.28k | if (isa<ConstantExpr>(this)) |
1679 | 0 | return cast<ConstantInt>(this->getSplatValue())->getValue(); |
1680 | | // For non-ConstantExpr we use getAggregateElement as a fast path to avoid |
1681 | | // calling getSplatValue in release builds. |
1682 | 1.28k | assert(this->getSplatValue() && "Doesn't contain a unique integer!"); |
1683 | 0 | const Constant *C = this->getAggregateElement(0U); |
1684 | 1.28k | assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!"); |
1685 | 0 | return cast<ConstantInt>(C)->getValue(); |
1686 | 1.28k | } |
1687 | | |
1688 | | //---- ConstantPointerNull::get() implementation. |
1689 | | // |
1690 | | |
1691 | 19.3k | ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) { |
1692 | 19.3k | std::unique_ptr<ConstantPointerNull> &Entry = |
1693 | 19.3k | Ty->getContext().pImpl->CPNConstants[Ty]; |
1694 | 19.3k | if (!Entry) |
1695 | 8.40k | Entry.reset(new ConstantPointerNull(Ty)); |
1696 | | |
1697 | 19.3k | return Entry.get(); |
1698 | 19.3k | } |
1699 | | |
1700 | | /// Remove the constant from the constant table. |
1701 | 0 | void ConstantPointerNull::destroyConstantImpl() { |
1702 | 0 | getContext().pImpl->CPNConstants.erase(getType()); |
1703 | 0 | } |
1704 | | |
1705 | | //---- ConstantTargetNone::get() implementation. |
1706 | | // |
1707 | | |
1708 | 0 | ConstantTargetNone *ConstantTargetNone::get(TargetExtType *Ty) { |
1709 | 0 | assert(Ty->hasProperty(TargetExtType::HasZeroInit) && |
1710 | 0 | "Target extension type not allowed to have a zeroinitializer"); |
1711 | 0 | std::unique_ptr<ConstantTargetNone> &Entry = |
1712 | 0 | Ty->getContext().pImpl->CTNConstants[Ty]; |
1713 | 0 | if (!Entry) |
1714 | 0 | Entry.reset(new ConstantTargetNone(Ty)); |
1715 | |
|
1716 | 0 | return Entry.get(); |
1717 | 0 | } |
1718 | | |
1719 | | /// Remove the constant from the constant table. |
1720 | 0 | void ConstantTargetNone::destroyConstantImpl() { |
1721 | 0 | getContext().pImpl->CTNConstants.erase(getType()); |
1722 | 0 | } |
1723 | | |
1724 | 2.61M | UndefValue *UndefValue::get(Type *Ty) { |
1725 | 2.61M | std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty]; |
1726 | 2.61M | if (!Entry) |
1727 | 391k | Entry.reset(new UndefValue(Ty)); |
1728 | | |
1729 | 2.61M | return Entry.get(); |
1730 | 2.61M | } |
1731 | | |
1732 | | /// Remove the constant from the constant table. |
1733 | 0 | void UndefValue::destroyConstantImpl() { |
1734 | | // Free the constant and any dangling references to it. |
1735 | 0 | if (getValueID() == UndefValueVal) { |
1736 | 0 | getContext().pImpl->UVConstants.erase(getType()); |
1737 | 0 | } else if (getValueID() == PoisonValueVal) { |
1738 | 0 | getContext().pImpl->PVConstants.erase(getType()); |
1739 | 0 | } |
1740 | 0 | llvm_unreachable("Not a undef or a poison!"); |
1741 | 0 | } |
1742 | | |
1743 | 1.49M | PoisonValue *PoisonValue::get(Type *Ty) { |
1744 | 1.49M | std::unique_ptr<PoisonValue> &Entry = Ty->getContext().pImpl->PVConstants[Ty]; |
1745 | 1.49M | if (!Entry) |
1746 | 189k | Entry.reset(new PoisonValue(Ty)); |
1747 | | |
1748 | 1.49M | return Entry.get(); |
1749 | 1.49M | } |
1750 | | |
1751 | | /// Remove the constant from the constant table. |
1752 | 0 | void PoisonValue::destroyConstantImpl() { |
1753 | | // Free the constant and any dangling references to it. |
1754 | 0 | getContext().pImpl->PVConstants.erase(getType()); |
1755 | 0 | } |
1756 | | |
1757 | 0 | BlockAddress *BlockAddress::get(BasicBlock *BB) { |
1758 | 0 | assert(BB->getParent() && "Block must have a parent"); |
1759 | 0 | return get(BB->getParent(), BB); |
1760 | 0 | } |
1761 | | |
1762 | 11.8k | BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) { |
1763 | 11.8k | BlockAddress *&BA = |
1764 | 11.8k | F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)]; |
1765 | 11.8k | if (!BA) |
1766 | 11.7k | BA = new BlockAddress(F, BB); |
1767 | | |
1768 | 11.8k | assert(BA->getFunction() == F && "Basic block moved between functions"); |
1769 | 0 | return BA; |
1770 | 11.8k | } |
1771 | | |
1772 | | BlockAddress::BlockAddress(Function *F, BasicBlock *BB) |
1773 | | : Constant(PointerType::get(F->getContext(), F->getAddressSpace()), |
1774 | 11.7k | Value::BlockAddressVal, &Op<0>(), 2) { |
1775 | 11.7k | setOperand(0, F); |
1776 | 11.7k | setOperand(1, BB); |
1777 | 11.7k | BB->AdjustBlockAddressRefCount(1); |
1778 | 11.7k | } |
1779 | | |
1780 | 0 | BlockAddress *BlockAddress::lookup(const BasicBlock *BB) { |
1781 | 0 | if (!BB->hasAddressTaken()) |
1782 | 0 | return nullptr; |
1783 | | |
1784 | 0 | const Function *F = BB->getParent(); |
1785 | 0 | assert(F && "Block must have a parent"); |
1786 | 0 | BlockAddress *BA = |
1787 | 0 | F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB)); |
1788 | 0 | assert(BA && "Refcount and block address map disagree!"); |
1789 | 0 | return BA; |
1790 | 0 | } |
1791 | | |
1792 | | /// Remove the constant from the constant table. |
1793 | 11.7k | void BlockAddress::destroyConstantImpl() { |
1794 | 11.7k | getFunction()->getType()->getContext().pImpl |
1795 | 11.7k | ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock())); |
1796 | 11.7k | getBasicBlock()->AdjustBlockAddressRefCount(-1); |
1797 | 11.7k | } |
1798 | | |
1799 | 2.53k | Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) { |
1800 | | // This could be replacing either the Basic Block or the Function. In either |
1801 | | // case, we have to remove the map entry. |
1802 | 2.53k | Function *NewF = getFunction(); |
1803 | 2.53k | BasicBlock *NewBB = getBasicBlock(); |
1804 | | |
1805 | 2.53k | if (From == NewF) |
1806 | 0 | NewF = cast<Function>(To->stripPointerCasts()); |
1807 | 2.53k | else { |
1808 | 2.53k | assert(From == NewBB && "From does not match any operand"); |
1809 | 0 | NewBB = cast<BasicBlock>(To); |
1810 | 2.53k | } |
1811 | | |
1812 | | // See if the 'new' entry already exists, if not, just update this in place |
1813 | | // and return early. |
1814 | 0 | BlockAddress *&NewBA = |
1815 | 2.53k | getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)]; |
1816 | 2.53k | if (NewBA) |
1817 | 1.65k | return NewBA; |
1818 | | |
1819 | 878 | getBasicBlock()->AdjustBlockAddressRefCount(-1); |
1820 | | |
1821 | | // Remove the old entry, this can't cause the map to rehash (just a |
1822 | | // tombstone will get added). |
1823 | 878 | getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(), |
1824 | 878 | getBasicBlock())); |
1825 | 878 | NewBA = this; |
1826 | 878 | setOperand(0, NewF); |
1827 | 878 | setOperand(1, NewBB); |
1828 | 878 | getBasicBlock()->AdjustBlockAddressRefCount(1); |
1829 | | |
1830 | | // If we just want to keep the existing value, then return null. |
1831 | | // Callers know that this means we shouldn't delete this value. |
1832 | 878 | return nullptr; |
1833 | 2.53k | } |
1834 | | |
1835 | 0 | DSOLocalEquivalent *DSOLocalEquivalent::get(GlobalValue *GV) { |
1836 | 0 | DSOLocalEquivalent *&Equiv = GV->getContext().pImpl->DSOLocalEquivalents[GV]; |
1837 | 0 | if (!Equiv) |
1838 | 0 | Equiv = new DSOLocalEquivalent(GV); |
1839 | |
|
1840 | 0 | assert(Equiv->getGlobalValue() == GV && |
1841 | 0 | "DSOLocalFunction does not match the expected global value"); |
1842 | 0 | return Equiv; |
1843 | 0 | } |
1844 | | |
1845 | | DSOLocalEquivalent::DSOLocalEquivalent(GlobalValue *GV) |
1846 | 0 | : Constant(GV->getType(), Value::DSOLocalEquivalentVal, &Op<0>(), 1) { |
1847 | 0 | setOperand(0, GV); |
1848 | 0 | } |
1849 | | |
1850 | | /// Remove the constant from the constant table. |
1851 | 0 | void DSOLocalEquivalent::destroyConstantImpl() { |
1852 | 0 | const GlobalValue *GV = getGlobalValue(); |
1853 | 0 | GV->getContext().pImpl->DSOLocalEquivalents.erase(GV); |
1854 | 0 | } |
1855 | | |
1856 | 0 | Value *DSOLocalEquivalent::handleOperandChangeImpl(Value *From, Value *To) { |
1857 | 0 | assert(From == getGlobalValue() && "Changing value does not match operand."); |
1858 | 0 | assert(isa<Constant>(To) && "Can only replace the operands with a constant"); |
1859 | | |
1860 | | // The replacement is with another global value. |
1861 | 0 | if (const auto *ToObj = dyn_cast<GlobalValue>(To)) { |
1862 | 0 | DSOLocalEquivalent *&NewEquiv = |
1863 | 0 | getContext().pImpl->DSOLocalEquivalents[ToObj]; |
1864 | 0 | if (NewEquiv) |
1865 | 0 | return llvm::ConstantExpr::getBitCast(NewEquiv, getType()); |
1866 | 0 | } |
1867 | | |
1868 | | // If the argument is replaced with a null value, just replace this constant |
1869 | | // with a null value. |
1870 | 0 | if (cast<Constant>(To)->isNullValue()) |
1871 | 0 | return To; |
1872 | | |
1873 | | // The replacement could be a bitcast or an alias to another function. We can |
1874 | | // replace it with a bitcast to the dso_local_equivalent of that function. |
1875 | 0 | auto *Func = cast<Function>(To->stripPointerCastsAndAliases()); |
1876 | 0 | DSOLocalEquivalent *&NewEquiv = getContext().pImpl->DSOLocalEquivalents[Func]; |
1877 | 0 | if (NewEquiv) |
1878 | 0 | return llvm::ConstantExpr::getBitCast(NewEquiv, getType()); |
1879 | | |
1880 | | // Replace this with the new one. |
1881 | 0 | getContext().pImpl->DSOLocalEquivalents.erase(getGlobalValue()); |
1882 | 0 | NewEquiv = this; |
1883 | 0 | setOperand(0, Func); |
1884 | |
|
1885 | 0 | if (Func->getType() != getType()) { |
1886 | | // It is ok to mutate the type here because this constant should always |
1887 | | // reflect the type of the function it's holding. |
1888 | 0 | mutateType(Func->getType()); |
1889 | 0 | } |
1890 | 0 | return nullptr; |
1891 | 0 | } |
1892 | | |
1893 | 3.14k | NoCFIValue *NoCFIValue::get(GlobalValue *GV) { |
1894 | 3.14k | NoCFIValue *&NC = GV->getContext().pImpl->NoCFIValues[GV]; |
1895 | 3.14k | if (!NC) |
1896 | 974 | NC = new NoCFIValue(GV); |
1897 | | |
1898 | 3.14k | assert(NC->getGlobalValue() == GV && |
1899 | 3.14k | "NoCFIValue does not match the expected global value"); |
1900 | 0 | return NC; |
1901 | 3.14k | } |
1902 | | |
1903 | | NoCFIValue::NoCFIValue(GlobalValue *GV) |
1904 | 974 | : Constant(GV->getType(), Value::NoCFIValueVal, &Op<0>(), 1) { |
1905 | 974 | setOperand(0, GV); |
1906 | 974 | } |
1907 | | |
1908 | | /// Remove the constant from the constant table. |
1909 | 974 | void NoCFIValue::destroyConstantImpl() { |
1910 | 974 | const GlobalValue *GV = getGlobalValue(); |
1911 | 974 | GV->getContext().pImpl->NoCFIValues.erase(GV); |
1912 | 974 | } |
1913 | | |
1914 | 0 | Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) { |
1915 | 0 | assert(From == getGlobalValue() && "Changing value does not match operand."); |
1916 | | |
1917 | 0 | GlobalValue *GV = dyn_cast<GlobalValue>(To->stripPointerCasts()); |
1918 | 0 | assert(GV && "Can only replace the operands with a global value"); |
1919 | | |
1920 | 0 | NoCFIValue *&NewNC = getContext().pImpl->NoCFIValues[GV]; |
1921 | 0 | if (NewNC) |
1922 | 0 | return llvm::ConstantExpr::getBitCast(NewNC, getType()); |
1923 | | |
1924 | 0 | getContext().pImpl->NoCFIValues.erase(getGlobalValue()); |
1925 | 0 | NewNC = this; |
1926 | 0 | setOperand(0, GV); |
1927 | |
|
1928 | 0 | if (GV->getType() != getType()) |
1929 | 0 | mutateType(GV->getType()); |
1930 | |
|
1931 | 0 | return nullptr; |
1932 | 0 | } |
1933 | | |
1934 | | //---- ConstantExpr::get() implementations. |
1935 | | // |
1936 | | |
1937 | | /// This is a utility function to handle folding of casts and lookup of the |
1938 | | /// cast in the ExprConstants map. It is used by the various get* methods below. |
1939 | | static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty, |
1940 | 1.35M | bool OnlyIfReduced = false) { |
1941 | 1.35M | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
1942 | | // Fold a few common cases |
1943 | 1.35M | if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty)) |
1944 | 1.27M | return FC; |
1945 | | |
1946 | 76.1k | if (OnlyIfReduced) |
1947 | 8.05k | return nullptr; |
1948 | | |
1949 | 68.1k | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
1950 | | |
1951 | | // Look up the constant in the table first to ensure uniqueness. |
1952 | 68.1k | ConstantExprKeyType Key(opc, C); |
1953 | | |
1954 | 68.1k | return pImpl->ExprConstants.getOrCreate(Ty, Key); |
1955 | 76.1k | } |
1956 | | |
1957 | | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty, |
1958 | 101k | bool OnlyIfReduced) { |
1959 | 101k | Instruction::CastOps opc = Instruction::CastOps(oc); |
1960 | 101k | assert(Instruction::isCast(opc) && "opcode out of range"); |
1961 | 0 | assert(isSupportedCastOp(opc) && |
1962 | 101k | "Cast opcode not supported as constant expression"); |
1963 | 0 | assert(C && Ty && "Null arguments to getCast"); |
1964 | 0 | assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"); |
1965 | | |
1966 | 0 | switch (opc) { |
1967 | 0 | default: |
1968 | 0 | llvm_unreachable("Invalid cast opcode"); |
1969 | 65.8k | case Instruction::Trunc: |
1970 | 65.8k | return getTrunc(C, Ty, OnlyIfReduced); |
1971 | 11.4k | case Instruction::PtrToInt: |
1972 | 11.4k | return getPtrToInt(C, Ty, OnlyIfReduced); |
1973 | 10.5k | case Instruction::IntToPtr: |
1974 | 10.5k | return getIntToPtr(C, Ty, OnlyIfReduced); |
1975 | 9.95k | case Instruction::BitCast: |
1976 | 9.95k | return getBitCast(C, Ty, OnlyIfReduced); |
1977 | 3.86k | case Instruction::AddrSpaceCast: |
1978 | 3.86k | return getAddrSpaceCast(C, Ty, OnlyIfReduced); |
1979 | 101k | } |
1980 | 101k | } |
1981 | | |
1982 | 0 | Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) { |
1983 | 0 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
1984 | 0 | return getBitCast(C, Ty); |
1985 | 0 | return getTrunc(C, Ty); |
1986 | 0 | } |
1987 | | |
1988 | 4.01k | Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) { |
1989 | 4.01k | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
1990 | 0 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
1991 | 4.01k | "Invalid cast"); |
1992 | | |
1993 | 4.01k | if (Ty->isIntOrIntVectorTy()) |
1994 | 0 | return getPtrToInt(S, Ty); |
1995 | | |
1996 | 4.01k | unsigned SrcAS = S->getType()->getPointerAddressSpace(); |
1997 | 4.01k | if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace()) |
1998 | 4.01k | return getAddrSpaceCast(S, Ty); |
1999 | | |
2000 | 0 | return getBitCast(S, Ty); |
2001 | 4.01k | } |
2002 | | |
2003 | | Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S, |
2004 | 43 | Type *Ty) { |
2005 | 43 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
2006 | 0 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
2007 | | |
2008 | 43 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
2009 | 0 | return getAddrSpaceCast(S, Ty); |
2010 | | |
2011 | 43 | return getBitCast(S, Ty); |
2012 | 43 | } |
2013 | | |
2014 | 1.25M | Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) { |
2015 | 1.25M | #ifndef NDEBUG |
2016 | 1.25M | bool fromVec = isa<VectorType>(C->getType()); |
2017 | 1.25M | bool toVec = isa<VectorType>(Ty); |
2018 | 1.25M | #endif |
2019 | 1.25M | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
2020 | 0 | assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer"); |
2021 | 0 | assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral"); |
2022 | 0 | assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
2023 | 1.25M | "SrcTy must be larger than DestTy for Trunc!"); |
2024 | | |
2025 | 0 | return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced); |
2026 | 1.25M | } |
2027 | | |
2028 | | Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy, |
2029 | 29.7k | bool OnlyIfReduced) { |
2030 | 29.7k | assert(C->getType()->isPtrOrPtrVectorTy() && |
2031 | 29.7k | "PtrToInt source must be pointer or pointer vector"); |
2032 | 0 | assert(DstTy->isIntOrIntVectorTy() && |
2033 | 29.7k | "PtrToInt destination must be integer or integer vector"); |
2034 | 0 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
2035 | 29.7k | if (isa<VectorType>(C->getType())) |
2036 | 469 | assert(cast<VectorType>(C->getType())->getElementCount() == |
2037 | 29.7k | cast<VectorType>(DstTy)->getElementCount() && |
2038 | 29.7k | "Invalid cast between a different number of vector elements"); |
2039 | 0 | return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced); |
2040 | 29.7k | } |
2041 | | |
2042 | | Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy, |
2043 | 41.2k | bool OnlyIfReduced) { |
2044 | 41.2k | assert(C->getType()->isIntOrIntVectorTy() && |
2045 | 41.2k | "IntToPtr source must be integer or integer vector"); |
2046 | 0 | assert(DstTy->isPtrOrPtrVectorTy() && |
2047 | 41.2k | "IntToPtr destination must be a pointer or pointer vector"); |
2048 | 0 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
2049 | 41.2k | if (isa<VectorType>(C->getType())) |
2050 | 70 | assert(cast<VectorType>(C->getType())->getElementCount() == |
2051 | 41.2k | cast<VectorType>(DstTy)->getElementCount() && |
2052 | 41.2k | "Invalid cast between a different number of vector elements"); |
2053 | 0 | return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced); |
2054 | 41.2k | } |
2055 | | |
2056 | | Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy, |
2057 | 22.4k | bool OnlyIfReduced) { |
2058 | 22.4k | assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) && |
2059 | 22.4k | "Invalid constantexpr bitcast!"); |
2060 | | |
2061 | | // It is common to ask for a bitcast of a value to its own type, handle this |
2062 | | // speedily. |
2063 | 22.4k | if (C->getType() == DstTy) return C; |
2064 | | |
2065 | 9.24k | return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced); |
2066 | 22.4k | } |
2067 | | |
2068 | | Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy, |
2069 | 11.1k | bool OnlyIfReduced) { |
2070 | 11.1k | assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) && |
2071 | 11.1k | "Invalid constantexpr addrspacecast!"); |
2072 | 0 | return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced); |
2073 | 11.1k | } |
2074 | | |
2075 | | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2, |
2076 | 1.54M | unsigned Flags, Type *OnlyIfReducedTy) { |
2077 | | // Check the operands for consistency first. |
2078 | 1.54M | assert(Instruction::isBinaryOp(Opcode) && |
2079 | 1.54M | "Invalid opcode in binary constant expression"); |
2080 | 0 | assert(isSupportedBinOp(Opcode) && |
2081 | 1.54M | "Binop not supported as constant expression"); |
2082 | 0 | assert(C1->getType() == C2->getType() && |
2083 | 1.54M | "Operand types in binary constant expression should match"); |
2084 | | |
2085 | 0 | #ifndef NDEBUG |
2086 | 0 | switch (Opcode) { |
2087 | 626k | case Instruction::Add: |
2088 | 1.04M | case Instruction::Sub: |
2089 | 1.32M | case Instruction::Mul: |
2090 | 1.32M | assert(C1->getType()->isIntOrIntVectorTy() && |
2091 | 1.32M | "Tried to create an integer operation on a non-integer type!"); |
2092 | 0 | break; |
2093 | 0 | case Instruction::And: |
2094 | 0 | case Instruction::Or: |
2095 | 165k | case Instruction::Xor: |
2096 | 165k | assert(C1->getType()->isIntOrIntVectorTy() && |
2097 | 165k | "Tried to create a logical operation on a non-integral type!"); |
2098 | 0 | break; |
2099 | 54.4k | case Instruction::Shl: |
2100 | 54.4k | case Instruction::LShr: |
2101 | 54.4k | case Instruction::AShr: |
2102 | 54.4k | assert(C1->getType()->isIntOrIntVectorTy() && |
2103 | 54.4k | "Tried to create a shift operation on a non-integer type!"); |
2104 | 0 | break; |
2105 | 0 | default: |
2106 | 0 | break; |
2107 | 1.54M | } |
2108 | 1.54M | #endif |
2109 | | |
2110 | 1.54M | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
2111 | 1.52M | return FC; |
2112 | | |
2113 | 26.1k | if (OnlyIfReducedTy == C1->getType()) |
2114 | 0 | return nullptr; |
2115 | | |
2116 | 26.1k | Constant *ArgVec[] = { C1, C2 }; |
2117 | 26.1k | ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags); |
2118 | | |
2119 | 26.1k | LLVMContextImpl *pImpl = C1->getContext().pImpl; |
2120 | 26.1k | return pImpl->ExprConstants.getOrCreate(C1->getType(), Key); |
2121 | 26.1k | } |
2122 | | |
2123 | 1.48M | bool ConstantExpr::isDesirableBinOp(unsigned Opcode) { |
2124 | 1.48M | switch (Opcode) { |
2125 | 25.0k | case Instruction::UDiv: |
2126 | 50.0k | case Instruction::SDiv: |
2127 | 96.5k | case Instruction::URem: |
2128 | 132k | case Instruction::SRem: |
2129 | 177k | case Instruction::FAdd: |
2130 | 206k | case Instruction::FSub: |
2131 | 238k | case Instruction::FMul: |
2132 | 267k | case Instruction::FDiv: |
2133 | 294k | case Instruction::FRem: |
2134 | 335k | case Instruction::And: |
2135 | 378k | case Instruction::Or: |
2136 | 406k | case Instruction::LShr: |
2137 | 434k | case Instruction::AShr: |
2138 | 434k | return false; |
2139 | 576k | case Instruction::Add: |
2140 | 674k | case Instruction::Sub: |
2141 | 957k | case Instruction::Mul: |
2142 | 1.00M | case Instruction::Shl: |
2143 | 1.05M | case Instruction::Xor: |
2144 | 1.05M | return true; |
2145 | 0 | default: |
2146 | 0 | llvm_unreachable("Argument must be binop opcode"); |
2147 | 1.48M | } |
2148 | 1.48M | } |
2149 | | |
2150 | 1.55M | bool ConstantExpr::isSupportedBinOp(unsigned Opcode) { |
2151 | 1.55M | switch (Opcode) { |
2152 | 2 | case Instruction::UDiv: |
2153 | 33 | case Instruction::SDiv: |
2154 | 126 | case Instruction::URem: |
2155 | 153 | case Instruction::SRem: |
2156 | 154 | case Instruction::FAdd: |
2157 | 192 | case Instruction::FSub: |
2158 | 218 | case Instruction::FMul: |
2159 | 224 | case Instruction::FDiv: |
2160 | 224 | case Instruction::FRem: |
2161 | 3.73k | case Instruction::And: |
2162 | 3.84k | case Instruction::Or: |
2163 | 3.84k | case Instruction::LShr: |
2164 | 5.90k | case Instruction::AShr: |
2165 | 5.90k | return false; |
2166 | 626k | case Instruction::Add: |
2167 | 1.04M | case Instruction::Sub: |
2168 | 1.32M | case Instruction::Mul: |
2169 | 1.38M | case Instruction::Shl: |
2170 | 1.55M | case Instruction::Xor: |
2171 | 1.55M | return true; |
2172 | 0 | default: |
2173 | 0 | llvm_unreachable("Argument must be binop opcode"); |
2174 | 1.55M | } |
2175 | 1.55M | } |
2176 | | |
2177 | 210k | bool ConstantExpr::isDesirableCastOp(unsigned Opcode) { |
2178 | 210k | switch (Opcode) { |
2179 | 21.3k | case Instruction::ZExt: |
2180 | 95.2k | case Instruction::SExt: |
2181 | 98.3k | case Instruction::FPTrunc: |
2182 | 99.0k | case Instruction::FPExt: |
2183 | 99.6k | case Instruction::UIToFP: |
2184 | 117k | case Instruction::SIToFP: |
2185 | 117k | case Instruction::FPToUI: |
2186 | 118k | case Instruction::FPToSI: |
2187 | 118k | return false; |
2188 | 65.7k | case Instruction::Trunc: |
2189 | 76.1k | case Instruction::PtrToInt: |
2190 | 85.7k | case Instruction::IntToPtr: |
2191 | 88.7k | case Instruction::BitCast: |
2192 | 92.2k | case Instruction::AddrSpaceCast: |
2193 | 92.2k | return true; |
2194 | 0 | default: |
2195 | 0 | llvm_unreachable("Argument must be cast opcode"); |
2196 | 210k | } |
2197 | 210k | } |
2198 | | |
2199 | 111k | bool ConstantExpr::isSupportedCastOp(unsigned Opcode) { |
2200 | 111k | switch (Opcode) { |
2201 | 495 | case Instruction::ZExt: |
2202 | 495 | case Instruction::SExt: |
2203 | 495 | case Instruction::FPTrunc: |
2204 | 495 | case Instruction::FPExt: |
2205 | 522 | case Instruction::UIToFP: |
2206 | 529 | case Instruction::SIToFP: |
2207 | 530 | case Instruction::FPToUI: |
2208 | 531 | case Instruction::FPToSI: |
2209 | 531 | return false; |
2210 | 65.9k | case Instruction::Trunc: |
2211 | 78.4k | case Instruction::PtrToInt: |
2212 | 90.0k | case Instruction::IntToPtr: |
2213 | 106k | case Instruction::BitCast: |
2214 | 111k | case Instruction::AddrSpaceCast: |
2215 | 111k | return true; |
2216 | 0 | default: |
2217 | 0 | llvm_unreachable("Argument must be cast opcode"); |
2218 | 111k | } |
2219 | 111k | } |
2220 | | |
2221 | 16 | Constant *ConstantExpr::getSizeOf(Type* Ty) { |
2222 | | // sizeof is implemented as: (i64) gep (Ty*)null, 1 |
2223 | | // Note that a non-inbounds gep is used, as null isn't within any object. |
2224 | 16 | Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
2225 | 16 | Constant *GEP = getGetElementPtr( |
2226 | 16 | Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); |
2227 | 16 | return getPtrToInt(GEP, |
2228 | 16 | Type::getInt64Ty(Ty->getContext())); |
2229 | 16 | } |
2230 | | |
2231 | 0 | Constant *ConstantExpr::getAlignOf(Type* Ty) { |
2232 | | // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1 |
2233 | | // Note that a non-inbounds gep is used, as null isn't within any object. |
2234 | 0 | Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty); |
2235 | 0 | Constant *NullPtr = Constant::getNullValue(PointerType::getUnqual(AligningTy->getContext())); |
2236 | 0 | Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0); |
2237 | 0 | Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
2238 | 0 | Constant *Indices[2] = { Zero, One }; |
2239 | 0 | Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices); |
2240 | 0 | return getPtrToInt(GEP, |
2241 | 0 | Type::getInt64Ty(Ty->getContext())); |
2242 | 0 | } |
2243 | | |
2244 | | Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1, |
2245 | 400k | Constant *C2, bool OnlyIfReduced) { |
2246 | 400k | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
2247 | | |
2248 | 0 | switch (Predicate) { |
2249 | 0 | default: llvm_unreachable("Invalid CmpInst predicate"); |
2250 | 17.1k | case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT: |
2251 | 35.8k | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE: |
2252 | 53.2k | case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO: |
2253 | 67.3k | case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: |
2254 | 82.0k | case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: |
2255 | 83.8k | case CmpInst::FCMP_TRUE: |
2256 | 83.8k | return getFCmp(Predicate, C1, C2, OnlyIfReduced); |
2257 | | |
2258 | 134k | case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: |
2259 | 198k | case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: |
2260 | 291k | case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: |
2261 | 316k | case CmpInst::ICMP_SLE: |
2262 | 316k | return getICmp(Predicate, C1, C2, OnlyIfReduced); |
2263 | 400k | } |
2264 | 400k | } |
2265 | | |
2266 | | Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C, |
2267 | | ArrayRef<Value *> Idxs, bool InBounds, |
2268 | | std::optional<unsigned> InRangeIndex, |
2269 | 216k | Type *OnlyIfReducedTy) { |
2270 | 216k | assert(Ty && "Must specify element type"); |
2271 | 0 | assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!"); |
2272 | | |
2273 | 216k | if (Constant *FC = |
2274 | 216k | ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs)) |
2275 | 32.1k | return FC; // Fold a few common cases. |
2276 | | |
2277 | 184k | assert(GetElementPtrInst::getIndexedType(Ty, Idxs) && |
2278 | 184k | "GEP indices invalid!");; |
2279 | | |
2280 | | // Get the result type of the getelementptr! |
2281 | 184k | Type *ReqTy = GetElementPtrInst::getGEPReturnType(C, Idxs); |
2282 | 184k | if (OnlyIfReducedTy == ReqTy) |
2283 | 250 | return nullptr; |
2284 | | |
2285 | 183k | auto EltCount = ElementCount::getFixed(0); |
2286 | 183k | if (VectorType *VecTy = dyn_cast<VectorType>(ReqTy)) |
2287 | 1.17k | EltCount = VecTy->getElementCount(); |
2288 | | |
2289 | | // Look up the constant in the table first to ensure uniqueness |
2290 | 183k | std::vector<Constant*> ArgVec; |
2291 | 183k | ArgVec.reserve(1 + Idxs.size()); |
2292 | 183k | ArgVec.push_back(C); |
2293 | 183k | auto GTI = gep_type_begin(Ty, Idxs), GTE = gep_type_end(Ty, Idxs); |
2294 | 537k | for (; GTI != GTE; ++GTI) { |
2295 | 353k | auto *Idx = cast<Constant>(GTI.getOperand()); |
2296 | 353k | assert( |
2297 | 353k | (!isa<VectorType>(Idx->getType()) || |
2298 | 353k | cast<VectorType>(Idx->getType())->getElementCount() == EltCount) && |
2299 | 353k | "getelementptr index type missmatch"); |
2300 | | |
2301 | 353k | if (GTI.isStruct() && Idx->getType()->isVectorTy()) { |
2302 | 0 | Idx = Idx->getSplatValue(); |
2303 | 353k | } else if (GTI.isSequential() && EltCount.isNonZero() && |
2304 | 353k | !Idx->getType()->isVectorTy()) { |
2305 | 33 | Idx = ConstantVector::getSplat(EltCount, Idx); |
2306 | 33 | } |
2307 | 353k | ArgVec.push_back(Idx); |
2308 | 353k | } |
2309 | | |
2310 | 183k | unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0; |
2311 | 183k | if (InRangeIndex && *InRangeIndex < 63) |
2312 | 5 | SubClassOptionalData |= (*InRangeIndex + 1) << 1; |
2313 | 183k | const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0, |
2314 | 183k | SubClassOptionalData, std::nullopt, Ty); |
2315 | | |
2316 | 183k | LLVMContextImpl *pImpl = C->getContext().pImpl; |
2317 | 183k | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
2318 | 184k | } |
2319 | | |
2320 | | Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS, |
2321 | 446k | Constant *RHS, bool OnlyIfReduced) { |
2322 | 446k | auto Predicate = static_cast<CmpInst::Predicate>(pred); |
2323 | 446k | assert(LHS->getType() == RHS->getType()); |
2324 | 0 | assert(CmpInst::isIntPredicate(Predicate) && "Invalid ICmp Predicate"); |
2325 | | |
2326 | 446k | if (Constant *FC = ConstantFoldCompareInstruction(Predicate, LHS, RHS)) |
2327 | 431k | return FC; // Fold a few common cases... |
2328 | | |
2329 | 14.6k | if (OnlyIfReduced) |
2330 | 13 | return nullptr; |
2331 | | |
2332 | | // Look up the constant in the table first to ensure uniqueness |
2333 | 14.6k | Constant *ArgVec[] = { LHS, RHS }; |
2334 | | // Get the key type with both the opcode and predicate |
2335 | 14.6k | const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, Predicate); |
2336 | | |
2337 | 14.6k | Type *ResultTy = Type::getInt1Ty(LHS->getContext()); |
2338 | 14.6k | if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) |
2339 | 2 | ResultTy = VectorType::get(ResultTy, VT->getElementCount()); |
2340 | | |
2341 | 14.6k | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
2342 | 14.6k | return pImpl->ExprConstants.getOrCreate(ResultTy, Key); |
2343 | 14.6k | } |
2344 | | |
2345 | | Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, |
2346 | 83.8k | Constant *RHS, bool OnlyIfReduced) { |
2347 | 83.8k | auto Predicate = static_cast<CmpInst::Predicate>(pred); |
2348 | 83.8k | assert(LHS->getType() == RHS->getType()); |
2349 | 0 | assert(CmpInst::isFPPredicate(Predicate) && "Invalid FCmp Predicate"); |
2350 | | |
2351 | 83.8k | if (Constant *FC = ConstantFoldCompareInstruction(Predicate, LHS, RHS)) |
2352 | 83.5k | return FC; // Fold a few common cases... |
2353 | | |
2354 | 377 | if (OnlyIfReduced) |
2355 | 0 | return nullptr; |
2356 | | |
2357 | | // Look up the constant in the table first to ensure uniqueness |
2358 | 377 | Constant *ArgVec[] = { LHS, RHS }; |
2359 | | // Get the key type with both the opcode and predicate |
2360 | 377 | const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, Predicate); |
2361 | | |
2362 | 377 | Type *ResultTy = Type::getInt1Ty(LHS->getContext()); |
2363 | 377 | if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) |
2364 | 0 | ResultTy = VectorType::get(ResultTy, VT->getElementCount()); |
2365 | | |
2366 | 377 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
2367 | 377 | return pImpl->ExprConstants.getOrCreate(ResultTy, Key); |
2368 | 377 | } |
2369 | | |
2370 | | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx, |
2371 | 306k | Type *OnlyIfReducedTy) { |
2372 | 306k | assert(Val->getType()->isVectorTy() && |
2373 | 306k | "Tried to create extractelement operation on non-vector type!"); |
2374 | 0 | assert(Idx->getType()->isIntegerTy() && |
2375 | 306k | "Extractelement index must be an integer type!"); |
2376 | | |
2377 | 306k | if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) |
2378 | 295k | return FC; // Fold a few common cases. |
2379 | | |
2380 | 10.8k | Type *ReqTy = cast<VectorType>(Val->getType())->getElementType(); |
2381 | 10.8k | if (OnlyIfReducedTy == ReqTy) |
2382 | 0 | return nullptr; |
2383 | | |
2384 | | // Look up the constant in the table first to ensure uniqueness |
2385 | 10.8k | Constant *ArgVec[] = { Val, Idx }; |
2386 | 10.8k | const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec); |
2387 | | |
2388 | 10.8k | LLVMContextImpl *pImpl = Val->getContext().pImpl; |
2389 | 10.8k | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
2390 | 10.8k | } |
2391 | | |
2392 | | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
2393 | 25.2k | Constant *Idx, Type *OnlyIfReducedTy) { |
2394 | 25.2k | assert(Val->getType()->isVectorTy() && |
2395 | 25.2k | "Tried to create insertelement operation on non-vector type!"); |
2396 | 0 | assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() && |
2397 | 25.2k | "Insertelement types must match!"); |
2398 | 0 | assert(Idx->getType()->isIntegerTy() && |
2399 | 25.2k | "Insertelement index must be i32 type!"); |
2400 | | |
2401 | 25.2k | if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) |
2402 | 21.2k | return FC; // Fold a few common cases. |
2403 | | |
2404 | 4.00k | if (OnlyIfReducedTy == Val->getType()) |
2405 | 0 | return nullptr; |
2406 | | |
2407 | | // Look up the constant in the table first to ensure uniqueness |
2408 | 4.00k | Constant *ArgVec[] = { Val, Elt, Idx }; |
2409 | 4.00k | const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec); |
2410 | | |
2411 | 4.00k | LLVMContextImpl *pImpl = Val->getContext().pImpl; |
2412 | 4.00k | return pImpl->ExprConstants.getOrCreate(Val->getType(), Key); |
2413 | 4.00k | } |
2414 | | |
2415 | | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
2416 | | ArrayRef<int> Mask, |
2417 | 20.5k | Type *OnlyIfReducedTy) { |
2418 | 20.5k | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
2419 | 20.5k | "Invalid shuffle vector constant expr operands!"); |
2420 | | |
2421 | 20.5k | if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) |
2422 | 17.0k | return FC; // Fold a few common cases. |
2423 | | |
2424 | 3.44k | unsigned NElts = Mask.size(); |
2425 | 3.44k | auto V1VTy = cast<VectorType>(V1->getType()); |
2426 | 3.44k | Type *EltTy = V1VTy->getElementType(); |
2427 | 3.44k | bool TypeIsScalable = isa<ScalableVectorType>(V1VTy); |
2428 | 3.44k | Type *ShufTy = VectorType::get(EltTy, NElts, TypeIsScalable); |
2429 | | |
2430 | 3.44k | if (OnlyIfReducedTy == ShufTy) |
2431 | 0 | return nullptr; |
2432 | | |
2433 | | // Look up the constant in the table first to ensure uniqueness |
2434 | 3.44k | Constant *ArgVec[] = {V1, V2}; |
2435 | 3.44k | ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, 0, Mask); |
2436 | | |
2437 | 3.44k | LLVMContextImpl *pImpl = ShufTy->getContext().pImpl; |
2438 | 3.44k | return pImpl->ExprConstants.getOrCreate(ShufTy, Key); |
2439 | 3.44k | } |
2440 | | |
2441 | 317k | Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) { |
2442 | 317k | assert(C->getType()->isIntOrIntVectorTy() && |
2443 | 317k | "Cannot NEG a nonintegral value!"); |
2444 | 0 | return getSub(ConstantInt::get(C->getType(), 0), C, HasNUW, HasNSW); |
2445 | 317k | } |
2446 | | |
2447 | 101k | Constant *ConstantExpr::getNot(Constant *C) { |
2448 | 101k | assert(C->getType()->isIntOrIntVectorTy() && |
2449 | 101k | "Cannot NOT a nonintegral value!"); |
2450 | 0 | return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType())); |
2451 | 101k | } |
2452 | | |
2453 | | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2, |
2454 | 10.8k | bool HasNUW, bool HasNSW) { |
2455 | 10.8k | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
2456 | 10.8k | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
2457 | 10.8k | return get(Instruction::Add, C1, C2, Flags); |
2458 | 10.8k | } |
2459 | | |
2460 | | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2, |
2461 | 318k | bool HasNUW, bool HasNSW) { |
2462 | 318k | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
2463 | 318k | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
2464 | 318k | return get(Instruction::Sub, C1, C2, Flags); |
2465 | 318k | } |
2466 | | |
2467 | | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2, |
2468 | 0 | bool HasNUW, bool HasNSW) { |
2469 | 0 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
2470 | 0 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
2471 | 0 | return get(Instruction::Mul, C1, C2, Flags); |
2472 | 0 | } |
2473 | | |
2474 | 15.4k | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
2475 | 15.4k | return get(Instruction::Xor, C1, C2); |
2476 | 15.4k | } |
2477 | | |
2478 | | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2, |
2479 | 2.34k | bool HasNUW, bool HasNSW) { |
2480 | 2.34k | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
2481 | 2.34k | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
2482 | 2.34k | return get(Instruction::Shl, C1, C2, Flags); |
2483 | 2.34k | } |
2484 | | |
2485 | 5.56k | Constant *ConstantExpr::getExactLogBase2(Constant *C) { |
2486 | 5.56k | Type *Ty = C->getType(); |
2487 | 5.56k | const APInt *IVal; |
2488 | 5.56k | if (match(C, m_APInt(IVal)) && IVal->isPowerOf2()) |
2489 | 1.56k | return ConstantInt::get(Ty, IVal->logBase2()); |
2490 | | |
2491 | | // FIXME: We can extract pow of 2 of splat constant for scalable vectors. |
2492 | 4.00k | auto *VecTy = dyn_cast<FixedVectorType>(Ty); |
2493 | 4.00k | if (!VecTy) |
2494 | 3.41k | return nullptr; |
2495 | | |
2496 | 590 | SmallVector<Constant *, 4> Elts; |
2497 | 874 | for (unsigned I = 0, E = VecTy->getNumElements(); I != E; ++I) { |
2498 | 826 | Constant *Elt = C->getAggregateElement(I); |
2499 | 826 | if (!Elt) |
2500 | 0 | return nullptr; |
2501 | | // Note that log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N. |
2502 | 826 | if (isa<UndefValue>(Elt)) { |
2503 | 82 | Elts.push_back(Constant::getNullValue(Ty->getScalarType())); |
2504 | 82 | continue; |
2505 | 82 | } |
2506 | 744 | if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2()) |
2507 | 542 | return nullptr; |
2508 | 202 | Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2())); |
2509 | 202 | } |
2510 | | |
2511 | 48 | return ConstantVector::get(Elts); |
2512 | 590 | } |
2513 | | |
2514 | | Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty, |
2515 | 2.08M | bool AllowRHSConstant, bool NSZ) { |
2516 | 2.08M | assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed"); |
2517 | | |
2518 | | // Commutative opcodes: it does not matter if AllowRHSConstant is set. |
2519 | 2.08M | if (Instruction::isCommutative(Opcode)) { |
2520 | 1.31M | switch (Opcode) { |
2521 | 635k | case Instruction::Add: // X + 0 = X |
2522 | 689k | case Instruction::Or: // X | 0 = X |
2523 | 883k | case Instruction::Xor: // X ^ 0 = X |
2524 | 883k | return Constant::getNullValue(Ty); |
2525 | 297k | case Instruction::Mul: // X * 1 = X |
2526 | 297k | return ConstantInt::get(Ty, 1); |
2527 | 57.6k | case Instruction::And: // X & -1 = X |
2528 | 57.6k | return Constant::getAllOnesValue(Ty); |
2529 | 45.5k | case Instruction::FAdd: // X + -0.0 = X |
2530 | 45.5k | return ConstantFP::getZero(Ty, !NSZ); |
2531 | 32.4k | case Instruction::FMul: // X * 1.0 = X |
2532 | 32.4k | return ConstantFP::get(Ty, 1.0); |
2533 | 0 | default: |
2534 | 0 | llvm_unreachable("Every commutative binop has an identity constant"); |
2535 | 1.31M | } |
2536 | 1.31M | } |
2537 | | |
2538 | | // Non-commutative opcodes: AllowRHSConstant must be set. |
2539 | 765k | if (!AllowRHSConstant) |
2540 | 763k | return nullptr; |
2541 | | |
2542 | 1.19k | switch (Opcode) { |
2543 | 412 | case Instruction::Sub: // X - 0 = X |
2544 | 641 | case Instruction::Shl: // X << 0 = X |
2545 | 855 | case Instruction::LShr: // X >>u 0 = X |
2546 | 925 | case Instruction::AShr: // X >> 0 = X |
2547 | 927 | case Instruction::FSub: // X - 0.0 = X |
2548 | 927 | return Constant::getNullValue(Ty); |
2549 | 76 | case Instruction::SDiv: // X / 1 = X |
2550 | 168 | case Instruction::UDiv: // X /u 1 = X |
2551 | 168 | return ConstantInt::get(Ty, 1); |
2552 | 10 | case Instruction::FDiv: // X / 1.0 = X |
2553 | 10 | return ConstantFP::get(Ty, 1.0); |
2554 | 87 | default: |
2555 | 87 | return nullptr; |
2556 | 1.19k | } |
2557 | 1.19k | } |
2558 | | |
2559 | 0 | Constant *ConstantExpr::getIntrinsicIdentity(Intrinsic::ID ID, Type *Ty) { |
2560 | 0 | switch (ID) { |
2561 | 0 | case Intrinsic::umax: |
2562 | 0 | return Constant::getNullValue(Ty); |
2563 | 0 | case Intrinsic::umin: |
2564 | 0 | return Constant::getAllOnesValue(Ty); |
2565 | 0 | case Intrinsic::smax: |
2566 | 0 | return Constant::getIntegerValue( |
2567 | 0 | Ty, APInt::getSignedMinValue(Ty->getIntegerBitWidth())); |
2568 | 0 | case Intrinsic::smin: |
2569 | 0 | return Constant::getIntegerValue( |
2570 | 0 | Ty, APInt::getSignedMaxValue(Ty->getIntegerBitWidth())); |
2571 | 0 | default: |
2572 | 0 | return nullptr; |
2573 | 0 | } |
2574 | 0 | } |
2575 | | |
2576 | | Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty, |
2577 | 0 | bool AllowRHSConstant, bool NSZ) { |
2578 | 0 | if (I->isBinaryOp()) |
2579 | 0 | return getBinOpIdentity(I->getOpcode(), Ty, AllowRHSConstant, NSZ); |
2580 | 0 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
2581 | 0 | return getIntrinsicIdentity(II->getIntrinsicID(), Ty); |
2582 | 0 | return nullptr; |
2583 | 0 | } |
2584 | | |
2585 | 25.3k | Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) { |
2586 | 25.3k | switch (Opcode) { |
2587 | 7.02k | default: |
2588 | | // Doesn't have an absorber. |
2589 | 7.02k | return nullptr; |
2590 | | |
2591 | 4.21k | case Instruction::Or: |
2592 | 4.21k | return Constant::getAllOnesValue(Ty); |
2593 | | |
2594 | 4.82k | case Instruction::And: |
2595 | 14.1k | case Instruction::Mul: |
2596 | 14.1k | return Constant::getNullValue(Ty); |
2597 | 25.3k | } |
2598 | 25.3k | } |
2599 | | |
2600 | | /// Remove the constant from the constant table. |
2601 | 103k | void ConstantExpr::destroyConstantImpl() { |
2602 | 103k | getType()->getContext().pImpl->ExprConstants.remove(this); |
2603 | 103k | } |
2604 | | |
2605 | 36 | const char *ConstantExpr::getOpcodeName() const { |
2606 | 36 | return Instruction::getOpcodeName(getOpcode()); |
2607 | 36 | } |
2608 | | |
2609 | | GetElementPtrConstantExpr::GetElementPtrConstantExpr( |
2610 | | Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy) |
2611 | | : ConstantExpr(DestTy, Instruction::GetElementPtr, |
2612 | | OperandTraits<GetElementPtrConstantExpr>::op_end(this) - |
2613 | | (IdxList.size() + 1), |
2614 | | IdxList.size() + 1), |
2615 | | SrcElementTy(SrcElementTy), |
2616 | 85.3k | ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) { |
2617 | 85.3k | Op<0>() = C; |
2618 | 85.3k | Use *OperandList = getOperandList(); |
2619 | 252k | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
2620 | 166k | OperandList[i+1] = IdxList[i]; |
2621 | 85.3k | } |
2622 | | |
2623 | 590k | Type *GetElementPtrConstantExpr::getSourceElementType() const { |
2624 | 590k | return SrcElementTy; |
2625 | 590k | } |
2626 | | |
2627 | 142k | Type *GetElementPtrConstantExpr::getResultElementType() const { |
2628 | 142k | return ResElementTy; |
2629 | 142k | } |
2630 | | |
2631 | | //===----------------------------------------------------------------------===// |
2632 | | // ConstantData* implementations |
2633 | | |
2634 | 6.39M | Type *ConstantDataSequential::getElementType() const { |
2635 | 6.39M | if (ArrayType *ATy = dyn_cast<ArrayType>(getType())) |
2636 | 89.8k | return ATy->getElementType(); |
2637 | 6.30M | return cast<VectorType>(getType())->getElementType(); |
2638 | 6.39M | } |
2639 | | |
2640 | 34.3k | StringRef ConstantDataSequential::getRawDataValues() const { |
2641 | 34.3k | return StringRef(DataElements, getNumElements()*getElementByteSize()); |
2642 | 34.3k | } |
2643 | | |
2644 | 497k | bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) { |
2645 | 497k | if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() || Ty->isDoubleTy()) |
2646 | 42.4k | return true; |
2647 | 454k | if (auto *IT = dyn_cast<IntegerType>(Ty)) { |
2648 | 442k | switch (IT->getBitWidth()) { |
2649 | 49.5k | case 8: |
2650 | 78.1k | case 16: |
2651 | 344k | case 32: |
2652 | 384k | case 64: |
2653 | 384k | return true; |
2654 | 57.8k | default: break; |
2655 | 442k | } |
2656 | 442k | } |
2657 | 70.6k | return false; |
2658 | 454k | } |
2659 | | |
2660 | 1.49M | unsigned ConstantDataSequential::getNumElements() const { |
2661 | 1.49M | if (ArrayType *AT = dyn_cast<ArrayType>(getType())) |
2662 | 30.6k | return AT->getNumElements(); |
2663 | 1.46M | return cast<FixedVectorType>(getType())->getNumElements(); |
2664 | 1.49M | } |
2665 | | |
2666 | | |
2667 | 1.11M | uint64_t ConstantDataSequential::getElementByteSize() const { |
2668 | 1.11M | return getElementType()->getPrimitiveSizeInBits()/8; |
2669 | 1.11M | } |
2670 | | |
2671 | | /// Return the start of the specified element. |
2672 | 1.05M | const char *ConstantDataSequential::getElementPointer(unsigned Elt) const { |
2673 | 1.05M | assert(Elt < getNumElements() && "Invalid Elt"); |
2674 | 0 | return DataElements+Elt*getElementByteSize(); |
2675 | 1.05M | } |
2676 | | |
2677 | | |
2678 | | /// Return true if the array is empty or all zeros. |
2679 | 217k | static bool isAllZeros(StringRef Arr) { |
2680 | 217k | for (char I : Arr) |
2681 | 584k | if (I != 0) |
2682 | 213k | return false; |
2683 | 3.52k | return true; |
2684 | 217k | } |
2685 | | |
2686 | | /// This is the underlying implementation of all of the |
2687 | | /// ConstantDataSequential::get methods. They all thunk down to here, providing |
2688 | | /// the correct element type. We take the bytes in as a StringRef because |
2689 | | /// we *want* an underlying "char*" to avoid TBAA type punning violations. |
2690 | 217k | Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) { |
2691 | 217k | #ifndef NDEBUG |
2692 | 217k | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) |
2693 | 6.20k | assert(isElementTypeCompatible(ATy->getElementType())); |
2694 | 211k | else |
2695 | 211k | assert(isElementTypeCompatible(cast<VectorType>(Ty)->getElementType())); |
2696 | 0 | #endif |
2697 | | // If the elements are all zero or there are no elements, return a CAZ, which |
2698 | | // is more dense and canonical. |
2699 | 217k | if (isAllZeros(Elements)) |
2700 | 3.52k | return ConstantAggregateZero::get(Ty); |
2701 | | |
2702 | | // Do a lookup to see if we have already formed one of these. |
2703 | 213k | auto &Slot = |
2704 | 213k | *Ty->getContext() |
2705 | 213k | .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr)) |
2706 | 213k | .first; |
2707 | | |
2708 | | // The bucket can point to a linked list of different CDS's that have the same |
2709 | | // body but different types. For example, 0,0,0,1 could be a 4 element array |
2710 | | // of i8, or a 1-element array of i32. They'll both end up in the same |
2711 | | /// StringMap bucket, linked up by their Next pointers. Walk the list. |
2712 | 213k | std::unique_ptr<ConstantDataSequential> *Entry = &Slot.second; |
2713 | 217k | for (; *Entry; Entry = &(*Entry)->Next) |
2714 | 120k | if ((*Entry)->getType() == Ty) |
2715 | 116k | return Entry->get(); |
2716 | | |
2717 | | // Okay, we didn't get a hit. Create a node of the right class, link it in, |
2718 | | // and return it. |
2719 | 97.2k | if (isa<ArrayType>(Ty)) { |
2720 | | // Use reset because std::make_unique can't access the constructor. |
2721 | 6.01k | Entry->reset(new ConstantDataArray(Ty, Slot.first().data())); |
2722 | 6.01k | return Entry->get(); |
2723 | 6.01k | } |
2724 | | |
2725 | 91.2k | assert(isa<VectorType>(Ty)); |
2726 | | // Use reset because std::make_unique can't access the constructor. |
2727 | 0 | Entry->reset(new ConstantDataVector(Ty, Slot.first().data())); |
2728 | 91.2k | return Entry->get(); |
2729 | 97.2k | } |
2730 | | |
2731 | 0 | void ConstantDataSequential::destroyConstantImpl() { |
2732 | | // Remove the constant from the StringMap. |
2733 | 0 | StringMap<std::unique_ptr<ConstantDataSequential>> &CDSConstants = |
2734 | 0 | getType()->getContext().pImpl->CDSConstants; |
2735 | |
|
2736 | 0 | auto Slot = CDSConstants.find(getRawDataValues()); |
2737 | |
|
2738 | 0 | assert(Slot != CDSConstants.end() && "CDS not found in uniquing table"); |
2739 | | |
2740 | 0 | std::unique_ptr<ConstantDataSequential> *Entry = &Slot->getValue(); |
2741 | | |
2742 | | // Remove the entry from the hash table. |
2743 | 0 | if (!(*Entry)->Next) { |
2744 | | // If there is only one value in the bucket (common case) it must be this |
2745 | | // entry, and removing the entry should remove the bucket completely. |
2746 | 0 | assert(Entry->get() == this && "Hash mismatch in ConstantDataSequential"); |
2747 | 0 | getContext().pImpl->CDSConstants.erase(Slot); |
2748 | 0 | return; |
2749 | 0 | } |
2750 | | |
2751 | | // Otherwise, there are multiple entries linked off the bucket, unlink the |
2752 | | // node we care about but keep the bucket around. |
2753 | 0 | while (true) { |
2754 | 0 | std::unique_ptr<ConstantDataSequential> &Node = *Entry; |
2755 | 0 | assert(Node && "Didn't find entry in its uniquing hash table!"); |
2756 | | // If we found our entry, unlink it from the list and we're done. |
2757 | 0 | if (Node.get() == this) { |
2758 | 0 | Node = std::move(Node->Next); |
2759 | 0 | return; |
2760 | 0 | } |
2761 | | |
2762 | 0 | Entry = &Node->Next; |
2763 | 0 | } |
2764 | 0 | } |
2765 | | |
2766 | | /// getFP() constructors - Return a constant of array type with a float |
2767 | | /// element type taken from argument `ElementType', and count taken from |
2768 | | /// argument `Elts'. The amount of bits of the contained type must match the |
2769 | | /// number of bits of the type contained in the passed in ArrayRef. |
2770 | | /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note |
2771 | | /// that this can return a ConstantAggregateZero object. |
2772 | 0 | Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint16_t> Elts) { |
2773 | 0 | assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) && |
2774 | 0 | "Element type is not a 16-bit float type"); |
2775 | 0 | Type *Ty = ArrayType::get(ElementType, Elts.size()); |
2776 | 0 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2777 | 0 | return getImpl(StringRef(Data, Elts.size() * 2), Ty); |
2778 | 0 | } |
2779 | 201 | Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint32_t> Elts) { |
2780 | 201 | assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type"); |
2781 | 0 | Type *Ty = ArrayType::get(ElementType, Elts.size()); |
2782 | 201 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2783 | 201 | return getImpl(StringRef(Data, Elts.size() * 4), Ty); |
2784 | 201 | } |
2785 | 73 | Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint64_t> Elts) { |
2786 | 73 | assert(ElementType->isDoubleTy() && |
2787 | 73 | "Element type is not a 64-bit float type"); |
2788 | 0 | Type *Ty = ArrayType::get(ElementType, Elts.size()); |
2789 | 73 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2790 | 73 | return getImpl(StringRef(Data, Elts.size() * 8), Ty); |
2791 | 73 | } |
2792 | | |
2793 | | Constant *ConstantDataArray::getString(LLVMContext &Context, |
2794 | 4.94k | StringRef Str, bool AddNull) { |
2795 | 4.94k | if (!AddNull) { |
2796 | 541 | const uint8_t *Data = Str.bytes_begin(); |
2797 | 541 | return get(Context, ArrayRef(Data, Str.size())); |
2798 | 541 | } |
2799 | | |
2800 | 4.40k | SmallVector<uint8_t, 64> ElementVals; |
2801 | 4.40k | ElementVals.append(Str.begin(), Str.end()); |
2802 | 4.40k | ElementVals.push_back(0); |
2803 | 4.40k | return get(Context, ElementVals); |
2804 | 4.94k | } |
2805 | | |
2806 | | /// get() constructors - Return a constant with vector type with an element |
2807 | | /// count and element type matching the ArrayRef passed in. Note that this |
2808 | | /// can return a ConstantAggregateZero object. |
2809 | 25.9k | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){ |
2810 | 25.9k | auto *Ty = FixedVectorType::get(Type::getInt8Ty(Context), Elts.size()); |
2811 | 25.9k | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2812 | 25.9k | return getImpl(StringRef(Data, Elts.size() * 1), Ty); |
2813 | 25.9k | } |
2814 | 14.1k | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){ |
2815 | 14.1k | auto *Ty = FixedVectorType::get(Type::getInt16Ty(Context), Elts.size()); |
2816 | 14.1k | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2817 | 14.1k | return getImpl(StringRef(Data, Elts.size() * 2), Ty); |
2818 | 14.1k | } |
2819 | 139k | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){ |
2820 | 139k | auto *Ty = FixedVectorType::get(Type::getInt32Ty(Context), Elts.size()); |
2821 | 139k | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2822 | 139k | return getImpl(StringRef(Data, Elts.size() * 4), Ty); |
2823 | 139k | } |
2824 | 17.3k | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){ |
2825 | 17.3k | auto *Ty = FixedVectorType::get(Type::getInt64Ty(Context), Elts.size()); |
2826 | 17.3k | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2827 | 17.3k | return getImpl(StringRef(Data, Elts.size() * 8), Ty); |
2828 | 17.3k | } |
2829 | 0 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) { |
2830 | 0 | auto *Ty = FixedVectorType::get(Type::getFloatTy(Context), Elts.size()); |
2831 | 0 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2832 | 0 | return getImpl(StringRef(Data, Elts.size() * 4), Ty); |
2833 | 0 | } |
2834 | 0 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) { |
2835 | 0 | auto *Ty = FixedVectorType::get(Type::getDoubleTy(Context), Elts.size()); |
2836 | 0 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2837 | 0 | return getImpl(StringRef(Data, Elts.size() * 8), Ty); |
2838 | 0 | } |
2839 | | |
2840 | | /// getFP() constructors - Return a constant of vector type with a float |
2841 | | /// element type taken from argument `ElementType', and count taken from |
2842 | | /// argument `Elts'. The amount of bits of the contained type must match the |
2843 | | /// number of bits of the type contained in the passed in ArrayRef. |
2844 | | /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note |
2845 | | /// that this can return a ConstantAggregateZero object. |
2846 | | Constant *ConstantDataVector::getFP(Type *ElementType, |
2847 | 752 | ArrayRef<uint16_t> Elts) { |
2848 | 752 | assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) && |
2849 | 752 | "Element type is not a 16-bit float type"); |
2850 | 0 | auto *Ty = FixedVectorType::get(ElementType, Elts.size()); |
2851 | 752 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2852 | 752 | return getImpl(StringRef(Data, Elts.size() * 2), Ty); |
2853 | 752 | } |
2854 | | Constant *ConstantDataVector::getFP(Type *ElementType, |
2855 | 10.1k | ArrayRef<uint32_t> Elts) { |
2856 | 10.1k | assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type"); |
2857 | 0 | auto *Ty = FixedVectorType::get(ElementType, Elts.size()); |
2858 | 10.1k | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2859 | 10.1k | return getImpl(StringRef(Data, Elts.size() * 4), Ty); |
2860 | 10.1k | } |
2861 | | Constant *ConstantDataVector::getFP(Type *ElementType, |
2862 | 3.83k | ArrayRef<uint64_t> Elts) { |
2863 | 3.83k | assert(ElementType->isDoubleTy() && |
2864 | 3.83k | "Element type is not a 64-bit float type"); |
2865 | 0 | auto *Ty = FixedVectorType::get(ElementType, Elts.size()); |
2866 | 3.83k | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
2867 | 3.83k | return getImpl(StringRef(Data, Elts.size() * 8), Ty); |
2868 | 3.83k | } |
2869 | | |
2870 | 34.1k | Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { |
2871 | 34.1k | assert(isElementTypeCompatible(V->getType()) && |
2872 | 34.1k | "Element type not compatible with ConstantData"); |
2873 | 34.1k | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
2874 | 32.0k | if (CI->getType()->isIntegerTy(8)) { |
2875 | 5.51k | SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue()); |
2876 | 5.51k | return get(V->getContext(), Elts); |
2877 | 5.51k | } |
2878 | 26.5k | if (CI->getType()->isIntegerTy(16)) { |
2879 | 2.00k | SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue()); |
2880 | 2.00k | return get(V->getContext(), Elts); |
2881 | 2.00k | } |
2882 | 24.5k | if (CI->getType()->isIntegerTy(32)) { |
2883 | 18.2k | SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue()); |
2884 | 18.2k | return get(V->getContext(), Elts); |
2885 | 18.2k | } |
2886 | 6.33k | assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type"); |
2887 | 0 | SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue()); |
2888 | 6.33k | return get(V->getContext(), Elts); |
2889 | 24.5k | } |
2890 | | |
2891 | 2.04k | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
2892 | 2.04k | if (CFP->getType()->isHalfTy()) { |
2893 | 63 | SmallVector<uint16_t, 16> Elts( |
2894 | 63 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
2895 | 63 | return getFP(V->getType(), Elts); |
2896 | 63 | } |
2897 | 1.98k | if (CFP->getType()->isBFloatTy()) { |
2898 | 2 | SmallVector<uint16_t, 16> Elts( |
2899 | 2 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
2900 | 2 | return getFP(V->getType(), Elts); |
2901 | 2 | } |
2902 | 1.97k | if (CFP->getType()->isFloatTy()) { |
2903 | 1.46k | SmallVector<uint32_t, 16> Elts( |
2904 | 1.46k | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
2905 | 1.46k | return getFP(V->getType(), Elts); |
2906 | 1.46k | } |
2907 | 517 | if (CFP->getType()->isDoubleTy()) { |
2908 | 517 | SmallVector<uint64_t, 16> Elts( |
2909 | 517 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
2910 | 517 | return getFP(V->getType(), Elts); |
2911 | 517 | } |
2912 | 517 | } |
2913 | 0 | return ConstantVector::getSplat(ElementCount::getFixed(NumElts), V); |
2914 | 2.04k | } |
2915 | | |
2916 | | |
2917 | 922k | uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { |
2918 | 922k | assert(isa<IntegerType>(getElementType()) && |
2919 | 922k | "Accessor can only be used when element is an integer"); |
2920 | 0 | const char *EltPtr = getElementPointer(Elt); |
2921 | | |
2922 | | // The data is stored in host byte order, make sure to cast back to the right |
2923 | | // type to load with the right endianness. |
2924 | 922k | switch (getElementType()->getIntegerBitWidth()) { |
2925 | 0 | default: llvm_unreachable("Invalid bitwidth for CDS"); |
2926 | 235k | case 8: |
2927 | 235k | return *reinterpret_cast<const uint8_t *>(EltPtr); |
2928 | 96.1k | case 16: |
2929 | 96.1k | return *reinterpret_cast<const uint16_t *>(EltPtr); |
2930 | 528k | case 32: |
2931 | 528k | return *reinterpret_cast<const uint32_t *>(EltPtr); |
2932 | 62.6k | case 64: |
2933 | 62.6k | return *reinterpret_cast<const uint64_t *>(EltPtr); |
2934 | 922k | } |
2935 | 922k | } |
2936 | | |
2937 | 84.5k | APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const { |
2938 | 84.5k | assert(isa<IntegerType>(getElementType()) && |
2939 | 84.5k | "Accessor can only be used when element is an integer"); |
2940 | 0 | const char *EltPtr = getElementPointer(Elt); |
2941 | | |
2942 | | // The data is stored in host byte order, make sure to cast back to the right |
2943 | | // type to load with the right endianness. |
2944 | 84.5k | switch (getElementType()->getIntegerBitWidth()) { |
2945 | 0 | default: llvm_unreachable("Invalid bitwidth for CDS"); |
2946 | 11.5k | case 8: { |
2947 | 11.5k | auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr); |
2948 | 11.5k | return APInt(8, EltVal); |
2949 | 0 | } |
2950 | 13.3k | case 16: { |
2951 | 13.3k | auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr); |
2952 | 13.3k | return APInt(16, EltVal); |
2953 | 0 | } |
2954 | 39.0k | case 32: { |
2955 | 39.0k | auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr); |
2956 | 39.0k | return APInt(32, EltVal); |
2957 | 0 | } |
2958 | 20.5k | case 64: { |
2959 | 20.5k | auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr); |
2960 | 20.5k | return APInt(64, EltVal); |
2961 | 0 | } |
2962 | 84.5k | } |
2963 | 84.5k | } |
2964 | | |
2965 | 46.2k | APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const { |
2966 | 46.2k | const char *EltPtr = getElementPointer(Elt); |
2967 | | |
2968 | 46.2k | switch (getElementType()->getTypeID()) { |
2969 | 0 | default: |
2970 | 0 | llvm_unreachable("Accessor can only be used when element is float/double!"); |
2971 | 613 | case Type::HalfTyID: { |
2972 | 613 | auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr); |
2973 | 613 | return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal)); |
2974 | 0 | } |
2975 | 8 | case Type::BFloatTyID: { |
2976 | 8 | auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr); |
2977 | 8 | return APFloat(APFloat::BFloat(), APInt(16, EltVal)); |
2978 | 0 | } |
2979 | 34.8k | case Type::FloatTyID: { |
2980 | 34.8k | auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr); |
2981 | 34.8k | return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal)); |
2982 | 0 | } |
2983 | 10.7k | case Type::DoubleTyID: { |
2984 | 10.7k | auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr); |
2985 | 10.7k | return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal)); |
2986 | 0 | } |
2987 | 46.2k | } |
2988 | 46.2k | } |
2989 | | |
2990 | 0 | float ConstantDataSequential::getElementAsFloat(unsigned Elt) const { |
2991 | 0 | assert(getElementType()->isFloatTy() && |
2992 | 0 | "Accessor can only be used when element is a 'float'"); |
2993 | 0 | return *reinterpret_cast<const float *>(getElementPointer(Elt)); |
2994 | 0 | } |
2995 | | |
2996 | 0 | double ConstantDataSequential::getElementAsDouble(unsigned Elt) const { |
2997 | 0 | assert(getElementType()->isDoubleTy() && |
2998 | 0 | "Accessor can only be used when element is a 'float'"); |
2999 | 0 | return *reinterpret_cast<const double *>(getElementPointer(Elt)); |
3000 | 0 | } |
3001 | | |
3002 | 653k | Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const { |
3003 | 653k | if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() || |
3004 | 653k | getElementType()->isFloatTy() || getElementType()->isDoubleTy()) |
3005 | 40.3k | return ConstantFP::get(getContext(), getElementAsAPFloat(Elt)); |
3006 | | |
3007 | 612k | return ConstantInt::get(getElementType(), getElementAsInteger(Elt)); |
3008 | 653k | } |
3009 | | |
3010 | 8.77k | bool ConstantDataSequential::isString(unsigned CharSize) const { |
3011 | 8.77k | return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize); |
3012 | 8.77k | } |
3013 | | |
3014 | 0 | bool ConstantDataSequential::isCString() const { |
3015 | 0 | if (!isString()) |
3016 | 0 | return false; |
3017 | | |
3018 | 0 | StringRef Str = getAsString(); |
3019 | | |
3020 | | // The last value must be nul. |
3021 | 0 | if (Str.back() != 0) return false; |
3022 | | |
3023 | | // Other elements must be non-nul. |
3024 | 0 | return !Str.drop_back().contains(0); |
3025 | 0 | } |
3026 | | |
3027 | 25.2k | bool ConstantDataVector::isSplatData() const { |
3028 | 25.2k | const char *Base = getRawDataValues().data(); |
3029 | | |
3030 | | // Compare elements 1+ to the 0'th element. |
3031 | 25.2k | unsigned EltSize = getElementByteSize(); |
3032 | 87.1k | for (unsigned i = 1, e = getNumElements(); i != e; ++i) |
3033 | 71.1k | if (memcmp(Base, Base+i*EltSize, EltSize)) |
3034 | 9.27k | return false; |
3035 | | |
3036 | 16.0k | return true; |
3037 | 25.2k | } |
3038 | | |
3039 | 341k | bool ConstantDataVector::isSplat() const { |
3040 | 341k | if (!IsSplatSet) { |
3041 | 25.2k | IsSplatSet = true; |
3042 | 25.2k | IsSplat = isSplatData(); |
3043 | 25.2k | } |
3044 | 341k | return IsSplat; |
3045 | 341k | } |
3046 | | |
3047 | 341k | Constant *ConstantDataVector::getSplatValue() const { |
3048 | | // If they're all the same, return the 0th one as a representative. |
3049 | 341k | return isSplat() ? getElementAsConstant(0) : nullptr; |
3050 | 341k | } |
3051 | | |
3052 | | //===----------------------------------------------------------------------===// |
3053 | | // handleOperandChange implementations |
3054 | | |
3055 | | /// Update this constant array to change uses of |
3056 | | /// 'From' to be uses of 'To'. This must update the uniquing data structures |
3057 | | /// etc. |
3058 | | /// |
3059 | | /// Note that we intentionally replace all uses of From with To here. Consider |
3060 | | /// a large array that uses 'From' 1000 times. By handling this case all here, |
3061 | | /// ConstantArray::handleOperandChange is only invoked once, and that |
3062 | | /// single invocation handles all 1000 uses. Handling them one at a time would |
3063 | | /// work, but would be really slow because it would have to unique each updated |
3064 | | /// array instance. |
3065 | | /// |
3066 | 13.0k | void Constant::handleOperandChange(Value *From, Value *To) { |
3067 | 13.0k | Value *Replacement = nullptr; |
3068 | 13.0k | switch (getValueID()) { |
3069 | 0 | default: |
3070 | 0 | llvm_unreachable("Not a constant!"); |
3071 | 0 | #define HANDLE_CONSTANT(Name) \ |
3072 | 13.0k | case Value::Name##Val: \ |
3073 | 13.0k | Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \ |
3074 | 13.0k | break; |
3075 | 13.0k | #include "llvm/IR/Value.def" |
3076 | 13.0k | } |
3077 | | |
3078 | | // If handleOperandChangeImpl returned nullptr, then it handled |
3079 | | // replacing itself and we don't want to delete or replace anything else here. |
3080 | 13.0k | if (!Replacement) |
3081 | 11.3k | return; |
3082 | | |
3083 | | // I do need to replace this with an existing value. |
3084 | 1.66k | assert(Replacement != this && "I didn't contain From!"); |
3085 | | |
3086 | | // Everyone using this now uses the replacement. |
3087 | 0 | replaceAllUsesWith(Replacement); |
3088 | | |
3089 | | // Delete the old constant! |
3090 | 1.66k | destroyConstant(); |
3091 | 1.66k | } |
3092 | | |
3093 | 9.56k | Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) { |
3094 | 9.56k | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
3095 | 0 | Constant *ToC = cast<Constant>(To); |
3096 | | |
3097 | 9.56k | SmallVector<Constant*, 8> Values; |
3098 | 9.56k | Values.reserve(getNumOperands()); // Build replacement array. |
3099 | | |
3100 | | // Fill values with the modified operands of the constant array. Also, |
3101 | | // compute whether this turns into an all-zeros array. |
3102 | 9.56k | unsigned NumUpdated = 0; |
3103 | | |
3104 | | // Keep track of whether all the values in the array are "ToC". |
3105 | 9.56k | bool AllSame = true; |
3106 | 9.56k | Use *OperandList = getOperandList(); |
3107 | 9.56k | unsigned OperandNo = 0; |
3108 | 117k | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
3109 | 108k | Constant *Val = cast<Constant>(O->get()); |
3110 | 108k | if (Val == From) { |
3111 | 11.2k | OperandNo = (O - OperandList); |
3112 | 11.2k | Val = ToC; |
3113 | 11.2k | ++NumUpdated; |
3114 | 11.2k | } |
3115 | 108k | Values.push_back(Val); |
3116 | 108k | AllSame &= Val == ToC; |
3117 | 108k | } |
3118 | | |
3119 | 9.56k | if (AllSame && ToC->isNullValue()) |
3120 | 0 | return ConstantAggregateZero::get(getType()); |
3121 | | |
3122 | 9.56k | if (AllSame && isa<UndefValue>(ToC)) |
3123 | 0 | return UndefValue::get(getType()); |
3124 | | |
3125 | | // Check for any other type of constant-folding. |
3126 | 9.56k | if (Constant *C = getImpl(getType(), Values)) |
3127 | 0 | return C; |
3128 | | |
3129 | | // Update to the new value. |
3130 | 9.56k | return getContext().pImpl->ArrayConstants.replaceOperandsInPlace( |
3131 | 9.56k | Values, this, From, ToC, NumUpdated, OperandNo); |
3132 | 9.56k | } |
3133 | | |
3134 | 0 | Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) { |
3135 | 0 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
3136 | 0 | Constant *ToC = cast<Constant>(To); |
3137 | |
|
3138 | 0 | Use *OperandList = getOperandList(); |
3139 | |
|
3140 | 0 | SmallVector<Constant*, 8> Values; |
3141 | 0 | Values.reserve(getNumOperands()); // Build replacement struct. |
3142 | | |
3143 | | // Fill values with the modified operands of the constant struct. Also, |
3144 | | // compute whether this turns into an all-zeros struct. |
3145 | 0 | unsigned NumUpdated = 0; |
3146 | 0 | bool AllSame = true; |
3147 | 0 | unsigned OperandNo = 0; |
3148 | 0 | for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) { |
3149 | 0 | Constant *Val = cast<Constant>(O->get()); |
3150 | 0 | if (Val == From) { |
3151 | 0 | OperandNo = (O - OperandList); |
3152 | 0 | Val = ToC; |
3153 | 0 | ++NumUpdated; |
3154 | 0 | } |
3155 | 0 | Values.push_back(Val); |
3156 | 0 | AllSame &= Val == ToC; |
3157 | 0 | } |
3158 | |
|
3159 | 0 | if (AllSame && ToC->isNullValue()) |
3160 | 0 | return ConstantAggregateZero::get(getType()); |
3161 | | |
3162 | 0 | if (AllSame && isa<UndefValue>(ToC)) |
3163 | 0 | return UndefValue::get(getType()); |
3164 | | |
3165 | | // Update to the new value. |
3166 | 0 | return getContext().pImpl->StructConstants.replaceOperandsInPlace( |
3167 | 0 | Values, this, From, ToC, NumUpdated, OperandNo); |
3168 | 0 | } |
3169 | | |
3170 | 624 | Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) { |
3171 | 624 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
3172 | 0 | Constant *ToC = cast<Constant>(To); |
3173 | | |
3174 | 624 | SmallVector<Constant*, 8> Values; |
3175 | 624 | Values.reserve(getNumOperands()); // Build replacement array... |
3176 | 624 | unsigned NumUpdated = 0; |
3177 | 624 | unsigned OperandNo = 0; |
3178 | 7.73k | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
3179 | 7.11k | Constant *Val = getOperand(i); |
3180 | 7.11k | if (Val == From) { |
3181 | 683 | OperandNo = i; |
3182 | 683 | ++NumUpdated; |
3183 | 683 | Val = ToC; |
3184 | 683 | } |
3185 | 7.11k | Values.push_back(Val); |
3186 | 7.11k | } |
3187 | | |
3188 | 624 | if (Constant *C = getImpl(Values)) |
3189 | 0 | return C; |
3190 | | |
3191 | | // Update to the new value. |
3192 | 624 | return getContext().pImpl->VectorConstants.replaceOperandsInPlace( |
3193 | 624 | Values, this, From, ToC, NumUpdated, OperandNo); |
3194 | 624 | } |
3195 | | |
3196 | 275 | Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) { |
3197 | 275 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
3198 | 0 | Constant *To = cast<Constant>(ToV); |
3199 | | |
3200 | 275 | SmallVector<Constant*, 8> NewOps; |
3201 | 275 | unsigned NumUpdated = 0; |
3202 | 275 | unsigned OperandNo = 0; |
3203 | 823 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
3204 | 548 | Constant *Op = getOperand(i); |
3205 | 548 | if (Op == From) { |
3206 | 275 | OperandNo = i; |
3207 | 275 | ++NumUpdated; |
3208 | 275 | Op = To; |
3209 | 275 | } |
3210 | 548 | NewOps.push_back(Op); |
3211 | 548 | } |
3212 | 275 | assert(NumUpdated && "I didn't contain From!"); |
3213 | | |
3214 | 275 | if (Constant *C = getWithOperands(NewOps, getType(), true)) |
3215 | 0 | return C; |
3216 | | |
3217 | | // Update to the new value. |
3218 | 275 | return getContext().pImpl->ExprConstants.replaceOperandsInPlace( |
3219 | 275 | NewOps, this, From, To, NumUpdated, OperandNo); |
3220 | 275 | } |
3221 | | |
3222 | 5 | Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const { |
3223 | 5 | SmallVector<Value *, 4> ValueOperands(operands()); |
3224 | 5 | ArrayRef<Value*> Ops(ValueOperands); |
3225 | | |
3226 | 5 | switch (getOpcode()) { |
3227 | 0 | case Instruction::Trunc: |
3228 | 0 | case Instruction::ZExt: |
3229 | 0 | case Instruction::SExt: |
3230 | 0 | case Instruction::FPTrunc: |
3231 | 0 | case Instruction::FPExt: |
3232 | 0 | case Instruction::UIToFP: |
3233 | 0 | case Instruction::SIToFP: |
3234 | 0 | case Instruction::FPToUI: |
3235 | 0 | case Instruction::FPToSI: |
3236 | 0 | case Instruction::PtrToInt: |
3237 | 5 | case Instruction::IntToPtr: |
3238 | 5 | case Instruction::BitCast: |
3239 | 5 | case Instruction::AddrSpaceCast: |
3240 | 5 | return CastInst::Create((Instruction::CastOps)getOpcode(), Ops[0], |
3241 | 5 | getType(), "", InsertBefore); |
3242 | 0 | case Instruction::InsertElement: |
3243 | 0 | return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "", InsertBefore); |
3244 | 0 | case Instruction::ExtractElement: |
3245 | 0 | return ExtractElementInst::Create(Ops[0], Ops[1], "", InsertBefore); |
3246 | 0 | case Instruction::ShuffleVector: |
3247 | 0 | return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "", |
3248 | 0 | InsertBefore); |
3249 | | |
3250 | 0 | case Instruction::GetElementPtr: { |
3251 | 0 | const auto *GO = cast<GEPOperator>(this); |
3252 | 0 | if (GO->isInBounds()) |
3253 | 0 | return GetElementPtrInst::CreateInBounds( |
3254 | 0 | GO->getSourceElementType(), Ops[0], Ops.slice(1), "", InsertBefore); |
3255 | 0 | return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0], |
3256 | 0 | Ops.slice(1), "", InsertBefore); |
3257 | 0 | } |
3258 | 0 | case Instruction::ICmp: |
3259 | 0 | case Instruction::FCmp: |
3260 | 0 | return CmpInst::Create((Instruction::OtherOps)getOpcode(), |
3261 | 0 | (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1], |
3262 | 0 | "", InsertBefore); |
3263 | 0 | default: |
3264 | 0 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
3265 | 0 | BinaryOperator *BO = BinaryOperator::Create( |
3266 | 0 | (Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "", InsertBefore); |
3267 | 0 | if (isa<OverflowingBinaryOperator>(BO)) { |
3268 | 0 | BO->setHasNoUnsignedWrap(SubclassOptionalData & |
3269 | 0 | OverflowingBinaryOperator::NoUnsignedWrap); |
3270 | 0 | BO->setHasNoSignedWrap(SubclassOptionalData & |
3271 | 0 | OverflowingBinaryOperator::NoSignedWrap); |
3272 | 0 | } |
3273 | 0 | if (isa<PossiblyExactOperator>(BO)) |
3274 | 0 | BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact); |
3275 | 0 | return BO; |
3276 | 5 | } |
3277 | 5 | } |