LLVM 24.0.0git
Value.cpp
Go to the documentation of this file.
1//===- Value.cpp - Value Representation for llubi -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements utility functions for the value representation.
10//
11//===----------------------------------------------------------------------===//
12
16
17namespace llvm::ubi {
18
24
27 assert(!Obj && Wildcard && "The memory object has been determined.");
28 auto Res = makeIntrusiveRefCnt<Provenance>(*this);
29 Res->Obj = &KnownObj;
30 Res->Tag = APInt();
31 return Res;
32}
33
34void Pointer::print(raw_ostream &OS) const {
35 SmallString<32> AddrStr;
36 Address.toStringUnsigned(AddrStr, 16);
37 OS << "ptr 0x" << AddrStr << " [";
38 if (MemoryObject *Obj = Prov->getMemoryObject()) {
39 if (Obj->isIRGlobalValue())
40 OS << "@";
41 OS << Obj->getName();
42 if (Address != Obj->getAddress())
43 OS << " + " << (Address - Obj->getAddress());
44 MemoryObjectState State = Obj->getState();
45 if (Obj->isInaccessible())
46 OS << " (inaccessible)";
47 else if (State != MemoryObjectState::Alive)
48 OS << (State == MemoryObjectState::Dead ? " (dead)" : " (dangling)");
49 } else {
50 OS << (Prov->isWildcard() ? "wildcard" : "nullary");
51 }
52 // TODO: print provenance
53 OS << "]";
54}
55
56AnyValue Pointer::null(unsigned AS, const DataLayout &DL) {
57 return AnyValue(Pointer(Provenance::nullary(), DL.getNullPtrValue(AS)));
58}
59
60bool Pointer::isNullPtr(unsigned AS, const DataLayout &DL) const {
61 return Address == DL.getNullPtrValue(AS);
62}
63
64void AnyValue::print(Context &Ctx, raw_ostream &OS) const {
65 switch (Kind) {
67 if (IntVal.getBitWidth() == 1) {
68 OS << (IntVal.getBoolValue() ? "T" : "F");
69 break;
70 }
71 OS << "i" << IntVal.getBitWidth() << ' ' << IntVal;
72 break;
73 case StorageKind::Float: {
74 switch (APFloat::SemanticsToEnum(FloatVal.getSemantics())) {
75 default:
76 llvm_unreachable("invalid fltSemantics");
78 OS << "half ";
79 break;
81 OS << "bfloat ";
82 break;
84 OS << "float ";
85 break;
87 OS << "double ";
88 break;
90 OS << "x86_fp80 ";
91 break;
93 OS << "fp128 ";
94 break;
96 OS << "ppc_fp128 ";
97 break;
98 }
99 // We cannot reuse Value::print due to lack of LLVMContext here.
100 // Similar to writeAPFloatInternal, output the FP constant value in
101 // exponential notation if it is lossless, otherwise output it in
102 // hexadecimal notation.
103 SmallString<16> StrVal;
104 if (FloatVal.toStringRoundTrip(StrVal, /*FormatPrecision=*/6,
105 /*FormatMaxPadding=*/0,
106 /*TruncateZero=*/false)) {
107 OS << StrVal;
108 } else {
109 StrVal.clear();
110 APInt Bits = FloatVal.bitcastToAPInt();
111 Bits.toStringUnsigned(StrVal, 16);
112 size_t MaxDigits = divideCeil(Bits.getBitWidth(), 4);
113 OS << "0x";
114 for (size_t Digits = StrVal.size(); Digits != MaxDigits; ++Digits)
115 OS << '0';
116 OS << StrVal;
117 }
118 break;
119 }
121 PtrVal.print(OS);
122 break;
124 ByteVal.print(Ctx, OS);
125 break;
127 OS << "poison";
128 break;
130 OS << "none";
131 break;
133 OS << "{ ";
134 for (size_t I = 0, E = AggVal.size(); I != E; ++I) {
135 if (I != 0)
136 OS << ", ";
137 AggVal[I].print(Ctx, OS);
138 }
139 OS << " }";
140 break;
141 }
142}
143
144void AnyValue::destroy() {
145 switch (Kind) {
147 IntVal.~APInt();
148 break;
151 break;
153 PtrVal.~Pointer();
154 break;
157 break;
160 break;
162 AggVal.~vector();
163 break;
164 }
165}
166
168 switch (Other.Kind) {
169 case StorageKind::Integer:
170 new (&IntVal) APInt(Other.IntVal);
171 break;
172 case StorageKind::Float:
173 new (&FloatVal) APFloat(Other.FloatVal);
174 break;
175 case StorageKind::Pointer:
176 new (&PtrVal) Pointer(Other.PtrVal);
177 break;
178 case StorageKind::Byte:
179 new (&ByteVal) ByteValue(Other.ByteVal);
180 break;
181 case StorageKind::Poison:
182 case StorageKind::None:
183 break;
184 case StorageKind::Aggregate:
185 new (&AggVal) std::vector<AnyValue>(Other.AggVal);
186 break;
187 }
188}
190 switch (Other.Kind) {
191 case StorageKind::Integer:
192 new (&IntVal) APInt(std::move(Other.IntVal));
193 break;
194 case StorageKind::Float:
195 new (&FloatVal) APFloat(std::move(Other.FloatVal));
196 break;
197 case StorageKind::Pointer:
198 new (&PtrVal) Pointer(std::move(Other.PtrVal));
199 break;
200 case StorageKind::Byte:
201 new (&ByteVal) ByteValue(std::move(Other.ByteVal));
202 break;
203 case StorageKind::Poison:
204 case StorageKind::None:
205 break;
206 case StorageKind::Aggregate:
207 new (&AggVal) std::vector<AnyValue>(std::move(Other.AggVal));
208 break;
209 }
210}
211
213 if (&Other == this)
214 return *this;
215
216 destroy();
217 Kind = Other.Kind;
218 switch (Other.Kind) {
220 new (&IntVal) APInt(Other.IntVal);
221 break;
223 new (&FloatVal) APFloat(Other.FloatVal);
224 break;
226 new (&PtrVal) Pointer(Other.PtrVal);
227 break;
229 new (&ByteVal) ByteValue(Other.ByteVal);
230 break;
233 break;
235 new (&AggVal) std::vector<AnyValue>(Other.AggVal);
236 break;
237 }
238
239 return *this;
240}
242 if (&Other == this)
243 return *this;
244 destroy();
245 Kind = Other.Kind;
246 switch (Other.Kind) {
248 new (&IntVal) APInt(std::move(Other.IntVal));
249 break;
251 new (&FloatVal) APFloat(std::move(Other.FloatVal));
252 break;
254 new (&PtrVal) Pointer(std::move(Other.PtrVal));
255 break;
257 new (&ByteVal) ByteValue(std::move(Other.ByteVal));
258 break;
261 break;
263 new (&AggVal) std::vector<AnyValue>(std::move(Other.AggVal));
264 break;
265 }
266
267 return *this;
268}
269
271 if (Ty->isFloatingPointTy() || Ty->isIntegerTy() || Ty->isPointerTy())
272 return AnyValue::poison();
273 if (Ty->isByteTy())
274 return ByteValue::poison(Ty->getByteBitWidth(),
275 Ctx.getDataLayout().isLittleEndian());
276 if (auto *VecTy = dyn_cast<VectorType>(Ty)) {
277 uint32_t NumElements = Ctx.getEVL(VecTy->getElementCount());
278 return AnyValue(std::vector<AnyValue>(
279 NumElements, getPoisonValue(Ctx, VecTy->getScalarType())));
280 }
281 if (auto *ArrTy = dyn_cast<ArrayType>(Ty)) {
282 uint64_t NumElements = ArrTy->getNumElements();
283 return AnyValue(std::vector<AnyValue>(
284 NumElements, getPoisonValue(Ctx, ArrTy->getElementType())));
285 }
286 if (auto *StructTy = dyn_cast<StructType>(Ty)) {
287 std::vector<AnyValue> Elements;
288 Elements.reserve(StructTy->getNumElements());
289 for (uint32_t I = 0, E = StructTy->getNumElements(); I != E; ++I)
290 Elements.push_back(getPoisonValue(Ctx, StructTy->getElementType(I)));
291 return AnyValue(std::move(Elements));
292 }
293 llvm_unreachable("Unsupported type");
294}
296 if (Ty->isIntegerTy())
297 return AnyValue(APInt::getZero(Ty->getIntegerBitWidth()));
298 if (Ty->isFloatingPointTy())
299 return AnyValue(APFloat::getZero(Ty->getFltSemantics()));
300 if (Ty->isPointerTy())
301 return Pointer::null(Ty->getPointerAddressSpace(), Ctx.getDataLayout());
302 if (Ty->isByteTy())
303 return ByteValue::zero(Ty->getByteBitWidth(),
304 Ctx.getDataLayout().isLittleEndian());
305 if (auto *VecTy = dyn_cast<VectorType>(Ty)) {
306 uint32_t NumElements = Ctx.getEVL(VecTy->getElementCount());
307 return AnyValue(std::vector<AnyValue>(
308 NumElements, getNullValue(Ctx, VecTy->getElementType())));
309 }
310 if (auto *ArrTy = dyn_cast<ArrayType>(Ty)) {
311 uint64_t NumElements = ArrTy->getNumElements();
312 return AnyValue(std::vector<AnyValue>(
313 NumElements, getNullValue(Ctx, ArrTy->getElementType())));
314 }
315 if (auto *StructTy = dyn_cast<StructType>(Ty)) {
316 std::vector<AnyValue> Elements;
317 Elements.reserve(StructTy->getNumElements());
318 for (uint32_t I = 0, E = StructTy->getNumElements(); I != E; ++I)
319 Elements.push_back(getNullValue(Ctx, StructTy->getElementType(I)));
320 return AnyValue(std::move(Elements));
321 }
322 llvm_unreachable("Unsupported type");
323}
324
325AnyValue AnyValue::getVectorSplat(const AnyValue &Scalar, size_t NumElements) {
326 assert(!Scalar.isAggregate() && !Scalar.isNone() && "Expect a scalar value");
327 return AnyValue(std::vector<AnyValue>(NumElements, Scalar));
328}
329
330ByteValue::ByteValue(const APInt &V, bool IsLittleEndian)
331 : BitWidth(V.getBitWidth()), IsLittleEndian(IsLittleEndian) {
332 Val.resize(divideCeil(BitWidth, 8));
333 MutableBytesView View(Val, IsLittleEndian);
334 for (uint32_t I = 0; I < BitWidth; I += 8)
335 View[I / 8] = Byte::concrete(static_cast<uint8_t>(
336 V.extractBitsAsZExtValue(std::min(BitWidth - I, 8U), I)));
337}
338ByteValue ByteValue::zero(uint32_t BitWidth, bool IsLittleEndian) {
339 return ByteValue(
340 BitWidth, std::vector<Byte>(divideCeil(BitWidth, 8), Byte::concrete(0)),
341 IsLittleEndian);
342}
343
344ByteValue ByteValue::poison(uint32_t BitWidth, bool IsLittleEndian) {
345 return ByteValue(BitWidth,
346 std::vector<Byte>(divideCeil(BitWidth, 8), Byte::poison()),
348}
349
350void ByteValue::print(Context &Ctx, raw_ostream &OS) const {
351 OS << 'b' << BitWidth << ' ';
352
353 auto PrintByte = [&](const Byte &V) {
354 bool IsFullByte = (BitWidth & 7) == 0 ||
355 (IsLittleEndian ? &Val.back() : &Val.front()) != &V;
356 // Try to print a byte in short form
357 if (IsFullByte && V.ConcreteMask == 255 && V.TagMask == 0) {
358 // Concrete value without provenance.
359 OS << "0x" << hexdigit(V.Value >> 4) << hexdigit(V.Value & 15);
360 } else if (IsFullByte && V.ConcreteMask == 0) {
361 assert(V.Value == 0 && "Byte values don't contain undef bits.");
362 // Poison bytes.
363 OS << "0x!!";
364 } else {
365 uint32_t BitEnd = IsFullByte ? 8 : BitWidth & 7;
366 for (uint32_t I = 0; I != BitEnd; ++I) {
367 uint32_t Mask = 1U << (BitEnd - 1 - I);
368 if (V.ConcreteMask & Mask)
369 OS << (V.Value & Mask ? '1' : '0');
370 else {
371 assert((V.Value & Mask) == 0 &&
372 "Byte values don't contain undef bits.");
373 OS << '!';
374 }
375 }
376 assert((V.ConcreteMask & V.TagMask) == V.TagMask);
377 if (V.TagMask) {
378 // Print tags if available.
379 OS << '(';
380 for (uint32_t I = 0; I != BitEnd; ++I) {
381 uint32_t Mask = 1U << (BitEnd - 1 - I);
382 if (V.TagMask & Mask)
383 OS << (V.TagValue & Mask ? '1' : '0');
384 else
385 OS << '!';
386 }
387 OS << ')';
388 }
389 }
390 OS << ' ';
391 };
392
393 auto &DL = Ctx.getDataLayout();
394 unsigned PtrWidthForAS0 = DL.getPointerSizeInBits(0);
395 Type *PtrTy = PointerType::getUnqual(Ctx.getContext());
396
397 if (PtrWidthForAS0 % 8 == 0 && BitWidth % PtrWidthForAS0 == 0) {
398 // Try to treat the bytes value as an array of pointers in address space 0.
399 unsigned PtrSize = PtrWidthForAS0 / 8;
400 for (size_t I = 0, E = Val.size(); I != E; I += PtrSize) {
401 ArrayRef<Byte> Slice = ArrayRef(Val).slice(I, PtrSize);
402 if (all_of(Slice, [](const Byte &V) {
403 assert((V.ConcreteMask & V.TagMask) == V.TagMask);
404 return V.TagMask == 255;
405 })) {
406 AnyValue Res = Ctx.fromBytes(Slice, PtrTy);
407 if (Res.isPointer()) {
408 Res.asPointer().print(OS);
409 OS << ' ';
410 continue;
411 }
412 }
413
414 // Otherwise, fallback into bytes array
415 for (size_t J = 0; J != PtrSize; ++J)
416 PrintByte(Val[I + J]);
417 }
418 } else {
419 for (const Byte &V : Val)
420 PrintByte(V);
421 }
422}
423
424} // namespace llvm::ubi
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the SmallString class.
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition APFloat.cpp:459
~APFloat()=default
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1216
Class for arbitrary precision integers.
Definition APInt.h:78
~APInt()
Destructor.
Definition APInt.h:186
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:196
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:185
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:28
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
bool isPointer() const
Definition Value.h:377
static LLVM_ABI AnyValue getPoisonValue(Context &Ctx, Type *Ty)
Definition Value.cpp:270
static AnyValue poison()
Use getPoisonValue unless you know the scalar type is integer/fp/ptr!
Definition Value.h:366
std::vector< AnyValue > AggVal
Definition Value.h:342
const Pointer & asPointer() const
Definition Value.h:412
LLVM_ABI void print(Context &Ctx, raw_ostream &OS) const
Definition Value.cpp:64
static LLVM_ABI AnyValue getVectorSplat(const AnyValue &Scalar, size_t NumElements)
Definition Value.cpp:325
LLVM_ABI AnyValue & operator=(const AnyValue &)
Definition Value.cpp:212
APFloat FloatVal
Definition Value.h:339
ByteValue ByteVal
Definition Value.h:341
static LLVM_ABI AnyValue getNullValue(Context &Ctx, Type *Ty)
Definition Value.cpp:295
Represents a scalar byte value.
Definition Value.h:257
static LLVM_ABI ByteValue poison(uint32_t BitWidth, bool IsLittleEndian)
Definition Value.cpp:344
LLVM_ABI void print(Context &Ctx, raw_ostream &OS) const
Definition Value.cpp:350
static LLVM_ABI ByteValue zero(uint32_t BitWidth, bool IsLittleEndian)
Definition Value.cpp:338
LLVM_ABI ByteValue(const APInt &V, bool IsLittleEndian)
Definition Value.cpp:330
uint32_t getBitWidth() const
Definition Value.h:297
The global context for the interpreter.
Definition Context.h:227
static LLVM_ABI AnyValue null(unsigned AS, const DataLayout &DL)
Definition Value.cpp:56
LLVM_ABI bool isNullPtr(unsigned AS, const DataLayout &DL) const
Definition Value.cpp:60
LLVM_ABI void print(raw_ostream &OS) const
Definition Value.cpp:34
static LLVM_ABI IntrusiveRefCntPtr< Provenance > nullary()
Definition Value.cpp:19
LLVM_ABI IntrusiveRefCntPtr< Provenance > getWithKnownMemoryObject(MemoryObject &Obj)
Definition Value.cpp:26
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
MemoryObjectState
Definition Context.h:41
BytesView< MutableArrayRef< Byte > > MutableBytesView
Definition Context.h:211
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
IntrusiveRefCntPtr< T > makeIntrusiveRefCnt(Args &&...A)
Factory function for creating intrusive ref counted pointers.
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:389
@ Other
Any other memory.
Definition ModRef.h:68
ArrayRef(const T &OneElt) -> ArrayRef< T >
Representation of a byte in memory.
Definition Value.h:43
static Byte poison()
Definition Value.h:50
static Byte concrete(uint8_t Val)
Definition Value.h:52