|
| 1 | +//===--- SILConstants.h - SIL constant representation -----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This defines an interface to represent SIL level structured constants in a |
| 14 | +// memory efficient way. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_SIL_CONSTANTS_H |
| 19 | +#define SWIFT_SIL_CONSTANTS_H |
| 20 | + |
| 21 | +#include "swift/SIL/SILValue.h" |
| 22 | +#include "llvm/Support/CommandLine.h" |
| 23 | + |
| 24 | + |
| 25 | +namespace swift { |
| 26 | +class SingleValueInstruction; |
| 27 | +class SILValue; |
| 28 | +class SILBuilder; |
| 29 | +class SerializedSILLoader; |
| 30 | + |
| 31 | +struct APIntSymbolicValue; |
| 32 | +struct ArraySymbolicValue; |
| 33 | +struct EnumWithPayloadSymbolicValue; |
| 34 | +struct UnknownSymbolicValue; |
| 35 | + |
| 36 | +extern llvm::cl::opt<unsigned> ConstExprLimit; |
| 37 | + |
| 38 | +/// When we fail to constant fold a value, this captures a reason why, |
| 39 | +/// allowing the caller to produce a specific diagnostic. The "Unknown" |
| 40 | +/// SymbolicValue representation also includes a pointer to the SILNode in |
| 41 | +/// question that was problematic. |
| 42 | +enum class UnknownReason { |
| 43 | + // TODO: Eliminate the default code, by making classifications for each |
| 44 | + // failure mode. |
| 45 | + Default, |
| 46 | + |
| 47 | + /// The constant expression was too big. This is reported on a random |
| 48 | + /// instruction within the constexpr that triggered the issue. |
| 49 | + TooManyInstructions, |
| 50 | + |
| 51 | + /// A control flow loop was found. |
| 52 | + Loop, |
| 53 | + |
| 54 | + /// Integer overflow detected. |
| 55 | + Overflow, |
| 56 | + |
| 57 | + /// Unspecified trap detected. |
| 58 | + Trap, |
| 59 | +}; |
| 60 | + |
| 61 | +/// This is the symbolic value tracked for each SILValue in a scope. We |
| 62 | +/// support multiple representational forms for the constant node in order to |
| 63 | +/// avoid pointless memory bloat + copying. This is intended to be a |
| 64 | +/// light-weight POD type we can put in hash tables and pass around by-value. |
| 65 | +/// |
| 66 | +/// Internally, this value has multiple ways to represent the same sorts of |
| 67 | +/// symbolic values (e.g. to save memory). It provides a simpler public |
| 68 | +/// interface though. |
| 69 | +class SymbolicValue { |
| 70 | +private: |
| 71 | + enum RepresentationKind { |
| 72 | + /// This symbolic value cannot be determined, carries multiple values |
| 73 | + /// (i.e., varies dynamically at the top level), or is of some type that |
| 74 | + /// we cannot analyze and propagate (e.g. NSObject). |
| 75 | + /// |
| 76 | + RK_Unknown, |
| 77 | + |
| 78 | + /// This value is known to be a metatype reference. The type is stored |
| 79 | + /// in the "metatype" member. |
| 80 | + RK_Metatype, |
| 81 | + |
| 82 | + /// This value is known to be a function reference, e.g. through |
| 83 | + /// function_ref directly, or a devirtualized method reference. |
| 84 | + RK_Function, |
| 85 | + |
| 86 | + /// This value is represented with a bump-pointer allocated APInt. |
| 87 | + RK_Integer, |
| 88 | + |
| 89 | + /// This value is represented with an inline integer representation. |
| 90 | + RK_IntegerInline, |
| 91 | + |
| 92 | + /// This value is a struct or tuple of constants. This is tracked by the |
| 93 | + /// "aggregate" member of the value union. |
| 94 | + RK_Aggregate, |
| 95 | + }; |
| 96 | + |
| 97 | + union { |
| 98 | + /// When the value is Unknown, this contains information about the |
| 99 | + /// unfoldable part of the computation. |
| 100 | + UnknownSymbolicValue *unknown; |
| 101 | + |
| 102 | + /// This is always a SILType with an object category. This is the value |
| 103 | + /// of the underlying instance type, not the MetatypeType. |
| 104 | + TypeBase *metatype; |
| 105 | + |
| 106 | + SILFunction *function; |
| 107 | + |
| 108 | + /// When this SymbolicValue is of "Integer" kind, this pointer stores |
| 109 | + /// the words of the APInt value it holds. |
| 110 | + uint64_t *integer; |
| 111 | + |
| 112 | + /// This holds the bits of an integer for an inline representation. |
| 113 | + uint64_t integerInline; |
| 114 | + |
| 115 | + /// When this SymbolicValue is of "Aggregate" kind, this pointer stores |
| 116 | + /// information about the array elements and count. |
| 117 | + const SymbolicValue *aggregate; |
| 118 | + } value; |
| 119 | + |
| 120 | + RepresentationKind representationKind : 8; |
| 121 | + |
| 122 | + union { |
| 123 | + /// This is the reason code for RK_Unknown values. |
| 124 | + UnknownReason unknown_reason : 32; |
| 125 | + |
| 126 | + /// This is the number of bits in an RK_Integer or RK_IntegerInline |
| 127 | + /// representation, which makes the number of entries in the list derivable. |
| 128 | + unsigned integer_bitwidth; |
| 129 | + |
| 130 | + /// This is the number of elements for an RK_Aggregate representation. |
| 131 | + unsigned aggregate_numElements; |
| 132 | + } aux; |
| 133 | + |
| 134 | +public: |
| 135 | + /// This enum is used to indicate the sort of value held by a SymbolicValue |
| 136 | + /// independent of its concrete representation. This is the public |
| 137 | + /// interface to SymbolicValue. |
| 138 | + enum Kind { |
| 139 | + /// This is a value that isn't a constant. |
| 140 | + Unknown, |
| 141 | + |
| 142 | + /// This is a known metatype value. |
| 143 | + Metatype, |
| 144 | + |
| 145 | + /// This is a function, represented as a SILFunction. |
| 146 | + Function, |
| 147 | + |
| 148 | + /// This is an integer constant. |
| 149 | + Integer, |
| 150 | + |
| 151 | + /// This can be an array, struct, tuple, etc. |
| 152 | + Aggregate, |
| 153 | + }; |
| 154 | + |
| 155 | + /// For constant values, return the type classification of this value. |
| 156 | + Kind getKind() const; |
| 157 | + |
| 158 | + /// Return true if this represents a constant value. |
| 159 | + bool isConstant() const { |
| 160 | + auto kind = getKind(); |
| 161 | + return kind != Unknown; |
| 162 | + } |
| 163 | + |
| 164 | + static SymbolicValue getUnknown(SILNode *node, UnknownReason reason, |
| 165 | + llvm::ArrayRef<SourceLoc> callStack, |
| 166 | + ASTContext &astContext); |
| 167 | + |
| 168 | + /// Return true if this represents an unknown result. |
| 169 | + bool isUnknown() const { return getKind() == Unknown; } |
| 170 | + |
| 171 | + /// Return the call stack for an unknown result. |
| 172 | + ArrayRef<SourceLoc> getUnknownCallStack() const; |
| 173 | + |
| 174 | + /// Return the node that triggered an unknown result. |
| 175 | + SILNode *getUnknownNode() const; |
| 176 | + |
| 177 | + /// Return the reason an unknown result was generated. |
| 178 | + UnknownReason getUnknownReason() const; |
| 179 | + |
| 180 | + static SymbolicValue getMetatype(CanType type) { |
| 181 | + SymbolicValue result; |
| 182 | + result.representationKind = RK_Metatype; |
| 183 | + result.value.metatype = type.getPointer(); |
| 184 | + return result; |
| 185 | + } |
| 186 | + |
| 187 | + CanType getMetatypeValue() const { |
| 188 | + assert(representationKind == RK_Metatype); |
| 189 | + return CanType(value.metatype); |
| 190 | + } |
| 191 | + |
| 192 | + static SymbolicValue getFunction(SILFunction *fn) { |
| 193 | + assert(fn && "Function cannot be null"); |
| 194 | + SymbolicValue result; |
| 195 | + result.representationKind = RK_Function; |
| 196 | + result.value.function = fn; |
| 197 | + return result; |
| 198 | + } |
| 199 | + |
| 200 | + SILFunction *getFunctionValue() const { |
| 201 | + assert(getKind() == Function); |
| 202 | + return value.function; |
| 203 | + } |
| 204 | + |
| 205 | + static SymbolicValue getInteger(int64_t value, unsigned bitWidth); |
| 206 | + static SymbolicValue getInteger(const APInt &value, |
| 207 | + ASTContext &astContext); |
| 208 | + |
| 209 | + APInt getIntegerValue() const; |
| 210 | + unsigned getIntegerValueBitWidth() const; |
| 211 | + |
| 212 | + /// This returns an aggregate value with the specified elements in it. This |
| 213 | + /// copies the elements into the specified ASTContext. |
| 214 | + static SymbolicValue getAggregate(ArrayRef<SymbolicValue> elements, |
| 215 | + ASTContext &astContext); |
| 216 | + |
| 217 | + ArrayRef<SymbolicValue> getAggregateValue() const; |
| 218 | + |
| 219 | + //===--------------------------------------------------------------------===// |
| 220 | + // Helpers |
| 221 | + |
| 222 | + /// Dig through single element aggregates, return the ultimate thing inside of |
| 223 | + /// it. This is useful when dealing with integers and floats, because they |
| 224 | + /// are often wrapped in single-element struct wrappers. |
| 225 | + SymbolicValue lookThroughSingleElementAggregates() const; |
| 226 | + |
| 227 | + /// Given that this is an 'Unknown' value, emit diagnostic notes providing |
| 228 | + /// context about what the problem is. If there is no location for some |
| 229 | + /// reason, we fall back to using the specified location. |
| 230 | + void emitUnknownDiagnosticNotes(SILLocation fallbackLoc); |
| 231 | + |
| 232 | + /// Clone this SymbolicValue into the specified ASTContext and return the new |
| 233 | + /// version. This only works for valid constants. |
| 234 | + SymbolicValue cloneInto(ASTContext &astContext) const; |
| 235 | + |
| 236 | + void print(llvm::raw_ostream &os, unsigned indent = 0) const; |
| 237 | + void dump() const; |
| 238 | +}; |
| 239 | + |
| 240 | +static_assert(sizeof(SymbolicValue) == 2 * sizeof(void *), |
| 241 | + "SymbolicValue should stay small and POD"); |
| 242 | + |
| 243 | +inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, SymbolicValue val) { |
| 244 | + val.print(os); |
| 245 | + return os; |
| 246 | +} |
| 247 | + |
| 248 | +} // end namespace swift |
| 249 | + |
| 250 | +#endif |
0 commit comments