Skip to content

Commit

Permalink
[opaque pointer types] Cleanup CGBuilder's Create*GEP.
Browse files Browse the repository at this point in the history
The various EltSize, Offset, DataLayout, and StructLayout arguments
are all computable from the Address's element type and the DataLayout
which the CGBuilder already has access to.

After having previously asserted that the computed values are the same
as those passed in, now remove the redundant arguments from
CGBuilder's Create*GEP functions.

Differential Revision: https://reviews.llvm.org/D57767

llvm-svn: 353629
  • Loading branch information
jyknight committed Feb 9, 2019
1 parent a561d46 commit 751fe28
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 338 deletions.
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGAtomic.cpp
Expand Up @@ -201,7 +201,7 @@ namespace {
assert(LVal.isSimple());
Address addr = getAtomicAddress();
if (hasPadding())
addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
addr = CGF.Builder.CreateStructGEP(addr, 0);

return LValue::MakeAddr(addr, getValueType(), CGF.getContext(),
LVal.getBaseInfo(), LVal.getTBAAInfo());
Expand Down Expand Up @@ -1356,7 +1356,7 @@ RValue AtomicInfo::convertAtomicTempToRValue(Address addr,

// Drill into the padding structure if we have one.
if (hasPadding())
addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
addr = CGF.Builder.CreateStructGEP(addr, 0);

// Otherwise, just convert the temporary to an r-value using the
// normal conversion routine.
Expand Down
66 changes: 27 additions & 39 deletions clang/lib/CodeGen/CGBlocks.cpp
Expand Up @@ -836,9 +836,8 @@ static void enterBlockScope(CodeGenFunction &CGF, BlockDecl *block) {
}

// GEP down to the address.
Address addr = CGF.Builder.CreateStructGEP(blockInfo.LocalAddress,
capture.getIndex(),
capture.getOffset());
Address addr =
CGF.Builder.CreateStructGEP(blockInfo.LocalAddress, capture.getIndex());

// We can use that GEP as the dominating IP.
if (!blockInfo.DominatingIP)
Expand Down Expand Up @@ -975,27 +974,24 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
flags |= BLOCK_IS_NOESCAPE | BLOCK_IS_GLOBAL;
}

auto projectField =
[&](unsigned index, CharUnits offset, const Twine &name) -> Address {
return Builder.CreateStructGEP(blockAddr, index, offset, name);
};
auto storeField =
[&](llvm::Value *value, unsigned index, CharUnits offset,
const Twine &name) {
Builder.CreateStore(value, projectField(index, offset, name));
};
auto projectField = [&](unsigned index, const Twine &name) -> Address {
return Builder.CreateStructGEP(blockAddr, index, name);
};
auto storeField = [&](llvm::Value *value, unsigned index, const Twine &name) {
Builder.CreateStore(value, projectField(index, name));
};

// Initialize the block header.
{
// We assume all the header fields are densely packed.
unsigned index = 0;
CharUnits offset;
auto addHeaderField =
[&](llvm::Value *value, CharUnits size, const Twine &name) {
storeField(value, index, offset, name);
offset += size;
index++;
};
auto addHeaderField = [&](llvm::Value *value, CharUnits size,
const Twine &name) {
storeField(value, index, name);
offset += size;
index++;
};

if (!IsOpenCL) {
addHeaderField(isa, getPointerSize(), "block.isa");
Expand Down Expand Up @@ -1031,8 +1027,8 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {

// First, 'this'.
if (blockDecl->capturesCXXThis()) {
Address addr = projectField(blockInfo.CXXThisIndex, blockInfo.CXXThisOffset,
"block.captured-this.addr");
Address addr =
projectField(blockInfo.CXXThisIndex, "block.captured-this.addr");
Builder.CreateStore(LoadCXXThis(), addr);
}

Expand All @@ -1048,8 +1044,7 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {

// This will be a [[type]]*, except that a byref entry will just be
// an i8**.
Address blockField =
projectField(capture.getIndex(), capture.getOffset(), "block.captured");
Address blockField = projectField(capture.getIndex(), "block.captured");

// Compute the address of the thing we're going to move into the
// block literal.
Expand All @@ -1068,7 +1063,6 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
// This is a [[type]]*, except that a byref entry will just be an i8**.
src = Builder.CreateStructGEP(LoadBlockStruct(),
enclosingCapture.getIndex(),
enclosingCapture.getOffset(),
"block.capture.addr");
} else {
auto I = LocalDeclMap.find(variable);
Expand Down Expand Up @@ -1330,9 +1324,8 @@ Address CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable) {
// Handle constant captures.
if (capture.isConstant()) return LocalDeclMap.find(variable)->second;

Address addr =
Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
capture.getOffset(), "block.capture.addr");
Address addr = Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
"block.capture.addr");

if (variable->isEscapingByref()) {
// addr should be a void** right now. Load, then cast the result
Expand Down Expand Up @@ -1615,9 +1608,8 @@ CodeGenFunction::GenerateBlockFunction(GlobalDecl GD,
// If we have a C++ 'this' reference, go ahead and force it into
// existence now.
if (blockDecl->capturesCXXThis()) {
Address addr =
Builder.CreateStructGEP(LoadBlockStruct(), blockInfo.CXXThisIndex,
blockInfo.CXXThisOffset, "block.captured-this");
Address addr = Builder.CreateStructGEP(
LoadBlockStruct(), blockInfo.CXXThisIndex, "block.captured-this");
CXXThisValue = Builder.CreateLoad(addr, "this");
}

Expand Down Expand Up @@ -2060,8 +2052,8 @@ CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
BlockFieldFlags flags = CopiedCapture.CopyFlags;

unsigned index = capture.getIndex();
Address srcField = Builder.CreateStructGEP(src, index, capture.getOffset());
Address dstField = Builder.CreateStructGEP(dst, index, capture.getOffset());
Address srcField = Builder.CreateStructGEP(src, index);
Address dstField = Builder.CreateStructGEP(dst, index);

switch (CopiedCapture.CopyKind) {
case BlockCaptureEntityKind::CXXRecord:
Expand Down Expand Up @@ -2249,8 +2241,7 @@ CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) {
const CGBlockInfo::Capture &capture = *DestroyedCapture.Capture;
BlockFieldFlags flags = DestroyedCapture.DisposeFlags;

Address srcField =
Builder.CreateStructGEP(src, capture.getIndex(), capture.getOffset());
Address srcField = Builder.CreateStructGEP(src, capture.getIndex());

pushCaptureCleanup(DestroyedCapture.DisposeKind, srcField,
CI.getVariable()->getType(), flags,
Expand Down Expand Up @@ -2710,13 +2701,11 @@ Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr,
const llvm::Twine &name) {
// Chase the forwarding address if requested.
if (followForward) {
Address forwardingAddr =
Builder.CreateStructGEP(baseAddr, 1, getPointerSize(), "forwarding");
Address forwardingAddr = Builder.CreateStructGEP(baseAddr, 1, "forwarding");
baseAddr = Address(Builder.CreateLoad(forwardingAddr), info.ByrefAlignment);
}

return Builder.CreateStructGEP(baseAddr, info.FieldIndex,
info.FieldOffset, name);
return Builder.CreateStructGEP(baseAddr, info.FieldIndex, name);
}

/// BuildByrefInfo - This routine changes a __block variable declared as T x
Expand Down Expand Up @@ -2834,8 +2823,7 @@ void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
CharUnits nextHeaderOffset;
auto storeHeaderField = [&](llvm::Value *value, CharUnits fieldSize,
const Twine &name) {
auto fieldAddr = Builder.CreateStructGEP(addr, nextHeaderIndex,
nextHeaderOffset, name);
auto fieldAddr = Builder.CreateStructGEP(addr, nextHeaderIndex, name);
Builder.CreateStore(value, fieldAddr);

nextHeaderIndex++;
Expand Down
74 changes: 18 additions & 56 deletions clang/lib/CodeGen/CGBuilder.h
Expand Up @@ -167,33 +167,25 @@ class CGBuilderTy : public CGBuilderBaseTy {
return Address(Ptr, Addr.getAlignment());
}

/// Given
/// %addr = {T1, T2...}* ...
/// produce
/// %name = getelementptr inbounds %addr, i32 0, i32 index
///
/// This API assumes that drilling into a struct like this is always an
/// inbounds operation.
using CGBuilderBaseTy::CreateStructGEP;
Address CreateStructGEP(Address Addr, unsigned Index, CharUnits Offset,
Address CreateStructGEP(Address Addr, unsigned Index,
const llvm::Twine &Name = "") {
#ifndef NDEBUG
llvm::StructType *ElTy = cast<llvm::StructType>(Addr.getElementType());
const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
const llvm::StructLayout *SL = DL.getStructLayout(ElTy);
assert(SL->getElementOffset(Index) == (uint64_t)Offset.getQuantity());
#endif
const llvm::StructLayout *Layout = DL.getStructLayout(ElTy);
auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index));

return Address(CreateStructGEP(Addr.getElementType(),
Addr.getPointer(), Index, Name),
Addr.getAlignment().alignmentAtOffset(Offset));
}
Address CreateStructGEP(Address Addr, unsigned Index,
const llvm::StructLayout *Layout,
const llvm::Twine &Name = "") {
#ifndef NDEBUG
llvm::StructType *ElTy = cast<llvm::StructType>(Addr.getElementType());
const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
const llvm::StructLayout *SL = DL.getStructLayout(ElTy);
assert(Layout == SL);
#endif

auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index));
return CreateStructGEP(Addr, Index, Offset, Name);
}

/// Given
/// %addr = [n x T]* ...
Expand All @@ -203,24 +195,6 @@ class CGBuilderTy : public CGBuilderBaseTy {
///
/// This API assumes that drilling into an array like this is always
/// an inbounds operation.
///
/// \param EltSize - the size of the type T in bytes
Address CreateConstArrayGEP(Address Addr, uint64_t Index, CharUnits EltSize,
const llvm::Twine &Name = "") {
#ifndef NDEBUG
llvm::ArrayType *ElTy = cast<llvm::ArrayType>(Addr.getElementType());
const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
assert(DL.getTypeAllocSize(ElTy->getElementType()) ==
(uint64_t)EltSize.getQuantity());
#endif

return Address(CreateInBoundsGEP(Addr.getPointer(),
{getSize(CharUnits::Zero()),
getSize(Index)},
Name),
Addr.getAlignment().alignmentAtOffset(Index * EltSize));
}

Address CreateConstArrayGEP(Address Addr, uint64_t Index,
const llvm::Twine &Name = "") {
llvm::ArrayType *ElTy = cast<llvm::ArrayType>(Addr.getElementType());
Expand All @@ -239,16 +213,11 @@ class CGBuilderTy : public CGBuilderBaseTy {
/// produce
/// %name = getelementptr inbounds %addr, i64 index
/// where i64 is actually the target word size.
///
/// \param EltSize - the size of the type T in bytes
Address CreateConstInBoundsGEP(Address Addr, uint64_t Index,
CharUnits EltSize,
const llvm::Twine &Name = "") {
#ifndef NDEBUG
llvm::Type *ElTy = Addr.getElementType();
const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
assert(DL.getTypeAllocSize(ElTy) == (uint64_t)EltSize.getQuantity());
#endif
CharUnits EltSize = CharUnits::fromQuantity(DL.getTypeAllocSize(ElTy));

return Address(CreateInBoundsGEP(Addr.getElementType(), Addr.getPointer(),
getSize(Index), Name),
Expand All @@ -260,15 +229,12 @@ class CGBuilderTy : public CGBuilderBaseTy {
/// produce
/// %name = getelementptr inbounds %addr, i64 index
/// where i64 is actually the target word size.
///
/// \param EltSize - the size of the type T in bytes
Address CreateConstGEP(Address Addr, uint64_t Index, CharUnits EltSize,
Address CreateConstGEP(Address Addr, uint64_t Index,
const llvm::Twine &Name = "") {
#ifndef NDEBUG
const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
assert(DL.getTypeAllocSize(Addr.getElementType()) ==
(uint64_t)EltSize.getQuantity());
#endif
CharUnits EltSize =
CharUnits::fromQuantity(DL.getTypeAllocSize(Addr.getElementType()));

return Address(CreateGEP(Addr.getElementType(), Addr.getPointer(),
getSize(Index), Name),
Addr.getAlignment().alignmentAtOffset(Index * EltSize));
Expand All @@ -289,13 +255,9 @@ class CGBuilderTy : public CGBuilderBaseTy {
}

using CGBuilderBaseTy::CreateConstInBoundsGEP2_32;
Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0,
unsigned Idx1, const llvm::DataLayout &DL,
const llvm::Twine &Name = "") {
#ifndef NDEBUG
const llvm::DataLayout &DL2 = BB->getParent()->getParent()->getDataLayout();
assert(DL == DL2);
#endif
Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1,
const llvm::Twine &Name = "") {
const llvm::DataLayout &DL = BB->getParent()->getParent()->getDataLayout();

auto *GEP = cast<llvm::GetElementPtrInst>(CreateConstInBoundsGEP2_32(
Addr.getElementType(), Addr.getPointer(), Idx0, Idx1, Name));
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/CodeGen/CGBuiltin.cpp
Expand Up @@ -2527,8 +2527,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
// Store the stack pointer to the setjmp buffer.
Value *StackAddr =
Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
Address StackSaveSlot =
Builder.CreateConstInBoundsGEP(Buf, 2, getPointerSize());
Address StackSaveSlot = Builder.CreateConstInBoundsGEP(Buf, 2);
Builder.CreateStore(StackAddr, StackSaveSlot);

// Call LLVM's EH setjmp, which is lightweight.
Expand Down

0 comments on commit 751fe28

Please sign in to comment.