1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
//===--- Explosion.h - Exploded R-Value Representation ----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A storage structure for holding an exploded r-value. An exploded
// r-value has been separated into individual components. Only types
// with non-resilient structure may be exploded.
//
// The standard use for explosions is for argument-passing.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_EXPLOSION_H
#define SWIFT_IRGEN_EXPLOSION_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "IRGen.h"
#include "IRGenFunction.h"
namespace swift {
namespace irgen {
/// The representation for an explosion is just a list of raw LLVM
/// values. The meaning of these values is imposed externally by the
/// type infos, except that it is expected that they will be passed
/// as arguments in exactly this way.
class Explosion {
unsigned NextValue;
SmallVector<llvm::Value*, 8> Values;
public:
Explosion() : NextValue(0) {}
// We want to be a move-only type.
Explosion(const Explosion &) = delete;
Explosion &operator=(const Explosion &) = delete;
Explosion(Explosion &&other) : NextValue(0) {
// Do an uninitialized copy of the non-consumed elements.
Values.resize_for_overwrite(other.size());
std::uninitialized_copy(other.begin(), other.end(), Values.begin());
// Remove everything from the other explosion.
other.reset();
}
Explosion &operator=(Explosion &&o) {
assert(empty() && "explosion had values remaining when reassigned!");
NextValue = o.NextValue;
Values.swap(o.Values);
o.reset();
return *this;
}
Explosion(llvm::Value *singleValue) : NextValue(0) {
add(singleValue);
}
~Explosion() {
assert(empty() && "explosion had values remaining when destroyed!");
}
bool empty() const {
return NextValue == Values.size();
}
size_t size() const {
return Values.size() - NextValue;
}
using iterator = SmallVector<llvm::Value *, 8>::iterator;
iterator begin() { return Values.begin() + NextValue; }
iterator end() { return Values.end(); }
using const_iterator = SmallVector<llvm::Value *, 8>::const_iterator;
const_iterator begin() const { return Values.begin() + NextValue; }
const_iterator end() const { return Values.end(); }
/// Add a value to the end of this exploded r-value.
void add(llvm::Value *value) {
assert(value && "adding null value to explosion");
assert(NextValue == 0 && "adding to partially-claimed explosion?");
Values.push_back(value);
}
void add(ArrayRef<llvm::Value*> values) {
#ifndef NDEBUG
for (auto value : values)
assert(value && "adding null value to explosion");
#endif
assert(NextValue == 0 && "adding to partially-claimed explosion?");
Values.append(values.begin(), values.end());
}
void insert(unsigned index, llvm::Value *value) {
Values.insert(Values.begin() + index, value);
}
/// Return an array containing the given range of values. The values
/// are not claimed.
ArrayRef<llvm::Value*> getRange(unsigned from, unsigned to) const {
assert(from <= to);
assert(to <= Values.size());
return llvm::ArrayRef(begin() + from, to - from);
}
/// Return an array containing all of the remaining values. The values
/// are not claimed.
ArrayRef<llvm::Value *> getAll() {
return llvm::ArrayRef(begin(), Values.size() - NextValue);
}
/// Transfer ownership of the next N values to the given explosion.
void transferInto(Explosion &other, unsigned n) {
other.add(claim(n));
}
/// The next N values have been claimed in some indirect way (e.g.
/// using getRange() and the like); just give up on them.
void markClaimed(unsigned n) {
assert(NextValue + n <= Values.size());
NextValue += n;
}
/// Claim and return the next value in this explosion.
llvm::Value *claimNext() {
assert(NextValue < Values.size());
return Values[NextValue++];
}
llvm::Constant *claimNextConstant() {
return cast<llvm::Constant>(claimNext());
}
/// Claim and return the next N values in this explosion.
ArrayRef<llvm::Value*> claim(unsigned n) {
assert(NextValue + n <= Values.size());
auto array = llvm::ArrayRef(begin(), n);
NextValue += n;
return array;
}
/// Claim and return all the values in this explosion.
ArrayRef<llvm::Value*> claimAll() {
return claim(size());
}
// These are all kindof questionable.
/// Without changing any state, take the last claimed value,
/// if there is one.
llvm::Value *getLastClaimed() {
assert(NextValue > 0);
return Values[NextValue-1];
}
/// Claim and remove the last item in the array.
/// Unlike the normal 'claim' methods, the item is gone forever.
llvm::Value *takeLast() {
assert(!empty());
auto result = Values.back();
Values.pop_back();
return result;
}
/// Reset this explosion.
void reset() {
NextValue = 0;
Values.clear();
}
/// Do an operation while borrowing the values from this explosion.
template <class Fn>
void borrowing(Fn &&fn) {
auto savedNextValue = NextValue;
fn(*this);
assert(empty() &&
"didn't claim all values from the explosion during the borrow");
NextValue = savedNextValue;
}
void print(llvm::raw_ostream &OS);
void dump();
};
/// An explosion schema is essentially the type of an Explosion.
class ExplosionSchema {
public:
/// The schema for one atom of the explosion.
class Element {
llvm::Type *Type;
Alignment::int_type Align;
Element() = default;
public:
static Element forScalar(llvm::Type *type) {
Element e;
e.Type = type;
e.Align = 0;
return e;
}
static Element forAggregate(llvm::Type *type, Alignment align) {
assert(align.getValue() != 0 && "alignment with zero value!");
Element e;
e.Type = type;
e.Align = align.getValue();
return e;
}
bool isScalar() const { return Align == 0; }
llvm::Type *getScalarType() const { assert(isScalar()); return Type; }
bool isAggregate() const { return !isScalar(); }
llvm::Type *getAggregateType() const {
assert(isAggregate());
return Type;
}
Alignment getAggregateAlignment() const {
assert(isAggregate());
return Alignment(Align);
}
};
private:
SmallVector<Element, 8> Elements;
bool ContainsAggregate;
public:
ExplosionSchema() : ContainsAggregate(false) {}
/// Return the number of elements in this schema.
unsigned size() const { return Elements.size(); }
bool empty() const { return Elements.empty(); }
/// Does this schema contain an aggregate element?
bool containsAggregate() const { return ContainsAggregate; }
/// Does this schema consist solely of one aggregate element?
bool isSingleAggregate() const {
return size() == 1 && containsAggregate();
}
const Element &operator[](size_t index) const {
return Elements[index];
}
using iterator = SmallVectorImpl<Element>::iterator;
using const_iterator = SmallVectorImpl<Element>::const_iterator;
iterator begin() { return Elements.begin(); }
iterator end() { return Elements.end(); }
const_iterator begin() const { return Elements.begin(); }
const_iterator end() const { return Elements.end(); }
void add(Element e) {
Elements.push_back(e);
ContainsAggregate |= e.isAggregate();
}
/// Produce the correct type for a direct return of this schema,
/// which is assumed to contain only scalars. This is defined as:
/// - void, if the schema is empty;
/// - the element type, if the schema contains exactly one element;
/// - an anonymous struct type concatenating those types, otherwise.
llvm::Type *getScalarResultType(IRGenModule &IGM) const;
};
/// A peepholed explosion of an optional scalar value.
class OptionalExplosion {
public:
enum Kind {
/// The value is known statically to be `Optional.none`.
/// The explosion is empty.
None,
/// The value is known statically to be `Optional.some(x)`.
/// The explosion is the wrapped value `x`.
Some,
/// It is unknown statically what case the optional is in.
/// The explosion is an optional value.
Optional
};
private:
Kind kind;
Explosion value;
OptionalExplosion(Kind kind) : kind(kind) {}
public:
static OptionalExplosion forNone() {
return None;
}
template <class Fn>
static OptionalExplosion forSome(Fn &&fn) {
OptionalExplosion result(Some);
std::forward<Fn>(fn)(result.value);
return result;
}
template <class Fn>
static OptionalExplosion forOptional(Fn &&fn) {
OptionalExplosion result(Optional);
std::forward<Fn>(fn)(result.value);
return result;
}
Kind getKind() const { return kind; }
bool isNone() const { return kind == None; }
bool isSome() const { return kind == Some; }
bool isOptional() const { return kind == Optional; }
Explosion &getSomeExplosion() {
assert(kind == Some);
return value;
}
Explosion &getOptionalExplosion() {
assert(kind == Optional);
return value;
}
};
} // end namespace irgen
} // end namespace swift
#endif
|