10b57cec5SDimitry Andric //===-- Constants.cpp - Implement Constant nodes --------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the Constant* classes.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
140b57cec5SDimitry Andric #include "LLVMContextImpl.h"
150b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
160b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
181fd87a68SDimitry Andric #include "llvm/IR/BasicBlock.h"
1981ad6265SDimitry Andric #include "llvm/IR/ConstantFold.h"
200b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
211fd87a68SDimitry Andric #include "llvm/IR/Function.h"
220b57cec5SDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h"
231fd87a68SDimitry Andric #include "llvm/IR/GlobalAlias.h"
241fd87a68SDimitry Andric #include "llvm/IR/GlobalIFunc.h"
250b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
261fd87a68SDimitry Andric #include "llvm/IR/GlobalVariable.h"
270b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
280b57cec5SDimitry Andric #include "llvm/IR/Operator.h"
298bcb0991SDimitry Andric #include "llvm/IR/PatternMatch.h"
300b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
310b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
320b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
330b57cec5SDimitry Andric #include <algorithm>
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric using namespace llvm;
36480093f4SDimitry Andric using namespace PatternMatch;
370b57cec5SDimitry Andric
38*0fca6ea1SDimitry Andric // As set of temporary options to help migrate how splats are represented.
39*0fca6ea1SDimitry Andric static cl::opt<bool> UseConstantIntForFixedLengthSplat(
40*0fca6ea1SDimitry Andric "use-constant-int-for-fixed-length-splat", cl::init(false), cl::Hidden,
41*0fca6ea1SDimitry Andric cl::desc("Use ConstantInt's native fixed-length vector splat support."));
42*0fca6ea1SDimitry Andric static cl::opt<bool> UseConstantFPForFixedLengthSplat(
43*0fca6ea1SDimitry Andric "use-constant-fp-for-fixed-length-splat", cl::init(false), cl::Hidden,
44*0fca6ea1SDimitry Andric cl::desc("Use ConstantFP's native fixed-length vector splat support."));
45*0fca6ea1SDimitry Andric static cl::opt<bool> UseConstantIntForScalableSplat(
46*0fca6ea1SDimitry Andric "use-constant-int-for-scalable-splat", cl::init(false), cl::Hidden,
47*0fca6ea1SDimitry Andric cl::desc("Use ConstantInt's native scalable vector splat support."));
48*0fca6ea1SDimitry Andric static cl::opt<bool> UseConstantFPForScalableSplat(
49*0fca6ea1SDimitry Andric "use-constant-fp-for-scalable-splat", cl::init(false), cl::Hidden,
50*0fca6ea1SDimitry Andric cl::desc("Use ConstantFP's native scalable vector splat support."));
51*0fca6ea1SDimitry Andric
520b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
530b57cec5SDimitry Andric // Constant Class
540b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
550b57cec5SDimitry Andric
isNegativeZeroValue() const560b57cec5SDimitry Andric bool Constant::isNegativeZeroValue() const {
570b57cec5SDimitry Andric // Floating point values have an explicit -0.0 value.
580b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
590b57cec5SDimitry Andric return CFP->isZero() && CFP->isNegative();
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric // Equivalent for a vector of -0.0's.
62fe6060f1SDimitry Andric if (getType()->isVectorTy())
63fe6060f1SDimitry Andric if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
64fe6060f1SDimitry Andric return SplatCFP->isNegativeZeroValue();
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric // We've already handled true FP case; any other FP vectors can't represent -0.0.
670b57cec5SDimitry Andric if (getType()->isFPOrFPVectorTy())
680b57cec5SDimitry Andric return false;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric // Otherwise, just use +0.0.
710b57cec5SDimitry Andric return isNullValue();
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric // Return true iff this constant is positive zero (floating point), negative
750b57cec5SDimitry Andric // zero (floating point), or a null value.
isZeroValue() const760b57cec5SDimitry Andric bool Constant::isZeroValue() const {
770b57cec5SDimitry Andric // Floating point values have an explicit -0.0 value.
780b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
790b57cec5SDimitry Andric return CFP->isZero();
800b57cec5SDimitry Andric
81fe6060f1SDimitry Andric // Check for constant splat vectors of 1 values.
82fe6060f1SDimitry Andric if (getType()->isVectorTy())
83fe6060f1SDimitry Andric if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
84fe6060f1SDimitry Andric return SplatCFP->isZero();
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric // Otherwise, just use +0.0.
870b57cec5SDimitry Andric return isNullValue();
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
isNullValue() const900b57cec5SDimitry Andric bool Constant::isNullValue() const {
910b57cec5SDimitry Andric // 0 is null.
920b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
930b57cec5SDimitry Andric return CI->isZero();
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric // +0.0 is null.
960b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
97fe6060f1SDimitry Andric // ppc_fp128 determine isZero using high order double only
98fe6060f1SDimitry Andric // Should check the bitwise value to make sure all bits are zero.
99fe6060f1SDimitry Andric return CFP->isExactlyValue(+0.0);
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric // constant zero is zero for aggregates, cpnull is null for pointers, none for
1020b57cec5SDimitry Andric // tokens.
1030b57cec5SDimitry Andric return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||
104bdd1243dSDimitry Andric isa<ConstantTokenNone>(this) || isa<ConstantTargetNone>(this);
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric
isAllOnesValue() const1070b57cec5SDimitry Andric bool Constant::isAllOnesValue() const {
1080b57cec5SDimitry Andric // Check for -1 integers
1090b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1100b57cec5SDimitry Andric return CI->isMinusOne();
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric // Check for FP which are bitcasted from -1 integers
1130b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
114349cc55cSDimitry Andric return CFP->getValueAPF().bitcastToAPInt().isAllOnes();
1150b57cec5SDimitry Andric
116fe6060f1SDimitry Andric // Check for constant splat vectors of 1 values.
117fe6060f1SDimitry Andric if (getType()->isVectorTy())
118fe6060f1SDimitry Andric if (const auto *SplatVal = getSplatValue())
119fe6060f1SDimitry Andric return SplatVal->isAllOnesValue();
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric return false;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
isOneValue() const1240b57cec5SDimitry Andric bool Constant::isOneValue() const {
1250b57cec5SDimitry Andric // Check for 1 integers
1260b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1270b57cec5SDimitry Andric return CI->isOne();
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric // Check for FP which are bitcasted from 1 integers
1300b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
131349cc55cSDimitry Andric return CFP->getValueAPF().bitcastToAPInt().isOne();
1320b57cec5SDimitry Andric
133fe6060f1SDimitry Andric // Check for constant splat vectors of 1 values.
134fe6060f1SDimitry Andric if (getType()->isVectorTy())
135fe6060f1SDimitry Andric if (const auto *SplatVal = getSplatValue())
136fe6060f1SDimitry Andric return SplatVal->isOneValue();
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric return false;
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric
isNotOneValue() const141480093f4SDimitry Andric bool Constant::isNotOneValue() const {
142480093f4SDimitry Andric // Check for 1 integers
143480093f4SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
144480093f4SDimitry Andric return !CI->isOneValue();
145480093f4SDimitry Andric
146480093f4SDimitry Andric // Check for FP which are bitcasted from 1 integers
147480093f4SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
148349cc55cSDimitry Andric return !CFP->getValueAPF().bitcastToAPInt().isOne();
149480093f4SDimitry Andric
150480093f4SDimitry Andric // Check that vectors don't contain 1
151fe6060f1SDimitry Andric if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
152fe6060f1SDimitry Andric for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
153fe6060f1SDimitry Andric Constant *Elt = getAggregateElement(I);
154480093f4SDimitry Andric if (!Elt || !Elt->isNotOneValue())
155480093f4SDimitry Andric return false;
156480093f4SDimitry Andric }
157480093f4SDimitry Andric return true;
158480093f4SDimitry Andric }
159480093f4SDimitry Andric
160fe6060f1SDimitry Andric // Check for splats that don't contain 1
161fe6060f1SDimitry Andric if (getType()->isVectorTy())
162fe6060f1SDimitry Andric if (const auto *SplatVal = getSplatValue())
163fe6060f1SDimitry Andric return SplatVal->isNotOneValue();
164fe6060f1SDimitry Andric
165480093f4SDimitry Andric // It *may* contain 1, we can't tell.
166480093f4SDimitry Andric return false;
167480093f4SDimitry Andric }
168480093f4SDimitry Andric
isMinSignedValue() const1690b57cec5SDimitry Andric bool Constant::isMinSignedValue() const {
1700b57cec5SDimitry Andric // Check for INT_MIN integers
1710b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1720b57cec5SDimitry Andric return CI->isMinValue(/*isSigned=*/true);
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andric // Check for FP which are bitcasted from INT_MIN integers
1750b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
1760b57cec5SDimitry Andric return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
1770b57cec5SDimitry Andric
178fe6060f1SDimitry Andric // Check for splats of INT_MIN values.
179fe6060f1SDimitry Andric if (getType()->isVectorTy())
180fe6060f1SDimitry Andric if (const auto *SplatVal = getSplatValue())
181fe6060f1SDimitry Andric return SplatVal->isMinSignedValue();
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric return false;
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric
isNotMinSignedValue() const1860b57cec5SDimitry Andric bool Constant::isNotMinSignedValue() const {
1870b57cec5SDimitry Andric // Check for INT_MIN integers
1880b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1890b57cec5SDimitry Andric return !CI->isMinValue(/*isSigned=*/true);
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric // Check for FP which are bitcasted from INT_MIN integers
1920b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
1930b57cec5SDimitry Andric return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric // Check that vectors don't contain INT_MIN
196fe6060f1SDimitry Andric if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
197fe6060f1SDimitry Andric for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
198fe6060f1SDimitry Andric Constant *Elt = getAggregateElement(I);
1990b57cec5SDimitry Andric if (!Elt || !Elt->isNotMinSignedValue())
2000b57cec5SDimitry Andric return false;
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric return true;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric
205fe6060f1SDimitry Andric // Check for splats that aren't INT_MIN
206fe6060f1SDimitry Andric if (getType()->isVectorTy())
207fe6060f1SDimitry Andric if (const auto *SplatVal = getSplatValue())
208fe6060f1SDimitry Andric return SplatVal->isNotMinSignedValue();
209fe6060f1SDimitry Andric
2100b57cec5SDimitry Andric // It *may* contain INT_MIN, we can't tell.
2110b57cec5SDimitry Andric return false;
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric
isFiniteNonZeroFP() const2140b57cec5SDimitry Andric bool Constant::isFiniteNonZeroFP() const {
2150b57cec5SDimitry Andric if (auto *CFP = dyn_cast<ConstantFP>(this))
2160b57cec5SDimitry Andric return CFP->getValueAPF().isFiniteNonZero();
217fe6060f1SDimitry Andric
218fe6060f1SDimitry Andric if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
219fe6060f1SDimitry Andric for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
220fe6060f1SDimitry Andric auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));
2210b57cec5SDimitry Andric if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
2220b57cec5SDimitry Andric return false;
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric return true;
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric
227fe6060f1SDimitry Andric if (getType()->isVectorTy())
228fe6060f1SDimitry Andric if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
229fe6060f1SDimitry Andric return SplatCFP->isFiniteNonZeroFP();
230fe6060f1SDimitry Andric
231fe6060f1SDimitry Andric // It *may* contain finite non-zero, we can't tell.
232fe6060f1SDimitry Andric return false;
233fe6060f1SDimitry Andric }
234fe6060f1SDimitry Andric
isNormalFP() const2350b57cec5SDimitry Andric bool Constant::isNormalFP() const {
2360b57cec5SDimitry Andric if (auto *CFP = dyn_cast<ConstantFP>(this))
2370b57cec5SDimitry Andric return CFP->getValueAPF().isNormal();
238fe6060f1SDimitry Andric
239fe6060f1SDimitry Andric if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
240fe6060f1SDimitry Andric for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
241fe6060f1SDimitry Andric auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));
2420b57cec5SDimitry Andric if (!CFP || !CFP->getValueAPF().isNormal())
2430b57cec5SDimitry Andric return false;
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric return true;
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric
248fe6060f1SDimitry Andric if (getType()->isVectorTy())
249fe6060f1SDimitry Andric if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
250fe6060f1SDimitry Andric return SplatCFP->isNormalFP();
251fe6060f1SDimitry Andric
252fe6060f1SDimitry Andric // It *may* contain a normal fp value, we can't tell.
253fe6060f1SDimitry Andric return false;
254fe6060f1SDimitry Andric }
255fe6060f1SDimitry Andric
hasExactInverseFP() const2560b57cec5SDimitry Andric bool Constant::hasExactInverseFP() const {
2570b57cec5SDimitry Andric if (auto *CFP = dyn_cast<ConstantFP>(this))
2580b57cec5SDimitry Andric return CFP->getValueAPF().getExactInverse(nullptr);
259fe6060f1SDimitry Andric
260fe6060f1SDimitry Andric if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
261fe6060f1SDimitry Andric for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
262fe6060f1SDimitry Andric auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));
2630b57cec5SDimitry Andric if (!CFP || !CFP->getValueAPF().getExactInverse(nullptr))
2640b57cec5SDimitry Andric return false;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric return true;
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric
269fe6060f1SDimitry Andric if (getType()->isVectorTy())
270fe6060f1SDimitry Andric if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
271fe6060f1SDimitry Andric return SplatCFP->hasExactInverseFP();
272fe6060f1SDimitry Andric
273fe6060f1SDimitry Andric // It *may* have an exact inverse fp value, we can't tell.
274fe6060f1SDimitry Andric return false;
275fe6060f1SDimitry Andric }
276fe6060f1SDimitry Andric
isNaN() const2770b57cec5SDimitry Andric bool Constant::isNaN() const {
2780b57cec5SDimitry Andric if (auto *CFP = dyn_cast<ConstantFP>(this))
2790b57cec5SDimitry Andric return CFP->isNaN();
280fe6060f1SDimitry Andric
281fe6060f1SDimitry Andric if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
282fe6060f1SDimitry Andric for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
283fe6060f1SDimitry Andric auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));
2840b57cec5SDimitry Andric if (!CFP || !CFP->isNaN())
2850b57cec5SDimitry Andric return false;
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric return true;
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric
290fe6060f1SDimitry Andric if (getType()->isVectorTy())
291fe6060f1SDimitry Andric if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
292fe6060f1SDimitry Andric return SplatCFP->isNaN();
293fe6060f1SDimitry Andric
294fe6060f1SDimitry Andric // It *may* be NaN, we can't tell.
295fe6060f1SDimitry Andric return false;
296fe6060f1SDimitry Andric }
297fe6060f1SDimitry Andric
isElementWiseEqual(Value * Y) const2988bcb0991SDimitry Andric bool Constant::isElementWiseEqual(Value *Y) const {
2998bcb0991SDimitry Andric // Are they fully identical?
3008bcb0991SDimitry Andric if (this == Y)
3018bcb0991SDimitry Andric return true;
302480093f4SDimitry Andric
303480093f4SDimitry Andric // The input value must be a vector constant with the same type.
3045ffd83dbSDimitry Andric auto *VTy = dyn_cast<VectorType>(getType());
3055ffd83dbSDimitry Andric if (!isa<Constant>(Y) || !VTy || VTy != Y->getType())
3065ffd83dbSDimitry Andric return false;
3075ffd83dbSDimitry Andric
3085ffd83dbSDimitry Andric // TODO: Compare pointer constants?
3095ffd83dbSDimitry Andric if (!(VTy->getElementType()->isIntegerTy() ||
3105ffd83dbSDimitry Andric VTy->getElementType()->isFloatingPointTy()))
3118bcb0991SDimitry Andric return false;
312480093f4SDimitry Andric
313480093f4SDimitry Andric // They may still be identical element-wise (if they have `undef`s).
3145ffd83dbSDimitry Andric // Bitcast to integer to allow exact bitwise comparison for all types.
3155ffd83dbSDimitry Andric Type *IntTy = VectorType::getInteger(VTy);
3165ffd83dbSDimitry Andric Constant *C0 = ConstantExpr::getBitCast(const_cast<Constant *>(this), IntTy);
3175ffd83dbSDimitry Andric Constant *C1 = ConstantExpr::getBitCast(cast<Constant>(Y), IntTy);
318*0fca6ea1SDimitry Andric Constant *CmpEq = ConstantFoldCompareInstruction(ICmpInst::ICMP_EQ, C0, C1);
319*0fca6ea1SDimitry Andric return CmpEq && (isa<PoisonValue>(CmpEq) || match(CmpEq, m_One()));
3208bcb0991SDimitry Andric }
3218bcb0991SDimitry Andric
322e8d8bef9SDimitry Andric static bool
containsUndefinedElement(const Constant * C,function_ref<bool (const Constant *)> HasFn)323e8d8bef9SDimitry Andric containsUndefinedElement(const Constant *C,
324e8d8bef9SDimitry Andric function_ref<bool(const Constant *)> HasFn) {
325e8d8bef9SDimitry Andric if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
326e8d8bef9SDimitry Andric if (HasFn(C))
327e8d8bef9SDimitry Andric return true;
328e8d8bef9SDimitry Andric if (isa<ConstantAggregateZero>(C))
329e8d8bef9SDimitry Andric return false;
330e8d8bef9SDimitry Andric if (isa<ScalableVectorType>(C->getType()))
331e8d8bef9SDimitry Andric return false;
332e8d8bef9SDimitry Andric
333e8d8bef9SDimitry Andric for (unsigned i = 0, e = cast<FixedVectorType>(VTy)->getNumElements();
3348c6f6c0cSDimitry Andric i != e; ++i) {
3358c6f6c0cSDimitry Andric if (Constant *Elem = C->getAggregateElement(i))
3368c6f6c0cSDimitry Andric if (HasFn(Elem))
3370b57cec5SDimitry Andric return true;
3385ffd83dbSDimitry Andric }
3398c6f6c0cSDimitry Andric }
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andric return false;
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric
containsUndefOrPoisonElement() const344e8d8bef9SDimitry Andric bool Constant::containsUndefOrPoisonElement() const {
345e8d8bef9SDimitry Andric return containsUndefinedElement(
346e8d8bef9SDimitry Andric this, [&](const auto *C) { return isa<UndefValue>(C); });
347e8d8bef9SDimitry Andric }
348e8d8bef9SDimitry Andric
containsPoisonElement() const349e8d8bef9SDimitry Andric bool Constant::containsPoisonElement() const {
350e8d8bef9SDimitry Andric return containsUndefinedElement(
351e8d8bef9SDimitry Andric this, [&](const auto *C) { return isa<PoisonValue>(C); });
352e8d8bef9SDimitry Andric }
353e8d8bef9SDimitry Andric
containsUndefElement() const354bdd1243dSDimitry Andric bool Constant::containsUndefElement() const {
355bdd1243dSDimitry Andric return containsUndefinedElement(this, [&](const auto *C) {
356bdd1243dSDimitry Andric return isa<UndefValue>(C) && !isa<PoisonValue>(C);
357bdd1243dSDimitry Andric });
358bdd1243dSDimitry Andric }
359bdd1243dSDimitry Andric
containsConstantExpression() const3600b57cec5SDimitry Andric bool Constant::containsConstantExpression() const {
361e8d8bef9SDimitry Andric if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
3625ffd83dbSDimitry Andric for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
3630b57cec5SDimitry Andric if (isa<ConstantExpr>(getAggregateElement(i)))
3640b57cec5SDimitry Andric return true;
3655ffd83dbSDimitry Andric }
3660b57cec5SDimitry Andric return false;
3670b57cec5SDimitry Andric }
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric /// Constructor to create a '0' constant of arbitrary type.
getNullValue(Type * Ty)3700b57cec5SDimitry Andric Constant *Constant::getNullValue(Type *Ty) {
3710b57cec5SDimitry Andric switch (Ty->getTypeID()) {
3720b57cec5SDimitry Andric case Type::IntegerTyID:
3730b57cec5SDimitry Andric return ConstantInt::get(Ty, 0);
3740b57cec5SDimitry Andric case Type::HalfTyID:
3755ffd83dbSDimitry Andric case Type::BFloatTyID:
3760b57cec5SDimitry Andric case Type::FloatTyID:
3770b57cec5SDimitry Andric case Type::DoubleTyID:
3780b57cec5SDimitry Andric case Type::X86_FP80TyID:
3790b57cec5SDimitry Andric case Type::FP128TyID:
3800b57cec5SDimitry Andric case Type::PPC_FP128TyID:
38181ad6265SDimitry Andric return ConstantFP::get(Ty->getContext(),
38281ad6265SDimitry Andric APFloat::getZero(Ty->getFltSemantics()));
3830b57cec5SDimitry Andric case Type::PointerTyID:
3840b57cec5SDimitry Andric return ConstantPointerNull::get(cast<PointerType>(Ty));
3850b57cec5SDimitry Andric case Type::StructTyID:
3860b57cec5SDimitry Andric case Type::ArrayTyID:
3875ffd83dbSDimitry Andric case Type::FixedVectorTyID:
3885ffd83dbSDimitry Andric case Type::ScalableVectorTyID:
3890b57cec5SDimitry Andric return ConstantAggregateZero::get(Ty);
3900b57cec5SDimitry Andric case Type::TokenTyID:
3910b57cec5SDimitry Andric return ConstantTokenNone::get(Ty->getContext());
392bdd1243dSDimitry Andric case Type::TargetExtTyID:
393bdd1243dSDimitry Andric return ConstantTargetNone::get(cast<TargetExtType>(Ty));
3940b57cec5SDimitry Andric default:
3950b57cec5SDimitry Andric // Function, Label, or Opaque type?
3960b57cec5SDimitry Andric llvm_unreachable("Cannot create a null constant of that type!");
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric
getIntegerValue(Type * Ty,const APInt & V)4000b57cec5SDimitry Andric Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
4010b57cec5SDimitry Andric Type *ScalarTy = Ty->getScalarType();
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric // Create the base integer constant.
4040b57cec5SDimitry Andric Constant *C = ConstantInt::get(Ty->getContext(), V);
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andric // Convert an integer to a pointer, if necessary.
4070b57cec5SDimitry Andric if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
4080b57cec5SDimitry Andric C = ConstantExpr::getIntToPtr(C, PTy);
4090b57cec5SDimitry Andric
4100b57cec5SDimitry Andric // Broadcast a scalar to a vector, if necessary.
4110b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
4125ffd83dbSDimitry Andric C = ConstantVector::getSplat(VTy->getElementCount(), C);
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andric return C;
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
getAllOnesValue(Type * Ty)4170b57cec5SDimitry Andric Constant *Constant::getAllOnesValue(Type *Ty) {
4180b57cec5SDimitry Andric if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
4190b57cec5SDimitry Andric return ConstantInt::get(Ty->getContext(),
420349cc55cSDimitry Andric APInt::getAllOnes(ITy->getBitWidth()));
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric if (Ty->isFloatingPointTy()) {
423349cc55cSDimitry Andric APFloat FL = APFloat::getAllOnesValue(Ty->getFltSemantics());
4240b57cec5SDimitry Andric return ConstantFP::get(Ty->getContext(), FL);
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric VectorType *VTy = cast<VectorType>(Ty);
4285ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(),
4290b57cec5SDimitry Andric getAllOnesValue(VTy->getElementType()));
4300b57cec5SDimitry Andric }
4310b57cec5SDimitry Andric
getAggregateElement(unsigned Elt) const4320b57cec5SDimitry Andric Constant *Constant::getAggregateElement(unsigned Elt) const {
433fe6060f1SDimitry Andric assert((getType()->isAggregateType() || getType()->isVectorTy()) &&
434fe6060f1SDimitry Andric "Must be an aggregate/vector constant");
435fe6060f1SDimitry Andric
436e8d8bef9SDimitry Andric if (const auto *CC = dyn_cast<ConstantAggregate>(this))
4370b57cec5SDimitry Andric return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
4380b57cec5SDimitry Andric
439fe6060f1SDimitry Andric if (const auto *CAZ = dyn_cast<ConstantAggregateZero>(this))
440fe6060f1SDimitry Andric return Elt < CAZ->getElementCount().getKnownMinValue()
441fe6060f1SDimitry Andric ? CAZ->getElementValue(Elt)
442fe6060f1SDimitry Andric : nullptr;
443fe6060f1SDimitry Andric
444e8d8bef9SDimitry Andric // FIXME: getNumElements() will fail for non-fixed vector types.
445e8d8bef9SDimitry Andric if (isa<ScalableVectorType>(getType()))
446e8d8bef9SDimitry Andric return nullptr;
447e8d8bef9SDimitry Andric
448e8d8bef9SDimitry Andric if (const auto *PV = dyn_cast<PoisonValue>(this))
449e8d8bef9SDimitry Andric return Elt < PV->getNumElements() ? PV->getElementValue(Elt) : nullptr;
450e8d8bef9SDimitry Andric
451e8d8bef9SDimitry Andric if (const auto *UV = dyn_cast<UndefValue>(this))
4520b57cec5SDimitry Andric return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
4530b57cec5SDimitry Andric
454e8d8bef9SDimitry Andric if (const auto *CDS = dyn_cast<ConstantDataSequential>(this))
4550b57cec5SDimitry Andric return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
4560b57cec5SDimitry Andric : nullptr;
457fe6060f1SDimitry Andric
4580b57cec5SDimitry Andric return nullptr;
4590b57cec5SDimitry Andric }
4600b57cec5SDimitry Andric
getAggregateElement(Constant * Elt) const4610b57cec5SDimitry Andric Constant *Constant::getAggregateElement(Constant *Elt) const {
4620b57cec5SDimitry Andric assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
4630b57cec5SDimitry Andric if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) {
4640b57cec5SDimitry Andric // Check if the constant fits into an uint64_t.
4650b57cec5SDimitry Andric if (CI->getValue().getActiveBits() > 64)
4660b57cec5SDimitry Andric return nullptr;
4670b57cec5SDimitry Andric return getAggregateElement(CI->getZExtValue());
4680b57cec5SDimitry Andric }
4690b57cec5SDimitry Andric return nullptr;
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric
destroyConstant()4720b57cec5SDimitry Andric void Constant::destroyConstant() {
4730b57cec5SDimitry Andric /// First call destroyConstantImpl on the subclass. This gives the subclass
4740b57cec5SDimitry Andric /// a chance to remove the constant from any maps/pools it's contained in.
4750b57cec5SDimitry Andric switch (getValueID()) {
4760b57cec5SDimitry Andric default:
4770b57cec5SDimitry Andric llvm_unreachable("Not a constant!");
4780b57cec5SDimitry Andric #define HANDLE_CONSTANT(Name) \
4790b57cec5SDimitry Andric case Value::Name##Val: \
4800b57cec5SDimitry Andric cast<Name>(this)->destroyConstantImpl(); \
4810b57cec5SDimitry Andric break;
4820b57cec5SDimitry Andric #include "llvm/IR/Value.def"
4830b57cec5SDimitry Andric }
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andric // When a Constant is destroyed, there may be lingering
4860b57cec5SDimitry Andric // references to the constant by other constants in the constant pool. These
4870b57cec5SDimitry Andric // constants are implicitly dependent on the module that is being deleted,
4880b57cec5SDimitry Andric // but they don't know that. Because we only find out when the CPV is
4890b57cec5SDimitry Andric // deleted, we must now notify all of our users (that should only be
4900b57cec5SDimitry Andric // Constants) that they are, in fact, invalid now and should be deleted.
4910b57cec5SDimitry Andric //
4920b57cec5SDimitry Andric while (!use_empty()) {
4930b57cec5SDimitry Andric Value *V = user_back();
4940b57cec5SDimitry Andric #ifndef NDEBUG // Only in -g mode...
4950b57cec5SDimitry Andric if (!isa<Constant>(V)) {
4960b57cec5SDimitry Andric dbgs() << "While deleting: " << *this
4970b57cec5SDimitry Andric << "\n\nUse still stuck around after Def is destroyed: " << *V
4980b57cec5SDimitry Andric << "\n\n";
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric #endif
5010b57cec5SDimitry Andric assert(isa<Constant>(V) && "References remain to Constant being destroyed");
5020b57cec5SDimitry Andric cast<Constant>(V)->destroyConstant();
5030b57cec5SDimitry Andric
5040b57cec5SDimitry Andric // The constant should remove itself from our use list...
5050b57cec5SDimitry Andric assert((use_empty() || user_back() != V) && "Constant not removed!");
5060b57cec5SDimitry Andric }
5070b57cec5SDimitry Andric
5080b57cec5SDimitry Andric // Value has no outstanding references it is safe to delete it now...
5095ffd83dbSDimitry Andric deleteConstant(this);
5105ffd83dbSDimitry Andric }
5115ffd83dbSDimitry Andric
deleteConstant(Constant * C)5125ffd83dbSDimitry Andric void llvm::deleteConstant(Constant *C) {
5135ffd83dbSDimitry Andric switch (C->getValueID()) {
5145ffd83dbSDimitry Andric case Constant::ConstantIntVal:
5155ffd83dbSDimitry Andric delete static_cast<ConstantInt *>(C);
5165ffd83dbSDimitry Andric break;
5175ffd83dbSDimitry Andric case Constant::ConstantFPVal:
5185ffd83dbSDimitry Andric delete static_cast<ConstantFP *>(C);
5195ffd83dbSDimitry Andric break;
5205ffd83dbSDimitry Andric case Constant::ConstantAggregateZeroVal:
5215ffd83dbSDimitry Andric delete static_cast<ConstantAggregateZero *>(C);
5225ffd83dbSDimitry Andric break;
5235ffd83dbSDimitry Andric case Constant::ConstantArrayVal:
5245ffd83dbSDimitry Andric delete static_cast<ConstantArray *>(C);
5255ffd83dbSDimitry Andric break;
5265ffd83dbSDimitry Andric case Constant::ConstantStructVal:
5275ffd83dbSDimitry Andric delete static_cast<ConstantStruct *>(C);
5285ffd83dbSDimitry Andric break;
5295ffd83dbSDimitry Andric case Constant::ConstantVectorVal:
5305ffd83dbSDimitry Andric delete static_cast<ConstantVector *>(C);
5315ffd83dbSDimitry Andric break;
5325ffd83dbSDimitry Andric case Constant::ConstantPointerNullVal:
5335ffd83dbSDimitry Andric delete static_cast<ConstantPointerNull *>(C);
5345ffd83dbSDimitry Andric break;
5355ffd83dbSDimitry Andric case Constant::ConstantDataArrayVal:
5365ffd83dbSDimitry Andric delete static_cast<ConstantDataArray *>(C);
5375ffd83dbSDimitry Andric break;
5385ffd83dbSDimitry Andric case Constant::ConstantDataVectorVal:
5395ffd83dbSDimitry Andric delete static_cast<ConstantDataVector *>(C);
5405ffd83dbSDimitry Andric break;
5415ffd83dbSDimitry Andric case Constant::ConstantTokenNoneVal:
5425ffd83dbSDimitry Andric delete static_cast<ConstantTokenNone *>(C);
5435ffd83dbSDimitry Andric break;
5445ffd83dbSDimitry Andric case Constant::BlockAddressVal:
5455ffd83dbSDimitry Andric delete static_cast<BlockAddress *>(C);
5465ffd83dbSDimitry Andric break;
547e8d8bef9SDimitry Andric case Constant::DSOLocalEquivalentVal:
548e8d8bef9SDimitry Andric delete static_cast<DSOLocalEquivalent *>(C);
549e8d8bef9SDimitry Andric break;
5500eae32dcSDimitry Andric case Constant::NoCFIValueVal:
5510eae32dcSDimitry Andric delete static_cast<NoCFIValue *>(C);
5520eae32dcSDimitry Andric break;
553*0fca6ea1SDimitry Andric case Constant::ConstantPtrAuthVal:
554*0fca6ea1SDimitry Andric delete static_cast<ConstantPtrAuth *>(C);
555*0fca6ea1SDimitry Andric break;
5565ffd83dbSDimitry Andric case Constant::UndefValueVal:
5575ffd83dbSDimitry Andric delete static_cast<UndefValue *>(C);
5585ffd83dbSDimitry Andric break;
559e8d8bef9SDimitry Andric case Constant::PoisonValueVal:
560e8d8bef9SDimitry Andric delete static_cast<PoisonValue *>(C);
561e8d8bef9SDimitry Andric break;
5625ffd83dbSDimitry Andric case Constant::ConstantExprVal:
563bdd1243dSDimitry Andric if (isa<CastConstantExpr>(C))
564bdd1243dSDimitry Andric delete static_cast<CastConstantExpr *>(C);
5655ffd83dbSDimitry Andric else if (isa<BinaryConstantExpr>(C))
5665ffd83dbSDimitry Andric delete static_cast<BinaryConstantExpr *>(C);
5675ffd83dbSDimitry Andric else if (isa<ExtractElementConstantExpr>(C))
5685ffd83dbSDimitry Andric delete static_cast<ExtractElementConstantExpr *>(C);
5695ffd83dbSDimitry Andric else if (isa<InsertElementConstantExpr>(C))
5705ffd83dbSDimitry Andric delete static_cast<InsertElementConstantExpr *>(C);
5715ffd83dbSDimitry Andric else if (isa<ShuffleVectorConstantExpr>(C))
5725ffd83dbSDimitry Andric delete static_cast<ShuffleVectorConstantExpr *>(C);
5735ffd83dbSDimitry Andric else if (isa<GetElementPtrConstantExpr>(C))
5745ffd83dbSDimitry Andric delete static_cast<GetElementPtrConstantExpr *>(C);
5755ffd83dbSDimitry Andric else
5765ffd83dbSDimitry Andric llvm_unreachable("Unexpected constant expr");
5775ffd83dbSDimitry Andric break;
5785ffd83dbSDimitry Andric default:
5795ffd83dbSDimitry Andric llvm_unreachable("Unexpected constant");
5805ffd83dbSDimitry Andric }
5810b57cec5SDimitry Andric }
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric /// Check if C contains a GlobalValue for which Predicate is true.
5840b57cec5SDimitry Andric static bool
ConstHasGlobalValuePredicate(const Constant * C,bool (* Predicate)(const GlobalValue *))5850b57cec5SDimitry Andric ConstHasGlobalValuePredicate(const Constant *C,
5860b57cec5SDimitry Andric bool (*Predicate)(const GlobalValue *)) {
5870b57cec5SDimitry Andric SmallPtrSet<const Constant *, 8> Visited;
5880b57cec5SDimitry Andric SmallVector<const Constant *, 8> WorkList;
5890b57cec5SDimitry Andric WorkList.push_back(C);
5900b57cec5SDimitry Andric Visited.insert(C);
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andric while (!WorkList.empty()) {
5930b57cec5SDimitry Andric const Constant *WorkItem = WorkList.pop_back_val();
5940b57cec5SDimitry Andric if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
5950b57cec5SDimitry Andric if (Predicate(GV))
5960b57cec5SDimitry Andric return true;
5970b57cec5SDimitry Andric for (const Value *Op : WorkItem->operands()) {
5980b57cec5SDimitry Andric const Constant *ConstOp = dyn_cast<Constant>(Op);
5990b57cec5SDimitry Andric if (!ConstOp)
6000b57cec5SDimitry Andric continue;
6010b57cec5SDimitry Andric if (Visited.insert(ConstOp).second)
6020b57cec5SDimitry Andric WorkList.push_back(ConstOp);
6030b57cec5SDimitry Andric }
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric return false;
6060b57cec5SDimitry Andric }
6070b57cec5SDimitry Andric
isThreadDependent() const6080b57cec5SDimitry Andric bool Constant::isThreadDependent() const {
6090b57cec5SDimitry Andric auto DLLImportPredicate = [](const GlobalValue *GV) {
6100b57cec5SDimitry Andric return GV->isThreadLocal();
6110b57cec5SDimitry Andric };
6120b57cec5SDimitry Andric return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
6130b57cec5SDimitry Andric }
6140b57cec5SDimitry Andric
isDLLImportDependent() const6150b57cec5SDimitry Andric bool Constant::isDLLImportDependent() const {
6160b57cec5SDimitry Andric auto DLLImportPredicate = [](const GlobalValue *GV) {
6170b57cec5SDimitry Andric return GV->hasDLLImportStorageClass();
6180b57cec5SDimitry Andric };
6190b57cec5SDimitry Andric return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
6200b57cec5SDimitry Andric }
6210b57cec5SDimitry Andric
isConstantUsed() const6220b57cec5SDimitry Andric bool Constant::isConstantUsed() const {
6230b57cec5SDimitry Andric for (const User *U : users()) {
6240b57cec5SDimitry Andric const Constant *UC = dyn_cast<Constant>(U);
6250b57cec5SDimitry Andric if (!UC || isa<GlobalValue>(UC))
6260b57cec5SDimitry Andric return true;
6270b57cec5SDimitry Andric
6280b57cec5SDimitry Andric if (UC->isConstantUsed())
6290b57cec5SDimitry Andric return true;
6300b57cec5SDimitry Andric }
6310b57cec5SDimitry Andric return false;
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric
needsDynamicRelocation() const634fe6060f1SDimitry Andric bool Constant::needsDynamicRelocation() const {
635fe6060f1SDimitry Andric return getRelocationInfo() == GlobalRelocation;
636fe6060f1SDimitry Andric }
637fe6060f1SDimitry Andric
needsRelocation() const6380b57cec5SDimitry Andric bool Constant::needsRelocation() const {
639fe6060f1SDimitry Andric return getRelocationInfo() != NoRelocation;
640fe6060f1SDimitry Andric }
641fe6060f1SDimitry Andric
getRelocationInfo() const642fe6060f1SDimitry Andric Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
6430b57cec5SDimitry Andric if (isa<GlobalValue>(this))
644fe6060f1SDimitry Andric return GlobalRelocation; // Global reference.
6450b57cec5SDimitry Andric
6460b57cec5SDimitry Andric if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
647fe6060f1SDimitry Andric return BA->getFunction()->getRelocationInfo();
6480b57cec5SDimitry Andric
6498bcb0991SDimitry Andric if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
6500b57cec5SDimitry Andric if (CE->getOpcode() == Instruction::Sub) {
6510b57cec5SDimitry Andric ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
6520b57cec5SDimitry Andric ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
6530b57cec5SDimitry Andric if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
6548bcb0991SDimitry Andric RHS->getOpcode() == Instruction::PtrToInt) {
6558bcb0991SDimitry Andric Constant *LHSOp0 = LHS->getOperand(0);
6568bcb0991SDimitry Andric Constant *RHSOp0 = RHS->getOperand(0);
6578bcb0991SDimitry Andric
6588bcb0991SDimitry Andric // While raw uses of blockaddress need to be relocated, differences
6598bcb0991SDimitry Andric // between two of them don't when they are for labels in the same
6608bcb0991SDimitry Andric // function. This is a common idiom when creating a table for the
6618bcb0991SDimitry Andric // indirect goto extension, so we handle it efficiently here.
6628bcb0991SDimitry Andric if (isa<BlockAddress>(LHSOp0) && isa<BlockAddress>(RHSOp0) &&
6638bcb0991SDimitry Andric cast<BlockAddress>(LHSOp0)->getFunction() ==
6648bcb0991SDimitry Andric cast<BlockAddress>(RHSOp0)->getFunction())
665fe6060f1SDimitry Andric return NoRelocation;
6668bcb0991SDimitry Andric
6678bcb0991SDimitry Andric // Relative pointers do not need to be dynamically relocated.
668e8d8bef9SDimitry Andric if (auto *RHSGV =
669e8d8bef9SDimitry Andric dyn_cast<GlobalValue>(RHSOp0->stripInBoundsConstantOffsets())) {
670e8d8bef9SDimitry Andric auto *LHS = LHSOp0->stripInBoundsConstantOffsets();
671e8d8bef9SDimitry Andric if (auto *LHSGV = dyn_cast<GlobalValue>(LHS)) {
6728bcb0991SDimitry Andric if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal())
673fe6060f1SDimitry Andric return LocalRelocation;
674e8d8bef9SDimitry Andric } else if (isa<DSOLocalEquivalent>(LHS)) {
675e8d8bef9SDimitry Andric if (RHSGV->isDSOLocal())
676fe6060f1SDimitry Andric return LocalRelocation;
677e8d8bef9SDimitry Andric }
678e8d8bef9SDimitry Andric }
6798bcb0991SDimitry Andric }
6808bcb0991SDimitry Andric }
6810b57cec5SDimitry Andric }
6820b57cec5SDimitry Andric
683fe6060f1SDimitry Andric PossibleRelocationsTy Result = NoRelocation;
6840b57cec5SDimitry Andric for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
685fe6060f1SDimitry Andric Result =
686fe6060f1SDimitry Andric std::max(cast<Constant>(getOperand(i))->getRelocationInfo(), Result);
6870b57cec5SDimitry Andric
6880b57cec5SDimitry Andric return Result;
6890b57cec5SDimitry Andric }
6900b57cec5SDimitry Andric
691349cc55cSDimitry Andric /// Return true if the specified constantexpr is dead. This involves
692349cc55cSDimitry Andric /// recursively traversing users of the constantexpr.
693349cc55cSDimitry Andric /// If RemoveDeadUsers is true, also remove dead users at the same time.
constantIsDead(const Constant * C,bool RemoveDeadUsers)694349cc55cSDimitry Andric static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) {
6950b57cec5SDimitry Andric if (isa<GlobalValue>(C)) return false; // Cannot remove this
6960b57cec5SDimitry Andric
697349cc55cSDimitry Andric Value::const_user_iterator I = C->user_begin(), E = C->user_end();
698349cc55cSDimitry Andric while (I != E) {
699349cc55cSDimitry Andric const Constant *User = dyn_cast<Constant>(*I);
7000b57cec5SDimitry Andric if (!User) return false; // Non-constant usage;
701349cc55cSDimitry Andric if (!constantIsDead(User, RemoveDeadUsers))
7020b57cec5SDimitry Andric return false; // Constant wasn't dead
703349cc55cSDimitry Andric
704349cc55cSDimitry Andric // Just removed User, so the iterator was invalidated.
705349cc55cSDimitry Andric // Since we return immediately upon finding a live user, we can always
706349cc55cSDimitry Andric // restart from user_begin().
707349cc55cSDimitry Andric if (RemoveDeadUsers)
708349cc55cSDimitry Andric I = C->user_begin();
709349cc55cSDimitry Andric else
710349cc55cSDimitry Andric ++I;
7110b57cec5SDimitry Andric }
7120b57cec5SDimitry Andric
71381ad6265SDimitry Andric if (RemoveDeadUsers) {
71481ad6265SDimitry Andric // If C is only used by metadata, it should not be preserved but should
71581ad6265SDimitry Andric // have its uses replaced.
71681ad6265SDimitry Andric ReplaceableMetadataImpl::SalvageDebugInfo(*C);
7170b57cec5SDimitry Andric const_cast<Constant *>(C)->destroyConstant();
71881ad6265SDimitry Andric }
7190b57cec5SDimitry Andric
720349cc55cSDimitry Andric return true;
721349cc55cSDimitry Andric }
7220b57cec5SDimitry Andric
removeDeadConstantUsers() const7230b57cec5SDimitry Andric void Constant::removeDeadConstantUsers() const {
7240b57cec5SDimitry Andric Value::const_user_iterator I = user_begin(), E = user_end();
7250b57cec5SDimitry Andric Value::const_user_iterator LastNonDeadUser = E;
7260b57cec5SDimitry Andric while (I != E) {
7270b57cec5SDimitry Andric const Constant *User = dyn_cast<Constant>(*I);
7280b57cec5SDimitry Andric if (!User) {
7290b57cec5SDimitry Andric LastNonDeadUser = I;
7300b57cec5SDimitry Andric ++I;
7310b57cec5SDimitry Andric continue;
7320b57cec5SDimitry Andric }
7330b57cec5SDimitry Andric
734349cc55cSDimitry Andric if (!constantIsDead(User, /* RemoveDeadUsers= */ true)) {
7350b57cec5SDimitry Andric // If the constant wasn't dead, remember that this was the last live use
7360b57cec5SDimitry Andric // and move on to the next constant.
7370b57cec5SDimitry Andric LastNonDeadUser = I;
7380b57cec5SDimitry Andric ++I;
7390b57cec5SDimitry Andric continue;
7400b57cec5SDimitry Andric }
7410b57cec5SDimitry Andric
7420b57cec5SDimitry Andric // If the constant was dead, then the iterator is invalidated.
7438bcb0991SDimitry Andric if (LastNonDeadUser == E)
7440b57cec5SDimitry Andric I = user_begin();
7458bcb0991SDimitry Andric else
7468bcb0991SDimitry Andric I = std::next(LastNonDeadUser);
7470b57cec5SDimitry Andric }
7480b57cec5SDimitry Andric }
7490b57cec5SDimitry Andric
hasOneLiveUse() const75004eeddc0SDimitry Andric bool Constant::hasOneLiveUse() const { return hasNLiveUses(1); }
75104eeddc0SDimitry Andric
hasZeroLiveUses() const75204eeddc0SDimitry Andric bool Constant::hasZeroLiveUses() const { return hasNLiveUses(0); }
75304eeddc0SDimitry Andric
hasNLiveUses(unsigned N) const75404eeddc0SDimitry Andric bool Constant::hasNLiveUses(unsigned N) const {
755349cc55cSDimitry Andric unsigned NumUses = 0;
75604eeddc0SDimitry Andric for (const Use &U : uses()) {
75704eeddc0SDimitry Andric const Constant *User = dyn_cast<Constant>(U.getUser());
758349cc55cSDimitry Andric if (!User || !constantIsDead(User, /* RemoveDeadUsers= */ false)) {
759349cc55cSDimitry Andric ++NumUses;
760349cc55cSDimitry Andric
76104eeddc0SDimitry Andric if (NumUses > N)
762349cc55cSDimitry Andric return false;
763349cc55cSDimitry Andric }
764349cc55cSDimitry Andric }
76504eeddc0SDimitry Andric return NumUses == N;
766349cc55cSDimitry Andric }
767349cc55cSDimitry Andric
replaceUndefsWith(Constant * C,Constant * Replacement)768480093f4SDimitry Andric Constant *Constant::replaceUndefsWith(Constant *C, Constant *Replacement) {
769480093f4SDimitry Andric assert(C && Replacement && "Expected non-nullptr constant arguments");
770480093f4SDimitry Andric Type *Ty = C->getType();
771480093f4SDimitry Andric if (match(C, m_Undef())) {
772480093f4SDimitry Andric assert(Ty == Replacement->getType() && "Expected matching types");
773480093f4SDimitry Andric return Replacement;
774480093f4SDimitry Andric }
775480093f4SDimitry Andric
776480093f4SDimitry Andric // Don't know how to deal with this constant.
7775ffd83dbSDimitry Andric auto *VTy = dyn_cast<FixedVectorType>(Ty);
7785ffd83dbSDimitry Andric if (!VTy)
779480093f4SDimitry Andric return C;
780480093f4SDimitry Andric
7815ffd83dbSDimitry Andric unsigned NumElts = VTy->getNumElements();
782480093f4SDimitry Andric SmallVector<Constant *, 32> NewC(NumElts);
783480093f4SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) {
784480093f4SDimitry Andric Constant *EltC = C->getAggregateElement(i);
785480093f4SDimitry Andric assert((!EltC || EltC->getType() == Replacement->getType()) &&
786480093f4SDimitry Andric "Expected matching types");
787480093f4SDimitry Andric NewC[i] = EltC && match(EltC, m_Undef()) ? Replacement : EltC;
788480093f4SDimitry Andric }
789480093f4SDimitry Andric return ConstantVector::get(NewC);
790480093f4SDimitry Andric }
7910b57cec5SDimitry Andric
mergeUndefsWith(Constant * C,Constant * Other)792e8d8bef9SDimitry Andric Constant *Constant::mergeUndefsWith(Constant *C, Constant *Other) {
793e8d8bef9SDimitry Andric assert(C && Other && "Expected non-nullptr constant arguments");
794e8d8bef9SDimitry Andric if (match(C, m_Undef()))
795e8d8bef9SDimitry Andric return C;
796e8d8bef9SDimitry Andric
797e8d8bef9SDimitry Andric Type *Ty = C->getType();
798e8d8bef9SDimitry Andric if (match(Other, m_Undef()))
799e8d8bef9SDimitry Andric return UndefValue::get(Ty);
800e8d8bef9SDimitry Andric
801e8d8bef9SDimitry Andric auto *VTy = dyn_cast<FixedVectorType>(Ty);
802e8d8bef9SDimitry Andric if (!VTy)
803e8d8bef9SDimitry Andric return C;
804e8d8bef9SDimitry Andric
805e8d8bef9SDimitry Andric Type *EltTy = VTy->getElementType();
806e8d8bef9SDimitry Andric unsigned NumElts = VTy->getNumElements();
807e8d8bef9SDimitry Andric assert(isa<FixedVectorType>(Other->getType()) &&
808e8d8bef9SDimitry Andric cast<FixedVectorType>(Other->getType())->getNumElements() == NumElts &&
809e8d8bef9SDimitry Andric "Type mismatch");
810e8d8bef9SDimitry Andric
811e8d8bef9SDimitry Andric bool FoundExtraUndef = false;
812e8d8bef9SDimitry Andric SmallVector<Constant *, 32> NewC(NumElts);
813e8d8bef9SDimitry Andric for (unsigned I = 0; I != NumElts; ++I) {
814e8d8bef9SDimitry Andric NewC[I] = C->getAggregateElement(I);
815e8d8bef9SDimitry Andric Constant *OtherEltC = Other->getAggregateElement(I);
816e8d8bef9SDimitry Andric assert(NewC[I] && OtherEltC && "Unknown vector element");
817e8d8bef9SDimitry Andric if (!match(NewC[I], m_Undef()) && match(OtherEltC, m_Undef())) {
818e8d8bef9SDimitry Andric NewC[I] = UndefValue::get(EltTy);
819e8d8bef9SDimitry Andric FoundExtraUndef = true;
820e8d8bef9SDimitry Andric }
821e8d8bef9SDimitry Andric }
822e8d8bef9SDimitry Andric if (FoundExtraUndef)
823e8d8bef9SDimitry Andric return ConstantVector::get(NewC);
824e8d8bef9SDimitry Andric return C;
825e8d8bef9SDimitry Andric }
8260b57cec5SDimitry Andric
isManifestConstant() const82723408297SDimitry Andric bool Constant::isManifestConstant() const {
82823408297SDimitry Andric if (isa<ConstantData>(this))
82923408297SDimitry Andric return true;
83023408297SDimitry Andric if (isa<ConstantAggregate>(this) || isa<ConstantExpr>(this)) {
83123408297SDimitry Andric for (const Value *Op : operand_values())
83223408297SDimitry Andric if (!cast<Constant>(Op)->isManifestConstant())
83323408297SDimitry Andric return false;
83423408297SDimitry Andric return true;
83523408297SDimitry Andric }
83623408297SDimitry Andric return false;
83723408297SDimitry Andric }
83823408297SDimitry Andric
8390b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8400b57cec5SDimitry Andric // ConstantInt
8410b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8420b57cec5SDimitry Andric
ConstantInt(Type * Ty,const APInt & V)843*0fca6ea1SDimitry Andric ConstantInt::ConstantInt(Type *Ty, const APInt &V)
8440b57cec5SDimitry Andric : ConstantData(Ty, ConstantIntVal), Val(V) {
845*0fca6ea1SDimitry Andric assert(V.getBitWidth() ==
846*0fca6ea1SDimitry Andric cast<IntegerType>(Ty->getScalarType())->getBitWidth() &&
847*0fca6ea1SDimitry Andric "Invalid constant for type");
8480b57cec5SDimitry Andric }
8490b57cec5SDimitry Andric
getTrue(LLVMContext & Context)8500b57cec5SDimitry Andric ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
8510b57cec5SDimitry Andric LLVMContextImpl *pImpl = Context.pImpl;
8520b57cec5SDimitry Andric if (!pImpl->TheTrueVal)
8530b57cec5SDimitry Andric pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
8540b57cec5SDimitry Andric return pImpl->TheTrueVal;
8550b57cec5SDimitry Andric }
8560b57cec5SDimitry Andric
getFalse(LLVMContext & Context)8570b57cec5SDimitry Andric ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
8580b57cec5SDimitry Andric LLVMContextImpl *pImpl = Context.pImpl;
8590b57cec5SDimitry Andric if (!pImpl->TheFalseVal)
8600b57cec5SDimitry Andric pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
8610b57cec5SDimitry Andric return pImpl->TheFalseVal;
8620b57cec5SDimitry Andric }
8630b57cec5SDimitry Andric
getBool(LLVMContext & Context,bool V)864e8d8bef9SDimitry Andric ConstantInt *ConstantInt::getBool(LLVMContext &Context, bool V) {
865e8d8bef9SDimitry Andric return V ? getTrue(Context) : getFalse(Context);
866e8d8bef9SDimitry Andric }
867e8d8bef9SDimitry Andric
getTrue(Type * Ty)8680b57cec5SDimitry Andric Constant *ConstantInt::getTrue(Type *Ty) {
8690b57cec5SDimitry Andric assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
8700b57cec5SDimitry Andric ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());
8710b57cec5SDimitry Andric if (auto *VTy = dyn_cast<VectorType>(Ty))
8725ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), TrueC);
8730b57cec5SDimitry Andric return TrueC;
8740b57cec5SDimitry Andric }
8750b57cec5SDimitry Andric
getFalse(Type * Ty)8760b57cec5SDimitry Andric Constant *ConstantInt::getFalse(Type *Ty) {
8770b57cec5SDimitry Andric assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
8780b57cec5SDimitry Andric ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());
8790b57cec5SDimitry Andric if (auto *VTy = dyn_cast<VectorType>(Ty))
8805ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), FalseC);
8810b57cec5SDimitry Andric return FalseC;
8820b57cec5SDimitry Andric }
8830b57cec5SDimitry Andric
getBool(Type * Ty,bool V)884e8d8bef9SDimitry Andric Constant *ConstantInt::getBool(Type *Ty, bool V) {
885e8d8bef9SDimitry Andric return V ? getTrue(Ty) : getFalse(Ty);
886e8d8bef9SDimitry Andric }
887e8d8bef9SDimitry Andric
8880b57cec5SDimitry Andric // Get a ConstantInt from an APInt.
get(LLVMContext & Context,const APInt & V)8890b57cec5SDimitry Andric ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
8900b57cec5SDimitry Andric // get an existing value or the insertion position
8910b57cec5SDimitry Andric LLVMContextImpl *pImpl = Context.pImpl;
89206c3fb27SDimitry Andric std::unique_ptr<ConstantInt> &Slot =
89306c3fb27SDimitry Andric V.isZero() ? pImpl->IntZeroConstants[V.getBitWidth()]
89406c3fb27SDimitry Andric : V.isOne() ? pImpl->IntOneConstants[V.getBitWidth()]
89506c3fb27SDimitry Andric : pImpl->IntConstants[V];
8960b57cec5SDimitry Andric if (!Slot) {
8970b57cec5SDimitry Andric // Get the corresponding integer type for the bit width of the value.
8980b57cec5SDimitry Andric IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
8990b57cec5SDimitry Andric Slot.reset(new ConstantInt(ITy, V));
9000b57cec5SDimitry Andric }
9010b57cec5SDimitry Andric assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
9020b57cec5SDimitry Andric return Slot.get();
9030b57cec5SDimitry Andric }
9040b57cec5SDimitry Andric
905*0fca6ea1SDimitry Andric // Get a ConstantInt vector with each lane set to the same APInt.
get(LLVMContext & Context,ElementCount EC,const APInt & V)906*0fca6ea1SDimitry Andric ConstantInt *ConstantInt::get(LLVMContext &Context, ElementCount EC,
907*0fca6ea1SDimitry Andric const APInt &V) {
908*0fca6ea1SDimitry Andric // Get an existing value or the insertion position.
909*0fca6ea1SDimitry Andric std::unique_ptr<ConstantInt> &Slot =
910*0fca6ea1SDimitry Andric Context.pImpl->IntSplatConstants[std::make_pair(EC, V)];
911*0fca6ea1SDimitry Andric if (!Slot) {
912*0fca6ea1SDimitry Andric IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
913*0fca6ea1SDimitry Andric VectorType *VTy = VectorType::get(ITy, EC);
914*0fca6ea1SDimitry Andric Slot.reset(new ConstantInt(VTy, V));
915*0fca6ea1SDimitry Andric }
916*0fca6ea1SDimitry Andric
917*0fca6ea1SDimitry Andric #ifndef NDEBUG
918*0fca6ea1SDimitry Andric IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
919*0fca6ea1SDimitry Andric VectorType *VTy = VectorType::get(ITy, EC);
920*0fca6ea1SDimitry Andric assert(Slot->getType() == VTy);
921*0fca6ea1SDimitry Andric #endif
922*0fca6ea1SDimitry Andric return Slot.get();
923*0fca6ea1SDimitry Andric }
924*0fca6ea1SDimitry Andric
get(Type * Ty,uint64_t V,bool isSigned)9250b57cec5SDimitry Andric Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
9260b57cec5SDimitry Andric Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
9270b57cec5SDimitry Andric
9280b57cec5SDimitry Andric // For vectors, broadcast the value.
9290b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
9305ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
9310b57cec5SDimitry Andric
9320b57cec5SDimitry Andric return C;
9330b57cec5SDimitry Andric }
9340b57cec5SDimitry Andric
get(IntegerType * Ty,uint64_t V,bool isSigned)9350b57cec5SDimitry Andric ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
9360b57cec5SDimitry Andric return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
9370b57cec5SDimitry Andric }
9380b57cec5SDimitry Andric
get(Type * Ty,const APInt & V)9390b57cec5SDimitry Andric Constant *ConstantInt::get(Type *Ty, const APInt& V) {
9400b57cec5SDimitry Andric ConstantInt *C = get(Ty->getContext(), V);
9410b57cec5SDimitry Andric assert(C->getType() == Ty->getScalarType() &&
9420b57cec5SDimitry Andric "ConstantInt type doesn't match the type implied by its value!");
9430b57cec5SDimitry Andric
9440b57cec5SDimitry Andric // For vectors, broadcast the value.
9450b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
9465ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
9470b57cec5SDimitry Andric
9480b57cec5SDimitry Andric return C;
9490b57cec5SDimitry Andric }
9500b57cec5SDimitry Andric
get(IntegerType * Ty,StringRef Str,uint8_t radix)9510b57cec5SDimitry Andric ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
9520b57cec5SDimitry Andric return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
9530b57cec5SDimitry Andric }
9540b57cec5SDimitry Andric
9550b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()9560b57cec5SDimitry Andric void ConstantInt::destroyConstantImpl() {
9570b57cec5SDimitry Andric llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
9580b57cec5SDimitry Andric }
9590b57cec5SDimitry Andric
9600b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
9610b57cec5SDimitry Andric // ConstantFP
9620b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
9630b57cec5SDimitry Andric
get(Type * Ty,double V)9640b57cec5SDimitry Andric Constant *ConstantFP::get(Type *Ty, double V) {
9650b57cec5SDimitry Andric LLVMContext &Context = Ty->getContext();
9660b57cec5SDimitry Andric
9670b57cec5SDimitry Andric APFloat FV(V);
9680b57cec5SDimitry Andric bool ignored;
969e8d8bef9SDimitry Andric FV.convert(Ty->getScalarType()->getFltSemantics(),
9700b57cec5SDimitry Andric APFloat::rmNearestTiesToEven, &ignored);
9710b57cec5SDimitry Andric Constant *C = get(Context, FV);
9720b57cec5SDimitry Andric
9730b57cec5SDimitry Andric // For vectors, broadcast the value.
9740b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
9755ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
9760b57cec5SDimitry Andric
9770b57cec5SDimitry Andric return C;
9780b57cec5SDimitry Andric }
9790b57cec5SDimitry Andric
get(Type * Ty,const APFloat & V)9800b57cec5SDimitry Andric Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
9810b57cec5SDimitry Andric ConstantFP *C = get(Ty->getContext(), V);
9820b57cec5SDimitry Andric assert(C->getType() == Ty->getScalarType() &&
9830b57cec5SDimitry Andric "ConstantFP type doesn't match the type implied by its value!");
9840b57cec5SDimitry Andric
9850b57cec5SDimitry Andric // For vectors, broadcast the value.
9860b57cec5SDimitry Andric if (auto *VTy = dyn_cast<VectorType>(Ty))
9875ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
9880b57cec5SDimitry Andric
9890b57cec5SDimitry Andric return C;
9900b57cec5SDimitry Andric }
9910b57cec5SDimitry Andric
get(Type * Ty,StringRef Str)9920b57cec5SDimitry Andric Constant *ConstantFP::get(Type *Ty, StringRef Str) {
9930b57cec5SDimitry Andric LLVMContext &Context = Ty->getContext();
9940b57cec5SDimitry Andric
995e8d8bef9SDimitry Andric APFloat FV(Ty->getScalarType()->getFltSemantics(), Str);
9960b57cec5SDimitry Andric Constant *C = get(Context, FV);
9970b57cec5SDimitry Andric
9980b57cec5SDimitry Andric // For vectors, broadcast the value.
9990b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
10005ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
10010b57cec5SDimitry Andric
10020b57cec5SDimitry Andric return C;
10030b57cec5SDimitry Andric }
10040b57cec5SDimitry Andric
getNaN(Type * Ty,bool Negative,uint64_t Payload)10050b57cec5SDimitry Andric Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {
1006e8d8bef9SDimitry Andric const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
10070b57cec5SDimitry Andric APFloat NaN = APFloat::getNaN(Semantics, Negative, Payload);
10080b57cec5SDimitry Andric Constant *C = get(Ty->getContext(), NaN);
10090b57cec5SDimitry Andric
10100b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
10115ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
10120b57cec5SDimitry Andric
10130b57cec5SDimitry Andric return C;
10140b57cec5SDimitry Andric }
10150b57cec5SDimitry Andric
getQNaN(Type * Ty,bool Negative,APInt * Payload)10160b57cec5SDimitry Andric Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {
1017e8d8bef9SDimitry Andric const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
10180b57cec5SDimitry Andric APFloat NaN = APFloat::getQNaN(Semantics, Negative, Payload);
10190b57cec5SDimitry Andric Constant *C = get(Ty->getContext(), NaN);
10200b57cec5SDimitry Andric
10210b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
10225ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
10230b57cec5SDimitry Andric
10240b57cec5SDimitry Andric return C;
10250b57cec5SDimitry Andric }
10260b57cec5SDimitry Andric
getSNaN(Type * Ty,bool Negative,APInt * Payload)10270b57cec5SDimitry Andric Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {
1028e8d8bef9SDimitry Andric const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
10290b57cec5SDimitry Andric APFloat NaN = APFloat::getSNaN(Semantics, Negative, Payload);
10300b57cec5SDimitry Andric Constant *C = get(Ty->getContext(), NaN);
10310b57cec5SDimitry Andric
10320b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
10335ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
10340b57cec5SDimitry Andric
10350b57cec5SDimitry Andric return C;
10360b57cec5SDimitry Andric }
10370b57cec5SDimitry Andric
getZero(Type * Ty,bool Negative)103881ad6265SDimitry Andric Constant *ConstantFP::getZero(Type *Ty, bool Negative) {
1039e8d8bef9SDimitry Andric const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
104081ad6265SDimitry Andric APFloat NegZero = APFloat::getZero(Semantics, Negative);
10410b57cec5SDimitry Andric Constant *C = get(Ty->getContext(), NegZero);
10420b57cec5SDimitry Andric
10430b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
10445ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
10450b57cec5SDimitry Andric
10460b57cec5SDimitry Andric return C;
10470b57cec5SDimitry Andric }
10480b57cec5SDimitry Andric
10490b57cec5SDimitry Andric
10500b57cec5SDimitry Andric // ConstantFP accessors.
get(LLVMContext & Context,const APFloat & V)10510b57cec5SDimitry Andric ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
10520b57cec5SDimitry Andric LLVMContextImpl* pImpl = Context.pImpl;
10530b57cec5SDimitry Andric
10540b57cec5SDimitry Andric std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
10550b57cec5SDimitry Andric
10560b57cec5SDimitry Andric if (!Slot) {
1057e8d8bef9SDimitry Andric Type *Ty = Type::getFloatingPointTy(Context, V.getSemantics());
10580b57cec5SDimitry Andric Slot.reset(new ConstantFP(Ty, V));
10590b57cec5SDimitry Andric }
10600b57cec5SDimitry Andric
10610b57cec5SDimitry Andric return Slot.get();
10620b57cec5SDimitry Andric }
10630b57cec5SDimitry Andric
1064*0fca6ea1SDimitry Andric // Get a ConstantFP vector with each lane set to the same APFloat.
get(LLVMContext & Context,ElementCount EC,const APFloat & V)1065*0fca6ea1SDimitry Andric ConstantFP *ConstantFP::get(LLVMContext &Context, ElementCount EC,
1066*0fca6ea1SDimitry Andric const APFloat &V) {
1067*0fca6ea1SDimitry Andric // Get an existing value or the insertion position.
1068*0fca6ea1SDimitry Andric std::unique_ptr<ConstantFP> &Slot =
1069*0fca6ea1SDimitry Andric Context.pImpl->FPSplatConstants[std::make_pair(EC, V)];
1070*0fca6ea1SDimitry Andric if (!Slot) {
1071*0fca6ea1SDimitry Andric Type *EltTy = Type::getFloatingPointTy(Context, V.getSemantics());
1072*0fca6ea1SDimitry Andric VectorType *VTy = VectorType::get(EltTy, EC);
1073*0fca6ea1SDimitry Andric Slot.reset(new ConstantFP(VTy, V));
1074*0fca6ea1SDimitry Andric }
1075*0fca6ea1SDimitry Andric
1076*0fca6ea1SDimitry Andric #ifndef NDEBUG
1077*0fca6ea1SDimitry Andric Type *EltTy = Type::getFloatingPointTy(Context, V.getSemantics());
1078*0fca6ea1SDimitry Andric VectorType *VTy = VectorType::get(EltTy, EC);
1079*0fca6ea1SDimitry Andric assert(Slot->getType() == VTy);
1080*0fca6ea1SDimitry Andric #endif
1081*0fca6ea1SDimitry Andric return Slot.get();
1082*0fca6ea1SDimitry Andric }
1083*0fca6ea1SDimitry Andric
getInfinity(Type * Ty,bool Negative)10840b57cec5SDimitry Andric Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
1085e8d8bef9SDimitry Andric const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
10860b57cec5SDimitry Andric Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
10870b57cec5SDimitry Andric
10880b57cec5SDimitry Andric if (VectorType *VTy = dyn_cast<VectorType>(Ty))
10895ffd83dbSDimitry Andric return ConstantVector::getSplat(VTy->getElementCount(), C);
10900b57cec5SDimitry Andric
10910b57cec5SDimitry Andric return C;
10920b57cec5SDimitry Andric }
10930b57cec5SDimitry Andric
ConstantFP(Type * Ty,const APFloat & V)10940b57cec5SDimitry Andric ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
10950b57cec5SDimitry Andric : ConstantData(Ty, ConstantFPVal), Val(V) {
1096*0fca6ea1SDimitry Andric assert(&V.getSemantics() == &Ty->getScalarType()->getFltSemantics() &&
10970b57cec5SDimitry Andric "FP type Mismatch");
10980b57cec5SDimitry Andric }
10990b57cec5SDimitry Andric
isExactlyValue(const APFloat & V) const11000b57cec5SDimitry Andric bool ConstantFP::isExactlyValue(const APFloat &V) const {
11010b57cec5SDimitry Andric return Val.bitwiseIsEqual(V);
11020b57cec5SDimitry Andric }
11030b57cec5SDimitry Andric
11040b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()11050b57cec5SDimitry Andric void ConstantFP::destroyConstantImpl() {
11060b57cec5SDimitry Andric llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
11070b57cec5SDimitry Andric }
11080b57cec5SDimitry Andric
11090b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11100b57cec5SDimitry Andric // ConstantAggregateZero Implementation
11110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11120b57cec5SDimitry Andric
getSequentialElement() const11130b57cec5SDimitry Andric Constant *ConstantAggregateZero::getSequentialElement() const {
11145ffd83dbSDimitry Andric if (auto *AT = dyn_cast<ArrayType>(getType()))
11155ffd83dbSDimitry Andric return Constant::getNullValue(AT->getElementType());
11165ffd83dbSDimitry Andric return Constant::getNullValue(cast<VectorType>(getType())->getElementType());
11170b57cec5SDimitry Andric }
11180b57cec5SDimitry Andric
getStructElement(unsigned Elt) const11190b57cec5SDimitry Andric Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
11200b57cec5SDimitry Andric return Constant::getNullValue(getType()->getStructElementType(Elt));
11210b57cec5SDimitry Andric }
11220b57cec5SDimitry Andric
getElementValue(Constant * C) const11230b57cec5SDimitry Andric Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
11245ffd83dbSDimitry Andric if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
11250b57cec5SDimitry Andric return getSequentialElement();
11260b57cec5SDimitry Andric return getStructElement(cast<ConstantInt>(C)->getZExtValue());
11270b57cec5SDimitry Andric }
11280b57cec5SDimitry Andric
getElementValue(unsigned Idx) const11290b57cec5SDimitry Andric Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
11305ffd83dbSDimitry Andric if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
11310b57cec5SDimitry Andric return getSequentialElement();
11320b57cec5SDimitry Andric return getStructElement(Idx);
11330b57cec5SDimitry Andric }
11340b57cec5SDimitry Andric
getElementCount() const1135fe6060f1SDimitry Andric ElementCount ConstantAggregateZero::getElementCount() const {
11360b57cec5SDimitry Andric Type *Ty = getType();
11370b57cec5SDimitry Andric if (auto *AT = dyn_cast<ArrayType>(Ty))
1138fe6060f1SDimitry Andric return ElementCount::getFixed(AT->getNumElements());
11390b57cec5SDimitry Andric if (auto *VT = dyn_cast<VectorType>(Ty))
1140fe6060f1SDimitry Andric return VT->getElementCount();
1141fe6060f1SDimitry Andric return ElementCount::getFixed(Ty->getStructNumElements());
11420b57cec5SDimitry Andric }
11430b57cec5SDimitry Andric
11440b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11450b57cec5SDimitry Andric // UndefValue Implementation
11460b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11470b57cec5SDimitry Andric
getSequentialElement() const11480b57cec5SDimitry Andric UndefValue *UndefValue::getSequentialElement() const {
11495ffd83dbSDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))
11505ffd83dbSDimitry Andric return UndefValue::get(ATy->getElementType());
11515ffd83dbSDimitry Andric return UndefValue::get(cast<VectorType>(getType())->getElementType());
11520b57cec5SDimitry Andric }
11530b57cec5SDimitry Andric
getStructElement(unsigned Elt) const11540b57cec5SDimitry Andric UndefValue *UndefValue::getStructElement(unsigned Elt) const {
11550b57cec5SDimitry Andric return UndefValue::get(getType()->getStructElementType(Elt));
11560b57cec5SDimitry Andric }
11570b57cec5SDimitry Andric
getElementValue(Constant * C) const11580b57cec5SDimitry Andric UndefValue *UndefValue::getElementValue(Constant *C) const {
11595ffd83dbSDimitry Andric if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
11600b57cec5SDimitry Andric return getSequentialElement();
11610b57cec5SDimitry Andric return getStructElement(cast<ConstantInt>(C)->getZExtValue());
11620b57cec5SDimitry Andric }
11630b57cec5SDimitry Andric
getElementValue(unsigned Idx) const11640b57cec5SDimitry Andric UndefValue *UndefValue::getElementValue(unsigned Idx) const {
11655ffd83dbSDimitry Andric if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
11660b57cec5SDimitry Andric return getSequentialElement();
11670b57cec5SDimitry Andric return getStructElement(Idx);
11680b57cec5SDimitry Andric }
11690b57cec5SDimitry Andric
getNumElements() const11700b57cec5SDimitry Andric unsigned UndefValue::getNumElements() const {
11710b57cec5SDimitry Andric Type *Ty = getType();
11725ffd83dbSDimitry Andric if (auto *AT = dyn_cast<ArrayType>(Ty))
11735ffd83dbSDimitry Andric return AT->getNumElements();
11745ffd83dbSDimitry Andric if (auto *VT = dyn_cast<VectorType>(Ty))
1175e8d8bef9SDimitry Andric return cast<FixedVectorType>(VT)->getNumElements();
11760b57cec5SDimitry Andric return Ty->getStructNumElements();
11770b57cec5SDimitry Andric }
11780b57cec5SDimitry Andric
11790b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1180e8d8bef9SDimitry Andric // PoisonValue Implementation
1181e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
1182e8d8bef9SDimitry Andric
getSequentialElement() const1183e8d8bef9SDimitry Andric PoisonValue *PoisonValue::getSequentialElement() const {
1184e8d8bef9SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))
1185e8d8bef9SDimitry Andric return PoisonValue::get(ATy->getElementType());
1186e8d8bef9SDimitry Andric return PoisonValue::get(cast<VectorType>(getType())->getElementType());
1187e8d8bef9SDimitry Andric }
1188e8d8bef9SDimitry Andric
getStructElement(unsigned Elt) const1189e8d8bef9SDimitry Andric PoisonValue *PoisonValue::getStructElement(unsigned Elt) const {
1190e8d8bef9SDimitry Andric return PoisonValue::get(getType()->getStructElementType(Elt));
1191e8d8bef9SDimitry Andric }
1192e8d8bef9SDimitry Andric
getElementValue(Constant * C) const1193e8d8bef9SDimitry Andric PoisonValue *PoisonValue::getElementValue(Constant *C) const {
1194e8d8bef9SDimitry Andric if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
1195e8d8bef9SDimitry Andric return getSequentialElement();
1196e8d8bef9SDimitry Andric return getStructElement(cast<ConstantInt>(C)->getZExtValue());
1197e8d8bef9SDimitry Andric }
1198e8d8bef9SDimitry Andric
getElementValue(unsigned Idx) const1199e8d8bef9SDimitry Andric PoisonValue *PoisonValue::getElementValue(unsigned Idx) const {
1200e8d8bef9SDimitry Andric if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
1201e8d8bef9SDimitry Andric return getSequentialElement();
1202e8d8bef9SDimitry Andric return getStructElement(Idx);
1203e8d8bef9SDimitry Andric }
1204e8d8bef9SDimitry Andric
1205e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
12060b57cec5SDimitry Andric // ConstantXXX Classes
12070b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12080b57cec5SDimitry Andric
12090b57cec5SDimitry Andric template <typename ItTy, typename EltTy>
rangeOnlyContains(ItTy Start,ItTy End,EltTy Elt)12100b57cec5SDimitry Andric static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
12110b57cec5SDimitry Andric for (; Start != End; ++Start)
12120b57cec5SDimitry Andric if (*Start != Elt)
12130b57cec5SDimitry Andric return false;
12140b57cec5SDimitry Andric return true;
12150b57cec5SDimitry Andric }
12160b57cec5SDimitry Andric
12170b57cec5SDimitry Andric template <typename SequentialTy, typename ElementTy>
getIntSequenceIfElementsMatch(ArrayRef<Constant * > V)12180b57cec5SDimitry Andric static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
12190b57cec5SDimitry Andric assert(!V.empty() && "Cannot get empty int sequence.");
12200b57cec5SDimitry Andric
12210b57cec5SDimitry Andric SmallVector<ElementTy, 16> Elts;
12220b57cec5SDimitry Andric for (Constant *C : V)
12230b57cec5SDimitry Andric if (auto *CI = dyn_cast<ConstantInt>(C))
12240b57cec5SDimitry Andric Elts.push_back(CI->getZExtValue());
12250b57cec5SDimitry Andric else
12260b57cec5SDimitry Andric return nullptr;
12270b57cec5SDimitry Andric return SequentialTy::get(V[0]->getContext(), Elts);
12280b57cec5SDimitry Andric }
12290b57cec5SDimitry Andric
12300b57cec5SDimitry Andric template <typename SequentialTy, typename ElementTy>
getFPSequenceIfElementsMatch(ArrayRef<Constant * > V)12310b57cec5SDimitry Andric static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
12320b57cec5SDimitry Andric assert(!V.empty() && "Cannot get empty FP sequence.");
12330b57cec5SDimitry Andric
12340b57cec5SDimitry Andric SmallVector<ElementTy, 16> Elts;
12350b57cec5SDimitry Andric for (Constant *C : V)
12360b57cec5SDimitry Andric if (auto *CFP = dyn_cast<ConstantFP>(C))
12370b57cec5SDimitry Andric Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
12380b57cec5SDimitry Andric else
12390b57cec5SDimitry Andric return nullptr;
12405ffd83dbSDimitry Andric return SequentialTy::getFP(V[0]->getType(), Elts);
12410b57cec5SDimitry Andric }
12420b57cec5SDimitry Andric
12430b57cec5SDimitry Andric template <typename SequenceTy>
getSequenceIfElementsMatch(Constant * C,ArrayRef<Constant * > V)12440b57cec5SDimitry Andric static Constant *getSequenceIfElementsMatch(Constant *C,
12450b57cec5SDimitry Andric ArrayRef<Constant *> V) {
12460b57cec5SDimitry Andric // We speculatively build the elements here even if it turns out that there is
12470b57cec5SDimitry Andric // a constantexpr or something else weird, since it is so uncommon for that to
12480b57cec5SDimitry Andric // happen.
12490b57cec5SDimitry Andric if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
12500b57cec5SDimitry Andric if (CI->getType()->isIntegerTy(8))
12510b57cec5SDimitry Andric return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
12520b57cec5SDimitry Andric else if (CI->getType()->isIntegerTy(16))
12530b57cec5SDimitry Andric return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
12540b57cec5SDimitry Andric else if (CI->getType()->isIntegerTy(32))
12550b57cec5SDimitry Andric return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
12560b57cec5SDimitry Andric else if (CI->getType()->isIntegerTy(64))
12570b57cec5SDimitry Andric return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
12580b57cec5SDimitry Andric } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
12595ffd83dbSDimitry Andric if (CFP->getType()->isHalfTy() || CFP->getType()->isBFloatTy())
12600b57cec5SDimitry Andric return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
12610b57cec5SDimitry Andric else if (CFP->getType()->isFloatTy())
12620b57cec5SDimitry Andric return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
12630b57cec5SDimitry Andric else if (CFP->getType()->isDoubleTy())
12640b57cec5SDimitry Andric return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
12650b57cec5SDimitry Andric }
12660b57cec5SDimitry Andric
12670b57cec5SDimitry Andric return nullptr;
12680b57cec5SDimitry Andric }
12690b57cec5SDimitry Andric
ConstantAggregate(Type * T,ValueTy VT,ArrayRef<Constant * > V)12705ffd83dbSDimitry Andric ConstantAggregate::ConstantAggregate(Type *T, ValueTy VT,
12710b57cec5SDimitry Andric ArrayRef<Constant *> V)
12720b57cec5SDimitry Andric : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
12730b57cec5SDimitry Andric V.size()) {
12740b57cec5SDimitry Andric llvm::copy(V, op_begin());
12750b57cec5SDimitry Andric
12760b57cec5SDimitry Andric // Check that types match, unless this is an opaque struct.
12775ffd83dbSDimitry Andric if (auto *ST = dyn_cast<StructType>(T)) {
12780b57cec5SDimitry Andric if (ST->isOpaque())
12790b57cec5SDimitry Andric return;
12800b57cec5SDimitry Andric for (unsigned I = 0, E = V.size(); I != E; ++I)
12815ffd83dbSDimitry Andric assert(V[I]->getType() == ST->getTypeAtIndex(I) &&
12825ffd83dbSDimitry Andric "Initializer for struct element doesn't match!");
12835ffd83dbSDimitry Andric }
12840b57cec5SDimitry Andric }
12850b57cec5SDimitry Andric
ConstantArray(ArrayType * T,ArrayRef<Constant * > V)12860b57cec5SDimitry Andric ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
12870b57cec5SDimitry Andric : ConstantAggregate(T, ConstantArrayVal, V) {
12880b57cec5SDimitry Andric assert(V.size() == T->getNumElements() &&
12890b57cec5SDimitry Andric "Invalid initializer for constant array");
12900b57cec5SDimitry Andric }
12910b57cec5SDimitry Andric
get(ArrayType * Ty,ArrayRef<Constant * > V)12920b57cec5SDimitry Andric Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
12930b57cec5SDimitry Andric if (Constant *C = getImpl(Ty, V))
12940b57cec5SDimitry Andric return C;
12950b57cec5SDimitry Andric return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
12960b57cec5SDimitry Andric }
12970b57cec5SDimitry Andric
getImpl(ArrayType * Ty,ArrayRef<Constant * > V)12980b57cec5SDimitry Andric Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
12990b57cec5SDimitry Andric // Empty arrays are canonicalized to ConstantAggregateZero.
13000b57cec5SDimitry Andric if (V.empty())
13010b57cec5SDimitry Andric return ConstantAggregateZero::get(Ty);
13020b57cec5SDimitry Andric
13030eae32dcSDimitry Andric for (Constant *C : V) {
13040eae32dcSDimitry Andric assert(C->getType() == Ty->getElementType() &&
13050b57cec5SDimitry Andric "Wrong type in array element initializer");
13060eae32dcSDimitry Andric (void)C;
13070b57cec5SDimitry Andric }
13080b57cec5SDimitry Andric
13090b57cec5SDimitry Andric // If this is an all-zero array, return a ConstantAggregateZero object. If
13100b57cec5SDimitry Andric // all undef, return an UndefValue, if "all simple", then return a
13110b57cec5SDimitry Andric // ConstantDataArray.
13120b57cec5SDimitry Andric Constant *C = V[0];
1313fe6060f1SDimitry Andric if (isa<PoisonValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
1314fe6060f1SDimitry Andric return PoisonValue::get(Ty);
1315fe6060f1SDimitry Andric
13160b57cec5SDimitry Andric if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
13170b57cec5SDimitry Andric return UndefValue::get(Ty);
13180b57cec5SDimitry Andric
13190b57cec5SDimitry Andric if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
13200b57cec5SDimitry Andric return ConstantAggregateZero::get(Ty);
13210b57cec5SDimitry Andric
13220b57cec5SDimitry Andric // Check to see if all of the elements are ConstantFP or ConstantInt and if
13230b57cec5SDimitry Andric // the element type is compatible with ConstantDataVector. If so, use it.
13240b57cec5SDimitry Andric if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
13250b57cec5SDimitry Andric return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
13260b57cec5SDimitry Andric
13270b57cec5SDimitry Andric // Otherwise, we really do want to create a ConstantArray.
13280b57cec5SDimitry Andric return nullptr;
13290b57cec5SDimitry Andric }
13300b57cec5SDimitry Andric
getTypeForElements(LLVMContext & Context,ArrayRef<Constant * > V,bool Packed)13310b57cec5SDimitry Andric StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
13320b57cec5SDimitry Andric ArrayRef<Constant*> V,
13330b57cec5SDimitry Andric bool Packed) {
13340b57cec5SDimitry Andric unsigned VecSize = V.size();
13350b57cec5SDimitry Andric SmallVector<Type*, 16> EltTypes(VecSize);
13360b57cec5SDimitry Andric for (unsigned i = 0; i != VecSize; ++i)
13370b57cec5SDimitry Andric EltTypes[i] = V[i]->getType();
13380b57cec5SDimitry Andric
13390b57cec5SDimitry Andric return StructType::get(Context, EltTypes, Packed);
13400b57cec5SDimitry Andric }
13410b57cec5SDimitry Andric
13420b57cec5SDimitry Andric
getTypeForElements(ArrayRef<Constant * > V,bool Packed)13430b57cec5SDimitry Andric StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
13440b57cec5SDimitry Andric bool Packed) {
13450b57cec5SDimitry Andric assert(!V.empty() &&
13460b57cec5SDimitry Andric "ConstantStruct::getTypeForElements cannot be called on empty list");
13470b57cec5SDimitry Andric return getTypeForElements(V[0]->getContext(), V, Packed);
13480b57cec5SDimitry Andric }
13490b57cec5SDimitry Andric
ConstantStruct(StructType * T,ArrayRef<Constant * > V)13500b57cec5SDimitry Andric ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
13510b57cec5SDimitry Andric : ConstantAggregate(T, ConstantStructVal, V) {
13520b57cec5SDimitry Andric assert((T->isOpaque() || V.size() == T->getNumElements()) &&
13530b57cec5SDimitry Andric "Invalid initializer for constant struct");
13540b57cec5SDimitry Andric }
13550b57cec5SDimitry Andric
13560b57cec5SDimitry Andric // ConstantStruct accessors.
get(StructType * ST,ArrayRef<Constant * > V)13570b57cec5SDimitry Andric Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
13580b57cec5SDimitry Andric assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
13590b57cec5SDimitry Andric "Incorrect # elements specified to ConstantStruct::get");
13600b57cec5SDimitry Andric
13610b57cec5SDimitry Andric // Create a ConstantAggregateZero value if all elements are zeros.
13620b57cec5SDimitry Andric bool isZero = true;
13630b57cec5SDimitry Andric bool isUndef = false;
1364fe6060f1SDimitry Andric bool isPoison = false;
13650b57cec5SDimitry Andric
13660b57cec5SDimitry Andric if (!V.empty()) {
13670b57cec5SDimitry Andric isUndef = isa<UndefValue>(V[0]);
1368fe6060f1SDimitry Andric isPoison = isa<PoisonValue>(V[0]);
13690b57cec5SDimitry Andric isZero = V[0]->isNullValue();
1370fe6060f1SDimitry Andric // PoisonValue inherits UndefValue, so its check is not necessary.
13710b57cec5SDimitry Andric if (isUndef || isZero) {
13720eae32dcSDimitry Andric for (Constant *C : V) {
13730eae32dcSDimitry Andric if (!C->isNullValue())
13740b57cec5SDimitry Andric isZero = false;
13750eae32dcSDimitry Andric if (!isa<PoisonValue>(C))
1376fe6060f1SDimitry Andric isPoison = false;
13770eae32dcSDimitry Andric if (isa<PoisonValue>(C) || !isa<UndefValue>(C))
13780b57cec5SDimitry Andric isUndef = false;
13790b57cec5SDimitry Andric }
13800b57cec5SDimitry Andric }
13810b57cec5SDimitry Andric }
13820b57cec5SDimitry Andric if (isZero)
13830b57cec5SDimitry Andric return ConstantAggregateZero::get(ST);
1384fe6060f1SDimitry Andric if (isPoison)
1385fe6060f1SDimitry Andric return PoisonValue::get(ST);
13860b57cec5SDimitry Andric if (isUndef)
13870b57cec5SDimitry Andric return UndefValue::get(ST);
13880b57cec5SDimitry Andric
13890b57cec5SDimitry Andric return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
13900b57cec5SDimitry Andric }
13910b57cec5SDimitry Andric
ConstantVector(VectorType * T,ArrayRef<Constant * > V)13920b57cec5SDimitry Andric ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
13930b57cec5SDimitry Andric : ConstantAggregate(T, ConstantVectorVal, V) {
1394e8d8bef9SDimitry Andric assert(V.size() == cast<FixedVectorType>(T)->getNumElements() &&
13950b57cec5SDimitry Andric "Invalid initializer for constant vector");
13960b57cec5SDimitry Andric }
13970b57cec5SDimitry Andric
13980b57cec5SDimitry Andric // ConstantVector accessors.
get(ArrayRef<Constant * > V)13990b57cec5SDimitry Andric Constant *ConstantVector::get(ArrayRef<Constant*> V) {
14000b57cec5SDimitry Andric if (Constant *C = getImpl(V))
14010b57cec5SDimitry Andric return C;
14025ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(V.front()->getType(), V.size());
14030b57cec5SDimitry Andric return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
14040b57cec5SDimitry Andric }
14050b57cec5SDimitry Andric
getImpl(ArrayRef<Constant * > V)14060b57cec5SDimitry Andric Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
14070b57cec5SDimitry Andric assert(!V.empty() && "Vectors can't be empty");
14085ffd83dbSDimitry Andric auto *T = FixedVectorType::get(V.front()->getType(), V.size());
14090b57cec5SDimitry Andric
14100b57cec5SDimitry Andric // If this is an all-undef or all-zero vector, return a
14110b57cec5SDimitry Andric // ConstantAggregateZero or UndefValue.
14120b57cec5SDimitry Andric Constant *C = V[0];
14130b57cec5SDimitry Andric bool isZero = C->isNullValue();
14140b57cec5SDimitry Andric bool isUndef = isa<UndefValue>(C);
1415e8d8bef9SDimitry Andric bool isPoison = isa<PoisonValue>(C);
1416*0fca6ea1SDimitry Andric bool isSplatFP = UseConstantFPForFixedLengthSplat && isa<ConstantFP>(C);
1417*0fca6ea1SDimitry Andric bool isSplatInt = UseConstantIntForFixedLengthSplat && isa<ConstantInt>(C);
14180b57cec5SDimitry Andric
1419*0fca6ea1SDimitry Andric if (isZero || isUndef || isSplatFP || isSplatInt) {
14200b57cec5SDimitry Andric for (unsigned i = 1, e = V.size(); i != e; ++i)
14210b57cec5SDimitry Andric if (V[i] != C) {
1422*0fca6ea1SDimitry Andric isZero = isUndef = isPoison = isSplatFP = isSplatInt = false;
14230b57cec5SDimitry Andric break;
14240b57cec5SDimitry Andric }
14250b57cec5SDimitry Andric }
14260b57cec5SDimitry Andric
14270b57cec5SDimitry Andric if (isZero)
14280b57cec5SDimitry Andric return ConstantAggregateZero::get(T);
1429e8d8bef9SDimitry Andric if (isPoison)
1430e8d8bef9SDimitry Andric return PoisonValue::get(T);
14310b57cec5SDimitry Andric if (isUndef)
14320b57cec5SDimitry Andric return UndefValue::get(T);
1433*0fca6ea1SDimitry Andric if (isSplatFP)
1434*0fca6ea1SDimitry Andric return ConstantFP::get(C->getContext(), T->getElementCount(),
1435*0fca6ea1SDimitry Andric cast<ConstantFP>(C)->getValue());
1436*0fca6ea1SDimitry Andric if (isSplatInt)
1437*0fca6ea1SDimitry Andric return ConstantInt::get(C->getContext(), T->getElementCount(),
1438*0fca6ea1SDimitry Andric cast<ConstantInt>(C)->getValue());
14390b57cec5SDimitry Andric
14400b57cec5SDimitry Andric // Check to see if all of the elements are ConstantFP or ConstantInt and if
14410b57cec5SDimitry Andric // the element type is compatible with ConstantDataVector. If so, use it.
14420b57cec5SDimitry Andric if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
14430b57cec5SDimitry Andric return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
14440b57cec5SDimitry Andric
14450b57cec5SDimitry Andric // Otherwise, the element type isn't compatible with ConstantDataVector, or
14460b57cec5SDimitry Andric // the operand list contains a ConstantExpr or something else strange.
14470b57cec5SDimitry Andric return nullptr;
14480b57cec5SDimitry Andric }
14490b57cec5SDimitry Andric
getSplat(ElementCount EC,Constant * V)14505ffd83dbSDimitry Andric Constant *ConstantVector::getSplat(ElementCount EC, Constant *V) {
1451e8d8bef9SDimitry Andric if (!EC.isScalable()) {
1452*0fca6ea1SDimitry Andric // Maintain special handling of zero.
1453*0fca6ea1SDimitry Andric if (!V->isNullValue()) {
1454*0fca6ea1SDimitry Andric if (UseConstantIntForFixedLengthSplat && isa<ConstantInt>(V))
1455*0fca6ea1SDimitry Andric return ConstantInt::get(V->getContext(), EC,
1456*0fca6ea1SDimitry Andric cast<ConstantInt>(V)->getValue());
1457*0fca6ea1SDimitry Andric if (UseConstantFPForFixedLengthSplat && isa<ConstantFP>(V))
1458*0fca6ea1SDimitry Andric return ConstantFP::get(V->getContext(), EC,
1459*0fca6ea1SDimitry Andric cast<ConstantFP>(V)->getValue());
1460*0fca6ea1SDimitry Andric }
1461*0fca6ea1SDimitry Andric
14620b57cec5SDimitry Andric // If this splat is compatible with ConstantDataVector, use it instead of
14630b57cec5SDimitry Andric // ConstantVector.
14640b57cec5SDimitry Andric if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
14650b57cec5SDimitry Andric ConstantDataSequential::isElementTypeCompatible(V->getType()))
1466e8d8bef9SDimitry Andric return ConstantDataVector::getSplat(EC.getKnownMinValue(), V);
14670b57cec5SDimitry Andric
1468e8d8bef9SDimitry Andric SmallVector<Constant *, 32> Elts(EC.getKnownMinValue(), V);
14690b57cec5SDimitry Andric return get(Elts);
14700b57cec5SDimitry Andric }
14710b57cec5SDimitry Andric
1472*0fca6ea1SDimitry Andric // Maintain special handling of zero.
1473*0fca6ea1SDimitry Andric if (!V->isNullValue()) {
1474*0fca6ea1SDimitry Andric if (UseConstantIntForScalableSplat && isa<ConstantInt>(V))
1475*0fca6ea1SDimitry Andric return ConstantInt::get(V->getContext(), EC,
1476*0fca6ea1SDimitry Andric cast<ConstantInt>(V)->getValue());
1477*0fca6ea1SDimitry Andric if (UseConstantFPForScalableSplat && isa<ConstantFP>(V))
1478*0fca6ea1SDimitry Andric return ConstantFP::get(V->getContext(), EC,
1479*0fca6ea1SDimitry Andric cast<ConstantFP>(V)->getValue());
1480*0fca6ea1SDimitry Andric }
1481*0fca6ea1SDimitry Andric
14825ffd83dbSDimitry Andric Type *VTy = VectorType::get(V->getType(), EC);
14835ffd83dbSDimitry Andric
14845ffd83dbSDimitry Andric if (V->isNullValue())
14855ffd83dbSDimitry Andric return ConstantAggregateZero::get(VTy);
14865ffd83dbSDimitry Andric else if (isa<UndefValue>(V))
14875ffd83dbSDimitry Andric return UndefValue::get(VTy);
14885ffd83dbSDimitry Andric
1489bdd1243dSDimitry Andric Type *IdxTy = Type::getInt64Ty(VTy->getContext());
14905ffd83dbSDimitry Andric
14915ffd83dbSDimitry Andric // Move scalar into vector.
1492349cc55cSDimitry Andric Constant *PoisonV = PoisonValue::get(VTy);
1493bdd1243dSDimitry Andric V = ConstantExpr::getInsertElement(PoisonV, V, ConstantInt::get(IdxTy, 0));
14945ffd83dbSDimitry Andric // Build shuffle mask to perform the splat.
1495e8d8bef9SDimitry Andric SmallVector<int, 8> Zeros(EC.getKnownMinValue(), 0);
14965ffd83dbSDimitry Andric // Splat.
1497349cc55cSDimitry Andric return ConstantExpr::getShuffleVector(V, PoisonV, Zeros);
14985ffd83dbSDimitry Andric }
14995ffd83dbSDimitry Andric
get(LLVMContext & Context)15000b57cec5SDimitry Andric ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
15010b57cec5SDimitry Andric LLVMContextImpl *pImpl = Context.pImpl;
15020b57cec5SDimitry Andric if (!pImpl->TheNoneToken)
15030b57cec5SDimitry Andric pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
15040b57cec5SDimitry Andric return pImpl->TheNoneToken.get();
15050b57cec5SDimitry Andric }
15060b57cec5SDimitry Andric
15070b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()15080b57cec5SDimitry Andric void ConstantTokenNone::destroyConstantImpl() {
15090b57cec5SDimitry Andric llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
15100b57cec5SDimitry Andric }
15110b57cec5SDimitry Andric
15120b57cec5SDimitry Andric // Utility function for determining if a ConstantExpr is a CastOp or not. This
15130b57cec5SDimitry Andric // can't be inline because we don't want to #include Instruction.h into
15140b57cec5SDimitry Andric // Constant.h
isCast() const1515*0fca6ea1SDimitry Andric bool ConstantExpr::isCast() const { return Instruction::isCast(getOpcode()); }
15160b57cec5SDimitry Andric
getShuffleMask() const15175ffd83dbSDimitry Andric ArrayRef<int> ConstantExpr::getShuffleMask() const {
15185ffd83dbSDimitry Andric return cast<ShuffleVectorConstantExpr>(this)->ShuffleMask;
15195ffd83dbSDimitry Andric }
15205ffd83dbSDimitry Andric
getShuffleMaskForBitcode() const15215ffd83dbSDimitry Andric Constant *ConstantExpr::getShuffleMaskForBitcode() const {
15225ffd83dbSDimitry Andric return cast<ShuffleVectorConstantExpr>(this)->ShuffleMaskForBitcode;
15235ffd83dbSDimitry Andric }
15245ffd83dbSDimitry Andric
getWithOperands(ArrayRef<Constant * > Ops,Type * Ty,bool OnlyIfReduced,Type * SrcTy) const15250b57cec5SDimitry Andric Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
15260b57cec5SDimitry Andric bool OnlyIfReduced, Type *SrcTy) const {
15270b57cec5SDimitry Andric assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
15280b57cec5SDimitry Andric
15290b57cec5SDimitry Andric // If no operands changed return self.
15300b57cec5SDimitry Andric if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
15310b57cec5SDimitry Andric return const_cast<ConstantExpr*>(this);
15320b57cec5SDimitry Andric
15330b57cec5SDimitry Andric Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
15340b57cec5SDimitry Andric switch (getOpcode()) {
15350b57cec5SDimitry Andric case Instruction::Trunc:
15360b57cec5SDimitry Andric case Instruction::ZExt:
15370b57cec5SDimitry Andric case Instruction::SExt:
15380b57cec5SDimitry Andric case Instruction::FPTrunc:
15390b57cec5SDimitry Andric case Instruction::FPExt:
15400b57cec5SDimitry Andric case Instruction::UIToFP:
15410b57cec5SDimitry Andric case Instruction::SIToFP:
15420b57cec5SDimitry Andric case Instruction::FPToUI:
15430b57cec5SDimitry Andric case Instruction::FPToSI:
15440b57cec5SDimitry Andric case Instruction::PtrToInt:
15450b57cec5SDimitry Andric case Instruction::IntToPtr:
15460b57cec5SDimitry Andric case Instruction::BitCast:
15470b57cec5SDimitry Andric case Instruction::AddrSpaceCast:
15480b57cec5SDimitry Andric return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
15490b57cec5SDimitry Andric case Instruction::InsertElement:
15500b57cec5SDimitry Andric return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
15510b57cec5SDimitry Andric OnlyIfReducedTy);
15520b57cec5SDimitry Andric case Instruction::ExtractElement:
15530b57cec5SDimitry Andric return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
15540b57cec5SDimitry Andric case Instruction::ShuffleVector:
15555ffd83dbSDimitry Andric return ConstantExpr::getShuffleVector(Ops[0], Ops[1], getShuffleMask(),
15560b57cec5SDimitry Andric OnlyIfReducedTy);
15570b57cec5SDimitry Andric case Instruction::GetElementPtr: {
15580b57cec5SDimitry Andric auto *GEPO = cast<GEPOperator>(this);
15590b57cec5SDimitry Andric assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
15600b57cec5SDimitry Andric return ConstantExpr::getGetElementPtr(
15610b57cec5SDimitry Andric SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
1562*0fca6ea1SDimitry Andric GEPO->getNoWrapFlags(), GEPO->getInRange(), OnlyIfReducedTy);
15630b57cec5SDimitry Andric }
15640b57cec5SDimitry Andric default:
15650b57cec5SDimitry Andric assert(getNumOperands() == 2 && "Must be binary operator?");
15660b57cec5SDimitry Andric return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
15670b57cec5SDimitry Andric OnlyIfReducedTy);
15680b57cec5SDimitry Andric }
15690b57cec5SDimitry Andric }
15700b57cec5SDimitry Andric
15710b57cec5SDimitry Andric
15720b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15730b57cec5SDimitry Andric // isValueValidForType implementations
15740b57cec5SDimitry Andric
isValueValidForType(Type * Ty,uint64_t Val)15750b57cec5SDimitry Andric bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
15760b57cec5SDimitry Andric unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
15770b57cec5SDimitry Andric if (Ty->isIntegerTy(1))
15780b57cec5SDimitry Andric return Val == 0 || Val == 1;
15790b57cec5SDimitry Andric return isUIntN(NumBits, Val);
15800b57cec5SDimitry Andric }
15810b57cec5SDimitry Andric
isValueValidForType(Type * Ty,int64_t Val)15820b57cec5SDimitry Andric bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
15830b57cec5SDimitry Andric unsigned NumBits = Ty->getIntegerBitWidth();
15840b57cec5SDimitry Andric if (Ty->isIntegerTy(1))
15850b57cec5SDimitry Andric return Val == 0 || Val == 1 || Val == -1;
15860b57cec5SDimitry Andric return isIntN(NumBits, Val);
15870b57cec5SDimitry Andric }
15880b57cec5SDimitry Andric
isValueValidForType(Type * Ty,const APFloat & Val)15890b57cec5SDimitry Andric bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
15900b57cec5SDimitry Andric // convert modifies in place, so make a copy.
15910b57cec5SDimitry Andric APFloat Val2 = APFloat(Val);
15920b57cec5SDimitry Andric bool losesInfo;
15930b57cec5SDimitry Andric switch (Ty->getTypeID()) {
15940b57cec5SDimitry Andric default:
15950b57cec5SDimitry Andric return false; // These can't be represented as floating point!
15960b57cec5SDimitry Andric
15970b57cec5SDimitry Andric // FIXME rounding mode needs to be more flexible
15980b57cec5SDimitry Andric case Type::HalfTyID: {
15990b57cec5SDimitry Andric if (&Val2.getSemantics() == &APFloat::IEEEhalf())
16000b57cec5SDimitry Andric return true;
16010b57cec5SDimitry Andric Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
16020b57cec5SDimitry Andric return !losesInfo;
16030b57cec5SDimitry Andric }
16045ffd83dbSDimitry Andric case Type::BFloatTyID: {
16055ffd83dbSDimitry Andric if (&Val2.getSemantics() == &APFloat::BFloat())
16065ffd83dbSDimitry Andric return true;
16075ffd83dbSDimitry Andric Val2.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven, &losesInfo);
16085ffd83dbSDimitry Andric return !losesInfo;
16095ffd83dbSDimitry Andric }
16100b57cec5SDimitry Andric case Type::FloatTyID: {
16110b57cec5SDimitry Andric if (&Val2.getSemantics() == &APFloat::IEEEsingle())
16120b57cec5SDimitry Andric return true;
16130b57cec5SDimitry Andric Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
16140b57cec5SDimitry Andric return !losesInfo;
16150b57cec5SDimitry Andric }
16160b57cec5SDimitry Andric case Type::DoubleTyID: {
16170b57cec5SDimitry Andric if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
16185ffd83dbSDimitry Andric &Val2.getSemantics() == &APFloat::BFloat() ||
16190b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEsingle() ||
16200b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEdouble())
16210b57cec5SDimitry Andric return true;
16220b57cec5SDimitry Andric Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
16230b57cec5SDimitry Andric return !losesInfo;
16240b57cec5SDimitry Andric }
16250b57cec5SDimitry Andric case Type::X86_FP80TyID:
16260b57cec5SDimitry Andric return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
16275ffd83dbSDimitry Andric &Val2.getSemantics() == &APFloat::BFloat() ||
16280b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEsingle() ||
16290b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEdouble() ||
16300b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::x87DoubleExtended();
16310b57cec5SDimitry Andric case Type::FP128TyID:
16320b57cec5SDimitry Andric return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
16335ffd83dbSDimitry Andric &Val2.getSemantics() == &APFloat::BFloat() ||
16340b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEsingle() ||
16350b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEdouble() ||
16360b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEquad();
16370b57cec5SDimitry Andric case Type::PPC_FP128TyID:
16380b57cec5SDimitry Andric return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
16395ffd83dbSDimitry Andric &Val2.getSemantics() == &APFloat::BFloat() ||
16400b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEsingle() ||
16410b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::IEEEdouble() ||
16420b57cec5SDimitry Andric &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
16430b57cec5SDimitry Andric }
16440b57cec5SDimitry Andric }
16450b57cec5SDimitry Andric
16460b57cec5SDimitry Andric
16470b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
16480b57cec5SDimitry Andric // Factory Function Implementation
16490b57cec5SDimitry Andric
get(Type * Ty)16500b57cec5SDimitry Andric ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
16510b57cec5SDimitry Andric assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
16520b57cec5SDimitry Andric "Cannot create an aggregate zero of non-aggregate type!");
16530b57cec5SDimitry Andric
16540b57cec5SDimitry Andric std::unique_ptr<ConstantAggregateZero> &Entry =
16550b57cec5SDimitry Andric Ty->getContext().pImpl->CAZConstants[Ty];
16560b57cec5SDimitry Andric if (!Entry)
16570b57cec5SDimitry Andric Entry.reset(new ConstantAggregateZero(Ty));
16580b57cec5SDimitry Andric
16590b57cec5SDimitry Andric return Entry.get();
16600b57cec5SDimitry Andric }
16610b57cec5SDimitry Andric
16620b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()16630b57cec5SDimitry Andric void ConstantAggregateZero::destroyConstantImpl() {
16640b57cec5SDimitry Andric getContext().pImpl->CAZConstants.erase(getType());
16650b57cec5SDimitry Andric }
16660b57cec5SDimitry Andric
16670b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()16680b57cec5SDimitry Andric void ConstantArray::destroyConstantImpl() {
16690b57cec5SDimitry Andric getType()->getContext().pImpl->ArrayConstants.remove(this);
16700b57cec5SDimitry Andric }
16710b57cec5SDimitry Andric
16720b57cec5SDimitry Andric
16730b57cec5SDimitry Andric //---- ConstantStruct::get() implementation...
16740b57cec5SDimitry Andric //
16750b57cec5SDimitry Andric
16760b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()16770b57cec5SDimitry Andric void ConstantStruct::destroyConstantImpl() {
16780b57cec5SDimitry Andric getType()->getContext().pImpl->StructConstants.remove(this);
16790b57cec5SDimitry Andric }
16800b57cec5SDimitry Andric
16810b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()16820b57cec5SDimitry Andric void ConstantVector::destroyConstantImpl() {
16830b57cec5SDimitry Andric getType()->getContext().pImpl->VectorConstants.remove(this);
16840b57cec5SDimitry Andric }
16850b57cec5SDimitry Andric
getSplatValue(bool AllowPoison) const1686*0fca6ea1SDimitry Andric Constant *Constant::getSplatValue(bool AllowPoison) const {
16870b57cec5SDimitry Andric assert(this->getType()->isVectorTy() && "Only valid for vectors!");
16880b57cec5SDimitry Andric if (isa<ConstantAggregateZero>(this))
16895ffd83dbSDimitry Andric return getNullValue(cast<VectorType>(getType())->getElementType());
16900b57cec5SDimitry Andric if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
16910b57cec5SDimitry Andric return CV->getSplatValue();
16920b57cec5SDimitry Andric if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1693*0fca6ea1SDimitry Andric return CV->getSplatValue(AllowPoison);
16945ffd83dbSDimitry Andric
16955ffd83dbSDimitry Andric // Check if this is a constant expression splat of the form returned by
16965ffd83dbSDimitry Andric // ConstantVector::getSplat()
16975ffd83dbSDimitry Andric const auto *Shuf = dyn_cast<ConstantExpr>(this);
16985ffd83dbSDimitry Andric if (Shuf && Shuf->getOpcode() == Instruction::ShuffleVector &&
16995ffd83dbSDimitry Andric isa<UndefValue>(Shuf->getOperand(1))) {
17005ffd83dbSDimitry Andric
17015ffd83dbSDimitry Andric const auto *IElt = dyn_cast<ConstantExpr>(Shuf->getOperand(0));
17025ffd83dbSDimitry Andric if (IElt && IElt->getOpcode() == Instruction::InsertElement &&
17035ffd83dbSDimitry Andric isa<UndefValue>(IElt->getOperand(0))) {
17045ffd83dbSDimitry Andric
17055ffd83dbSDimitry Andric ArrayRef<int> Mask = Shuf->getShuffleMask();
17065ffd83dbSDimitry Andric Constant *SplatVal = IElt->getOperand(1);
17075ffd83dbSDimitry Andric ConstantInt *Index = dyn_cast<ConstantInt>(IElt->getOperand(2));
17085ffd83dbSDimitry Andric
17095ffd83dbSDimitry Andric if (Index && Index->getValue() == 0 &&
1710e8d8bef9SDimitry Andric llvm::all_of(Mask, [](int I) { return I == 0; }))
17115ffd83dbSDimitry Andric return SplatVal;
17125ffd83dbSDimitry Andric }
17135ffd83dbSDimitry Andric }
17145ffd83dbSDimitry Andric
17150b57cec5SDimitry Andric return nullptr;
17160b57cec5SDimitry Andric }
17170b57cec5SDimitry Andric
getSplatValue(bool AllowPoison) const1718*0fca6ea1SDimitry Andric Constant *ConstantVector::getSplatValue(bool AllowPoison) const {
17190b57cec5SDimitry Andric // Check out first element.
17200b57cec5SDimitry Andric Constant *Elt = getOperand(0);
17210b57cec5SDimitry Andric // Then make sure all remaining elements point to the same value.
1722480093f4SDimitry Andric for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1723480093f4SDimitry Andric Constant *OpC = getOperand(I);
1724480093f4SDimitry Andric if (OpC == Elt)
1725480093f4SDimitry Andric continue;
1726480093f4SDimitry Andric
1727480093f4SDimitry Andric // Strict mode: any mismatch is not a splat.
1728*0fca6ea1SDimitry Andric if (!AllowPoison)
17290b57cec5SDimitry Andric return nullptr;
1730480093f4SDimitry Andric
1731*0fca6ea1SDimitry Andric // Allow poison mode: ignore poison elements.
1732*0fca6ea1SDimitry Andric if (isa<PoisonValue>(OpC))
1733480093f4SDimitry Andric continue;
1734480093f4SDimitry Andric
1735480093f4SDimitry Andric // If we do not have a defined element yet, use the current operand.
1736*0fca6ea1SDimitry Andric if (isa<PoisonValue>(Elt))
1737480093f4SDimitry Andric Elt = OpC;
1738480093f4SDimitry Andric
1739480093f4SDimitry Andric if (OpC != Elt)
1740480093f4SDimitry Andric return nullptr;
1741480093f4SDimitry Andric }
17420b57cec5SDimitry Andric return Elt;
17430b57cec5SDimitry Andric }
17440b57cec5SDimitry Andric
getUniqueInteger() const17450b57cec5SDimitry Andric const APInt &Constant::getUniqueInteger() const {
17460b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
17470b57cec5SDimitry Andric return CI->getValue();
1748bdd1243dSDimitry Andric // Scalable vectors can use a ConstantExpr to build a splat.
1749bdd1243dSDimitry Andric if (isa<ConstantExpr>(this))
1750bdd1243dSDimitry Andric return cast<ConstantInt>(this->getSplatValue())->getValue();
1751bdd1243dSDimitry Andric // For non-ConstantExpr we use getAggregateElement as a fast path to avoid
1752bdd1243dSDimitry Andric // calling getSplatValue in release builds.
17530b57cec5SDimitry Andric assert(this->getSplatValue() && "Doesn't contain a unique integer!");
17540b57cec5SDimitry Andric const Constant *C = this->getAggregateElement(0U);
17550b57cec5SDimitry Andric assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
17560b57cec5SDimitry Andric return cast<ConstantInt>(C)->getValue();
17570b57cec5SDimitry Andric }
17580b57cec5SDimitry Andric
toConstantRange() const1759*0fca6ea1SDimitry Andric ConstantRange Constant::toConstantRange() const {
1760*0fca6ea1SDimitry Andric if (auto *CI = dyn_cast<ConstantInt>(this))
1761*0fca6ea1SDimitry Andric return ConstantRange(CI->getValue());
1762*0fca6ea1SDimitry Andric
1763*0fca6ea1SDimitry Andric unsigned BitWidth = getType()->getScalarSizeInBits();
1764*0fca6ea1SDimitry Andric if (!getType()->isVectorTy())
1765*0fca6ea1SDimitry Andric return ConstantRange::getFull(BitWidth);
1766*0fca6ea1SDimitry Andric
1767*0fca6ea1SDimitry Andric if (auto *CI = dyn_cast_or_null<ConstantInt>(
1768*0fca6ea1SDimitry Andric getSplatValue(/*AllowPoison=*/true)))
1769*0fca6ea1SDimitry Andric return ConstantRange(CI->getValue());
1770*0fca6ea1SDimitry Andric
1771*0fca6ea1SDimitry Andric if (auto *CDV = dyn_cast<ConstantDataVector>(this)) {
1772*0fca6ea1SDimitry Andric ConstantRange CR = ConstantRange::getEmpty(BitWidth);
1773*0fca6ea1SDimitry Andric for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I)
1774*0fca6ea1SDimitry Andric CR = CR.unionWith(CDV->getElementAsAPInt(I));
1775*0fca6ea1SDimitry Andric return CR;
1776*0fca6ea1SDimitry Andric }
1777*0fca6ea1SDimitry Andric
1778*0fca6ea1SDimitry Andric if (auto *CV = dyn_cast<ConstantVector>(this)) {
1779*0fca6ea1SDimitry Andric ConstantRange CR = ConstantRange::getEmpty(BitWidth);
1780*0fca6ea1SDimitry Andric for (unsigned I = 0, E = CV->getNumOperands(); I < E; ++I) {
1781*0fca6ea1SDimitry Andric Constant *Elem = CV->getOperand(I);
1782*0fca6ea1SDimitry Andric if (!Elem)
1783*0fca6ea1SDimitry Andric return ConstantRange::getFull(BitWidth);
1784*0fca6ea1SDimitry Andric if (isa<PoisonValue>(Elem))
1785*0fca6ea1SDimitry Andric continue;
1786*0fca6ea1SDimitry Andric auto *CI = dyn_cast<ConstantInt>(Elem);
1787*0fca6ea1SDimitry Andric if (!CI)
1788*0fca6ea1SDimitry Andric return ConstantRange::getFull(BitWidth);
1789*0fca6ea1SDimitry Andric CR = CR.unionWith(CI->getValue());
1790*0fca6ea1SDimitry Andric }
1791*0fca6ea1SDimitry Andric return CR;
1792*0fca6ea1SDimitry Andric }
1793*0fca6ea1SDimitry Andric
1794*0fca6ea1SDimitry Andric return ConstantRange::getFull(BitWidth);
1795*0fca6ea1SDimitry Andric }
1796*0fca6ea1SDimitry Andric
17970b57cec5SDimitry Andric //---- ConstantPointerNull::get() implementation.
17980b57cec5SDimitry Andric //
17990b57cec5SDimitry Andric
get(PointerType * Ty)18000b57cec5SDimitry Andric ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
18010b57cec5SDimitry Andric std::unique_ptr<ConstantPointerNull> &Entry =
18020b57cec5SDimitry Andric Ty->getContext().pImpl->CPNConstants[Ty];
18030b57cec5SDimitry Andric if (!Entry)
18040b57cec5SDimitry Andric Entry.reset(new ConstantPointerNull(Ty));
18050b57cec5SDimitry Andric
18060b57cec5SDimitry Andric return Entry.get();
18070b57cec5SDimitry Andric }
18080b57cec5SDimitry Andric
18090b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()18100b57cec5SDimitry Andric void ConstantPointerNull::destroyConstantImpl() {
18110b57cec5SDimitry Andric getContext().pImpl->CPNConstants.erase(getType());
18120b57cec5SDimitry Andric }
18130b57cec5SDimitry Andric
1814bdd1243dSDimitry Andric //---- ConstantTargetNone::get() implementation.
1815bdd1243dSDimitry Andric //
1816bdd1243dSDimitry Andric
get(TargetExtType * Ty)1817bdd1243dSDimitry Andric ConstantTargetNone *ConstantTargetNone::get(TargetExtType *Ty) {
1818bdd1243dSDimitry Andric assert(Ty->hasProperty(TargetExtType::HasZeroInit) &&
1819bdd1243dSDimitry Andric "Target extension type not allowed to have a zeroinitializer");
1820bdd1243dSDimitry Andric std::unique_ptr<ConstantTargetNone> &Entry =
1821bdd1243dSDimitry Andric Ty->getContext().pImpl->CTNConstants[Ty];
1822bdd1243dSDimitry Andric if (!Entry)
1823bdd1243dSDimitry Andric Entry.reset(new ConstantTargetNone(Ty));
1824bdd1243dSDimitry Andric
1825bdd1243dSDimitry Andric return Entry.get();
1826bdd1243dSDimitry Andric }
1827bdd1243dSDimitry Andric
1828bdd1243dSDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()1829bdd1243dSDimitry Andric void ConstantTargetNone::destroyConstantImpl() {
1830bdd1243dSDimitry Andric getContext().pImpl->CTNConstants.erase(getType());
1831bdd1243dSDimitry Andric }
1832bdd1243dSDimitry Andric
get(Type * Ty)18330b57cec5SDimitry Andric UndefValue *UndefValue::get(Type *Ty) {
18340b57cec5SDimitry Andric std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
18350b57cec5SDimitry Andric if (!Entry)
18360b57cec5SDimitry Andric Entry.reset(new UndefValue(Ty));
18370b57cec5SDimitry Andric
18380b57cec5SDimitry Andric return Entry.get();
18390b57cec5SDimitry Andric }
18400b57cec5SDimitry Andric
18410b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()18420b57cec5SDimitry Andric void UndefValue::destroyConstantImpl() {
18430b57cec5SDimitry Andric // Free the constant and any dangling references to it.
1844e8d8bef9SDimitry Andric if (getValueID() == UndefValueVal) {
18450b57cec5SDimitry Andric getContext().pImpl->UVConstants.erase(getType());
1846e8d8bef9SDimitry Andric } else if (getValueID() == PoisonValueVal) {
1847e8d8bef9SDimitry Andric getContext().pImpl->PVConstants.erase(getType());
1848e8d8bef9SDimitry Andric }
1849e8d8bef9SDimitry Andric llvm_unreachable("Not a undef or a poison!");
1850e8d8bef9SDimitry Andric }
1851e8d8bef9SDimitry Andric
get(Type * Ty)1852e8d8bef9SDimitry Andric PoisonValue *PoisonValue::get(Type *Ty) {
1853e8d8bef9SDimitry Andric std::unique_ptr<PoisonValue> &Entry = Ty->getContext().pImpl->PVConstants[Ty];
1854e8d8bef9SDimitry Andric if (!Entry)
1855e8d8bef9SDimitry Andric Entry.reset(new PoisonValue(Ty));
1856e8d8bef9SDimitry Andric
1857e8d8bef9SDimitry Andric return Entry.get();
1858e8d8bef9SDimitry Andric }
1859e8d8bef9SDimitry Andric
1860e8d8bef9SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()1861e8d8bef9SDimitry Andric void PoisonValue::destroyConstantImpl() {
1862e8d8bef9SDimitry Andric // Free the constant and any dangling references to it.
1863e8d8bef9SDimitry Andric getContext().pImpl->PVConstants.erase(getType());
18640b57cec5SDimitry Andric }
18650b57cec5SDimitry Andric
get(BasicBlock * BB)18660b57cec5SDimitry Andric BlockAddress *BlockAddress::get(BasicBlock *BB) {
18670b57cec5SDimitry Andric assert(BB->getParent() && "Block must have a parent");
18680b57cec5SDimitry Andric return get(BB->getParent(), BB);
18690b57cec5SDimitry Andric }
18700b57cec5SDimitry Andric
get(Function * F,BasicBlock * BB)18710b57cec5SDimitry Andric BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
18720b57cec5SDimitry Andric BlockAddress *&BA =
18730b57cec5SDimitry Andric F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
18740b57cec5SDimitry Andric if (!BA)
18750b57cec5SDimitry Andric BA = new BlockAddress(F, BB);
18760b57cec5SDimitry Andric
18770b57cec5SDimitry Andric assert(BA->getFunction() == F && "Basic block moved between functions");
18780b57cec5SDimitry Andric return BA;
18790b57cec5SDimitry Andric }
18800b57cec5SDimitry Andric
BlockAddress(Function * F,BasicBlock * BB)18810b57cec5SDimitry Andric BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
18825f757f3fSDimitry Andric : Constant(PointerType::get(F->getContext(), F->getAddressSpace()),
1883fe6060f1SDimitry Andric Value::BlockAddressVal, &Op<0>(), 2) {
18840b57cec5SDimitry Andric setOperand(0, F);
18850b57cec5SDimitry Andric setOperand(1, BB);
18860b57cec5SDimitry Andric BB->AdjustBlockAddressRefCount(1);
18870b57cec5SDimitry Andric }
18880b57cec5SDimitry Andric
lookup(const BasicBlock * BB)18890b57cec5SDimitry Andric BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
18900b57cec5SDimitry Andric if (!BB->hasAddressTaken())
18910b57cec5SDimitry Andric return nullptr;
18920b57cec5SDimitry Andric
18930b57cec5SDimitry Andric const Function *F = BB->getParent();
18940b57cec5SDimitry Andric assert(F && "Block must have a parent");
18950b57cec5SDimitry Andric BlockAddress *BA =
18960b57cec5SDimitry Andric F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
18970b57cec5SDimitry Andric assert(BA && "Refcount and block address map disagree!");
18980b57cec5SDimitry Andric return BA;
18990b57cec5SDimitry Andric }
19000b57cec5SDimitry Andric
19010b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()19020b57cec5SDimitry Andric void BlockAddress::destroyConstantImpl() {
19030b57cec5SDimitry Andric getFunction()->getType()->getContext().pImpl
19040b57cec5SDimitry Andric ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
19050b57cec5SDimitry Andric getBasicBlock()->AdjustBlockAddressRefCount(-1);
19060b57cec5SDimitry Andric }
19070b57cec5SDimitry Andric
handleOperandChangeImpl(Value * From,Value * To)19080b57cec5SDimitry Andric Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
19090b57cec5SDimitry Andric // This could be replacing either the Basic Block or the Function. In either
19100b57cec5SDimitry Andric // case, we have to remove the map entry.
19110b57cec5SDimitry Andric Function *NewF = getFunction();
19120b57cec5SDimitry Andric BasicBlock *NewBB = getBasicBlock();
19130b57cec5SDimitry Andric
19140b57cec5SDimitry Andric if (From == NewF)
19150b57cec5SDimitry Andric NewF = cast<Function>(To->stripPointerCasts());
19160b57cec5SDimitry Andric else {
19170b57cec5SDimitry Andric assert(From == NewBB && "From does not match any operand");
19180b57cec5SDimitry Andric NewBB = cast<BasicBlock>(To);
19190b57cec5SDimitry Andric }
19200b57cec5SDimitry Andric
19210b57cec5SDimitry Andric // See if the 'new' entry already exists, if not, just update this in place
19220b57cec5SDimitry Andric // and return early.
19230b57cec5SDimitry Andric BlockAddress *&NewBA =
19240b57cec5SDimitry Andric getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
19250b57cec5SDimitry Andric if (NewBA)
19260b57cec5SDimitry Andric return NewBA;
19270b57cec5SDimitry Andric
19280b57cec5SDimitry Andric getBasicBlock()->AdjustBlockAddressRefCount(-1);
19290b57cec5SDimitry Andric
19300b57cec5SDimitry Andric // Remove the old entry, this can't cause the map to rehash (just a
19310b57cec5SDimitry Andric // tombstone will get added).
19320b57cec5SDimitry Andric getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
19330b57cec5SDimitry Andric getBasicBlock()));
19340b57cec5SDimitry Andric NewBA = this;
19350b57cec5SDimitry Andric setOperand(0, NewF);
19360b57cec5SDimitry Andric setOperand(1, NewBB);
19370b57cec5SDimitry Andric getBasicBlock()->AdjustBlockAddressRefCount(1);
19380b57cec5SDimitry Andric
19390b57cec5SDimitry Andric // If we just want to keep the existing value, then return null.
19400b57cec5SDimitry Andric // Callers know that this means we shouldn't delete this value.
19410b57cec5SDimitry Andric return nullptr;
19420b57cec5SDimitry Andric }
19430b57cec5SDimitry Andric
get(GlobalValue * GV)1944e8d8bef9SDimitry Andric DSOLocalEquivalent *DSOLocalEquivalent::get(GlobalValue *GV) {
1945e8d8bef9SDimitry Andric DSOLocalEquivalent *&Equiv = GV->getContext().pImpl->DSOLocalEquivalents[GV];
1946e8d8bef9SDimitry Andric if (!Equiv)
1947e8d8bef9SDimitry Andric Equiv = new DSOLocalEquivalent(GV);
1948e8d8bef9SDimitry Andric
1949e8d8bef9SDimitry Andric assert(Equiv->getGlobalValue() == GV &&
1950e8d8bef9SDimitry Andric "DSOLocalFunction does not match the expected global value");
1951e8d8bef9SDimitry Andric return Equiv;
1952e8d8bef9SDimitry Andric }
1953e8d8bef9SDimitry Andric
DSOLocalEquivalent(GlobalValue * GV)1954e8d8bef9SDimitry Andric DSOLocalEquivalent::DSOLocalEquivalent(GlobalValue *GV)
1955e8d8bef9SDimitry Andric : Constant(GV->getType(), Value::DSOLocalEquivalentVal, &Op<0>(), 1) {
1956e8d8bef9SDimitry Andric setOperand(0, GV);
1957e8d8bef9SDimitry Andric }
1958e8d8bef9SDimitry Andric
1959e8d8bef9SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()1960e8d8bef9SDimitry Andric void DSOLocalEquivalent::destroyConstantImpl() {
1961e8d8bef9SDimitry Andric const GlobalValue *GV = getGlobalValue();
1962e8d8bef9SDimitry Andric GV->getContext().pImpl->DSOLocalEquivalents.erase(GV);
1963e8d8bef9SDimitry Andric }
1964e8d8bef9SDimitry Andric
handleOperandChangeImpl(Value * From,Value * To)1965e8d8bef9SDimitry Andric Value *DSOLocalEquivalent::handleOperandChangeImpl(Value *From, Value *To) {
1966e8d8bef9SDimitry Andric assert(From == getGlobalValue() && "Changing value does not match operand.");
1967e8d8bef9SDimitry Andric assert(isa<Constant>(To) && "Can only replace the operands with a constant");
1968e8d8bef9SDimitry Andric
1969e8d8bef9SDimitry Andric // The replacement is with another global value.
1970e8d8bef9SDimitry Andric if (const auto *ToObj = dyn_cast<GlobalValue>(To)) {
1971e8d8bef9SDimitry Andric DSOLocalEquivalent *&NewEquiv =
1972e8d8bef9SDimitry Andric getContext().pImpl->DSOLocalEquivalents[ToObj];
1973e8d8bef9SDimitry Andric if (NewEquiv)
1974e8d8bef9SDimitry Andric return llvm::ConstantExpr::getBitCast(NewEquiv, getType());
1975e8d8bef9SDimitry Andric }
1976e8d8bef9SDimitry Andric
1977e8d8bef9SDimitry Andric // If the argument is replaced with a null value, just replace this constant
1978e8d8bef9SDimitry Andric // with a null value.
1979e8d8bef9SDimitry Andric if (cast<Constant>(To)->isNullValue())
1980e8d8bef9SDimitry Andric return To;
1981e8d8bef9SDimitry Andric
1982e8d8bef9SDimitry Andric // The replacement could be a bitcast or an alias to another function. We can
1983e8d8bef9SDimitry Andric // replace it with a bitcast to the dso_local_equivalent of that function.
1984e8d8bef9SDimitry Andric auto *Func = cast<Function>(To->stripPointerCastsAndAliases());
1985e8d8bef9SDimitry Andric DSOLocalEquivalent *&NewEquiv = getContext().pImpl->DSOLocalEquivalents[Func];
1986e8d8bef9SDimitry Andric if (NewEquiv)
1987e8d8bef9SDimitry Andric return llvm::ConstantExpr::getBitCast(NewEquiv, getType());
1988e8d8bef9SDimitry Andric
1989e8d8bef9SDimitry Andric // Replace this with the new one.
1990e8d8bef9SDimitry Andric getContext().pImpl->DSOLocalEquivalents.erase(getGlobalValue());
1991e8d8bef9SDimitry Andric NewEquiv = this;
1992e8d8bef9SDimitry Andric setOperand(0, Func);
1993fe6060f1SDimitry Andric
1994fe6060f1SDimitry Andric if (Func->getType() != getType()) {
1995fe6060f1SDimitry Andric // It is ok to mutate the type here because this constant should always
1996fe6060f1SDimitry Andric // reflect the type of the function it's holding.
1997fe6060f1SDimitry Andric mutateType(Func->getType());
1998fe6060f1SDimitry Andric }
1999e8d8bef9SDimitry Andric return nullptr;
2000e8d8bef9SDimitry Andric }
2001e8d8bef9SDimitry Andric
get(GlobalValue * GV)20020eae32dcSDimitry Andric NoCFIValue *NoCFIValue::get(GlobalValue *GV) {
20030eae32dcSDimitry Andric NoCFIValue *&NC = GV->getContext().pImpl->NoCFIValues[GV];
20040eae32dcSDimitry Andric if (!NC)
20050eae32dcSDimitry Andric NC = new NoCFIValue(GV);
20060eae32dcSDimitry Andric
20070eae32dcSDimitry Andric assert(NC->getGlobalValue() == GV &&
20080eae32dcSDimitry Andric "NoCFIValue does not match the expected global value");
20090eae32dcSDimitry Andric return NC;
20100eae32dcSDimitry Andric }
20110eae32dcSDimitry Andric
NoCFIValue(GlobalValue * GV)20120eae32dcSDimitry Andric NoCFIValue::NoCFIValue(GlobalValue *GV)
20130eae32dcSDimitry Andric : Constant(GV->getType(), Value::NoCFIValueVal, &Op<0>(), 1) {
20140eae32dcSDimitry Andric setOperand(0, GV);
20150eae32dcSDimitry Andric }
20160eae32dcSDimitry Andric
20170eae32dcSDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()20180eae32dcSDimitry Andric void NoCFIValue::destroyConstantImpl() {
20190eae32dcSDimitry Andric const GlobalValue *GV = getGlobalValue();
20200eae32dcSDimitry Andric GV->getContext().pImpl->NoCFIValues.erase(GV);
20210eae32dcSDimitry Andric }
20220eae32dcSDimitry Andric
handleOperandChangeImpl(Value * From,Value * To)20230eae32dcSDimitry Andric Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) {
20240eae32dcSDimitry Andric assert(From == getGlobalValue() && "Changing value does not match operand.");
20250eae32dcSDimitry Andric
20260eae32dcSDimitry Andric GlobalValue *GV = dyn_cast<GlobalValue>(To->stripPointerCasts());
20270eae32dcSDimitry Andric assert(GV && "Can only replace the operands with a global value");
20280eae32dcSDimitry Andric
20290eae32dcSDimitry Andric NoCFIValue *&NewNC = getContext().pImpl->NoCFIValues[GV];
20300eae32dcSDimitry Andric if (NewNC)
20310eae32dcSDimitry Andric return llvm::ConstantExpr::getBitCast(NewNC, getType());
20320eae32dcSDimitry Andric
20330eae32dcSDimitry Andric getContext().pImpl->NoCFIValues.erase(getGlobalValue());
20340eae32dcSDimitry Andric NewNC = this;
20350eae32dcSDimitry Andric setOperand(0, GV);
20360eae32dcSDimitry Andric
20370eae32dcSDimitry Andric if (GV->getType() != getType())
20380eae32dcSDimitry Andric mutateType(GV->getType());
20390eae32dcSDimitry Andric
20400eae32dcSDimitry Andric return nullptr;
20410eae32dcSDimitry Andric }
20420eae32dcSDimitry Andric
2043*0fca6ea1SDimitry Andric //---- ConstantPtrAuth::get() implementations.
2044*0fca6ea1SDimitry Andric //
2045*0fca6ea1SDimitry Andric
get(Constant * Ptr,ConstantInt * Key,ConstantInt * Disc,Constant * AddrDisc)2046*0fca6ea1SDimitry Andric ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key,
2047*0fca6ea1SDimitry Andric ConstantInt *Disc, Constant *AddrDisc) {
2048*0fca6ea1SDimitry Andric Constant *ArgVec[] = {Ptr, Key, Disc, AddrDisc};
2049*0fca6ea1SDimitry Andric ConstantPtrAuthKeyType MapKey(ArgVec);
2050*0fca6ea1SDimitry Andric LLVMContextImpl *pImpl = Ptr->getContext().pImpl;
2051*0fca6ea1SDimitry Andric return pImpl->ConstantPtrAuths.getOrCreate(Ptr->getType(), MapKey);
2052*0fca6ea1SDimitry Andric }
2053*0fca6ea1SDimitry Andric
getWithSameSchema(Constant * Pointer) const2054*0fca6ea1SDimitry Andric ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const {
2055*0fca6ea1SDimitry Andric return get(Pointer, getKey(), getDiscriminator(), getAddrDiscriminator());
2056*0fca6ea1SDimitry Andric }
2057*0fca6ea1SDimitry Andric
ConstantPtrAuth(Constant * Ptr,ConstantInt * Key,ConstantInt * Disc,Constant * AddrDisc)2058*0fca6ea1SDimitry Andric ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key,
2059*0fca6ea1SDimitry Andric ConstantInt *Disc, Constant *AddrDisc)
2060*0fca6ea1SDimitry Andric : Constant(Ptr->getType(), Value::ConstantPtrAuthVal, &Op<0>(), 4) {
2061*0fca6ea1SDimitry Andric assert(Ptr->getType()->isPointerTy());
2062*0fca6ea1SDimitry Andric assert(Key->getBitWidth() == 32);
2063*0fca6ea1SDimitry Andric assert(Disc->getBitWidth() == 64);
2064*0fca6ea1SDimitry Andric assert(AddrDisc->getType()->isPointerTy());
2065*0fca6ea1SDimitry Andric setOperand(0, Ptr);
2066*0fca6ea1SDimitry Andric setOperand(1, Key);
2067*0fca6ea1SDimitry Andric setOperand(2, Disc);
2068*0fca6ea1SDimitry Andric setOperand(3, AddrDisc);
2069*0fca6ea1SDimitry Andric }
2070*0fca6ea1SDimitry Andric
2071*0fca6ea1SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()2072*0fca6ea1SDimitry Andric void ConstantPtrAuth::destroyConstantImpl() {
2073*0fca6ea1SDimitry Andric getType()->getContext().pImpl->ConstantPtrAuths.remove(this);
2074*0fca6ea1SDimitry Andric }
2075*0fca6ea1SDimitry Andric
handleOperandChangeImpl(Value * From,Value * ToV)2076*0fca6ea1SDimitry Andric Value *ConstantPtrAuth::handleOperandChangeImpl(Value *From, Value *ToV) {
2077*0fca6ea1SDimitry Andric assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2078*0fca6ea1SDimitry Andric Constant *To = cast<Constant>(ToV);
2079*0fca6ea1SDimitry Andric
2080*0fca6ea1SDimitry Andric SmallVector<Constant *, 4> Values;
2081*0fca6ea1SDimitry Andric Values.reserve(getNumOperands());
2082*0fca6ea1SDimitry Andric
2083*0fca6ea1SDimitry Andric unsigned NumUpdated = 0;
2084*0fca6ea1SDimitry Andric
2085*0fca6ea1SDimitry Andric Use *OperandList = getOperandList();
2086*0fca6ea1SDimitry Andric unsigned OperandNo = 0;
2087*0fca6ea1SDimitry Andric for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2088*0fca6ea1SDimitry Andric Constant *Val = cast<Constant>(O->get());
2089*0fca6ea1SDimitry Andric if (Val == From) {
2090*0fca6ea1SDimitry Andric OperandNo = (O - OperandList);
2091*0fca6ea1SDimitry Andric Val = To;
2092*0fca6ea1SDimitry Andric ++NumUpdated;
2093*0fca6ea1SDimitry Andric }
2094*0fca6ea1SDimitry Andric Values.push_back(Val);
2095*0fca6ea1SDimitry Andric }
2096*0fca6ea1SDimitry Andric
2097*0fca6ea1SDimitry Andric return getContext().pImpl->ConstantPtrAuths.replaceOperandsInPlace(
2098*0fca6ea1SDimitry Andric Values, this, From, To, NumUpdated, OperandNo);
2099*0fca6ea1SDimitry Andric }
2100*0fca6ea1SDimitry Andric
isKnownCompatibleWith(const Value * Key,const Value * Discriminator,const DataLayout & DL) const2101*0fca6ea1SDimitry Andric bool ConstantPtrAuth::isKnownCompatibleWith(const Value *Key,
2102*0fca6ea1SDimitry Andric const Value *Discriminator,
2103*0fca6ea1SDimitry Andric const DataLayout &DL) const {
2104*0fca6ea1SDimitry Andric // If the keys are different, there's no chance for this to be compatible.
2105*0fca6ea1SDimitry Andric if (getKey() != Key)
2106*0fca6ea1SDimitry Andric return false;
2107*0fca6ea1SDimitry Andric
2108*0fca6ea1SDimitry Andric // We can have 3 kinds of discriminators:
2109*0fca6ea1SDimitry Andric // - simple, integer-only: `i64 x, ptr null` vs. `i64 x`
2110*0fca6ea1SDimitry Andric // - address-only: `i64 0, ptr p` vs. `ptr p`
2111*0fca6ea1SDimitry Andric // - blended address/integer: `i64 x, ptr p` vs. `@llvm.ptrauth.blend(p, x)`
2112*0fca6ea1SDimitry Andric
2113*0fca6ea1SDimitry Andric // If this constant has a simple discriminator (integer, no address), easy:
2114*0fca6ea1SDimitry Andric // it's compatible iff the provided full discriminator is also a simple
2115*0fca6ea1SDimitry Andric // discriminator, identical to our integer discriminator.
2116*0fca6ea1SDimitry Andric if (!hasAddressDiscriminator())
2117*0fca6ea1SDimitry Andric return getDiscriminator() == Discriminator;
2118*0fca6ea1SDimitry Andric
2119*0fca6ea1SDimitry Andric // Otherwise, we can isolate address and integer discriminator components.
2120*0fca6ea1SDimitry Andric const Value *AddrDiscriminator = nullptr;
2121*0fca6ea1SDimitry Andric
2122*0fca6ea1SDimitry Andric // This constant may or may not have an integer discriminator (instead of 0).
2123*0fca6ea1SDimitry Andric if (!getDiscriminator()->isNullValue()) {
2124*0fca6ea1SDimitry Andric // If it does, there's an implicit blend. We need to have a matching blend
2125*0fca6ea1SDimitry Andric // intrinsic in the provided full discriminator.
2126*0fca6ea1SDimitry Andric if (!match(Discriminator,
2127*0fca6ea1SDimitry Andric m_Intrinsic<Intrinsic::ptrauth_blend>(
2128*0fca6ea1SDimitry Andric m_Value(AddrDiscriminator), m_Specific(getDiscriminator()))))
2129*0fca6ea1SDimitry Andric return false;
2130*0fca6ea1SDimitry Andric } else {
2131*0fca6ea1SDimitry Andric // Otherwise, interpret the provided full discriminator as address-only.
2132*0fca6ea1SDimitry Andric AddrDiscriminator = Discriminator;
2133*0fca6ea1SDimitry Andric }
2134*0fca6ea1SDimitry Andric
2135*0fca6ea1SDimitry Andric // Either way, we can now focus on comparing the address discriminators.
2136*0fca6ea1SDimitry Andric
2137*0fca6ea1SDimitry Andric // Discriminators are i64, so the provided addr disc may be a ptrtoint.
2138*0fca6ea1SDimitry Andric if (auto *Cast = dyn_cast<PtrToIntOperator>(AddrDiscriminator))
2139*0fca6ea1SDimitry Andric AddrDiscriminator = Cast->getPointerOperand();
2140*0fca6ea1SDimitry Andric
2141*0fca6ea1SDimitry Andric // Beyond that, we're only interested in compatible pointers.
2142*0fca6ea1SDimitry Andric if (getAddrDiscriminator()->getType() != AddrDiscriminator->getType())
2143*0fca6ea1SDimitry Andric return false;
2144*0fca6ea1SDimitry Andric
2145*0fca6ea1SDimitry Andric // These are often the same constant GEP, making them trivially equivalent.
2146*0fca6ea1SDimitry Andric if (getAddrDiscriminator() == AddrDiscriminator)
2147*0fca6ea1SDimitry Andric return true;
2148*0fca6ea1SDimitry Andric
2149*0fca6ea1SDimitry Andric // Finally, they may be equivalent base+offset expressions.
2150*0fca6ea1SDimitry Andric APInt Off1(DL.getIndexTypeSizeInBits(getAddrDiscriminator()->getType()), 0);
2151*0fca6ea1SDimitry Andric auto *Base1 = getAddrDiscriminator()->stripAndAccumulateConstantOffsets(
2152*0fca6ea1SDimitry Andric DL, Off1, /*AllowNonInbounds=*/true);
2153*0fca6ea1SDimitry Andric
2154*0fca6ea1SDimitry Andric APInt Off2(DL.getIndexTypeSizeInBits(AddrDiscriminator->getType()), 0);
2155*0fca6ea1SDimitry Andric auto *Base2 = AddrDiscriminator->stripAndAccumulateConstantOffsets(
2156*0fca6ea1SDimitry Andric DL, Off2, /*AllowNonInbounds=*/true);
2157*0fca6ea1SDimitry Andric
2158*0fca6ea1SDimitry Andric return Base1 == Base2 && Off1 == Off2;
2159*0fca6ea1SDimitry Andric }
2160*0fca6ea1SDimitry Andric
21610b57cec5SDimitry Andric //---- ConstantExpr::get() implementations.
21620b57cec5SDimitry Andric //
21630b57cec5SDimitry Andric
21640b57cec5SDimitry Andric /// This is a utility function to handle folding of casts and lookup of the
21650b57cec5SDimitry Andric /// cast in the ExprConstants map. It is used by the various get* methods below.
getFoldedCast(Instruction::CastOps opc,Constant * C,Type * Ty,bool OnlyIfReduced=false)21660b57cec5SDimitry Andric static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
21670b57cec5SDimitry Andric bool OnlyIfReduced = false) {
21680b57cec5SDimitry Andric assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
21690b57cec5SDimitry Andric // Fold a few common cases
21700b57cec5SDimitry Andric if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
21710b57cec5SDimitry Andric return FC;
21720b57cec5SDimitry Andric
21730b57cec5SDimitry Andric if (OnlyIfReduced)
21740b57cec5SDimitry Andric return nullptr;
21750b57cec5SDimitry Andric
21760b57cec5SDimitry Andric LLVMContextImpl *pImpl = Ty->getContext().pImpl;
21770b57cec5SDimitry Andric
21780b57cec5SDimitry Andric // Look up the constant in the table first to ensure uniqueness.
21790b57cec5SDimitry Andric ConstantExprKeyType Key(opc, C);
21800b57cec5SDimitry Andric
21810b57cec5SDimitry Andric return pImpl->ExprConstants.getOrCreate(Ty, Key);
21820b57cec5SDimitry Andric }
21830b57cec5SDimitry Andric
getCast(unsigned oc,Constant * C,Type * Ty,bool OnlyIfReduced)21840b57cec5SDimitry Andric Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
21850b57cec5SDimitry Andric bool OnlyIfReduced) {
21860b57cec5SDimitry Andric Instruction::CastOps opc = Instruction::CastOps(oc);
21870b57cec5SDimitry Andric assert(Instruction::isCast(opc) && "opcode out of range");
21885f757f3fSDimitry Andric assert(isSupportedCastOp(opc) &&
21895f757f3fSDimitry Andric "Cast opcode not supported as constant expression");
21900b57cec5SDimitry Andric assert(C && Ty && "Null arguments to getCast");
21910b57cec5SDimitry Andric assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
21920b57cec5SDimitry Andric
21930b57cec5SDimitry Andric switch (opc) {
21940b57cec5SDimitry Andric default:
21950b57cec5SDimitry Andric llvm_unreachable("Invalid cast opcode");
21960b57cec5SDimitry Andric case Instruction::Trunc:
21970b57cec5SDimitry Andric return getTrunc(C, Ty, OnlyIfReduced);
21980b57cec5SDimitry Andric case Instruction::PtrToInt:
21990b57cec5SDimitry Andric return getPtrToInt(C, Ty, OnlyIfReduced);
22000b57cec5SDimitry Andric case Instruction::IntToPtr:
22010b57cec5SDimitry Andric return getIntToPtr(C, Ty, OnlyIfReduced);
22020b57cec5SDimitry Andric case Instruction::BitCast:
22030b57cec5SDimitry Andric return getBitCast(C, Ty, OnlyIfReduced);
22040b57cec5SDimitry Andric case Instruction::AddrSpaceCast:
22050b57cec5SDimitry Andric return getAddrSpaceCast(C, Ty, OnlyIfReduced);
22060b57cec5SDimitry Andric }
22070b57cec5SDimitry Andric }
22080b57cec5SDimitry Andric
getTruncOrBitCast(Constant * C,Type * Ty)22090b57cec5SDimitry Andric Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
22100b57cec5SDimitry Andric if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
22110b57cec5SDimitry Andric return getBitCast(C, Ty);
22120b57cec5SDimitry Andric return getTrunc(C, Ty);
22130b57cec5SDimitry Andric }
22140b57cec5SDimitry Andric
getPointerCast(Constant * S,Type * Ty)22150b57cec5SDimitry Andric Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
22160b57cec5SDimitry Andric assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
22170b57cec5SDimitry Andric assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
22180b57cec5SDimitry Andric "Invalid cast");
22190b57cec5SDimitry Andric
22200b57cec5SDimitry Andric if (Ty->isIntOrIntVectorTy())
22210b57cec5SDimitry Andric return getPtrToInt(S, Ty);
22220b57cec5SDimitry Andric
22230b57cec5SDimitry Andric unsigned SrcAS = S->getType()->getPointerAddressSpace();
22240b57cec5SDimitry Andric if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
22250b57cec5SDimitry Andric return getAddrSpaceCast(S, Ty);
22260b57cec5SDimitry Andric
22270b57cec5SDimitry Andric return getBitCast(S, Ty);
22280b57cec5SDimitry Andric }
22290b57cec5SDimitry Andric
getPointerBitCastOrAddrSpaceCast(Constant * S,Type * Ty)22300b57cec5SDimitry Andric Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
22310b57cec5SDimitry Andric Type *Ty) {
22320b57cec5SDimitry Andric assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
22330b57cec5SDimitry Andric assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
22340b57cec5SDimitry Andric
22350b57cec5SDimitry Andric if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
22360b57cec5SDimitry Andric return getAddrSpaceCast(S, Ty);
22370b57cec5SDimitry Andric
22380b57cec5SDimitry Andric return getBitCast(S, Ty);
22390b57cec5SDimitry Andric }
22400b57cec5SDimitry Andric
getTrunc(Constant * C,Type * Ty,bool OnlyIfReduced)22410b57cec5SDimitry Andric Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
22420b57cec5SDimitry Andric #ifndef NDEBUG
22435ffd83dbSDimitry Andric bool fromVec = isa<VectorType>(C->getType());
22445ffd83dbSDimitry Andric bool toVec = isa<VectorType>(Ty);
22450b57cec5SDimitry Andric #endif
22460b57cec5SDimitry Andric assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
22470b57cec5SDimitry Andric assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
22480b57cec5SDimitry Andric assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
22490b57cec5SDimitry Andric assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
22500b57cec5SDimitry Andric "SrcTy must be larger than DestTy for Trunc!");
22510b57cec5SDimitry Andric
22520b57cec5SDimitry Andric return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
22530b57cec5SDimitry Andric }
22540b57cec5SDimitry Andric
getPtrToInt(Constant * C,Type * DstTy,bool OnlyIfReduced)22550b57cec5SDimitry Andric Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
22560b57cec5SDimitry Andric bool OnlyIfReduced) {
22570b57cec5SDimitry Andric assert(C->getType()->isPtrOrPtrVectorTy() &&
22580b57cec5SDimitry Andric "PtrToInt source must be pointer or pointer vector");
22590b57cec5SDimitry Andric assert(DstTy->isIntOrIntVectorTy() &&
22600b57cec5SDimitry Andric "PtrToInt destination must be integer or integer vector");
22610b57cec5SDimitry Andric assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
22620b57cec5SDimitry Andric if (isa<VectorType>(C->getType()))
226381ad6265SDimitry Andric assert(cast<VectorType>(C->getType())->getElementCount() ==
226481ad6265SDimitry Andric cast<VectorType>(DstTy)->getElementCount() &&
22650b57cec5SDimitry Andric "Invalid cast between a different number of vector elements");
22660b57cec5SDimitry Andric return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
22670b57cec5SDimitry Andric }
22680b57cec5SDimitry Andric
getIntToPtr(Constant * C,Type * DstTy,bool OnlyIfReduced)22690b57cec5SDimitry Andric Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
22700b57cec5SDimitry Andric bool OnlyIfReduced) {
22710b57cec5SDimitry Andric assert(C->getType()->isIntOrIntVectorTy() &&
22720b57cec5SDimitry Andric "IntToPtr source must be integer or integer vector");
22730b57cec5SDimitry Andric assert(DstTy->isPtrOrPtrVectorTy() &&
22740b57cec5SDimitry Andric "IntToPtr destination must be a pointer or pointer vector");
22750b57cec5SDimitry Andric assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
22760b57cec5SDimitry Andric if (isa<VectorType>(C->getType()))
2277e8d8bef9SDimitry Andric assert(cast<VectorType>(C->getType())->getElementCount() ==
2278e8d8bef9SDimitry Andric cast<VectorType>(DstTy)->getElementCount() &&
22790b57cec5SDimitry Andric "Invalid cast between a different number of vector elements");
22800b57cec5SDimitry Andric return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
22810b57cec5SDimitry Andric }
22820b57cec5SDimitry Andric
getBitCast(Constant * C,Type * DstTy,bool OnlyIfReduced)22830b57cec5SDimitry Andric Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
22840b57cec5SDimitry Andric bool OnlyIfReduced) {
22850b57cec5SDimitry Andric assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
22860b57cec5SDimitry Andric "Invalid constantexpr bitcast!");
22870b57cec5SDimitry Andric
22880b57cec5SDimitry Andric // It is common to ask for a bitcast of a value to its own type, handle this
22890b57cec5SDimitry Andric // speedily.
22900b57cec5SDimitry Andric if (C->getType() == DstTy) return C;
22910b57cec5SDimitry Andric
22920b57cec5SDimitry Andric return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
22930b57cec5SDimitry Andric }
22940b57cec5SDimitry Andric
getAddrSpaceCast(Constant * C,Type * DstTy,bool OnlyIfReduced)22950b57cec5SDimitry Andric Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
22960b57cec5SDimitry Andric bool OnlyIfReduced) {
22970b57cec5SDimitry Andric assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
22980b57cec5SDimitry Andric "Invalid constantexpr addrspacecast!");
22990b57cec5SDimitry Andric return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
23000b57cec5SDimitry Andric }
23010b57cec5SDimitry Andric
get(unsigned Opcode,Constant * C1,Constant * C2,unsigned Flags,Type * OnlyIfReducedTy)23020b57cec5SDimitry Andric Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
23030b57cec5SDimitry Andric unsigned Flags, Type *OnlyIfReducedTy) {
23040b57cec5SDimitry Andric // Check the operands for consistency first.
23050b57cec5SDimitry Andric assert(Instruction::isBinaryOp(Opcode) &&
23060b57cec5SDimitry Andric "Invalid opcode in binary constant expression");
2307753f127fSDimitry Andric assert(isSupportedBinOp(Opcode) &&
2308753f127fSDimitry Andric "Binop not supported as constant expression");
23090b57cec5SDimitry Andric assert(C1->getType() == C2->getType() &&
23100b57cec5SDimitry Andric "Operand types in binary constant expression should match");
23110b57cec5SDimitry Andric
23120b57cec5SDimitry Andric #ifndef NDEBUG
23130b57cec5SDimitry Andric switch (Opcode) {
23140b57cec5SDimitry Andric case Instruction::Add:
23150b57cec5SDimitry Andric case Instruction::Sub:
23160b57cec5SDimitry Andric case Instruction::Mul:
23170b57cec5SDimitry Andric assert(C1->getType()->isIntOrIntVectorTy() &&
23180b57cec5SDimitry Andric "Tried to create an integer operation on a non-integer type!");
23190b57cec5SDimitry Andric break;
23200b57cec5SDimitry Andric case Instruction::And:
23210b57cec5SDimitry Andric case Instruction::Or:
23220b57cec5SDimitry Andric case Instruction::Xor:
23230b57cec5SDimitry Andric assert(C1->getType()->isIntOrIntVectorTy() &&
23240b57cec5SDimitry Andric "Tried to create a logical operation on a non-integral type!");
23250b57cec5SDimitry Andric break;
23260b57cec5SDimitry Andric default:
23270b57cec5SDimitry Andric break;
23280b57cec5SDimitry Andric }
23290b57cec5SDimitry Andric #endif
23300b57cec5SDimitry Andric
23310b57cec5SDimitry Andric if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
23320b57cec5SDimitry Andric return FC;
23330b57cec5SDimitry Andric
23340b57cec5SDimitry Andric if (OnlyIfReducedTy == C1->getType())
23350b57cec5SDimitry Andric return nullptr;
23360b57cec5SDimitry Andric
23370b57cec5SDimitry Andric Constant *ArgVec[] = {C1, C2};
2338*0fca6ea1SDimitry Andric ConstantExprKeyType Key(Opcode, ArgVec, Flags);
23390b57cec5SDimitry Andric
23400b57cec5SDimitry Andric LLVMContextImpl *pImpl = C1->getContext().pImpl;
23410b57cec5SDimitry Andric return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
23420b57cec5SDimitry Andric }
23430b57cec5SDimitry Andric
isDesirableBinOp(unsigned Opcode)2344753f127fSDimitry Andric bool ConstantExpr::isDesirableBinOp(unsigned Opcode) {
2345753f127fSDimitry Andric switch (Opcode) {
2346753f127fSDimitry Andric case Instruction::UDiv:
2347753f127fSDimitry Andric case Instruction::SDiv:
2348753f127fSDimitry Andric case Instruction::URem:
2349753f127fSDimitry Andric case Instruction::SRem:
2350753f127fSDimitry Andric case Instruction::FAdd:
2351753f127fSDimitry Andric case Instruction::FSub:
2352753f127fSDimitry Andric case Instruction::FMul:
2353753f127fSDimitry Andric case Instruction::FDiv:
2354753f127fSDimitry Andric case Instruction::FRem:
23555f757f3fSDimitry Andric case Instruction::And:
23565f757f3fSDimitry Andric case Instruction::Or:
23575f757f3fSDimitry Andric case Instruction::LShr:
23585f757f3fSDimitry Andric case Instruction::AShr:
2359*0fca6ea1SDimitry Andric case Instruction::Shl:
2360753f127fSDimitry Andric return false;
2361753f127fSDimitry Andric case Instruction::Add:
2362753f127fSDimitry Andric case Instruction::Sub:
2363753f127fSDimitry Andric case Instruction::Mul:
2364753f127fSDimitry Andric case Instruction::Xor:
2365753f127fSDimitry Andric return true;
2366753f127fSDimitry Andric default:
2367753f127fSDimitry Andric llvm_unreachable("Argument must be binop opcode");
2368753f127fSDimitry Andric }
2369753f127fSDimitry Andric }
2370753f127fSDimitry Andric
isSupportedBinOp(unsigned Opcode)2371753f127fSDimitry Andric bool ConstantExpr::isSupportedBinOp(unsigned Opcode) {
2372753f127fSDimitry Andric switch (Opcode) {
2373753f127fSDimitry Andric case Instruction::UDiv:
2374753f127fSDimitry Andric case Instruction::SDiv:
2375753f127fSDimitry Andric case Instruction::URem:
2376753f127fSDimitry Andric case Instruction::SRem:
2377753f127fSDimitry Andric case Instruction::FAdd:
2378753f127fSDimitry Andric case Instruction::FSub:
2379753f127fSDimitry Andric case Instruction::FMul:
2380753f127fSDimitry Andric case Instruction::FDiv:
2381753f127fSDimitry Andric case Instruction::FRem:
23825f757f3fSDimitry Andric case Instruction::And:
23835f757f3fSDimitry Andric case Instruction::Or:
23845f757f3fSDimitry Andric case Instruction::LShr:
23855f757f3fSDimitry Andric case Instruction::AShr:
2386*0fca6ea1SDimitry Andric case Instruction::Shl:
2387753f127fSDimitry Andric return false;
2388753f127fSDimitry Andric case Instruction::Add:
2389753f127fSDimitry Andric case Instruction::Sub:
2390753f127fSDimitry Andric case Instruction::Mul:
2391753f127fSDimitry Andric case Instruction::Xor:
2392753f127fSDimitry Andric return true;
2393753f127fSDimitry Andric default:
2394753f127fSDimitry Andric llvm_unreachable("Argument must be binop opcode");
2395753f127fSDimitry Andric }
2396753f127fSDimitry Andric }
2397753f127fSDimitry Andric
isDesirableCastOp(unsigned Opcode)23985f757f3fSDimitry Andric bool ConstantExpr::isDesirableCastOp(unsigned Opcode) {
23995f757f3fSDimitry Andric switch (Opcode) {
24005f757f3fSDimitry Andric case Instruction::ZExt:
24015f757f3fSDimitry Andric case Instruction::SExt:
24025f757f3fSDimitry Andric case Instruction::FPTrunc:
24035f757f3fSDimitry Andric case Instruction::FPExt:
24045f757f3fSDimitry Andric case Instruction::UIToFP:
24055f757f3fSDimitry Andric case Instruction::SIToFP:
24065f757f3fSDimitry Andric case Instruction::FPToUI:
24075f757f3fSDimitry Andric case Instruction::FPToSI:
24085f757f3fSDimitry Andric return false;
24095f757f3fSDimitry Andric case Instruction::Trunc:
24105f757f3fSDimitry Andric case Instruction::PtrToInt:
24115f757f3fSDimitry Andric case Instruction::IntToPtr:
24125f757f3fSDimitry Andric case Instruction::BitCast:
24135f757f3fSDimitry Andric case Instruction::AddrSpaceCast:
24145f757f3fSDimitry Andric return true;
24155f757f3fSDimitry Andric default:
24165f757f3fSDimitry Andric llvm_unreachable("Argument must be cast opcode");
24175f757f3fSDimitry Andric }
24185f757f3fSDimitry Andric }
24195f757f3fSDimitry Andric
isSupportedCastOp(unsigned Opcode)24205f757f3fSDimitry Andric bool ConstantExpr::isSupportedCastOp(unsigned Opcode) {
24215f757f3fSDimitry Andric switch (Opcode) {
24225f757f3fSDimitry Andric case Instruction::ZExt:
24235f757f3fSDimitry Andric case Instruction::SExt:
24245f757f3fSDimitry Andric case Instruction::FPTrunc:
24255f757f3fSDimitry Andric case Instruction::FPExt:
24265f757f3fSDimitry Andric case Instruction::UIToFP:
24275f757f3fSDimitry Andric case Instruction::SIToFP:
24285f757f3fSDimitry Andric case Instruction::FPToUI:
24295f757f3fSDimitry Andric case Instruction::FPToSI:
24305f757f3fSDimitry Andric return false;
24315f757f3fSDimitry Andric case Instruction::Trunc:
24325f757f3fSDimitry Andric case Instruction::PtrToInt:
24335f757f3fSDimitry Andric case Instruction::IntToPtr:
24345f757f3fSDimitry Andric case Instruction::BitCast:
24355f757f3fSDimitry Andric case Instruction::AddrSpaceCast:
24365f757f3fSDimitry Andric return true;
24375f757f3fSDimitry Andric default:
24385f757f3fSDimitry Andric llvm_unreachable("Argument must be cast opcode");
24395f757f3fSDimitry Andric }
24405f757f3fSDimitry Andric }
24415f757f3fSDimitry Andric
getSizeOf(Type * Ty)24420b57cec5SDimitry Andric Constant *ConstantExpr::getSizeOf(Type* Ty) {
24430b57cec5SDimitry Andric // sizeof is implemented as: (i64) gep (Ty*)null, 1
24440b57cec5SDimitry Andric // Note that a non-inbounds gep is used, as null isn't within any object.
24450b57cec5SDimitry Andric Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
24460b57cec5SDimitry Andric Constant *GEP = getGetElementPtr(
24470b57cec5SDimitry Andric Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
24480b57cec5SDimitry Andric return getPtrToInt(GEP,
24490b57cec5SDimitry Andric Type::getInt64Ty(Ty->getContext()));
24500b57cec5SDimitry Andric }
24510b57cec5SDimitry Andric
getAlignOf(Type * Ty)24520b57cec5SDimitry Andric Constant *ConstantExpr::getAlignOf(Type* Ty) {
24530b57cec5SDimitry Andric // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
24540b57cec5SDimitry Andric // Note that a non-inbounds gep is used, as null isn't within any object.
24550b57cec5SDimitry Andric Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
2456*0fca6ea1SDimitry Andric Constant *NullPtr =
2457*0fca6ea1SDimitry Andric Constant::getNullValue(PointerType::getUnqual(AligningTy->getContext()));
24580b57cec5SDimitry Andric Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
24590b57cec5SDimitry Andric Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
24600b57cec5SDimitry Andric Constant *Indices[2] = {Zero, One};
24610b57cec5SDimitry Andric Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
2462*0fca6ea1SDimitry Andric return getPtrToInt(GEP, Type::getInt64Ty(Ty->getContext()));
24630b57cec5SDimitry Andric }
24640b57cec5SDimitry Andric
getGetElementPtr(Type * Ty,Constant * C,ArrayRef<Value * > Idxs,GEPNoWrapFlags NW,std::optional<ConstantRange> InRange,Type * OnlyIfReducedTy)24650b57cec5SDimitry Andric Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
2466*0fca6ea1SDimitry Andric ArrayRef<Value *> Idxs,
2467*0fca6ea1SDimitry Andric GEPNoWrapFlags NW,
2468*0fca6ea1SDimitry Andric std::optional<ConstantRange> InRange,
24690b57cec5SDimitry Andric Type *OnlyIfReducedTy) {
2470fe6060f1SDimitry Andric assert(Ty && "Must specify element type");
247106c3fb27SDimitry Andric assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!");
24720b57cec5SDimitry Andric
2473*0fca6ea1SDimitry Andric if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InRange, Idxs))
24740b57cec5SDimitry Andric return FC; // Fold a few common cases.
24750b57cec5SDimitry Andric
2476*0fca6ea1SDimitry Andric assert(GetElementPtrInst::getIndexedType(Ty, Idxs) && "GEP indices invalid!");
2477*0fca6ea1SDimitry Andric ;
247806c3fb27SDimitry Andric
24790b57cec5SDimitry Andric // Get the result type of the getelementptr!
248006c3fb27SDimitry Andric Type *ReqTy = GetElementPtrInst::getGEPReturnType(C, Idxs);
24810b57cec5SDimitry Andric if (OnlyIfReducedTy == ReqTy)
24820b57cec5SDimitry Andric return nullptr;
24830b57cec5SDimitry Andric
248406c3fb27SDimitry Andric auto EltCount = ElementCount::getFixed(0);
248506c3fb27SDimitry Andric if (VectorType *VecTy = dyn_cast<VectorType>(ReqTy))
248606c3fb27SDimitry Andric EltCount = VecTy->getElementCount();
248706c3fb27SDimitry Andric
24880b57cec5SDimitry Andric // Look up the constant in the table first to ensure uniqueness
24890b57cec5SDimitry Andric std::vector<Constant*> ArgVec;
24900b57cec5SDimitry Andric ArgVec.reserve(1 + Idxs.size());
24910b57cec5SDimitry Andric ArgVec.push_back(C);
24925ffd83dbSDimitry Andric auto GTI = gep_type_begin(Ty, Idxs), GTE = gep_type_end(Ty, Idxs);
24935ffd83dbSDimitry Andric for (; GTI != GTE; ++GTI) {
24945ffd83dbSDimitry Andric auto *Idx = cast<Constant>(GTI.getOperand());
24955ffd83dbSDimitry Andric assert(
24965ffd83dbSDimitry Andric (!isa<VectorType>(Idx->getType()) ||
24975ffd83dbSDimitry Andric cast<VectorType>(Idx->getType())->getElementCount() == EltCount) &&
24980b57cec5SDimitry Andric "getelementptr index type missmatch");
24990b57cec5SDimitry Andric
25005ffd83dbSDimitry Andric if (GTI.isStruct() && Idx->getType()->isVectorTy()) {
25015ffd83dbSDimitry Andric Idx = Idx->getSplatValue();
2502e8d8bef9SDimitry Andric } else if (GTI.isSequential() && EltCount.isNonZero() &&
25035ffd83dbSDimitry Andric !Idx->getType()->isVectorTy()) {
25045ffd83dbSDimitry Andric Idx = ConstantVector::getSplat(EltCount, Idx);
25055ffd83dbSDimitry Andric }
25060b57cec5SDimitry Andric ArgVec.push_back(Idx);
25070b57cec5SDimitry Andric }
25080b57cec5SDimitry Andric
2509*0fca6ea1SDimitry Andric const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, NW.getRaw(),
2510*0fca6ea1SDimitry Andric std::nullopt, Ty, InRange);
25110b57cec5SDimitry Andric
25120b57cec5SDimitry Andric LLVMContextImpl *pImpl = C->getContext().pImpl;
25130b57cec5SDimitry Andric return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
25140b57cec5SDimitry Andric }
25150b57cec5SDimitry Andric
getExtractElement(Constant * Val,Constant * Idx,Type * OnlyIfReducedTy)25160b57cec5SDimitry Andric Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
25170b57cec5SDimitry Andric Type *OnlyIfReducedTy) {
25180b57cec5SDimitry Andric assert(Val->getType()->isVectorTy() &&
25190b57cec5SDimitry Andric "Tried to create extractelement operation on non-vector type!");
25200b57cec5SDimitry Andric assert(Idx->getType()->isIntegerTy() &&
25210b57cec5SDimitry Andric "Extractelement index must be an integer type!");
25220b57cec5SDimitry Andric
25230b57cec5SDimitry Andric if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
25240b57cec5SDimitry Andric return FC; // Fold a few common cases.
25250b57cec5SDimitry Andric
25265ffd83dbSDimitry Andric Type *ReqTy = cast<VectorType>(Val->getType())->getElementType();
25270b57cec5SDimitry Andric if (OnlyIfReducedTy == ReqTy)
25280b57cec5SDimitry Andric return nullptr;
25290b57cec5SDimitry Andric
25300b57cec5SDimitry Andric // Look up the constant in the table first to ensure uniqueness
25310b57cec5SDimitry Andric Constant *ArgVec[] = { Val, Idx };
25320b57cec5SDimitry Andric const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
25330b57cec5SDimitry Andric
25340b57cec5SDimitry Andric LLVMContextImpl *pImpl = Val->getContext().pImpl;
25350b57cec5SDimitry Andric return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
25360b57cec5SDimitry Andric }
25370b57cec5SDimitry Andric
getInsertElement(Constant * Val,Constant * Elt,Constant * Idx,Type * OnlyIfReducedTy)25380b57cec5SDimitry Andric Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
25390b57cec5SDimitry Andric Constant *Idx, Type *OnlyIfReducedTy) {
25400b57cec5SDimitry Andric assert(Val->getType()->isVectorTy() &&
25410b57cec5SDimitry Andric "Tried to create insertelement operation on non-vector type!");
25425ffd83dbSDimitry Andric assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() &&
25430b57cec5SDimitry Andric "Insertelement types must match!");
25440b57cec5SDimitry Andric assert(Idx->getType()->isIntegerTy() &&
25450b57cec5SDimitry Andric "Insertelement index must be i32 type!");
25460b57cec5SDimitry Andric
25470b57cec5SDimitry Andric if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
25480b57cec5SDimitry Andric return FC; // Fold a few common cases.
25490b57cec5SDimitry Andric
25500b57cec5SDimitry Andric if (OnlyIfReducedTy == Val->getType())
25510b57cec5SDimitry Andric return nullptr;
25520b57cec5SDimitry Andric
25530b57cec5SDimitry Andric // Look up the constant in the table first to ensure uniqueness
25540b57cec5SDimitry Andric Constant *ArgVec[] = { Val, Elt, Idx };
25550b57cec5SDimitry Andric const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
25560b57cec5SDimitry Andric
25570b57cec5SDimitry Andric LLVMContextImpl *pImpl = Val->getContext().pImpl;
25580b57cec5SDimitry Andric return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
25590b57cec5SDimitry Andric }
25600b57cec5SDimitry Andric
getShuffleVector(Constant * V1,Constant * V2,ArrayRef<int> Mask,Type * OnlyIfReducedTy)25610b57cec5SDimitry Andric Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
25625ffd83dbSDimitry Andric ArrayRef<int> Mask,
25635ffd83dbSDimitry Andric Type *OnlyIfReducedTy) {
25640b57cec5SDimitry Andric assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
25650b57cec5SDimitry Andric "Invalid shuffle vector constant expr operands!");
25660b57cec5SDimitry Andric
25670b57cec5SDimitry Andric if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
25680b57cec5SDimitry Andric return FC; // Fold a few common cases.
25690b57cec5SDimitry Andric
25705ffd83dbSDimitry Andric unsigned NElts = Mask.size();
25715ffd83dbSDimitry Andric auto V1VTy = cast<VectorType>(V1->getType());
25725ffd83dbSDimitry Andric Type *EltTy = V1VTy->getElementType();
25735ffd83dbSDimitry Andric bool TypeIsScalable = isa<ScalableVectorType>(V1VTy);
25745ffd83dbSDimitry Andric Type *ShufTy = VectorType::get(EltTy, NElts, TypeIsScalable);
25750b57cec5SDimitry Andric
25760b57cec5SDimitry Andric if (OnlyIfReducedTy == ShufTy)
25770b57cec5SDimitry Andric return nullptr;
25780b57cec5SDimitry Andric
25790b57cec5SDimitry Andric // Look up the constant in the table first to ensure uniqueness
25805ffd83dbSDimitry Andric Constant *ArgVec[] = {V1, V2};
2581*0fca6ea1SDimitry Andric ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, Mask);
25820b57cec5SDimitry Andric
25830b57cec5SDimitry Andric LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
25840b57cec5SDimitry Andric return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
25850b57cec5SDimitry Andric }
25860b57cec5SDimitry Andric
getNeg(Constant * C,bool HasNSW)2587*0fca6ea1SDimitry Andric Constant *ConstantExpr::getNeg(Constant *C, bool HasNSW) {
25880b57cec5SDimitry Andric assert(C->getType()->isIntOrIntVectorTy() &&
25890b57cec5SDimitry Andric "Cannot NEG a nonintegral value!");
2590*0fca6ea1SDimitry Andric return getSub(ConstantInt::get(C->getType(), 0), C, /*HasNUW=*/false, HasNSW);
25910b57cec5SDimitry Andric }
25920b57cec5SDimitry Andric
getNot(Constant * C)25930b57cec5SDimitry Andric Constant *ConstantExpr::getNot(Constant *C) {
25940b57cec5SDimitry Andric assert(C->getType()->isIntOrIntVectorTy() &&
25950b57cec5SDimitry Andric "Cannot NOT a nonintegral value!");
25960b57cec5SDimitry Andric return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
25970b57cec5SDimitry Andric }
25980b57cec5SDimitry Andric
getAdd(Constant * C1,Constant * C2,bool HasNUW,bool HasNSW)25990b57cec5SDimitry Andric Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
26000b57cec5SDimitry Andric bool HasNUW, bool HasNSW) {
26010b57cec5SDimitry Andric unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
26020b57cec5SDimitry Andric (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
26030b57cec5SDimitry Andric return get(Instruction::Add, C1, C2, Flags);
26040b57cec5SDimitry Andric }
26050b57cec5SDimitry Andric
getSub(Constant * C1,Constant * C2,bool HasNUW,bool HasNSW)26060b57cec5SDimitry Andric Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
26070b57cec5SDimitry Andric bool HasNUW, bool HasNSW) {
26080b57cec5SDimitry Andric unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
26090b57cec5SDimitry Andric (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
26100b57cec5SDimitry Andric return get(Instruction::Sub, C1, C2, Flags);
26110b57cec5SDimitry Andric }
26120b57cec5SDimitry Andric
getMul(Constant * C1,Constant * C2,bool HasNUW,bool HasNSW)26130b57cec5SDimitry Andric Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
26140b57cec5SDimitry Andric bool HasNUW, bool HasNSW) {
26150b57cec5SDimitry Andric unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
26160b57cec5SDimitry Andric (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
26170b57cec5SDimitry Andric return get(Instruction::Mul, C1, C2, Flags);
26180b57cec5SDimitry Andric }
26190b57cec5SDimitry Andric
getXor(Constant * C1,Constant * C2)26200b57cec5SDimitry Andric Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
26210b57cec5SDimitry Andric return get(Instruction::Xor, C1, C2);
26220b57cec5SDimitry Andric }
26230b57cec5SDimitry Andric
getExactLogBase2(Constant * C)2624e8d8bef9SDimitry Andric Constant *ConstantExpr::getExactLogBase2(Constant *C) {
2625e8d8bef9SDimitry Andric Type *Ty = C->getType();
2626e8d8bef9SDimitry Andric const APInt *IVal;
2627e8d8bef9SDimitry Andric if (match(C, m_APInt(IVal)) && IVal->isPowerOf2())
2628e8d8bef9SDimitry Andric return ConstantInt::get(Ty, IVal->logBase2());
2629e8d8bef9SDimitry Andric
2630e8d8bef9SDimitry Andric // FIXME: We can extract pow of 2 of splat constant for scalable vectors.
2631e8d8bef9SDimitry Andric auto *VecTy = dyn_cast<FixedVectorType>(Ty);
2632e8d8bef9SDimitry Andric if (!VecTy)
2633e8d8bef9SDimitry Andric return nullptr;
2634e8d8bef9SDimitry Andric
2635e8d8bef9SDimitry Andric SmallVector<Constant *, 4> Elts;
2636e8d8bef9SDimitry Andric for (unsigned I = 0, E = VecTy->getNumElements(); I != E; ++I) {
2637e8d8bef9SDimitry Andric Constant *Elt = C->getAggregateElement(I);
2638e8d8bef9SDimitry Andric if (!Elt)
2639e8d8bef9SDimitry Andric return nullptr;
2640e8d8bef9SDimitry Andric // Note that log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N.
2641e8d8bef9SDimitry Andric if (isa<UndefValue>(Elt)) {
2642e8d8bef9SDimitry Andric Elts.push_back(Constant::getNullValue(Ty->getScalarType()));
2643e8d8bef9SDimitry Andric continue;
2644e8d8bef9SDimitry Andric }
2645e8d8bef9SDimitry Andric if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
2646e8d8bef9SDimitry Andric return nullptr;
2647e8d8bef9SDimitry Andric Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2()));
2648e8d8bef9SDimitry Andric }
2649e8d8bef9SDimitry Andric
2650e8d8bef9SDimitry Andric return ConstantVector::get(Elts);
2651e8d8bef9SDimitry Andric }
2652e8d8bef9SDimitry Andric
getBinOpIdentity(unsigned Opcode,Type * Ty,bool AllowRHSConstant,bool NSZ)26530b57cec5SDimitry Andric Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
265481ad6265SDimitry Andric bool AllowRHSConstant, bool NSZ) {
26550b57cec5SDimitry Andric assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed");
26560b57cec5SDimitry Andric
26570b57cec5SDimitry Andric // Commutative opcodes: it does not matter if AllowRHSConstant is set.
26580b57cec5SDimitry Andric if (Instruction::isCommutative(Opcode)) {
26590b57cec5SDimitry Andric switch (Opcode) {
26600b57cec5SDimitry Andric case Instruction::Add: // X + 0 = X
26610b57cec5SDimitry Andric case Instruction::Or: // X | 0 = X
26620b57cec5SDimitry Andric case Instruction::Xor: // X ^ 0 = X
26630b57cec5SDimitry Andric return Constant::getNullValue(Ty);
26640b57cec5SDimitry Andric case Instruction::Mul: // X * 1 = X
26650b57cec5SDimitry Andric return ConstantInt::get(Ty, 1);
26660b57cec5SDimitry Andric case Instruction::And: // X & -1 = X
26670b57cec5SDimitry Andric return Constant::getAllOnesValue(Ty);
26680b57cec5SDimitry Andric case Instruction::FAdd: // X + -0.0 = X
266981ad6265SDimitry Andric return ConstantFP::getZero(Ty, !NSZ);
26700b57cec5SDimitry Andric case Instruction::FMul: // X * 1.0 = X
26710b57cec5SDimitry Andric return ConstantFP::get(Ty, 1.0);
26720b57cec5SDimitry Andric default:
26730b57cec5SDimitry Andric llvm_unreachable("Every commutative binop has an identity constant");
26740b57cec5SDimitry Andric }
26750b57cec5SDimitry Andric }
26760b57cec5SDimitry Andric
26770b57cec5SDimitry Andric // Non-commutative opcodes: AllowRHSConstant must be set.
26780b57cec5SDimitry Andric if (!AllowRHSConstant)
26790b57cec5SDimitry Andric return nullptr;
26800b57cec5SDimitry Andric
26810b57cec5SDimitry Andric switch (Opcode) {
26820b57cec5SDimitry Andric case Instruction::Sub: // X - 0 = X
26830b57cec5SDimitry Andric case Instruction::Shl: // X << 0 = X
26840b57cec5SDimitry Andric case Instruction::LShr: // X >>u 0 = X
26850b57cec5SDimitry Andric case Instruction::AShr: // X >> 0 = X
26860b57cec5SDimitry Andric case Instruction::FSub: // X - 0.0 = X
26870b57cec5SDimitry Andric return Constant::getNullValue(Ty);
26880b57cec5SDimitry Andric case Instruction::SDiv: // X / 1 = X
26890b57cec5SDimitry Andric case Instruction::UDiv: // X /u 1 = X
26900b57cec5SDimitry Andric return ConstantInt::get(Ty, 1);
26910b57cec5SDimitry Andric case Instruction::FDiv: // X / 1.0 = X
26920b57cec5SDimitry Andric return ConstantFP::get(Ty, 1.0);
26930b57cec5SDimitry Andric default:
26940b57cec5SDimitry Andric return nullptr;
26950b57cec5SDimitry Andric }
26960b57cec5SDimitry Andric }
26970b57cec5SDimitry Andric
getIntrinsicIdentity(Intrinsic::ID ID,Type * Ty)26985f757f3fSDimitry Andric Constant *ConstantExpr::getIntrinsicIdentity(Intrinsic::ID ID, Type *Ty) {
26995f757f3fSDimitry Andric switch (ID) {
27005f757f3fSDimitry Andric case Intrinsic::umax:
27015f757f3fSDimitry Andric return Constant::getNullValue(Ty);
27025f757f3fSDimitry Andric case Intrinsic::umin:
27035f757f3fSDimitry Andric return Constant::getAllOnesValue(Ty);
27045f757f3fSDimitry Andric case Intrinsic::smax:
27055f757f3fSDimitry Andric return Constant::getIntegerValue(
27065f757f3fSDimitry Andric Ty, APInt::getSignedMinValue(Ty->getIntegerBitWidth()));
27075f757f3fSDimitry Andric case Intrinsic::smin:
27085f757f3fSDimitry Andric return Constant::getIntegerValue(
27095f757f3fSDimitry Andric Ty, APInt::getSignedMaxValue(Ty->getIntegerBitWidth()));
27105f757f3fSDimitry Andric default:
27115f757f3fSDimitry Andric return nullptr;
27125f757f3fSDimitry Andric }
27135f757f3fSDimitry Andric }
27145f757f3fSDimitry Andric
getIdentity(Instruction * I,Type * Ty,bool AllowRHSConstant,bool NSZ)27155f757f3fSDimitry Andric Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty,
27165f757f3fSDimitry Andric bool AllowRHSConstant, bool NSZ) {
27175f757f3fSDimitry Andric if (I->isBinaryOp())
27185f757f3fSDimitry Andric return getBinOpIdentity(I->getOpcode(), Ty, AllowRHSConstant, NSZ);
27195f757f3fSDimitry Andric if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
27205f757f3fSDimitry Andric return getIntrinsicIdentity(II->getIntrinsicID(), Ty);
27215f757f3fSDimitry Andric return nullptr;
27225f757f3fSDimitry Andric }
27235f757f3fSDimitry Andric
getBinOpAbsorber(unsigned Opcode,Type * Ty)27240b57cec5SDimitry Andric Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
27250b57cec5SDimitry Andric switch (Opcode) {
27260b57cec5SDimitry Andric default:
27270b57cec5SDimitry Andric // Doesn't have an absorber.
27280b57cec5SDimitry Andric return nullptr;
27290b57cec5SDimitry Andric
27300b57cec5SDimitry Andric case Instruction::Or:
27310b57cec5SDimitry Andric return Constant::getAllOnesValue(Ty);
27320b57cec5SDimitry Andric
27330b57cec5SDimitry Andric case Instruction::And:
27340b57cec5SDimitry Andric case Instruction::Mul:
27350b57cec5SDimitry Andric return Constant::getNullValue(Ty);
27360b57cec5SDimitry Andric }
27370b57cec5SDimitry Andric }
27380b57cec5SDimitry Andric
27390b57cec5SDimitry Andric /// Remove the constant from the constant table.
destroyConstantImpl()27400b57cec5SDimitry Andric void ConstantExpr::destroyConstantImpl() {
27410b57cec5SDimitry Andric getType()->getContext().pImpl->ExprConstants.remove(this);
27420b57cec5SDimitry Andric }
27430b57cec5SDimitry Andric
getOpcodeName() const27440b57cec5SDimitry Andric const char *ConstantExpr::getOpcodeName() const {
27450b57cec5SDimitry Andric return Instruction::getOpcodeName(getOpcode());
27460b57cec5SDimitry Andric }
27470b57cec5SDimitry Andric
GetElementPtrConstantExpr(Type * SrcElementTy,Constant * C,ArrayRef<Constant * > IdxList,Type * DestTy,std::optional<ConstantRange> InRange)27480b57cec5SDimitry Andric GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2749*0fca6ea1SDimitry Andric Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy,
2750*0fca6ea1SDimitry Andric std::optional<ConstantRange> InRange)
27510b57cec5SDimitry Andric : ConstantExpr(DestTy, Instruction::GetElementPtr,
27520b57cec5SDimitry Andric OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
27530b57cec5SDimitry Andric (IdxList.size() + 1),
27540b57cec5SDimitry Andric IdxList.size() + 1),
27550b57cec5SDimitry Andric SrcElementTy(SrcElementTy),
2756*0fca6ea1SDimitry Andric ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)),
2757*0fca6ea1SDimitry Andric InRange(std::move(InRange)) {
27580b57cec5SDimitry Andric Op<0>() = C;
27590b57cec5SDimitry Andric Use *OperandList = getOperandList();
27600b57cec5SDimitry Andric for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
27610b57cec5SDimitry Andric OperandList[i+1] = IdxList[i];
27620b57cec5SDimitry Andric }
27630b57cec5SDimitry Andric
getSourceElementType() const27640b57cec5SDimitry Andric Type *GetElementPtrConstantExpr::getSourceElementType() const {
27650b57cec5SDimitry Andric return SrcElementTy;
27660b57cec5SDimitry Andric }
27670b57cec5SDimitry Andric
getResultElementType() const27680b57cec5SDimitry Andric Type *GetElementPtrConstantExpr::getResultElementType() const {
27690b57cec5SDimitry Andric return ResElementTy;
27700b57cec5SDimitry Andric }
27710b57cec5SDimitry Andric
getInRange() const2772*0fca6ea1SDimitry Andric std::optional<ConstantRange> GetElementPtrConstantExpr::getInRange() const {
2773*0fca6ea1SDimitry Andric return InRange;
2774*0fca6ea1SDimitry Andric }
2775*0fca6ea1SDimitry Andric
27760b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
27770b57cec5SDimitry Andric // ConstantData* implementations
27780b57cec5SDimitry Andric
getElementType() const27790b57cec5SDimitry Andric Type *ConstantDataSequential::getElementType() const {
27805ffd83dbSDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))
27815ffd83dbSDimitry Andric return ATy->getElementType();
27825ffd83dbSDimitry Andric return cast<VectorType>(getType())->getElementType();
27830b57cec5SDimitry Andric }
27840b57cec5SDimitry Andric
getRawDataValues() const27850b57cec5SDimitry Andric StringRef ConstantDataSequential::getRawDataValues() const {
27860b57cec5SDimitry Andric return StringRef(DataElements, getNumElements()*getElementByteSize());
27870b57cec5SDimitry Andric }
27880b57cec5SDimitry Andric
isElementTypeCompatible(Type * Ty)27890b57cec5SDimitry Andric bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
27905ffd83dbSDimitry Andric if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() || Ty->isDoubleTy())
27915ffd83dbSDimitry Andric return true;
27920b57cec5SDimitry Andric if (auto *IT = dyn_cast<IntegerType>(Ty)) {
27930b57cec5SDimitry Andric switch (IT->getBitWidth()) {
27940b57cec5SDimitry Andric case 8:
27950b57cec5SDimitry Andric case 16:
27960b57cec5SDimitry Andric case 32:
27970b57cec5SDimitry Andric case 64:
27980b57cec5SDimitry Andric return true;
27990b57cec5SDimitry Andric default: break;
28000b57cec5SDimitry Andric }
28010b57cec5SDimitry Andric }
28020b57cec5SDimitry Andric return false;
28030b57cec5SDimitry Andric }
28040b57cec5SDimitry Andric
getNumElements() const28050b57cec5SDimitry Andric unsigned ConstantDataSequential::getNumElements() const {
28060b57cec5SDimitry Andric if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
28070b57cec5SDimitry Andric return AT->getNumElements();
2808e8d8bef9SDimitry Andric return cast<FixedVectorType>(getType())->getNumElements();
28090b57cec5SDimitry Andric }
28100b57cec5SDimitry Andric
28110b57cec5SDimitry Andric
getElementByteSize() const28120b57cec5SDimitry Andric uint64_t ConstantDataSequential::getElementByteSize() const {
28130b57cec5SDimitry Andric return getElementType()->getPrimitiveSizeInBits()/8;
28140b57cec5SDimitry Andric }
28150b57cec5SDimitry Andric
28160b57cec5SDimitry Andric /// Return the start of the specified element.
getElementPointer(unsigned Elt) const28170b57cec5SDimitry Andric const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
28180b57cec5SDimitry Andric assert(Elt < getNumElements() && "Invalid Elt");
28190b57cec5SDimitry Andric return DataElements+Elt*getElementByteSize();
28200b57cec5SDimitry Andric }
28210b57cec5SDimitry Andric
28220b57cec5SDimitry Andric
28230b57cec5SDimitry Andric /// Return true if the array is empty or all zeros.
isAllZeros(StringRef Arr)28240b57cec5SDimitry Andric static bool isAllZeros(StringRef Arr) {
28250b57cec5SDimitry Andric for (char I : Arr)
28260b57cec5SDimitry Andric if (I != 0)
28270b57cec5SDimitry Andric return false;
28280b57cec5SDimitry Andric return true;
28290b57cec5SDimitry Andric }
28300b57cec5SDimitry Andric
28310b57cec5SDimitry Andric /// This is the underlying implementation of all of the
28320b57cec5SDimitry Andric /// ConstantDataSequential::get methods. They all thunk down to here, providing
28330b57cec5SDimitry Andric /// the correct element type. We take the bytes in as a StringRef because
28340b57cec5SDimitry Andric /// we *want* an underlying "char*" to avoid TBAA type punning violations.
getImpl(StringRef Elements,Type * Ty)28350b57cec5SDimitry Andric Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
28365ffd83dbSDimitry Andric #ifndef NDEBUG
28375ffd83dbSDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(Ty))
28385ffd83dbSDimitry Andric assert(isElementTypeCompatible(ATy->getElementType()));
28395ffd83dbSDimitry Andric else
28405ffd83dbSDimitry Andric assert(isElementTypeCompatible(cast<VectorType>(Ty)->getElementType()));
28415ffd83dbSDimitry Andric #endif
28420b57cec5SDimitry Andric // If the elements are all zero or there are no elements, return a CAZ, which
28430b57cec5SDimitry Andric // is more dense and canonical.
28440b57cec5SDimitry Andric if (isAllZeros(Elements))
28450b57cec5SDimitry Andric return ConstantAggregateZero::get(Ty);
28460b57cec5SDimitry Andric
28470b57cec5SDimitry Andric // Do a lookup to see if we have already formed one of these.
28480b57cec5SDimitry Andric auto &Slot =
28490b57cec5SDimitry Andric *Ty->getContext()
28500b57cec5SDimitry Andric .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
28510b57cec5SDimitry Andric .first;
28520b57cec5SDimitry Andric
28530b57cec5SDimitry Andric // The bucket can point to a linked list of different CDS's that have the same
28540b57cec5SDimitry Andric // body but different types. For example, 0,0,0,1 could be a 4 element array
28550b57cec5SDimitry Andric // of i8, or a 1-element array of i32. They'll both end up in the same
28560b57cec5SDimitry Andric /// StringMap bucket, linked up by their Next pointers. Walk the list.
2857e8d8bef9SDimitry Andric std::unique_ptr<ConstantDataSequential> *Entry = &Slot.second;
2858e8d8bef9SDimitry Andric for (; *Entry; Entry = &(*Entry)->Next)
2859e8d8bef9SDimitry Andric if ((*Entry)->getType() == Ty)
2860e8d8bef9SDimitry Andric return Entry->get();
28610b57cec5SDimitry Andric
28620b57cec5SDimitry Andric // Okay, we didn't get a hit. Create a node of the right class, link it in,
28630b57cec5SDimitry Andric // and return it.
2864e8d8bef9SDimitry Andric if (isa<ArrayType>(Ty)) {
2865e8d8bef9SDimitry Andric // Use reset because std::make_unique can't access the constructor.
2866e8d8bef9SDimitry Andric Entry->reset(new ConstantDataArray(Ty, Slot.first().data()));
2867e8d8bef9SDimitry Andric return Entry->get();
2868e8d8bef9SDimitry Andric }
28690b57cec5SDimitry Andric
28700b57cec5SDimitry Andric assert(isa<VectorType>(Ty));
2871e8d8bef9SDimitry Andric // Use reset because std::make_unique can't access the constructor.
2872e8d8bef9SDimitry Andric Entry->reset(new ConstantDataVector(Ty, Slot.first().data()));
2873e8d8bef9SDimitry Andric return Entry->get();
28740b57cec5SDimitry Andric }
28750b57cec5SDimitry Andric
destroyConstantImpl()28760b57cec5SDimitry Andric void ConstantDataSequential::destroyConstantImpl() {
28770b57cec5SDimitry Andric // Remove the constant from the StringMap.
2878e8d8bef9SDimitry Andric StringMap<std::unique_ptr<ConstantDataSequential>> &CDSConstants =
28790b57cec5SDimitry Andric getType()->getContext().pImpl->CDSConstants;
28800b57cec5SDimitry Andric
2881e8d8bef9SDimitry Andric auto Slot = CDSConstants.find(getRawDataValues());
28820b57cec5SDimitry Andric
28830b57cec5SDimitry Andric assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
28840b57cec5SDimitry Andric
2885e8d8bef9SDimitry Andric std::unique_ptr<ConstantDataSequential> *Entry = &Slot->getValue();
28860b57cec5SDimitry Andric
28870b57cec5SDimitry Andric // Remove the entry from the hash table.
28880b57cec5SDimitry Andric if (!(*Entry)->Next) {
28890b57cec5SDimitry Andric // If there is only one value in the bucket (common case) it must be this
28900b57cec5SDimitry Andric // entry, and removing the entry should remove the bucket completely.
2891e8d8bef9SDimitry Andric assert(Entry->get() == this && "Hash mismatch in ConstantDataSequential");
28920b57cec5SDimitry Andric getContext().pImpl->CDSConstants.erase(Slot);
2893e8d8bef9SDimitry Andric return;
28940b57cec5SDimitry Andric }
28950b57cec5SDimitry Andric
2896e8d8bef9SDimitry Andric // Otherwise, there are multiple entries linked off the bucket, unlink the
2897e8d8bef9SDimitry Andric // node we care about but keep the bucket around.
2898e8d8bef9SDimitry Andric while (true) {
2899e8d8bef9SDimitry Andric std::unique_ptr<ConstantDataSequential> &Node = *Entry;
2900e8d8bef9SDimitry Andric assert(Node && "Didn't find entry in its uniquing hash table!");
2901e8d8bef9SDimitry Andric // If we found our entry, unlink it from the list and we're done.
2902e8d8bef9SDimitry Andric if (Node.get() == this) {
2903e8d8bef9SDimitry Andric Node = std::move(Node->Next);
2904e8d8bef9SDimitry Andric return;
2905e8d8bef9SDimitry Andric }
2906e8d8bef9SDimitry Andric
2907e8d8bef9SDimitry Andric Entry = &Node->Next;
2908e8d8bef9SDimitry Andric }
29090b57cec5SDimitry Andric }
29100b57cec5SDimitry Andric
29115ffd83dbSDimitry Andric /// getFP() constructors - Return a constant of array type with a float
29125ffd83dbSDimitry Andric /// element type taken from argument `ElementType', and count taken from
29135ffd83dbSDimitry Andric /// argument `Elts'. The amount of bits of the contained type must match the
29145ffd83dbSDimitry Andric /// number of bits of the type contained in the passed in ArrayRef.
29155ffd83dbSDimitry Andric /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
29165ffd83dbSDimitry Andric /// that this can return a ConstantAggregateZero object.
getFP(Type * ElementType,ArrayRef<uint16_t> Elts)29175ffd83dbSDimitry Andric Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint16_t> Elts) {
29185ffd83dbSDimitry Andric assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) &&
29195ffd83dbSDimitry Andric "Element type is not a 16-bit float type");
29205ffd83dbSDimitry Andric Type *Ty = ArrayType::get(ElementType, Elts.size());
29210b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29220b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 2), Ty);
29230b57cec5SDimitry Andric }
getFP(Type * ElementType,ArrayRef<uint32_t> Elts)29245ffd83dbSDimitry Andric Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint32_t> Elts) {
29255ffd83dbSDimitry Andric assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type");
29265ffd83dbSDimitry Andric Type *Ty = ArrayType::get(ElementType, Elts.size());
29270b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29280b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 4), Ty);
29290b57cec5SDimitry Andric }
getFP(Type * ElementType,ArrayRef<uint64_t> Elts)29305ffd83dbSDimitry Andric Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint64_t> Elts) {
29315ffd83dbSDimitry Andric assert(ElementType->isDoubleTy() &&
29325ffd83dbSDimitry Andric "Element type is not a 64-bit float type");
29335ffd83dbSDimitry Andric Type *Ty = ArrayType::get(ElementType, Elts.size());
29340b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29350b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 8), Ty);
29360b57cec5SDimitry Andric }
29370b57cec5SDimitry Andric
getString(LLVMContext & Context,StringRef Str,bool AddNull)29380b57cec5SDimitry Andric Constant *ConstantDataArray::getString(LLVMContext &Context,
29390b57cec5SDimitry Andric StringRef Str, bool AddNull) {
29400b57cec5SDimitry Andric if (!AddNull) {
29410b57cec5SDimitry Andric const uint8_t *Data = Str.bytes_begin();
2942bdd1243dSDimitry Andric return get(Context, ArrayRef(Data, Str.size()));
29430b57cec5SDimitry Andric }
29440b57cec5SDimitry Andric
29450b57cec5SDimitry Andric SmallVector<uint8_t, 64> ElementVals;
29460b57cec5SDimitry Andric ElementVals.append(Str.begin(), Str.end());
29470b57cec5SDimitry Andric ElementVals.push_back(0);
29480b57cec5SDimitry Andric return get(Context, ElementVals);
29490b57cec5SDimitry Andric }
29500b57cec5SDimitry Andric
29510b57cec5SDimitry Andric /// get() constructors - Return a constant with vector type with an element
29520b57cec5SDimitry Andric /// count and element type matching the ArrayRef passed in. Note that this
29530b57cec5SDimitry Andric /// can return a ConstantAggregateZero object.
get(LLVMContext & Context,ArrayRef<uint8_t> Elts)29540b57cec5SDimitry Andric Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
29555ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(Type::getInt8Ty(Context), Elts.size());
29560b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29570b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 1), Ty);
29580b57cec5SDimitry Andric }
get(LLVMContext & Context,ArrayRef<uint16_t> Elts)29590b57cec5SDimitry Andric Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
29605ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(Type::getInt16Ty(Context), Elts.size());
29610b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29620b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 2), Ty);
29630b57cec5SDimitry Andric }
get(LLVMContext & Context,ArrayRef<uint32_t> Elts)29640b57cec5SDimitry Andric Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
29655ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(Type::getInt32Ty(Context), Elts.size());
29660b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29670b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 4), Ty);
29680b57cec5SDimitry Andric }
get(LLVMContext & Context,ArrayRef<uint64_t> Elts)29690b57cec5SDimitry Andric Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
29705ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(Type::getInt64Ty(Context), Elts.size());
29710b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29720b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 8), Ty);
29730b57cec5SDimitry Andric }
get(LLVMContext & Context,ArrayRef<float> Elts)29740b57cec5SDimitry Andric Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
29755ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(Type::getFloatTy(Context), Elts.size());
29760b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29770b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 4), Ty);
29780b57cec5SDimitry Andric }
get(LLVMContext & Context,ArrayRef<double> Elts)29790b57cec5SDimitry Andric Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
29805ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(Type::getDoubleTy(Context), Elts.size());
29810b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29820b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 8), Ty);
29830b57cec5SDimitry Andric }
29840b57cec5SDimitry Andric
29855ffd83dbSDimitry Andric /// getFP() constructors - Return a constant of vector type with a float
29865ffd83dbSDimitry Andric /// element type taken from argument `ElementType', and count taken from
29875ffd83dbSDimitry Andric /// argument `Elts'. The amount of bits of the contained type must match the
29885ffd83dbSDimitry Andric /// number of bits of the type contained in the passed in ArrayRef.
29895ffd83dbSDimitry Andric /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
29905ffd83dbSDimitry Andric /// that this can return a ConstantAggregateZero object.
getFP(Type * ElementType,ArrayRef<uint16_t> Elts)29915ffd83dbSDimitry Andric Constant *ConstantDataVector::getFP(Type *ElementType,
29920b57cec5SDimitry Andric ArrayRef<uint16_t> Elts) {
29935ffd83dbSDimitry Andric assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) &&
29945ffd83dbSDimitry Andric "Element type is not a 16-bit float type");
29955ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(ElementType, Elts.size());
29960b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
29970b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 2), Ty);
29980b57cec5SDimitry Andric }
getFP(Type * ElementType,ArrayRef<uint32_t> Elts)29995ffd83dbSDimitry Andric Constant *ConstantDataVector::getFP(Type *ElementType,
30000b57cec5SDimitry Andric ArrayRef<uint32_t> Elts) {
30015ffd83dbSDimitry Andric assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type");
30025ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(ElementType, Elts.size());
30030b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
30040b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 4), Ty);
30050b57cec5SDimitry Andric }
getFP(Type * ElementType,ArrayRef<uint64_t> Elts)30065ffd83dbSDimitry Andric Constant *ConstantDataVector::getFP(Type *ElementType,
30070b57cec5SDimitry Andric ArrayRef<uint64_t> Elts) {
30085ffd83dbSDimitry Andric assert(ElementType->isDoubleTy() &&
30095ffd83dbSDimitry Andric "Element type is not a 64-bit float type");
30105ffd83dbSDimitry Andric auto *Ty = FixedVectorType::get(ElementType, Elts.size());
30110b57cec5SDimitry Andric const char *Data = reinterpret_cast<const char *>(Elts.data());
30120b57cec5SDimitry Andric return getImpl(StringRef(Data, Elts.size() * 8), Ty);
30130b57cec5SDimitry Andric }
30140b57cec5SDimitry Andric
getSplat(unsigned NumElts,Constant * V)30150b57cec5SDimitry Andric Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
30160b57cec5SDimitry Andric assert(isElementTypeCompatible(V->getType()) &&
30170b57cec5SDimitry Andric "Element type not compatible with ConstantData");
30180b57cec5SDimitry Andric if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
30190b57cec5SDimitry Andric if (CI->getType()->isIntegerTy(8)) {
30200b57cec5SDimitry Andric SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
30210b57cec5SDimitry Andric return get(V->getContext(), Elts);
30220b57cec5SDimitry Andric }
30230b57cec5SDimitry Andric if (CI->getType()->isIntegerTy(16)) {
30240b57cec5SDimitry Andric SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
30250b57cec5SDimitry Andric return get(V->getContext(), Elts);
30260b57cec5SDimitry Andric }
30270b57cec5SDimitry Andric if (CI->getType()->isIntegerTy(32)) {
30280b57cec5SDimitry Andric SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
30290b57cec5SDimitry Andric return get(V->getContext(), Elts);
30300b57cec5SDimitry Andric }
30310b57cec5SDimitry Andric assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
30320b57cec5SDimitry Andric SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
30330b57cec5SDimitry Andric return get(V->getContext(), Elts);
30340b57cec5SDimitry Andric }
30350b57cec5SDimitry Andric
30360b57cec5SDimitry Andric if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
30370b57cec5SDimitry Andric if (CFP->getType()->isHalfTy()) {
30380b57cec5SDimitry Andric SmallVector<uint16_t, 16> Elts(
30390b57cec5SDimitry Andric NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
30405ffd83dbSDimitry Andric return getFP(V->getType(), Elts);
30415ffd83dbSDimitry Andric }
30425ffd83dbSDimitry Andric if (CFP->getType()->isBFloatTy()) {
30435ffd83dbSDimitry Andric SmallVector<uint16_t, 16> Elts(
30445ffd83dbSDimitry Andric NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
30455ffd83dbSDimitry Andric return getFP(V->getType(), Elts);
30460b57cec5SDimitry Andric }
30470b57cec5SDimitry Andric if (CFP->getType()->isFloatTy()) {
30480b57cec5SDimitry Andric SmallVector<uint32_t, 16> Elts(
30490b57cec5SDimitry Andric NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
30505ffd83dbSDimitry Andric return getFP(V->getType(), Elts);
30510b57cec5SDimitry Andric }
30520b57cec5SDimitry Andric if (CFP->getType()->isDoubleTy()) {
30530b57cec5SDimitry Andric SmallVector<uint64_t, 16> Elts(
30540b57cec5SDimitry Andric NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
30555ffd83dbSDimitry Andric return getFP(V->getType(), Elts);
30560b57cec5SDimitry Andric }
30570b57cec5SDimitry Andric }
3058e8d8bef9SDimitry Andric return ConstantVector::getSplat(ElementCount::getFixed(NumElts), V);
30590b57cec5SDimitry Andric }
30600b57cec5SDimitry Andric
30610b57cec5SDimitry Andric
getElementAsInteger(unsigned Elt) const30620b57cec5SDimitry Andric uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
30630b57cec5SDimitry Andric assert(isa<IntegerType>(getElementType()) &&
30640b57cec5SDimitry Andric "Accessor can only be used when element is an integer");
30650b57cec5SDimitry Andric const char *EltPtr = getElementPointer(Elt);
30660b57cec5SDimitry Andric
30670b57cec5SDimitry Andric // The data is stored in host byte order, make sure to cast back to the right
30680b57cec5SDimitry Andric // type to load with the right endianness.
30690b57cec5SDimitry Andric switch (getElementType()->getIntegerBitWidth()) {
30700b57cec5SDimitry Andric default: llvm_unreachable("Invalid bitwidth for CDS");
30710b57cec5SDimitry Andric case 8:
30720b57cec5SDimitry Andric return *reinterpret_cast<const uint8_t *>(EltPtr);
30730b57cec5SDimitry Andric case 16:
30740b57cec5SDimitry Andric return *reinterpret_cast<const uint16_t *>(EltPtr);
30750b57cec5SDimitry Andric case 32:
30760b57cec5SDimitry Andric return *reinterpret_cast<const uint32_t *>(EltPtr);
30770b57cec5SDimitry Andric case 64:
30780b57cec5SDimitry Andric return *reinterpret_cast<const uint64_t *>(EltPtr);
30790b57cec5SDimitry Andric }
30800b57cec5SDimitry Andric }
30810b57cec5SDimitry Andric
getElementAsAPInt(unsigned Elt) const30820b57cec5SDimitry Andric APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
30830b57cec5SDimitry Andric assert(isa<IntegerType>(getElementType()) &&
30840b57cec5SDimitry Andric "Accessor can only be used when element is an integer");
30850b57cec5SDimitry Andric const char *EltPtr = getElementPointer(Elt);
30860b57cec5SDimitry Andric
30870b57cec5SDimitry Andric // The data is stored in host byte order, make sure to cast back to the right
30880b57cec5SDimitry Andric // type to load with the right endianness.
30890b57cec5SDimitry Andric switch (getElementType()->getIntegerBitWidth()) {
30900b57cec5SDimitry Andric default: llvm_unreachable("Invalid bitwidth for CDS");
30910b57cec5SDimitry Andric case 8: {
30920b57cec5SDimitry Andric auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
30930b57cec5SDimitry Andric return APInt(8, EltVal);
30940b57cec5SDimitry Andric }
30950b57cec5SDimitry Andric case 16: {
30960b57cec5SDimitry Andric auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
30970b57cec5SDimitry Andric return APInt(16, EltVal);
30980b57cec5SDimitry Andric }
30990b57cec5SDimitry Andric case 32: {
31000b57cec5SDimitry Andric auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
31010b57cec5SDimitry Andric return APInt(32, EltVal);
31020b57cec5SDimitry Andric }
31030b57cec5SDimitry Andric case 64: {
31040b57cec5SDimitry Andric auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
31050b57cec5SDimitry Andric return APInt(64, EltVal);
31060b57cec5SDimitry Andric }
31070b57cec5SDimitry Andric }
31080b57cec5SDimitry Andric }
31090b57cec5SDimitry Andric
getElementAsAPFloat(unsigned Elt) const31100b57cec5SDimitry Andric APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
31110b57cec5SDimitry Andric const char *EltPtr = getElementPointer(Elt);
31120b57cec5SDimitry Andric
31130b57cec5SDimitry Andric switch (getElementType()->getTypeID()) {
31140b57cec5SDimitry Andric default:
31150b57cec5SDimitry Andric llvm_unreachable("Accessor can only be used when element is float/double!");
31160b57cec5SDimitry Andric case Type::HalfTyID: {
31170b57cec5SDimitry Andric auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
31180b57cec5SDimitry Andric return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
31190b57cec5SDimitry Andric }
31205ffd83dbSDimitry Andric case Type::BFloatTyID: {
31215ffd83dbSDimitry Andric auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
31225ffd83dbSDimitry Andric return APFloat(APFloat::BFloat(), APInt(16, EltVal));
31235ffd83dbSDimitry Andric }
31240b57cec5SDimitry Andric case Type::FloatTyID: {
31250b57cec5SDimitry Andric auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
31260b57cec5SDimitry Andric return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
31270b57cec5SDimitry Andric }
31280b57cec5SDimitry Andric case Type::DoubleTyID: {
31290b57cec5SDimitry Andric auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
31300b57cec5SDimitry Andric return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
31310b57cec5SDimitry Andric }
31320b57cec5SDimitry Andric }
31330b57cec5SDimitry Andric }
31340b57cec5SDimitry Andric
getElementAsFloat(unsigned Elt) const31350b57cec5SDimitry Andric float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
31360b57cec5SDimitry Andric assert(getElementType()->isFloatTy() &&
31370b57cec5SDimitry Andric "Accessor can only be used when element is a 'float'");
31380b57cec5SDimitry Andric return *reinterpret_cast<const float *>(getElementPointer(Elt));
31390b57cec5SDimitry Andric }
31400b57cec5SDimitry Andric
getElementAsDouble(unsigned Elt) const31410b57cec5SDimitry Andric double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
31420b57cec5SDimitry Andric assert(getElementType()->isDoubleTy() &&
31430b57cec5SDimitry Andric "Accessor can only be used when element is a 'float'");
31440b57cec5SDimitry Andric return *reinterpret_cast<const double *>(getElementPointer(Elt));
31450b57cec5SDimitry Andric }
31460b57cec5SDimitry Andric
getElementAsConstant(unsigned Elt) const31470b57cec5SDimitry Andric Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
31485ffd83dbSDimitry Andric if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() ||
31495ffd83dbSDimitry Andric getElementType()->isFloatTy() || getElementType()->isDoubleTy())
31500b57cec5SDimitry Andric return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
31510b57cec5SDimitry Andric
31520b57cec5SDimitry Andric return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
31530b57cec5SDimitry Andric }
31540b57cec5SDimitry Andric
isString(unsigned CharSize) const31550b57cec5SDimitry Andric bool ConstantDataSequential::isString(unsigned CharSize) const {
31560b57cec5SDimitry Andric return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
31570b57cec5SDimitry Andric }
31580b57cec5SDimitry Andric
isCString() const31590b57cec5SDimitry Andric bool ConstantDataSequential::isCString() const {
31600b57cec5SDimitry Andric if (!isString())
31610b57cec5SDimitry Andric return false;
31620b57cec5SDimitry Andric
31630b57cec5SDimitry Andric StringRef Str = getAsString();
31640b57cec5SDimitry Andric
31650b57cec5SDimitry Andric // The last value must be nul.
31660b57cec5SDimitry Andric if (Str.back() != 0) return false;
31670b57cec5SDimitry Andric
31680b57cec5SDimitry Andric // Other elements must be non-nul.
3169349cc55cSDimitry Andric return !Str.drop_back().contains(0);
31700b57cec5SDimitry Andric }
31710b57cec5SDimitry Andric
isSplatData() const31725ffd83dbSDimitry Andric bool ConstantDataVector::isSplatData() const {
31730b57cec5SDimitry Andric const char *Base = getRawDataValues().data();
31740b57cec5SDimitry Andric
31750b57cec5SDimitry Andric // Compare elements 1+ to the 0'th element.
31760b57cec5SDimitry Andric unsigned EltSize = getElementByteSize();
31770b57cec5SDimitry Andric for (unsigned i = 1, e = getNumElements(); i != e; ++i)
31780b57cec5SDimitry Andric if (memcmp(Base, Base+i*EltSize, EltSize))
31790b57cec5SDimitry Andric return false;
31800b57cec5SDimitry Andric
31810b57cec5SDimitry Andric return true;
31820b57cec5SDimitry Andric }
31830b57cec5SDimitry Andric
isSplat() const31845ffd83dbSDimitry Andric bool ConstantDataVector::isSplat() const {
31855ffd83dbSDimitry Andric if (!IsSplatSet) {
31865ffd83dbSDimitry Andric IsSplatSet = true;
31875ffd83dbSDimitry Andric IsSplat = isSplatData();
31885ffd83dbSDimitry Andric }
31895ffd83dbSDimitry Andric return IsSplat;
31905ffd83dbSDimitry Andric }
31915ffd83dbSDimitry Andric
getSplatValue() const31920b57cec5SDimitry Andric Constant *ConstantDataVector::getSplatValue() const {
31930b57cec5SDimitry Andric // If they're all the same, return the 0th one as a representative.
31940b57cec5SDimitry Andric return isSplat() ? getElementAsConstant(0) : nullptr;
31950b57cec5SDimitry Andric }
31960b57cec5SDimitry Andric
31970b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
31980b57cec5SDimitry Andric // handleOperandChange implementations
31990b57cec5SDimitry Andric
32000b57cec5SDimitry Andric /// Update this constant array to change uses of
32010b57cec5SDimitry Andric /// 'From' to be uses of 'To'. This must update the uniquing data structures
32020b57cec5SDimitry Andric /// etc.
32030b57cec5SDimitry Andric ///
32040b57cec5SDimitry Andric /// Note that we intentionally replace all uses of From with To here. Consider
32050b57cec5SDimitry Andric /// a large array that uses 'From' 1000 times. By handling this case all here,
32060b57cec5SDimitry Andric /// ConstantArray::handleOperandChange is only invoked once, and that
32070b57cec5SDimitry Andric /// single invocation handles all 1000 uses. Handling them one at a time would
32080b57cec5SDimitry Andric /// work, but would be really slow because it would have to unique each updated
32090b57cec5SDimitry Andric /// array instance.
32100b57cec5SDimitry Andric ///
handleOperandChange(Value * From,Value * To)32110b57cec5SDimitry Andric void Constant::handleOperandChange(Value *From, Value *To) {
32120b57cec5SDimitry Andric Value *Replacement = nullptr;
32130b57cec5SDimitry Andric switch (getValueID()) {
32140b57cec5SDimitry Andric default:
32150b57cec5SDimitry Andric llvm_unreachable("Not a constant!");
32160b57cec5SDimitry Andric #define HANDLE_CONSTANT(Name) \
32170b57cec5SDimitry Andric case Value::Name##Val: \
32180b57cec5SDimitry Andric Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
32190b57cec5SDimitry Andric break;
32200b57cec5SDimitry Andric #include "llvm/IR/Value.def"
32210b57cec5SDimitry Andric }
32220b57cec5SDimitry Andric
32230b57cec5SDimitry Andric // If handleOperandChangeImpl returned nullptr, then it handled
32240b57cec5SDimitry Andric // replacing itself and we don't want to delete or replace anything else here.
32250b57cec5SDimitry Andric if (!Replacement)
32260b57cec5SDimitry Andric return;
32270b57cec5SDimitry Andric
32280b57cec5SDimitry Andric // I do need to replace this with an existing value.
32290b57cec5SDimitry Andric assert(Replacement != this && "I didn't contain From!");
32300b57cec5SDimitry Andric
32310b57cec5SDimitry Andric // Everyone using this now uses the replacement.
32320b57cec5SDimitry Andric replaceAllUsesWith(Replacement);
32330b57cec5SDimitry Andric
32340b57cec5SDimitry Andric // Delete the old constant!
32350b57cec5SDimitry Andric destroyConstant();
32360b57cec5SDimitry Andric }
32370b57cec5SDimitry Andric
handleOperandChangeImpl(Value * From,Value * To)32380b57cec5SDimitry Andric Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
32390b57cec5SDimitry Andric assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
32400b57cec5SDimitry Andric Constant *ToC = cast<Constant>(To);
32410b57cec5SDimitry Andric
32420b57cec5SDimitry Andric SmallVector<Constant*, 8> Values;
32430b57cec5SDimitry Andric Values.reserve(getNumOperands()); // Build replacement array.
32440b57cec5SDimitry Andric
32450b57cec5SDimitry Andric // Fill values with the modified operands of the constant array. Also,
32460b57cec5SDimitry Andric // compute whether this turns into an all-zeros array.
32470b57cec5SDimitry Andric unsigned NumUpdated = 0;
32480b57cec5SDimitry Andric
32490b57cec5SDimitry Andric // Keep track of whether all the values in the array are "ToC".
32500b57cec5SDimitry Andric bool AllSame = true;
32510b57cec5SDimitry Andric Use *OperandList = getOperandList();
32520b57cec5SDimitry Andric unsigned OperandNo = 0;
32530b57cec5SDimitry Andric for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
32540b57cec5SDimitry Andric Constant *Val = cast<Constant>(O->get());
32550b57cec5SDimitry Andric if (Val == From) {
32560b57cec5SDimitry Andric OperandNo = (O - OperandList);
32570b57cec5SDimitry Andric Val = ToC;
32580b57cec5SDimitry Andric ++NumUpdated;
32590b57cec5SDimitry Andric }
32600b57cec5SDimitry Andric Values.push_back(Val);
32610b57cec5SDimitry Andric AllSame &= Val == ToC;
32620b57cec5SDimitry Andric }
32630b57cec5SDimitry Andric
32640b57cec5SDimitry Andric if (AllSame && ToC->isNullValue())
32650b57cec5SDimitry Andric return ConstantAggregateZero::get(getType());
32660b57cec5SDimitry Andric
32670b57cec5SDimitry Andric if (AllSame && isa<UndefValue>(ToC))
32680b57cec5SDimitry Andric return UndefValue::get(getType());
32690b57cec5SDimitry Andric
32700b57cec5SDimitry Andric // Check for any other type of constant-folding.
32710b57cec5SDimitry Andric if (Constant *C = getImpl(getType(), Values))
32720b57cec5SDimitry Andric return C;
32730b57cec5SDimitry Andric
32740b57cec5SDimitry Andric // Update to the new value.
32750b57cec5SDimitry Andric return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
32760b57cec5SDimitry Andric Values, this, From, ToC, NumUpdated, OperandNo);
32770b57cec5SDimitry Andric }
32780b57cec5SDimitry Andric
handleOperandChangeImpl(Value * From,Value * To)32790b57cec5SDimitry Andric Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
32800b57cec5SDimitry Andric assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
32810b57cec5SDimitry Andric Constant *ToC = cast<Constant>(To);
32820b57cec5SDimitry Andric
32830b57cec5SDimitry Andric Use *OperandList = getOperandList();
32840b57cec5SDimitry Andric
32850b57cec5SDimitry Andric SmallVector<Constant*, 8> Values;
32860b57cec5SDimitry Andric Values.reserve(getNumOperands()); // Build replacement struct.
32870b57cec5SDimitry Andric
32880b57cec5SDimitry Andric // Fill values with the modified operands of the constant struct. Also,
32890b57cec5SDimitry Andric // compute whether this turns into an all-zeros struct.
32900b57cec5SDimitry Andric unsigned NumUpdated = 0;
32910b57cec5SDimitry Andric bool AllSame = true;
32920b57cec5SDimitry Andric unsigned OperandNo = 0;
32930b57cec5SDimitry Andric for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
32940b57cec5SDimitry Andric Constant *Val = cast<Constant>(O->get());
32950b57cec5SDimitry Andric if (Val == From) {
32960b57cec5SDimitry Andric OperandNo = (O - OperandList);
32970b57cec5SDimitry Andric Val = ToC;
32980b57cec5SDimitry Andric ++NumUpdated;
32990b57cec5SDimitry Andric }
33000b57cec5SDimitry Andric Values.push_back(Val);
33010b57cec5SDimitry Andric AllSame &= Val == ToC;
33020b57cec5SDimitry Andric }
33030b57cec5SDimitry Andric
33040b57cec5SDimitry Andric if (AllSame && ToC->isNullValue())
33050b57cec5SDimitry Andric return ConstantAggregateZero::get(getType());
33060b57cec5SDimitry Andric
33070b57cec5SDimitry Andric if (AllSame && isa<UndefValue>(ToC))
33080b57cec5SDimitry Andric return UndefValue::get(getType());
33090b57cec5SDimitry Andric
33100b57cec5SDimitry Andric // Update to the new value.
33110b57cec5SDimitry Andric return getContext().pImpl->StructConstants.replaceOperandsInPlace(
33120b57cec5SDimitry Andric Values, this, From, ToC, NumUpdated, OperandNo);
33130b57cec5SDimitry Andric }
33140b57cec5SDimitry Andric
handleOperandChangeImpl(Value * From,Value * To)33150b57cec5SDimitry Andric Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
33160b57cec5SDimitry Andric assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
33170b57cec5SDimitry Andric Constant *ToC = cast<Constant>(To);
33180b57cec5SDimitry Andric
33190b57cec5SDimitry Andric SmallVector<Constant*, 8> Values;
33200b57cec5SDimitry Andric Values.reserve(getNumOperands()); // Build replacement array...
33210b57cec5SDimitry Andric unsigned NumUpdated = 0;
33220b57cec5SDimitry Andric unsigned OperandNo = 0;
33230b57cec5SDimitry Andric for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
33240b57cec5SDimitry Andric Constant *Val = getOperand(i);
33250b57cec5SDimitry Andric if (Val == From) {
33260b57cec5SDimitry Andric OperandNo = i;
33270b57cec5SDimitry Andric ++NumUpdated;
33280b57cec5SDimitry Andric Val = ToC;
33290b57cec5SDimitry Andric }
33300b57cec5SDimitry Andric Values.push_back(Val);
33310b57cec5SDimitry Andric }
33320b57cec5SDimitry Andric
33330b57cec5SDimitry Andric if (Constant *C = getImpl(Values))
33340b57cec5SDimitry Andric return C;
33350b57cec5SDimitry Andric
33360b57cec5SDimitry Andric // Update to the new value.
33370b57cec5SDimitry Andric return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
33380b57cec5SDimitry Andric Values, this, From, ToC, NumUpdated, OperandNo);
33390b57cec5SDimitry Andric }
33400b57cec5SDimitry Andric
handleOperandChangeImpl(Value * From,Value * ToV)33410b57cec5SDimitry Andric Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
33420b57cec5SDimitry Andric assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
33430b57cec5SDimitry Andric Constant *To = cast<Constant>(ToV);
33440b57cec5SDimitry Andric
33450b57cec5SDimitry Andric SmallVector<Constant*, 8> NewOps;
33460b57cec5SDimitry Andric unsigned NumUpdated = 0;
33470b57cec5SDimitry Andric unsigned OperandNo = 0;
33480b57cec5SDimitry Andric for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
33490b57cec5SDimitry Andric Constant *Op = getOperand(i);
33500b57cec5SDimitry Andric if (Op == From) {
33510b57cec5SDimitry Andric OperandNo = i;
33520b57cec5SDimitry Andric ++NumUpdated;
33530b57cec5SDimitry Andric Op = To;
33540b57cec5SDimitry Andric }
33550b57cec5SDimitry Andric NewOps.push_back(Op);
33560b57cec5SDimitry Andric }
33570b57cec5SDimitry Andric assert(NumUpdated && "I didn't contain From!");
33580b57cec5SDimitry Andric
33590b57cec5SDimitry Andric if (Constant *C = getWithOperands(NewOps, getType(), true))
33600b57cec5SDimitry Andric return C;
33610b57cec5SDimitry Andric
33620b57cec5SDimitry Andric // Update to the new value.
33630b57cec5SDimitry Andric return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
33640b57cec5SDimitry Andric NewOps, this, From, To, NumUpdated, OperandNo);
33650b57cec5SDimitry Andric }
33660b57cec5SDimitry Andric
getAsInstruction() const3367*0fca6ea1SDimitry Andric Instruction *ConstantExpr::getAsInstruction() const {
3368e8d8bef9SDimitry Andric SmallVector<Value *, 4> ValueOperands(operands());
33690b57cec5SDimitry Andric ArrayRef<Value*> Ops(ValueOperands);
33700b57cec5SDimitry Andric
33710b57cec5SDimitry Andric switch (getOpcode()) {
33720b57cec5SDimitry Andric case Instruction::Trunc:
33730b57cec5SDimitry Andric case Instruction::PtrToInt:
33740b57cec5SDimitry Andric case Instruction::IntToPtr:
33750b57cec5SDimitry Andric case Instruction::BitCast:
33760b57cec5SDimitry Andric case Instruction::AddrSpaceCast:
3377349cc55cSDimitry Andric return CastInst::Create((Instruction::CastOps)getOpcode(), Ops[0],
3378*0fca6ea1SDimitry Andric getType(), "");
33790b57cec5SDimitry Andric case Instruction::InsertElement:
3380*0fca6ea1SDimitry Andric return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "");
33810b57cec5SDimitry Andric case Instruction::ExtractElement:
3382*0fca6ea1SDimitry Andric return ExtractElementInst::Create(Ops[0], Ops[1], "");
33830b57cec5SDimitry Andric case Instruction::ShuffleVector:
3384*0fca6ea1SDimitry Andric return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "");
33850b57cec5SDimitry Andric
33860b57cec5SDimitry Andric case Instruction::GetElementPtr: {
33870b57cec5SDimitry Andric const auto *GO = cast<GEPOperator>(this);
33880b57cec5SDimitry Andric return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
3389*0fca6ea1SDimitry Andric Ops.slice(1), GO->getNoWrapFlags(), "");
33900b57cec5SDimitry Andric }
33910b57cec5SDimitry Andric default:
33920b57cec5SDimitry Andric assert(getNumOperands() == 2 && "Must be binary operator?");
3393349cc55cSDimitry Andric BinaryOperator *BO = BinaryOperator::Create(
3394*0fca6ea1SDimitry Andric (Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "");
33950b57cec5SDimitry Andric if (isa<OverflowingBinaryOperator>(BO)) {
33960b57cec5SDimitry Andric BO->setHasNoUnsignedWrap(SubclassOptionalData &
33970b57cec5SDimitry Andric OverflowingBinaryOperator::NoUnsignedWrap);
33980b57cec5SDimitry Andric BO->setHasNoSignedWrap(SubclassOptionalData &
33990b57cec5SDimitry Andric OverflowingBinaryOperator::NoSignedWrap);
34000b57cec5SDimitry Andric }
34010b57cec5SDimitry Andric if (isa<PossiblyExactOperator>(BO))
34020b57cec5SDimitry Andric BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
34030b57cec5SDimitry Andric return BO;
34040b57cec5SDimitry Andric }
34050b57cec5SDimitry Andric }
3406