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
|
//===--- ExtraInhabitants.cpp - Routines for extra inhabitants ------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements routines for working with extra inhabitants.
//
//===----------------------------------------------------------------------===//
#include "ExtraInhabitants.h"
#include "BitPatternBuilder.h"
#include "IRGenModule.h"
#include "IRGenFunction.h"
#include "SwiftTargetInfo.h"
#include "swift/ABI/MetadataValues.h"
using namespace swift;
using namespace irgen;
static uint8_t getNumLowObjCReservedBits(const IRGenModule &IGM) {
if (!IGM.ObjCInterop)
return 0;
// Get the index of the first non-reserved bit.
auto &mask = IGM.TargetInfo.ObjCPointerReservedBits;
return mask.asAPInt().countTrailingOnes();
}
PointerInfo PointerInfo::forHeapObject(const IRGenModule &IGM) {
return { Alignment(1), getNumLowObjCReservedBits(IGM), IsNotNullable };
}
PointerInfo PointerInfo::forFunction(const IRGenModule &IGM) {
return { Alignment(1), 0, IsNotNullable };
}
/*****************************************************************************/
/// Return the number of extra inhabitants for a pointer that reserves
/// the given number of low bits.
unsigned PointerInfo::getExtraInhabitantCount(const IRGenModule &IGM) const {
// FIXME: We could also make extra inhabitants using spare bits, but we
// probably don't need to.
uint64_t rawCount =
IGM.TargetInfo.LeastValidPointerValue >> NumReservedLowBits;
if (Nullable) rawCount--;
// The runtime limits the count.
return std::min(uint64_t(ValueWitnessFlags::MaxNumExtraInhabitants),
rawCount);
}
unsigned irgen::getHeapObjectExtraInhabitantCount(const IRGenModule &IGM) {
// This must be consistent with the extra inhabitant count produced
// by the runtime's getHeapObjectExtraInhabitantCount function in
// KnownMetadata.cpp.
return PointerInfo::forHeapObject(IGM).getExtraInhabitantCount(IGM);
}
/*****************************************************************************/
APInt PointerInfo::getFixedExtraInhabitantValue(const IRGenModule &IGM,
unsigned bits,
unsigned index,
unsigned offset) const {
unsigned pointerSizeInBits = IGM.getPointerSize().getValueInBits();
assert(index < getExtraInhabitantCount(IGM) &&
"pointer extra inhabitant out of bounds");
assert(bits >= pointerSizeInBits + offset);
if (Nullable) index++;
uint64_t value = (uint64_t)index << NumReservedLowBits;
auto valueBits = BitPatternBuilder(IGM.Triple.isLittleEndian());
valueBits.appendClearBits(offset);
valueBits.append(APInt(pointerSizeInBits, value));
valueBits.padWithClearBitsTo(bits);
return valueBits.build().value();
}
APInt irgen::getHeapObjectFixedExtraInhabitantValue(const IRGenModule &IGM,
unsigned bits,
unsigned index,
unsigned offset) {
// This must be consistent with the extra inhabitant calculation implemented
// in the runtime's storeHeapObjectExtraInhabitant and
// getHeapObjectExtraInhabitantIndex functions in KnownMetadata.cpp.
return PointerInfo::forHeapObject(IGM)
.getFixedExtraInhabitantValue(IGM, bits, index, offset);
}
/*****************************************************************************/
llvm::Value *PointerInfo::getExtraInhabitantIndex(IRGenFunction &IGF,
Address src) const {
llvm::BasicBlock *contBB = IGF.createBasicBlock("is-valid-pointer");
SmallVector<std::pair<llvm::BasicBlock*, llvm::Value*>, 3> phiValues;
auto invalidIndex = llvm::ConstantInt::getSigned(IGF.IGM.Int32Ty, -1);
src = IGF.Builder.CreateElementBitCast(src, IGF.IGM.SizeTy);
// Check if the inhabitant is below the least valid pointer value.
llvm::Value *val = IGF.Builder.CreateLoad(src);
{
llvm::Value *leastValid = llvm::ConstantInt::get(IGF.IGM.SizeTy,
IGF.IGM.TargetInfo.LeastValidPointerValue);
llvm::Value *isValid = IGF.Builder.CreateICmpUGE(val, leastValid);
phiValues.push_back({IGF.Builder.GetInsertBlock(), invalidIndex});
llvm::BasicBlock *invalidBB = IGF.createBasicBlock("is-invalid-pointer");
IGF.Builder.CreateCondBr(isValid, contBB, invalidBB);
IGF.Builder.emitBlock(invalidBB);
}
// If null is not an extra inhabitant, check if the inhabitant is null.
if (Nullable) {
auto null = llvm::ConstantInt::get(IGF.IGM.SizeTy, 0);
auto isNonNull = IGF.Builder.CreateICmpNE(val, null);
phiValues.push_back({IGF.Builder.GetInsertBlock(), invalidIndex});
llvm::BasicBlock *nonnullBB = IGF.createBasicBlock("is-nonnull-pointer");
IGF.Builder.CreateCondBr(isNonNull, nonnullBB, contBB);
IGF.Builder.emitBlock(nonnullBB);
}
// Check if the inhabitant has any reserved low bits set.
// FIXME: This check is unneeded if the type is known to be pure Swift.
if (NumReservedLowBits) {
auto objcMask =
llvm::ConstantInt::get(IGF.IGM.SizeTy, (1 << NumReservedLowBits) - 1);
llvm::Value *masked = IGF.Builder.CreateAnd(val, objcMask);
llvm::Value *maskedZero = IGF.Builder.CreateICmpEQ(masked,
llvm::ConstantInt::get(IGF.IGM.SizeTy, 0));
phiValues.push_back({IGF.Builder.GetInsertBlock(), invalidIndex});
llvm::BasicBlock *untaggedBB = IGF.createBasicBlock("is-untagged-pointer");
IGF.Builder.CreateCondBr(maskedZero, untaggedBB, contBB);
IGF.Builder.emitBlock(untaggedBB);
}
// The inhabitant is an invalid pointer. Derive its extra inhabitant index.
{
llvm::Value *index = val;
// Shift away the reserved bits.
if (NumReservedLowBits) {
index = IGF.Builder.CreateLShr(index,
IGF.IGM.getSize(Size(NumReservedLowBits)));
}
// Subtract one if we have a nullable type.
if (Nullable) {
index = IGF.Builder.CreateSub(index, IGF.IGM.getSize(Size(1)));
}
// Truncate down to i32 if necessary.
if (index->getType() != IGF.IGM.Int32Ty) {
index = IGF.Builder.CreateTrunc(index, IGF.IGM.Int32Ty);
}
phiValues.push_back({IGF.Builder.GetInsertBlock(), index});
IGF.Builder.CreateBr(contBB);
IGF.Builder.emitBlock(contBB);
}
// Build the result phi.
auto phi = IGF.Builder.CreatePHI(IGF.IGM.Int32Ty, phiValues.size());
for (auto &entry : phiValues) {
phi->addIncoming(entry.second, entry.first);
}
return phi;
}
llvm::Value *irgen::getHeapObjectExtraInhabitantIndex(IRGenFunction &IGF,
Address src) {
// This must be consistent with the extra inhabitant calculation implemented
// in the runtime's getHeapObjectExtraInhabitantIndex function in
// KnownMetadata.cpp.
return PointerInfo::forHeapObject(IGF.IGM).getExtraInhabitantIndex(IGF, src);
}
/*****************************************************************************/
void PointerInfo::storeExtraInhabitant(IRGenFunction &IGF,
llvm::Value *index,
Address dest) const {
if (index->getType() != IGF.IGM.SizeTy) {
index = IGF.Builder.CreateZExt(index, IGF.IGM.SizeTy);
}
if (Nullable) {
index = IGF.Builder.CreateAdd(index, IGF.IGM.getSize(Size(1)));
}
if (NumReservedLowBits) {
index = IGF.Builder.CreateShl(index,
llvm::ConstantInt::get(IGF.IGM.SizeTy, NumReservedLowBits));
}
dest = IGF.Builder.CreateElementBitCast(dest, IGF.IGM.SizeTy);
IGF.Builder.CreateStore(index, dest);
}
void irgen::storeHeapObjectExtraInhabitant(IRGenFunction &IGF,
llvm::Value *index,
Address dest) {
// This must be consistent with the extra inhabitant calculation implemented
// in the runtime's storeHeapObjectExtraInhabitant function in
// KnownMetadata.cpp.
PointerInfo::forHeapObject(IGF.IGM)
.storeExtraInhabitant(IGF, index, dest);
}
|