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