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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
//===--- ARCEntryPointBuilder.h ---------------------------------*- 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
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_LLVMPASSES_ARCENTRYPOINTBUILDER_H
#define SWIFT_LLVMPASSES_ARCENTRYPOINTBUILDER_H
#include "swift/Basic/LLVM.h"
#include "swift/Basic/NullablePtr.h"
#include "swift/Runtime/Config.h"
#include "swift/Runtime/RuntimeFnWrappersGen.h"
#include "llvm/ADT/APInt.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
namespace swift {
namespace RuntimeConstants {
const auto ReadNone = llvm::Attribute::ReadNone;
const auto ReadOnly = llvm::Attribute::ReadOnly;
const auto NoReturn = llvm::Attribute::NoReturn;
const auto NoUnwind = llvm::Attribute::NoUnwind;
const auto ZExt = llvm::Attribute::ZExt;
const auto FirstParamReturned = llvm::Attribute::Returned;
}
using namespace RuntimeConstants;
/// A class for building ARC entry points. It is a composition wrapper around an
/// IRBuilder and a constant Cache. It cannot be moved or copied. It is meant
/// to be created once and passed around by reference.
class ARCEntryPointBuilder {
using IRBuilder = llvm::IRBuilder<>;
using Constant = llvm::Constant;
using Type = llvm::Type;
using Function = llvm::Function;
using Instruction = llvm::Instruction;
using CallInst = llvm::CallInst;
using Value = llvm::Value;
using Module = llvm::Module;
using AttributeList = llvm::AttributeList;
using Attribute = llvm::Attribute;
using APInt = llvm::APInt;
// The builder which we are wrapping.
IRBuilder B;
// The constant cache.
NullablePtr<Constant> Retain;
NullablePtr<Constant> Release;
NullablePtr<Constant> CheckUnowned;
NullablePtr<Constant> RetainN;
NullablePtr<Constant> ReleaseN;
NullablePtr<Constant> UnknownObjectRetainN;
NullablePtr<Constant> UnknownObjectReleaseN;
NullablePtr<Constant> BridgeRetainN;
NullablePtr<Constant> BridgeReleaseN;
// The type cache.
NullablePtr<Type> ObjectPtrTy;
NullablePtr<Type> BridgeObjectPtrTy;
llvm::CallingConv::ID DefaultCC;
llvm::CallInst *CreateCall(Constant *Fn, Value *V) {
CallInst *CI = B.CreateCall(
cast<llvm::FunctionType>(cast<llvm::Function>(Fn)->getValueType()), Fn,
V);
if (auto Fun = llvm::dyn_cast<llvm::Function>(Fn))
CI->setCallingConv(Fun->getCallingConv());
return CI;
}
llvm::CallInst *CreateCall(Constant *Fn, llvm::ArrayRef<Value *> Args) {
CallInst *CI = B.CreateCall(
cast<llvm::FunctionType>(cast<llvm::Function>(Fn)->getValueType()), Fn,
Args);
if (auto Fun = llvm::dyn_cast<llvm::Function>(Fn))
CI->setCallingConv(Fun->getCallingConv());
return CI;
}
public:
ARCEntryPointBuilder(Function &F)
: B(&*F.begin()), Retain(), ObjectPtrTy(),
DefaultCC(SWIFT_DEFAULT_LLVM_CC) { }
~ARCEntryPointBuilder() = default;
ARCEntryPointBuilder(ARCEntryPointBuilder &&) = delete;
ARCEntryPointBuilder(const ARCEntryPointBuilder &) = delete;
ARCEntryPointBuilder &operator=(const ARCEntryPointBuilder &) = delete;
void operator=(ARCEntryPointBuilder &&C) = delete;
void setInsertPoint(Instruction *I) {
B.SetInsertPoint(I);
}
Value *createInsertValue(Value *V1, Value *V2, unsigned Idx) {
return B.CreateInsertValue(V1, V2, Idx);
}
Value *createExtractValue(Value *V, unsigned Idx) {
return B.CreateExtractValue(V, Idx);
}
Value *createIntToPtr(Value *V, Type *Ty) {
return B.CreateIntToPtr(V, Ty);
}
CallInst *createRetain(Value *V, CallInst *OrigI) {
// Cast just to make sure that we have the right type.
V = B.CreatePointerCast(V, getObjectPtrTy());
// Create the call.
CallInst *CI = CreateCall(getRetain(OrigI), V);
return CI;
}
CallInst *createRelease(Value *V, CallInst *OrigI) {
// Cast just to make sure that we have the right type.
V = B.CreatePointerCast(V, getObjectPtrTy());
// Create the call.
CallInst *CI = CreateCall(getRelease(OrigI), V);
return CI;
}
CallInst *createCheckUnowned(Value *V, CallInst *OrigI) {
// Cast just to make sure that we have the right type.
V = B.CreatePointerCast(V, getObjectPtrTy());
CallInst *CI = CreateCall(getCheckUnowned(OrigI), V);
return CI;
}
CallInst *createRetainN(Value *V, uint32_t n, CallInst *OrigI) {
// Cast just to make sure that we have the right object type.
V = B.CreatePointerCast(V, getObjectPtrTy());
CallInst *CI = CreateCall(getRetainN(OrigI), {V, getIntConstant(n)});
return CI;
}
CallInst *createReleaseN(Value *V, uint32_t n, CallInst *OrigI) {
// Cast just to make sure we have the right object type.
V = B.CreatePointerCast(V, getObjectPtrTy());
CallInst *CI = CreateCall(getReleaseN(OrigI), {V, getIntConstant(n)});
return CI;
}
CallInst *createUnknownObjectRetainN(Value *V, uint32_t n, CallInst *OrigI) {
// Cast just to make sure that we have the right object type.
V = B.CreatePointerCast(V, getObjectPtrTy());
CallInst *CI =
CreateCall(getUnknownObjectRetainN(OrigI), {V, getIntConstant(n)});
return CI;
}
CallInst *createUnknownObjectReleaseN(Value *V, uint32_t n, CallInst *OrigI) {
// Cast just to make sure we have the right object type.
V = B.CreatePointerCast(V, getObjectPtrTy());
CallInst *CI =
CreateCall(getUnknownObjectReleaseN(OrigI), {V, getIntConstant(n)});
return CI;
}
CallInst *createBridgeRetainN(Value *V, uint32_t n, CallInst *OrigI) {
// Cast just to make sure we have the right object type.
V = B.CreatePointerCast(V, getBridgeObjectPtrTy());
CallInst *CI = CreateCall(getBridgeRetainN(OrigI), {V, getIntConstant(n)});
return CI;
}
CallInst *createBridgeReleaseN(Value *V, uint32_t n, CallInst *OrigI) {
// Cast just to make sure we have the right object type.
V = B.CreatePointerCast(V, getBridgeObjectPtrTy());
CallInst *CI = CreateCall(getBridgeReleaseN(OrigI), {V, getIntConstant(n)});
return CI;
}
bool isNonAtomic(CallInst *I) {
// If we have an intrinsic, we know it must be an objc intrinsic. All objc
// intrinsics are atomic today.
if (I->getIntrinsicID() != llvm::Intrinsic::not_intrinsic)
return false;
return (I->getCalledFunction()->getName().contains("nonatomic"));
}
bool isAtomic(CallInst *I) {
return !isNonAtomic(I);
}
/// Perform a pointer cast of pointer value \p V to \p Ty if \p V has a
/// different type than \p Ty. If \p V equals \p Ty, just return V.
llvm::Value *maybeCast(llvm::Value *V, llvm::Type *Ty) {
if (V->getType() == Ty)
return V;
return B.CreatePointerCast(V, Ty);
}
private:
Module &getModule() {
return *B.GetInsertBlock()->getModule();
}
/// getRetain - Return a callable function for swift_retain.
Constant *getRetain(CallInst *OrigI) {
if (Retain)
return Retain.get();
auto *ObjectPtrTy = getObjectPtrTy();
llvm::Constant *cache = nullptr;
Retain = getRuntimeFn(
getModule(), cache,
isNonAtomic(OrigI) ? "swift_nonatomic_retain" : "swift_retain",
DefaultCC, RuntimeAvailability::AlwaysAvailable, {ObjectPtrTy},
{ObjectPtrTy}, {NoUnwind, FirstParamReturned}, {});
return Retain.get();
}
/// getRelease - Return a callable function for swift_release.
Constant *getRelease(CallInst *OrigI) {
if (Release)
return Release.get();
auto *ObjectPtrTy = getObjectPtrTy();
auto *VoidTy = Type::getVoidTy(getModule().getContext());
llvm::Constant *cache = nullptr;
Release = getRuntimeFn(getModule(), cache,
isNonAtomic(OrigI) ? "swift_nonatomic_release"
: "swift_release",
DefaultCC, RuntimeAvailability::AlwaysAvailable,
{VoidTy}, {ObjectPtrTy}, {NoUnwind}, {});
return Release.get();
}
Constant *getCheckUnowned(CallInst *OrigI) {
if (CheckUnowned)
return CheckUnowned.get();
auto *ObjectPtrTy = getObjectPtrTy();
auto &M = getModule();
auto AttrList = AttributeList::get(M.getContext(), 1, Attribute::NoCapture);
AttrList = AttrList.addFnAttribute(M.getContext(), Attribute::NoUnwind);
CheckUnowned = cast<llvm::Function>(
M.getOrInsertFunction("swift_checkUnowned", AttrList,
Type::getVoidTy(M.getContext()), ObjectPtrTy)
.getCallee());
if (llvm::Triple(M.getTargetTriple()).isOSBinFormatCOFF() &&
!llvm::Triple(M.getTargetTriple()).isOSCygMing())
if (auto *F = llvm::dyn_cast<llvm::Function>(CheckUnowned.get()))
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
return CheckUnowned.get();
}
/// getRetainN - Return a callable function for swift_retain_n.
Constant *getRetainN(CallInst *OrigI) {
if (RetainN)
return RetainN.get();
auto *ObjectPtrTy = getObjectPtrTy();
auto *Int32Ty = Type::getInt32Ty(getModule().getContext());
llvm::Constant *cache = nullptr;
RetainN = getRuntimeFn(
getModule(), cache,
isNonAtomic(OrigI) ? "swift_nonatomic_retain_n" : "swift_retain_n",
DefaultCC, RuntimeAvailability::AlwaysAvailable, {ObjectPtrTy},
{ObjectPtrTy, Int32Ty}, {NoUnwind, FirstParamReturned}, {});
return RetainN.get();
}
/// Return a callable function for swift_release_n.
Constant *getReleaseN(CallInst *OrigI) {
if (ReleaseN)
return ReleaseN.get();
auto *ObjectPtrTy = getObjectPtrTy();
auto *Int32Ty = Type::getInt32Ty(getModule().getContext());
auto *VoidTy = Type::getVoidTy(getModule().getContext());
llvm::Constant *cache = nullptr;
ReleaseN = getRuntimeFn(getModule(), cache,
isNonAtomic(OrigI) ? "swift_nonatomic_release_n"
: "swift_release_n",
DefaultCC, RuntimeAvailability::AlwaysAvailable,
{VoidTy}, {ObjectPtrTy, Int32Ty}, {NoUnwind}, {});
return ReleaseN.get();
}
/// getUnknownObjectRetainN - Return a callable function for
/// swift_unknownObjectRetain_n.
Constant *getUnknownObjectRetainN(CallInst *OrigI) {
if (UnknownObjectRetainN)
return UnknownObjectRetainN.get();
auto *ObjectPtrTy = getObjectPtrTy();
auto *Int32Ty = Type::getInt32Ty(getModule().getContext());
llvm::Constant *cache = nullptr;
UnknownObjectRetainN = getRuntimeFn(
getModule(), cache,
isNonAtomic(OrigI) ? "swift_nonatomic_unknownObjectRetain_n"
: "swift_unknownObjectRetain_n",
DefaultCC, RuntimeAvailability::AlwaysAvailable, {ObjectPtrTy},
{ObjectPtrTy, Int32Ty}, {NoUnwind, FirstParamReturned}, {});
return UnknownObjectRetainN.get();
}
/// Return a callable function for swift_unknownObjectRelease_n.
Constant *getUnknownObjectReleaseN(CallInst *OrigI) {
if (UnknownObjectReleaseN)
return UnknownObjectReleaseN.get();
auto *ObjectPtrTy = getObjectPtrTy();
auto *Int32Ty = Type::getInt32Ty(getModule().getContext());
auto *VoidTy = Type::getVoidTy(getModule().getContext());
llvm::Constant *cache = nullptr;
UnknownObjectReleaseN = getRuntimeFn(
getModule(), cache,
isNonAtomic(OrigI) ? "swift_nonatomic_unknownObjectRelease_n"
: "swift_unknownObjectRelease_n",
DefaultCC, RuntimeAvailability::AlwaysAvailable, {VoidTy},
{ObjectPtrTy, Int32Ty}, {NoUnwind}, {});
return UnknownObjectReleaseN.get();
}
/// Return a callable function for swift_bridgeRetain_n.
Constant *getBridgeRetainN(CallInst *OrigI) {
if (BridgeRetainN)
return BridgeRetainN.get();
auto *BridgeObjectPtrTy = getBridgeObjectPtrTy();
auto *Int32Ty = Type::getInt32Ty(getModule().getContext());
llvm::Constant *cache = nullptr;
BridgeRetainN = getRuntimeFn(
getModule(), cache,
isNonAtomic(OrigI) ? "swift_nonatomic_bridgeObjectRetain_n"
: "swift_bridgeObjectRetain_n",
DefaultCC, RuntimeAvailability::AlwaysAvailable, {BridgeObjectPtrTy},
{BridgeObjectPtrTy, Int32Ty}, {NoUnwind}, {});
return BridgeRetainN.get();
}
/// Return a callable function for swift_bridgeRelease_n.
Constant *getBridgeReleaseN(CallInst *OrigI) {
if (BridgeReleaseN)
return BridgeReleaseN.get();
auto *BridgeObjectPtrTy = getBridgeObjectPtrTy();
auto *Int32Ty = Type::getInt32Ty(getModule().getContext());
auto *VoidTy = Type::getVoidTy(getModule().getContext());
llvm::Constant *cache = nullptr;
BridgeReleaseN = getRuntimeFn(
getModule(), cache,
isNonAtomic(OrigI) ? "swift_nonatomic_bridgeObjectRelease_n"
: "swift_bridgeObjectRelease_n",
DefaultCC, RuntimeAvailability::AlwaysAvailable, {VoidTy},
{BridgeObjectPtrTy, Int32Ty}, {NoUnwind}, {});
return BridgeReleaseN.get();
}
Type *getNamedOpaquePtrTy(StringRef name) {
auto &M = getModule();
if (auto *ty = llvm::StructType::getTypeByName(M.getContext(), name)) {
return ty->getPointerTo();
}
// Otherwise, create an anonymous struct type.
auto *ty = llvm::StructType::create(M.getContext(), name);
return ty->getPointerTo();
}
Type *getObjectPtrTy() {
if (ObjectPtrTy)
return ObjectPtrTy.get();
ObjectPtrTy = getNamedOpaquePtrTy("swift.refcounted");
return ObjectPtrTy.get();
}
Type *getBridgeObjectPtrTy() {
if (BridgeObjectPtrTy)
return BridgeObjectPtrTy.get();
BridgeObjectPtrTy = getNamedOpaquePtrTy("swift.bridge");
return BridgeObjectPtrTy.get();
}
Constant *getIntConstant(uint32_t constant) {
auto &M = getModule();
auto *Int32Ty = Type::getInt32Ty(M.getContext());
return Constant::getIntegerValue(Int32Ty, APInt(32, constant));
}
};
} // end swift namespace
#endif
|