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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
//===--- IRBuilder.h - Swift IR Builder -------------------------*- 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
//
//===----------------------------------------------------------------------===//
//
// This file defines Swift's specialization of llvm::IRBuilder.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_IRBUILDER_H
#define SWIFT_IRGEN_IRBUILDER_H
#include "llvm/ADT/PointerUnion.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "swift/Basic/LLVM.h"
#include "Address.h"
#include "IRGen.h"
namespace swift {
namespace irgen {
class FunctionPointer;
class IRGenModule;
using IRBuilderBase = llvm::IRBuilder<>;
class IRBuilder : public IRBuilderBase {
public:
// Without this, it keeps resolving to llvm::IRBuilderBase because
// of the injected class name.
using IRBuilderBase = irgen::IRBuilderBase;
private:
/// The block containing the insertion point when the insertion
/// point was last cleared. Used only for preserving block
/// ordering.
llvm::BasicBlock *ClearedIP;
unsigned NumTrapBarriers = 0;
#ifndef NDEBUG
/// Whether debug information is requested. Only used in assertions.
bool DebugInfo;
#endif
// Set calling convention of the call instruction using
// the same calling convention as the callee function.
// This ensures that they are always compatible.
void setCallingConvUsingCallee(llvm::CallBase *Call) {
auto CalleeFn = Call->getCalledFunction();
if (CalleeFn) {
auto CC = CalleeFn->getCallingConv();
Call->setCallingConv(CC);
}
}
public:
IRBuilder(llvm::LLVMContext &Context, bool DebugInfo)
: IRBuilderBase(Context), ClearedIP(nullptr)
#ifndef NDEBUG
, DebugInfo(DebugInfo)
#endif
{}
/// Defined below.
class SavedInsertionPointRAII;
/// Determines if the current location is apparently reachable. The
/// invariant we maintain is that the insertion point of the builder
/// always points within a block unless the current location is
/// logically unreachable. All the low-level routines which emit
/// branches leave the insertion point in the original block, just
/// after the branch. High-level routines may then indicate
/// unreachability by clearing the insertion point.
bool hasValidIP() const { return GetInsertBlock() != nullptr; }
/// Determines whether we're currently inserting after a terminator.
/// This is really just there for asserts.
bool hasPostTerminatorIP() const {
return GetInsertBlock() != nullptr &&
!GetInsertBlock()->empty() &&
GetInsertBlock()->back().isTerminator();
}
void ClearInsertionPoint() {
assert(hasValidIP() && "clearing invalid insertion point!");
assert(ClearedIP == nullptr);
/// Whenever we clear the insertion point, remember where we were.
ClearedIP = GetInsertBlock();
IRBuilderBase::ClearInsertionPoint();
}
void SetInsertPoint(llvm::BasicBlock *BB) {
ClearedIP = nullptr;
IRBuilderBase::SetInsertPoint(BB);
}
void SetInsertPoint(llvm::BasicBlock *BB, llvm::BasicBlock::iterator before) {
ClearedIP = nullptr;
IRBuilderBase::SetInsertPoint(BB, before);
}
void SetInsertPoint(llvm::Instruction *I) {
ClearedIP = nullptr;
IRBuilderBase::SetInsertPoint(I);
}
/// Return the LLVM module we're inserting into.
llvm::Module *getModule() const {
if (auto BB = GetInsertBlock())
return BB->getModule();
assert(ClearedIP && "IRBuilder has no active or cleared insertion block");
return ClearedIP->getModule();
}
using IRBuilderBase::CreateAnd;
llvm::Value *CreateAnd(llvm::Value *LHS, llvm::Value *RHS,
const Twine &Name = "") {
if (auto *RC = dyn_cast<llvm::Constant>(RHS))
if (isa<llvm::ConstantInt>(RC) &&
cast<llvm::ConstantInt>(RC)->isMinusOne())
return LHS; // LHS & -1 -> LHS
return IRBuilderBase::CreateAnd(LHS, RHS, Name);
}
llvm::Value *CreateAnd(llvm::Value *LHS, const APInt &RHS,
const Twine &Name = "") {
return CreateAnd(LHS, llvm::ConstantInt::get(LHS->getType(), RHS), Name);
}
llvm::Value *CreateAnd(llvm::Value *LHS, uint64_t RHS, const Twine &Name = "") {
return CreateAnd(LHS, llvm::ConstantInt::get(LHS->getType(), RHS), Name);
}
using IRBuilderBase::CreateOr;
llvm::Value *CreateOr(llvm::Value *LHS, llvm::Value *RHS,
const Twine &Name = "") {
if (auto *RC = dyn_cast<llvm::Constant>(RHS))
if (RC->isNullValue())
return LHS; // LHS | 0 -> LHS
return IRBuilderBase::CreateOr(LHS, RHS, Name);
}
llvm::Value *CreateOr(llvm::Value *LHS, const APInt &RHS,
const Twine &Name = "") {
return CreateOr(LHS, llvm::ConstantInt::get(LHS->getType(), RHS), Name);
}
llvm::Value *CreateOr(llvm::Value *LHS, uint64_t RHS,
const Twine &Name = "") {
return CreateOr(LHS, llvm::ConstantInt::get(LHS->getType(), RHS), Name);
}
/// Don't create allocas this way; you'll get a dynamic alloca.
/// Use IGF::createAlloca or IGF::emitDynamicAlloca.
llvm::Value *CreateAlloca(llvm::Type *type, llvm::Value *arraySize,
const llvm::Twine &name = "") = delete;
llvm::LoadInst *CreateLoad(llvm::Value *addr, llvm::Type *elementType,
Alignment align, const llvm::Twine &name = "") {
llvm::LoadInst *load = IRBuilderBase::CreateLoad(elementType, addr, name);
load->setAlignment(llvm::MaybeAlign(align.getValue()).valueOrOne());
return load;
}
llvm::LoadInst *CreateLoad(Address addr, const llvm::Twine &name = "") {
return CreateLoad(addr.getAddress(), addr.getElementType(),
addr.getAlignment(), name);
}
llvm::StoreInst *CreateStore(llvm::Value *value, llvm::Value *addr,
Alignment align) {
llvm::StoreInst *store = IRBuilderBase::CreateStore(value, addr);
store->setAlignment(llvm::MaybeAlign(align.getValue()).valueOrOne());
return store;
}
llvm::StoreInst *CreateStore(llvm::Value *value, Address addr) {
return CreateStore(value, addr.getAddress(), addr.getAlignment());
}
// These are deleted because we want to force the caller to specify
// an alignment.
llvm::LoadInst *CreateLoad(llvm::Value *addr,
const llvm::Twine &name = "") = delete;
llvm::StoreInst *CreateStore(llvm::Value *value, llvm::Value *addr) = delete;
using IRBuilderBase::CreateStructGEP;
Address CreateStructGEP(Address address, unsigned index, Size offset,
const llvm::Twine &name = "") {
assert(isa<llvm::StructType>(address.getElementType()) ||
isa<llvm::ArrayType>(address.getElementType()));
llvm::Value *addr = CreateStructGEP(address.getElementType(),
address.getAddress(), index, name);
llvm::Type *elementType = nullptr;
if (auto *structTy = dyn_cast<llvm::StructType>(address.getElementType())) {
elementType = structTy->getElementType(index);
} else if (auto *arrTy =
dyn_cast<llvm::ArrayType>(address.getElementType())) {
elementType = arrTy->getElementType();
}
return Address(addr, elementType,
address.getAlignment().alignmentAtOffset(offset));
}
Address CreateStructGEP(Address address, unsigned index,
const llvm::StructLayout *layout,
const llvm::Twine &name = "") {
Size offset = Size(layout->getElementOffset(index));
return CreateStructGEP(address, index, offset, name);
}
/// Given a pointer to an array element, GEP to the array element
/// N elements past it. The type is not changed.
Address CreateConstArrayGEP(Address base, unsigned index, Size eltSize,
const llvm::Twine &name = "") {
auto addr = CreateConstInBoundsGEP1_32(base.getElementType(),
base.getAddress(), index, name);
return Address(addr, base.getElementType(),
base.getAlignment().alignmentAtOffset(eltSize * index));
}
/// Given a pointer to an array element, GEP to the array element
/// N elements past it. The type is not changed.
Address CreateArrayGEP(Address base, llvm::Value *index, Size eltSize,
const llvm::Twine &name = "") {
auto addr = CreateInBoundsGEP(base.getElementType(),
base.getAddress(), index, name);
// Given that Alignment doesn't remember offset alignment,
// the alignment at index 1 should be conservatively correct for
// any element in the array.
return Address(addr, base.getElementType(),
base.getAlignment().alignmentAtOffset(eltSize));
}
/// Given an i8*, GEP to N bytes past it.
Address CreateConstByteArrayGEP(Address base, Size offset,
const llvm::Twine &name = "") {
auto addr = CreateConstInBoundsGEP1_32(
base.getElementType(), base.getAddress(), offset.getValue(), name);
return Address(addr, base.getElementType(),
base.getAlignment().alignmentAtOffset(offset));
}
using IRBuilderBase::CreateBitCast;
/// Cast the given address to be a pointer to the given element type,
/// preserving the original address space.
Address CreateElementBitCast(Address address, llvm::Type *type,
const llvm::Twine &name = "") {
// Do nothing if the type doesn't change.
if (address.getElementType() == type) {
return address;
}
// Otherwise, cast to a pointer to the correct type.
auto origPtrType = address.getType();
return Address(
CreateBitCast(address.getAddress(),
type->getPointerTo(origPtrType->getAddressSpace())),
type, address.getAlignment());
}
/// Insert the given basic block after the IP block and move the
/// insertion point to it. Only valid if the IP is valid.
void emitBlock(llvm::BasicBlock *BB);
using IRBuilderBase::CreateMemCpy;
llvm::CallInst *CreateMemCpy(Address dest, Address src, Size size) {
return CreateMemCpy(
dest.getAddress(), llvm::MaybeAlign(dest.getAlignment().getValue()),
src.getAddress(), llvm::MaybeAlign(src.getAlignment().getValue()),
size.getValue());
}
llvm::CallInst *CreateMemCpy(Address dest, Address src, llvm::Value *size) {
return CreateMemCpy(dest.getAddress(),
llvm::MaybeAlign(dest.getAlignment().getValue()),
src.getAddress(),
llvm::MaybeAlign(src.getAlignment().getValue()), size);
}
using IRBuilderBase::CreateMemSet;
llvm::CallInst *CreateMemSet(Address dest, llvm::Value *value, Size size) {
return CreateMemSet(dest.getAddress(), value, size.getValue(),
llvm::MaybeAlign(dest.getAlignment().getValue()));
}
llvm::CallInst *CreateMemSet(Address dest, llvm::Value *value,
llvm::Value *size) {
return CreateMemSet(dest.getAddress(), value, size,
llvm::MaybeAlign(dest.getAlignment().getValue()));
}
using IRBuilderBase::CreateLifetimeStart;
llvm::CallInst *CreateLifetimeStart(Address buf, Size size) {
return CreateLifetimeStart(buf.getAddress(),
llvm::ConstantInt::get(Context, APInt(64, size.getValue())));
}
using IRBuilderBase::CreateLifetimeEnd;
llvm::CallInst *CreateLifetimeEnd(Address buf, Size size) {
return CreateLifetimeEnd(buf.getAddress(),
llvm::ConstantInt::get(Context, APInt(64, size.getValue())));
}
// We're intentionally not allowing direct use of
// llvm::IRBuilder::CreateCall in order to push code towards using
// FunctionPointer.
bool isTrapIntrinsic(llvm::Value *Callee) {
return Callee ==
llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::trap);
}
bool isTrapIntrinsic(llvm::Intrinsic::ID intrinsicID) {
return intrinsicID == llvm::Intrinsic::trap;
}
llvm::CallInst *CreateCall(llvm::Value *Callee, ArrayRef<llvm::Value *> Args,
const Twine &Name = "",
llvm::MDNode *FPMathTag = nullptr) = delete;
llvm::CallInst *CreateCall(llvm::FunctionType *FTy, llvm::Constant *Callee,
ArrayRef<llvm::Value *> Args,
const Twine &Name = "",
llvm::MDNode *FPMathTag = nullptr) {
assert((!DebugInfo || getCurrentDebugLocation()) && "no debugloc on call");
assert(!isTrapIntrinsic(Callee) && "Use CreateNonMergeableTrap");
auto Call = IRBuilderBase::CreateCall(FTy, Callee, Args, Name, FPMathTag);
setCallingConvUsingCallee(Call);
return Call;
}
llvm::CallInst *CreateCallWithoutDbgLoc(llvm::FunctionType *FTy,
llvm::Constant *Callee,
ArrayRef<llvm::Value *> Args,
const Twine &Name = "",
llvm::MDNode *FPMathTag = nullptr) {
// assert((!DebugInfo || getCurrentDebugLocation()) && "no debugloc on
// call");
assert(!isTrapIntrinsic(Callee) && "Use CreateNonMergeableTrap");
auto Call = IRBuilderBase::CreateCall(FTy, Callee, Args, Name, FPMathTag);
setCallingConvUsingCallee(Call);
return Call;
}
llvm::InvokeInst *
createInvoke(llvm::FunctionType *fTy, llvm::Constant *callee,
ArrayRef<llvm::Value *> args, llvm::BasicBlock *invokeNormalDest,
llvm::BasicBlock *invokeUnwindDest, const Twine &name = "") {
assert((!DebugInfo || getCurrentDebugLocation()) && "no debugloc on call");
auto call = IRBuilderBase::CreateInvoke(fTy, callee, invokeNormalDest,
invokeUnwindDest, args, name);
setCallingConvUsingCallee(call);
return call;
}
llvm::CallBase *CreateCallOrInvoke(const FunctionPointer &fn,
ArrayRef<llvm::Value *> args,
llvm::BasicBlock *invokeNormalDest,
llvm::BasicBlock *invokeUnwindDest);
llvm::CallInst *CreateCall(const FunctionPointer &fn,
ArrayRef<llvm::Value *> args);
llvm::CallInst *CreateCall(const FunctionPointer &fn,
ArrayRef<llvm::Value *> args, const Twine &Name) {
auto c = CreateCall(fn, args);
c->setName(Name);
return c;
}
llvm::CallInst *CreateAsmCall(llvm::InlineAsm *asmBlock,
ArrayRef<llvm::Value *> args) {
return IRBuilderBase::CreateCall(asmBlock, args);
}
/// Call an intrinsic with no type arguments.
llvm::CallInst *CreateIntrinsicCall(llvm::Intrinsic::ID intrinsicID,
ArrayRef<llvm::Value *> args,
const Twine &name = "") {
assert(!isTrapIntrinsic(intrinsicID) && "Use CreateNonMergeableTrap");
auto intrinsicFn =
llvm::Intrinsic::getDeclaration(getModule(), intrinsicID);
return CreateCallWithoutDbgLoc(
cast<llvm::FunctionType>(intrinsicFn->getValueType()), intrinsicFn,
args, name);
}
/// Call an intrinsic with type arguments.
llvm::CallInst *CreateIntrinsicCall(llvm::Intrinsic::ID intrinsicID,
ArrayRef<llvm::Type*> typeArgs,
ArrayRef<llvm::Value *> args,
const Twine &name = "") {
assert(!isTrapIntrinsic(intrinsicID) && "Use CreateNonMergeableTrap");
auto intrinsicFn =
llvm::Intrinsic::getDeclaration(getModule(), intrinsicID, typeArgs);
return CreateCallWithoutDbgLoc(
cast<llvm::FunctionType>(intrinsicFn->getValueType()), intrinsicFn,
args, name);
}
/// Create an expect intrinsic call.
llvm::CallInst *CreateExpect(llvm::Value *value,
llvm::Value *expected,
const Twine &name = "") {
return CreateIntrinsicCall(llvm::Intrinsic::expect,
{value->getType()},
{value, expected},
name);
}
/// Call the trap intrinsic. If optimizations are enabled, an inline asm
/// gadget is emitted before the trap. The gadget inhibits transforms which
/// merge trap calls together, which makes debugging crashes easier.
llvm::CallInst *CreateNonMergeableTrap(IRGenModule &IGM, StringRef failureMsg);
/// Split a first-class aggregate value into its component pieces.
template <unsigned N>
std::array<llvm::Value *, N> CreateSplit(llvm::Value *aggregate) {
assert(isa<llvm::StructType>(aggregate->getType()));
assert(cast<llvm::StructType>(aggregate->getType())->getNumElements() == N);
std::array<llvm::Value *, N> results;
for (unsigned i = 0; i != N; ++i) {
results[i] = CreateExtractValue(aggregate, i);
}
return results;
}
/// Combine the given values into a first-class aggregate.
llvm::Value *CreateCombine(llvm::StructType *aggregateType,
ArrayRef<llvm::Value*> values) {
assert(aggregateType->getNumElements() == values.size());
llvm::Value *result = llvm::UndefValue::get(aggregateType);
for (unsigned i = 0, e = values.size(); i != e; ++i) {
result = CreateInsertValue(result, values[i], i);
}
return result;
}
bool insertingAtEndOfBlock() const {
assert(hasValidIP() && "Must have insertion point to ask about it");
return InsertPt == BB->end();
}
};
/// Given a Builder as input to its constructor, this class resets the Builder
/// so it has the same insertion point at end of scope.
class IRBuilder::SavedInsertionPointRAII {
IRBuilder &builder;
PointerUnion<llvm::Instruction *, llvm::BasicBlock *> savedInsertionPoint;
public:
/// Constructor that saves a Builder's insertion point without changing the
/// builder's underlying insertion point.
SavedInsertionPointRAII(IRBuilder &inputBuilder)
: builder(inputBuilder), savedInsertionPoint() {
// If our builder does not have a valid insertion point, just put nullptr
// into SavedIP.
if (!builder.hasValidIP()) {
savedInsertionPoint = static_cast<llvm::BasicBlock *>(nullptr);
return;
}
// If we are inserting into the end of the block, stash the insertion block.
if (builder.insertingAtEndOfBlock()) {
savedInsertionPoint = builder.GetInsertBlock();
return;
}
// Otherwise, stash the instruction.
auto *i = &*builder.GetInsertPoint();
savedInsertionPoint = i;
}
SavedInsertionPointRAII(IRBuilder &b, llvm::Instruction *newInsertionPoint)
: SavedInsertionPointRAII(b) {
builder.SetInsertPoint(newInsertionPoint);
}
SavedInsertionPointRAII(IRBuilder &b, llvm::BasicBlock *block,
llvm::BasicBlock::iterator iter)
: SavedInsertionPointRAII(b) {
builder.SetInsertPoint(block, iter);
}
SavedInsertionPointRAII(IRBuilder &b, llvm::BasicBlock *insertionBlock)
: SavedInsertionPointRAII(b) {
builder.SetInsertPoint(insertionBlock);
}
SavedInsertionPointRAII(const SavedInsertionPointRAII &) = delete;
SavedInsertionPointRAII &operator=(const SavedInsertionPointRAII &) = delete;
SavedInsertionPointRAII(SavedInsertionPointRAII &&) = delete;
SavedInsertionPointRAII &operator=(SavedInsertionPointRAII &&) = delete;
~SavedInsertionPointRAII() {
if (savedInsertionPoint.isNull()) {
builder.ClearInsertionPoint();
} else if (savedInsertionPoint.is<llvm::Instruction *>()) {
builder.SetInsertPoint(savedInsertionPoint.get<llvm::Instruction *>());
} else {
builder.SetInsertPoint(savedInsertionPoint.get<llvm::BasicBlock *>());
}
}
};
} // end namespace irgen
} // end namespace swift
#endif
|