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
|
//===-- gen/abi-generic.h - Generic Target ABI helpers ----------*- C++ -*-===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// Contains helpers for handling rewrites common to more than one target ABI.
//
//===----------------------------------------------------------------------===//
#pragma once
#include "gen/abi.h"
#include "gen/irstate.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/structs.h"
#include "gen/tollvm.h"
struct LLTypeMemoryLayout {
// Structs and static arrays are folded recursively to scalars or anonymous
// structs.
// Pointer types are folded to an integer type.
// Vector types are folded to a universal vector type.
static LLType *fold(LLType *type) {
// T* => same-sized integer
if (type->isPointerTy()) {
return LLIntegerType::get(gIR->context(), getTypeBitSize(type));
}
// <N x T> => same-sized <M x i8>
if (type->isVectorTy()) {
const size_t sizeInBits = getTypeBitSize(type);
assert(sizeInBits % 8 == 0);
return llvm::VectorType::get(LLIntegerType::get(gIR->context(), 8),
sizeInBits / 8
#if LDC_LLVM_VER >= 1100
,
/*Scalable=*/false
#endif
);
}
if (LLStructType *structType = isaStruct(type)) {
unsigned numElements = structType->getNumElements();
// fold each element
std::vector<LLType *> elements;
elements.reserve(numElements);
for (unsigned i = 0; i < numElements; ++i) {
elements.push_back(fold(structType->getElementType(i)));
}
// single element? then discard wrapping struct
if (numElements == 1) {
return elements[0];
}
return LLStructType::get(gIR->context(), elements,
structType->isPacked());
}
if (LLArrayType *arrayType = isaArray(type)) {
unsigned numElements = arrayType->getNumElements();
LLType *foldedElementType = fold(arrayType->getElementType());
// single element? then fold to scalar
if (numElements == 1) {
return foldedElementType;
}
// otherwise: convert to struct of N folded elements
std::vector<LLType *> elements(numElements, foldedElementType);
return LLStructType::get(gIR->context(), elements);
}
return type;
}
// Checks two LLVM types for memory-layout equivalency.
static bool typesAreEquivalent(LLType *a, LLType *b) {
if (a == b) {
return true;
}
if (!a || !b) {
return false;
}
return fold(a) == fold(b);
}
};
//////////////////////////////////////////////////////////////////////////////
/// Removes padding fields for (non-union-containing!) structs
struct RemoveStructPadding : ABIRewrite {
LLValue *put(DValue *v, bool, bool) override {
return DtoUnpaddedStruct(v->type->toBasetype(), DtoLVal(v));
}
LLValue *getLVal(Type *dty, LLValue *v) override {
LLValue *lval = DtoAlloca(dty, ".RemoveStructPadding_dump");
// Make sure the padding is zero, so struct comparisons work.
// TODO: Only do this if there's padding, and/or only initialize padding.
DtoMemSetZero(lval, DtoConstSize_t(getTypeAllocSize(DtoType(dty))));
DtoPaddedStruct(dty->toBasetype(), v, lval);
return lval;
}
LLType *type(Type *t) override {
return DtoUnpaddedStructType(t->toBasetype());
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Base for ABI rewrites bit-casting an argument to another LL type.
* If the argument isn't in memory already, it is dumped to memory to perform
* the bit-cast.
*/
struct BaseBitcastABIRewrite : ABIRewrite {
static unsigned getMaxAlignment(LLType *llType, Type *dType) {
return std::max(getABITypeAlign(llType), DtoAlignment(dType));
}
LLValue *put(DValue *dv, bool, bool) override {
LLType *asType = type(dv->type);
const unsigned alignment = getMaxAlignment(asType, dv->type);
const char *name = ".BaseBitcastABIRewrite_arg";
if (!dv->isLVal()) {
LLValue *dump = DtoAllocaDump(dv, asType, alignment,
".BaseBitcastABIRewrite_arg_storage");
return DtoLoad(dump, name);
}
LLValue *address = DtoLVal(dv);
LLType *pointeeType = address->getType()->getPointerElementType();
if (getTypeStoreSize(asType) > getTypeAllocSize(pointeeType) ||
alignment > DtoAlignment(dv->type)) {
// not enough allocated memory or insufficiently aligned
LLValue *paddedDump = DtoRawAlloca(
asType, alignment, ".BaseBitcastABIRewrite_padded_arg_storage");
DtoMemCpy(paddedDump, address,
DtoConstSize_t(getTypeAllocSize(pointeeType)));
return DtoLoad(paddedDump, name);
}
address = DtoBitCast(address, getPtrToType(asType));
return DtoLoad(address, name);
}
LLValue *getLVal(Type *dty, LLValue *v) override {
const unsigned alignment = getMaxAlignment(v->getType(), dty);
return DtoAllocaDump(v, DtoType(dty), alignment,
".BaseBitcastABIRewrite_param_storage");
}
void applyToIfNotObsolete(IrFuncTyArg &arg, LLType *finalLType = nullptr) {
if (!finalLType)
finalLType = type(arg.type);
if (!LLTypeMemoryLayout::typesAreEquivalent(arg.ltype, finalLType))
applyTo(arg, finalLType);
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Bit-casts an argument based on the front-end toArgTypes* machinery.
*/
struct ArgTypesRewrite : BaseBitcastABIRewrite {
LLType *type(Type *t) override {
LLType *rewrittenType = TargetABI::getRewrittenArgType(t->toBasetype());
assert(rewrittenType);
return rewrittenType;
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Bit-casts an argument to an integer of the same or next bigger size.
*/
struct IntegerRewrite : BaseBitcastABIRewrite {
static LLType *getIntegerType(unsigned minSizeInBytes) {
if (minSizeInBytes > 16) {
return nullptr;
}
unsigned size = minSizeInBytes;
switch (minSizeInBytes) {
case 0:
size = 1;
break;
case 3:
size = 4;
break;
case 5:
case 6:
case 7:
size = 8;
break;
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
size = 16;
break;
default:
break;
}
return LLIntegerType::get(gIR->context(), size * 8);
}
LLType *type(Type *t) override { return getIntegerType(t->size()); }
};
//////////////////////////////////////////////////////////////////////////////
/**
* Implements indirect high-level-by-value semantics defined like this:
* Instead of passing a copy of the original argument directly to the callee,
* the caller makes a bitcopy on its stack first and then passes a pointer to
* that copy to the callee.
* The pointer is passed as regular parameter and hence occupies either a
* register or a function parameters stack slot.
*
* This differs from LLVM's byval attribute for pointer parameters.
* The byval attribute instructs LLVM to bitcopy the IR argument pointee onto
* the callee parameters stack. The callee's IR parameter is an implicit pointer
* to that private copy.
*/
struct IndirectByvalRewrite : ABIRewrite {
LLValue *put(DValue *v, bool isLValueExp, bool) override {
// if the argument expression is an rvalue and the LL value already in
// memory, then elide an additional copy
if (!isLValueExp && v->isLVal())
return DtoLVal(v);
return DtoAllocaDump(v, ".hidden_copy_for_IndirectByvalRewrite");
}
LLValue *getLVal(Type *dty, LLValue *v) override {
return DtoBitCast(v, DtoPtrToType(dty));
}
LLType *type(Type *t) override { return DtoPtrToType(t); }
void applyTo(IrFuncTyArg &arg, LLType *finalLType = nullptr) override {
ABIRewrite::applyTo(arg, finalLType);
// the copy is treated as a local variable of the callee
// hence add the NoAlias and NoCapture attributes
auto &attrs = arg.attrs;
attrs.clear();
attrs.addAttribute(LLAttribute::NoAlias);
attrs.addAttribute(LLAttribute::NoCapture);
if (auto alignment = DtoAlignment(arg.type))
attrs.addAlignmentAttr(alignment);
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Bit-casts a Homogeneous Floating-point/Vector Aggregate (HFVA) to an array
* of floats/vectors.
*/
struct HFVAToArray : BaseBitcastABIRewrite {
const int maxElements;
HFVAToArray(int max = 4) : maxElements(max) {}
LLType *type(Type *t) override {
LLType *hfvaType = nullptr;
if (TargetABI::isHFVA(t, maxElements, &hfvaType))
return hfvaType;
llvm_unreachable("Type t should be an HFVA");
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Bit-casts an argument to an array of integers of the specified size.
*/
template <int elementSize> struct CompositeToArray : BaseBitcastABIRewrite {
LLType *type(Type *t) override {
size_t length = (t->size() + elementSize - 1) / elementSize;
return LLArrayType::get(LLIntegerType::get(gIR->context(), elementSize * 8),
length);
}
};
// Bit-casts to an array of i32.
using CompositeToArray32 = CompositeToArray<4>;
// Bit-casts to an array of i64.
using CompositeToArray64 = CompositeToArray<8>;
|