2008-01-26 09:36:00 +08:00
|
|
|
//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2008-01-26 09:36:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Constant Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2010-03-31 06:26:10 +08:00
|
|
|
#include "CGRecordLayout.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenModule.h"
|
2017-08-16 05:42:52 +08:00
|
|
|
#include "ConstantEmitter.h"
|
2016-12-15 16:09:08 +08:00
|
|
|
#include "TargetInfo.h"
|
2008-10-06 13:59:01 +08:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-07-24 23:20:52 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2019-06-18 05:08:30 +08:00
|
|
|
#include "llvm/ADT/Sequence.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-06-18 05:08:30 +08:00
|
|
|
// ConstantAggregateBuilder
|
2010-04-14 01:45:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2015-06-10 08:27:52 +08:00
|
|
|
class ConstExprEmitter;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
struct ConstantAggregateBuilderUtils {
|
|
|
|
CodeGenModule &CGM;
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
ConstantAggregateBuilderUtils(CodeGenModule &CGM) : CGM(CGM) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-18 09:26:17 +08:00
|
|
|
CharUnits getAlignment(const llvm::Constant *C) const {
|
|
|
|
return CharUnits::fromQuantity(
|
2012-10-09 00:25:52 +08:00
|
|
|
CGM.getDataLayout().getABITypeAlignment(C->getType()));
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
CharUnits getSize(llvm::Type *Ty) const {
|
|
|
|
return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(Ty));
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
CharUnits getSize(const llvm::Constant *C) const {
|
|
|
|
return getSize(C->getType());
|
|
|
|
}
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::Constant *getPadding(CharUnits PadSize) const {
|
|
|
|
llvm::Type *Ty = CGM.Int8Ty;
|
|
|
|
if (PadSize > CharUnits::One())
|
|
|
|
Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
|
|
|
|
return llvm::UndefValue::get(Ty);
|
|
|
|
}
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::Constant *getZeroes(CharUnits ZeroSize) const {
|
|
|
|
llvm::Type *Ty = llvm::ArrayType::get(CGM.Int8Ty, ZeroSize.getQuantity());
|
|
|
|
return llvm::ConstantAggregateZero::get(Ty);
|
2014-10-20 07:40:06 +08:00
|
|
|
}
|
2019-06-18 05:08:30 +08:00
|
|
|
};
|
2014-10-20 07:40:06 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
/// Incremental builder for an llvm::Constant* holding a struct or array
|
|
|
|
/// constant.
|
|
|
|
class ConstantAggregateBuilder : private ConstantAggregateBuilderUtils {
|
|
|
|
/// The elements of the constant. These two arrays must have the same size;
|
|
|
|
/// Offsets[i] describes the offset of Elems[i] within the constant. The
|
|
|
|
/// elements are kept in increasing offset order, and we ensure that there
|
|
|
|
/// is no overlap: Offsets[i+1] >= Offsets[i] + getSize(Elemes[i]).
|
|
|
|
///
|
|
|
|
/// This may contain explicit padding elements (in order to create a
|
|
|
|
/// natural layout), but need not. Gaps between elements are implicitly
|
|
|
|
/// considered to be filled with undef.
|
|
|
|
llvm::SmallVector<llvm::Constant*, 32> Elems;
|
|
|
|
llvm::SmallVector<CharUnits, 32> Offsets;
|
|
|
|
|
|
|
|
/// The size of the constant (the maximum end offset of any added element).
|
|
|
|
/// May be larger than the end of Elems.back() if we split the last element
|
|
|
|
/// and removed some trailing undefs.
|
|
|
|
CharUnits Size = CharUnits::Zero();
|
|
|
|
|
|
|
|
/// This is true only if laying out Elems in order as the elements of a
|
|
|
|
/// non-packed LLVM struct will give the correct layout.
|
|
|
|
bool NaturalLayout = true;
|
|
|
|
|
|
|
|
bool split(size_t Index, CharUnits Hint);
|
|
|
|
Optional<size_t> splitAt(CharUnits Pos);
|
|
|
|
|
|
|
|
static llvm::Constant *buildFrom(CodeGenModule &CGM,
|
|
|
|
ArrayRef<llvm::Constant *> Elems,
|
|
|
|
ArrayRef<CharUnits> Offsets,
|
|
|
|
CharUnits StartOffset, CharUnits Size,
|
|
|
|
bool NaturalLayout, llvm::Type *DesiredTy,
|
|
|
|
bool AllowOversized);
|
2014-10-20 07:40:06 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
public:
|
|
|
|
ConstantAggregateBuilder(CodeGenModule &CGM)
|
|
|
|
: ConstantAggregateBuilderUtils(CGM) {}
|
|
|
|
|
|
|
|
/// Update or overwrite the value starting at \p Offset with \c C.
|
|
|
|
///
|
|
|
|
/// \param AllowOverwrite If \c true, this constant might overwrite (part of)
|
|
|
|
/// a constant that has already been added. This flag is only used to
|
|
|
|
/// detect bugs.
|
|
|
|
bool add(llvm::Constant *C, CharUnits Offset, bool AllowOverwrite);
|
|
|
|
|
|
|
|
/// Update or overwrite the bits starting at \p OffsetInBits with \p Bits.
|
|
|
|
bool addBits(llvm::APInt Bits, uint64_t OffsetInBits, bool AllowOverwrite);
|
|
|
|
|
|
|
|
/// Attempt to condense the value starting at \p Offset to a constant of type
|
|
|
|
/// \p DesiredTy.
|
|
|
|
void condense(CharUnits Offset, llvm::Type *DesiredTy);
|
|
|
|
|
|
|
|
/// Produce a constant representing the entire accumulated value, ideally of
|
|
|
|
/// the specified type. If \p AllowOversized, the constant might be larger
|
|
|
|
/// than implied by \p DesiredTy (eg, if there is a flexible array member).
|
|
|
|
/// Otherwise, the constant will be of exactly the same size as \p DesiredTy
|
|
|
|
/// even if we can't represent it as that type.
|
|
|
|
llvm::Constant *build(llvm::Type *DesiredTy, bool AllowOversized) const {
|
|
|
|
return buildFrom(CGM, Elems, Offsets, CharUnits::Zero(), Size,
|
|
|
|
NaturalLayout, DesiredTy, AllowOversized);
|
|
|
|
}
|
|
|
|
};
|
2014-10-20 07:40:06 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
template<typename Container, typename Range = std::initializer_list<
|
|
|
|
typename Container::value_type>>
|
|
|
|
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
|
|
|
|
assert(BeginOff <= EndOff && "invalid replacement range");
|
|
|
|
llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
|
|
|
|
}
|
2014-10-20 07:40:06 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool ConstantAggregateBuilder::add(llvm::Constant *C, CharUnits Offset,
|
|
|
|
bool AllowOverwrite) {
|
|
|
|
// Common case: appending to a layout.
|
|
|
|
if (Offset >= Size) {
|
|
|
|
CharUnits Align = getAlignment(C);
|
|
|
|
CharUnits AlignedSize = Size.alignTo(Align);
|
|
|
|
if (AlignedSize > Offset || Offset.alignTo(Align) != Offset)
|
|
|
|
NaturalLayout = false;
|
|
|
|
else if (AlignedSize < Offset) {
|
|
|
|
Elems.push_back(getPadding(Offset - Size));
|
|
|
|
Offsets.push_back(Size);
|
2014-10-20 07:40:06 +08:00
|
|
|
}
|
2019-06-18 05:08:30 +08:00
|
|
|
Elems.push_back(C);
|
|
|
|
Offsets.push_back(Offset);
|
|
|
|
Size = Offset + getSize(C);
|
|
|
|
return true;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// Uncommon case: constant overlaps what we've already created.
|
|
|
|
llvm::Optional<size_t> FirstElemToReplace = splitAt(Offset);
|
|
|
|
if (!FirstElemToReplace)
|
|
|
|
return false;
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
CharUnits CSize = getSize(C);
|
|
|
|
llvm::Optional<size_t> LastElemToReplace = splitAt(Offset + CSize);
|
|
|
|
if (!LastElemToReplace)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert((FirstElemToReplace == LastElemToReplace || AllowOverwrite) &&
|
|
|
|
"unexpectedly overwriting field");
|
|
|
|
|
|
|
|
replace(Elems, *FirstElemToReplace, *LastElemToReplace, {C});
|
|
|
|
replace(Offsets, *FirstElemToReplace, *LastElemToReplace, {Offset});
|
|
|
|
Size = std::max(Size, Offset + CSize);
|
|
|
|
NaturalLayout = false;
|
|
|
|
return true;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool ConstantAggregateBuilder::addBits(llvm::APInt Bits, uint64_t OffsetInBits,
|
|
|
|
bool AllowOverwrite) {
|
2011-03-12 20:03:11 +08:00
|
|
|
const ASTContext &Context = CGM.getContext();
|
2019-06-18 05:08:30 +08:00
|
|
|
const uint64_t CharWidth = CGM.getContext().getCharWidth();
|
|
|
|
|
|
|
|
// Offset of where we want the first bit to go within the bits of the
|
|
|
|
// current char.
|
|
|
|
unsigned OffsetWithinChar = OffsetInBits % CharWidth;
|
|
|
|
|
|
|
|
// We split bit-fields up into individual bytes. Walk over the bytes and
|
|
|
|
// update them.
|
2019-06-23 04:41:57 +08:00
|
|
|
for (CharUnits OffsetInChars =
|
|
|
|
Context.toCharUnitsFromBits(OffsetInBits - OffsetWithinChar);
|
2019-06-18 05:08:30 +08:00
|
|
|
/**/; ++OffsetInChars) {
|
|
|
|
// Number of bits we want to fill in this char.
|
|
|
|
unsigned WantedBits =
|
|
|
|
std::min((uint64_t)Bits.getBitWidth(), CharWidth - OffsetWithinChar);
|
|
|
|
|
|
|
|
// Get a char containing the bits we want in the right places. The other
|
|
|
|
// bits have unspecified values.
|
|
|
|
llvm::APInt BitsThisChar = Bits;
|
|
|
|
if (BitsThisChar.getBitWidth() < CharWidth)
|
|
|
|
BitsThisChar = BitsThisChar.zext(CharWidth);
|
|
|
|
if (CGM.getDataLayout().isBigEndian()) {
|
|
|
|
// Figure out how much to shift by. We may need to left-shift if we have
|
|
|
|
// less than one byte of Bits left.
|
|
|
|
int Shift = Bits.getBitWidth() - CharWidth + OffsetWithinChar;
|
|
|
|
if (Shift > 0)
|
|
|
|
BitsThisChar.lshrInPlace(Shift);
|
|
|
|
else if (Shift < 0)
|
|
|
|
BitsThisChar = BitsThisChar.shl(-Shift);
|
|
|
|
} else {
|
|
|
|
BitsThisChar = BitsThisChar.shl(OffsetWithinChar);
|
|
|
|
}
|
|
|
|
if (BitsThisChar.getBitWidth() > CharWidth)
|
|
|
|
BitsThisChar = BitsThisChar.trunc(CharWidth);
|
2010-04-14 01:45:57 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
if (WantedBits == CharWidth) {
|
|
|
|
// Got a full byte: just add it directly.
|
|
|
|
add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
|
|
|
|
OffsetInChars, AllowOverwrite);
|
|
|
|
} else {
|
|
|
|
// Partial byte: update the existing integer if there is one. If we
|
|
|
|
// can't split out a 1-CharUnit range to update, then we can't add
|
|
|
|
// these bits and fail the entire constant emission.
|
|
|
|
llvm::Optional<size_t> FirstElemToUpdate = splitAt(OffsetInChars);
|
|
|
|
if (!FirstElemToUpdate)
|
|
|
|
return false;
|
|
|
|
llvm::Optional<size_t> LastElemToUpdate =
|
|
|
|
splitAt(OffsetInChars + CharUnits::One());
|
|
|
|
if (!LastElemToUpdate)
|
|
|
|
return false;
|
|
|
|
assert(*LastElemToUpdate - *FirstElemToUpdate < 2 &&
|
|
|
|
"should have at most one element covering one byte");
|
|
|
|
|
|
|
|
// Figure out which bits we want and discard the rest.
|
|
|
|
llvm::APInt UpdateMask(CharWidth, 0);
|
|
|
|
if (CGM.getDataLayout().isBigEndian())
|
|
|
|
UpdateMask.setBits(CharWidth - OffsetWithinChar - WantedBits,
|
|
|
|
CharWidth - OffsetWithinChar);
|
|
|
|
else
|
|
|
|
UpdateMask.setBits(OffsetWithinChar, OffsetWithinChar + WantedBits);
|
|
|
|
BitsThisChar &= UpdateMask;
|
|
|
|
|
|
|
|
if (*FirstElemToUpdate == *LastElemToUpdate ||
|
|
|
|
Elems[*FirstElemToUpdate]->isNullValue() ||
|
|
|
|
isa<llvm::UndefValue>(Elems[*FirstElemToUpdate])) {
|
|
|
|
// All existing bits are either zero or undef.
|
|
|
|
add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
|
|
|
|
OffsetInChars, /*AllowOverwrite*/ true);
|
|
|
|
} else {
|
|
|
|
llvm::Constant *&ToUpdate = Elems[*FirstElemToUpdate];
|
|
|
|
// In order to perform a partial update, we need the existing bitwise
|
|
|
|
// value, which we can only extract for a constant int.
|
|
|
|
auto *CI = dyn_cast<llvm::ConstantInt>(ToUpdate);
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
|
|
|
// Because this is a 1-CharUnit range, the constant occupying it must
|
|
|
|
// be exactly one CharUnit wide.
|
|
|
|
assert(CI->getBitWidth() == CharWidth && "splitAt failed");
|
|
|
|
assert((!(CI->getValue() & UpdateMask) || AllowOverwrite) &&
|
|
|
|
"unexpectedly overwriting bitfield");
|
|
|
|
BitsThisChar |= (CI->getValue() & ~UpdateMask);
|
|
|
|
ToUpdate = llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// Stop if we've added all the bits.
|
|
|
|
if (WantedBits == Bits.getBitWidth())
|
|
|
|
break;
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// Remove the consumed bits from Bits.
|
|
|
|
if (!CGM.getDataLayout().isBigEndian())
|
|
|
|
Bits.lshrInPlace(WantedBits);
|
|
|
|
Bits = Bits.trunc(Bits.getBitWidth() - WantedBits);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// The remanining bits go at the start of the following bytes.
|
|
|
|
OffsetWithinChar = 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
/// Returns a position within Elems and Offsets such that all elements
|
|
|
|
/// before the returned index end before Pos and all elements at or after
|
|
|
|
/// the returned index begin at or after Pos. Splits elements as necessary
|
|
|
|
/// to ensure this. Returns None if we find something we can't split.
|
|
|
|
Optional<size_t> ConstantAggregateBuilder::splitAt(CharUnits Pos) {
|
|
|
|
if (Pos >= Size)
|
|
|
|
return Offsets.size();
|
|
|
|
|
|
|
|
while (true) {
|
2019-07-03 16:13:17 +08:00
|
|
|
auto FirstAfterPos = llvm::upper_bound(Offsets, Pos);
|
2019-06-18 05:08:30 +08:00
|
|
|
if (FirstAfterPos == Offsets.begin())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If we already have an element starting at Pos, we're done.
|
|
|
|
size_t LastAtOrBeforePosIndex = FirstAfterPos - Offsets.begin() - 1;
|
|
|
|
if (Offsets[LastAtOrBeforePosIndex] == Pos)
|
|
|
|
return LastAtOrBeforePosIndex;
|
|
|
|
|
|
|
|
// We found an element starting before Pos. Check for overlap.
|
|
|
|
if (Offsets[LastAtOrBeforePosIndex] +
|
|
|
|
getSize(Elems[LastAtOrBeforePosIndex]) <= Pos)
|
|
|
|
return LastAtOrBeforePosIndex + 1;
|
|
|
|
|
|
|
|
// Try to decompose it into smaller constants.
|
|
|
|
if (!split(LastAtOrBeforePosIndex, Pos))
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
/// Split the constant at index Index, if possible. Return true if we did.
|
|
|
|
/// Hint indicates the location at which we'd like to split, but may be
|
|
|
|
/// ignored.
|
|
|
|
bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
|
|
|
|
NaturalLayout = false;
|
|
|
|
llvm::Constant *C = Elems[Index];
|
|
|
|
CharUnits Offset = Offsets[Index];
|
|
|
|
|
|
|
|
if (auto *CA = dyn_cast<llvm::ConstantAggregate>(C)) {
|
|
|
|
replace(Elems, Index, Index + 1,
|
|
|
|
llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
|
|
|
|
[&](unsigned Op) { return CA->getOperand(Op); }));
|
|
|
|
if (auto *Seq = dyn_cast<llvm::SequentialType>(CA->getType())) {
|
|
|
|
// Array or vector.
|
|
|
|
CharUnits ElemSize = getSize(Seq->getElementType());
|
|
|
|
replace(
|
|
|
|
Offsets, Index, Index + 1,
|
|
|
|
llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
|
|
|
|
[&](unsigned Op) { return Offset + Op * ElemSize; }));
|
|
|
|
} else {
|
|
|
|
// Must be a struct.
|
|
|
|
auto *ST = cast<llvm::StructType>(CA->getType());
|
|
|
|
const llvm::StructLayout *Layout =
|
|
|
|
CGM.getDataLayout().getStructLayout(ST);
|
|
|
|
replace(Offsets, Index, Index + 1,
|
|
|
|
llvm::map_range(
|
|
|
|
llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) {
|
|
|
|
return Offset + CharUnits::fromQuantity(
|
|
|
|
Layout->getElementOffset(Op));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
if (auto *CDS = dyn_cast<llvm::ConstantDataSequential>(C)) {
|
|
|
|
// FIXME: If possible, split into two ConstantDataSequentials at Hint.
|
|
|
|
CharUnits ElemSize = getSize(CDS->getElementType());
|
|
|
|
replace(Elems, Index, Index + 1,
|
|
|
|
llvm::map_range(llvm::seq(0u, CDS->getNumElements()),
|
|
|
|
[&](unsigned Elem) {
|
|
|
|
return CDS->getElementAsConstant(Elem);
|
|
|
|
}));
|
|
|
|
replace(Offsets, Index, Index + 1,
|
|
|
|
llvm::map_range(
|
|
|
|
llvm::seq(0u, CDS->getNumElements()),
|
|
|
|
[&](unsigned Elem) { return Offset + Elem * ElemSize; }));
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-27 10:56:37 +08:00
|
|
|
|
2019-06-18 14:41:56 +08:00
|
|
|
if (isa<llvm::ConstantAggregateZero>(C)) {
|
2019-06-18 05:08:30 +08:00
|
|
|
CharUnits ElemSize = getSize(C);
|
|
|
|
assert(Hint > Offset && Hint < Offset + ElemSize && "nothing to split");
|
|
|
|
replace(Elems, Index, Index + 1,
|
|
|
|
{getZeroes(Hint - Offset), getZeroes(Offset + ElemSize - Hint)});
|
|
|
|
replace(Offsets, Index, Index + 1, {Offset, Hint});
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-27 10:56:37 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
if (isa<llvm::UndefValue>(C)) {
|
|
|
|
replace(Elems, Index, Index + 1, {});
|
|
|
|
replace(Offsets, Index, Index + 1, {});
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// FIXME: We could split a ConstantInt if the need ever arose.
|
|
|
|
// We don't need to do this to handle bit-fields because we always eagerly
|
|
|
|
// split them into 1-byte chunks.
|
2009-07-27 10:56:37 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-09 05:12:32 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
static llvm::Constant *
|
|
|
|
EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
|
|
|
|
llvm::Type *CommonElementType, unsigned ArrayBound,
|
|
|
|
SmallVectorImpl<llvm::Constant *> &Elements,
|
|
|
|
llvm::Constant *Filler);
|
|
|
|
|
|
|
|
llvm::Constant *ConstantAggregateBuilder::buildFrom(
|
|
|
|
CodeGenModule &CGM, ArrayRef<llvm::Constant *> Elems,
|
|
|
|
ArrayRef<CharUnits> Offsets, CharUnits StartOffset, CharUnits Size,
|
|
|
|
bool NaturalLayout, llvm::Type *DesiredTy, bool AllowOversized) {
|
|
|
|
ConstantAggregateBuilderUtils Utils(CGM);
|
|
|
|
|
|
|
|
if (Elems.empty())
|
|
|
|
return llvm::UndefValue::get(DesiredTy);
|
|
|
|
|
|
|
|
auto Offset = [&](size_t I) { return Offsets[I] - StartOffset; };
|
|
|
|
|
|
|
|
// If we want an array type, see if all the elements are the same type and
|
|
|
|
// appropriately spaced.
|
|
|
|
if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(DesiredTy)) {
|
|
|
|
assert(!AllowOversized && "oversized array emission not supported");
|
|
|
|
|
|
|
|
bool CanEmitArray = true;
|
|
|
|
llvm::Type *CommonType = Elems[0]->getType();
|
|
|
|
llvm::Constant *Filler = llvm::Constant::getNullValue(CommonType);
|
|
|
|
CharUnits ElemSize = Utils.getSize(ATy->getElementType());
|
|
|
|
SmallVector<llvm::Constant*, 32> ArrayElements;
|
|
|
|
for (size_t I = 0; I != Elems.size(); ++I) {
|
|
|
|
// Skip zeroes; we'll use a zero value as our array filler.
|
|
|
|
if (Elems[I]->isNullValue())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// All remaining elements must be the same type.
|
|
|
|
if (Elems[I]->getType() != CommonType ||
|
|
|
|
Offset(I) % ElemSize != 0) {
|
|
|
|
CanEmitArray = false;
|
|
|
|
break;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2019-06-18 05:08:30 +08:00
|
|
|
ArrayElements.resize(Offset(I) / ElemSize + 1, Filler);
|
|
|
|
ArrayElements.back() = Elems[I];
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
if (CanEmitArray) {
|
|
|
|
return EmitArrayConstant(CGM, ATy, CommonType, ATy->getNumElements(),
|
|
|
|
ArrayElements, Filler);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// Can't emit as an array, carry on to emit as a struct.
|
|
|
|
}
|
|
|
|
|
|
|
|
CharUnits DesiredSize = Utils.getSize(DesiredTy);
|
|
|
|
CharUnits Align = CharUnits::One();
|
|
|
|
for (llvm::Constant *C : Elems)
|
|
|
|
Align = std::max(Align, Utils.getAlignment(C));
|
|
|
|
CharUnits AlignedSize = Size.alignTo(Align);
|
|
|
|
|
|
|
|
bool Packed = false;
|
|
|
|
ArrayRef<llvm::Constant*> UnpackedElems = Elems;
|
|
|
|
llvm::SmallVector<llvm::Constant*, 32> UnpackedElemStorage;
|
|
|
|
if ((DesiredSize < AlignedSize && !AllowOversized) ||
|
|
|
|
DesiredSize.alignTo(Align) != DesiredSize) {
|
|
|
|
// The natural layout would be the wrong size; force use of a packed layout.
|
|
|
|
NaturalLayout = false;
|
|
|
|
Packed = true;
|
|
|
|
} else if (DesiredSize > AlignedSize) {
|
|
|
|
// The constant would be too small. Add padding to fix it.
|
|
|
|
UnpackedElemStorage.assign(Elems.begin(), Elems.end());
|
|
|
|
UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size));
|
|
|
|
UnpackedElems = UnpackedElemStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a natural layout, insert padding as necessary.
|
|
|
|
// As we go, double-check to see if we can actually just emit Elems
|
|
|
|
// as a non-packed struct and do so opportunistically if possible.
|
|
|
|
llvm::SmallVector<llvm::Constant*, 32> PackedElems;
|
|
|
|
if (!NaturalLayout) {
|
|
|
|
CharUnits SizeSoFar = CharUnits::Zero();
|
|
|
|
for (size_t I = 0; I != Elems.size(); ++I) {
|
|
|
|
CharUnits Align = Utils.getAlignment(Elems[I]);
|
|
|
|
CharUnits NaturalOffset = SizeSoFar.alignTo(Align);
|
|
|
|
CharUnits DesiredOffset = Offset(I);
|
|
|
|
assert(DesiredOffset >= SizeSoFar && "elements out of order");
|
|
|
|
|
|
|
|
if (DesiredOffset != NaturalOffset)
|
|
|
|
Packed = true;
|
|
|
|
if (DesiredOffset != SizeSoFar)
|
|
|
|
PackedElems.push_back(Utils.getPadding(DesiredOffset - SizeSoFar));
|
|
|
|
PackedElems.push_back(Elems[I]);
|
|
|
|
SizeSoFar = DesiredOffset + Utils.getSize(Elems[I]);
|
2010-07-06 02:03:30 +08:00
|
|
|
}
|
2019-06-18 05:08:30 +08:00
|
|
|
// If we're using the packed layout, pad it out to the desired size if
|
|
|
|
// necessary.
|
|
|
|
if (Packed) {
|
|
|
|
assert((SizeSoFar <= DesiredSize || AllowOversized) &&
|
|
|
|
"requested size is too small for contents");
|
|
|
|
if (SizeSoFar < DesiredSize)
|
|
|
|
PackedElems.push_back(Utils.getPadding(DesiredSize - SizeSoFar));
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::StructType *STy = llvm::ConstantStruct::getTypeForElements(
|
|
|
|
CGM.getLLVMContext(), Packed ? PackedElems : UnpackedElems, Packed);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// Pick the type to use. If the type is layout identical to the desired
|
|
|
|
// type then use it, otherwise use whatever the builder produced for us.
|
|
|
|
if (llvm::StructType *DesiredSTy = dyn_cast<llvm::StructType>(DesiredTy)) {
|
|
|
|
if (DesiredSTy->isLayoutIdentical(STy))
|
|
|
|
STy = DesiredSTy;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
return llvm::ConstantStruct::get(STy, Packed ? PackedElems : UnpackedElems);
|
|
|
|
}
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
void ConstantAggregateBuilder::condense(CharUnits Offset,
|
|
|
|
llvm::Type *DesiredTy) {
|
|
|
|
CharUnits Size = getSize(DesiredTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::Optional<size_t> FirstElemToReplace = splitAt(Offset);
|
|
|
|
if (!FirstElemToReplace)
|
|
|
|
return;
|
|
|
|
size_t First = *FirstElemToReplace;
|
|
|
|
|
|
|
|
llvm::Optional<size_t> LastElemToReplace = splitAt(Offset + Size);
|
|
|
|
if (!LastElemToReplace)
|
|
|
|
return;
|
|
|
|
size_t Last = *LastElemToReplace;
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
size_t Length = Last - First;
|
|
|
|
if (Length == 0)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
if (Length == 1 && Offsets[First] == Offset &&
|
|
|
|
getSize(Elems[First]) == Size) {
|
|
|
|
// Re-wrap single element structs if necessary. Otherwise, leave any single
|
|
|
|
// element constant of the right size alone even if it has the wrong type.
|
|
|
|
auto *STy = dyn_cast<llvm::StructType>(DesiredTy);
|
|
|
|
if (STy && STy->getNumElements() == 1 &&
|
|
|
|
STy->getElementType(0) == Elems[First]->getType())
|
|
|
|
Elems[First] = llvm::ConstantStruct::get(STy, Elems[First]);
|
|
|
|
return;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::Constant *Replacement = buildFrom(
|
|
|
|
CGM, makeArrayRef(Elems).slice(First, Length),
|
|
|
|
makeArrayRef(Offsets).slice(First, Length), Offset, getSize(DesiredTy),
|
|
|
|
/*known to have natural layout=*/false, DesiredTy, false);
|
|
|
|
replace(Elems, First, Last, {Replacement});
|
|
|
|
replace(Offsets, First, Last, {Offset});
|
|
|
|
}
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConstStructBuilder
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
class ConstStructBuilder {
|
|
|
|
CodeGenModule &CGM;
|
|
|
|
ConstantEmitter &Emitter;
|
|
|
|
ConstantAggregateBuilder &Builder;
|
|
|
|
CharUnits StartOffset;
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
public:
|
|
|
|
static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
|
|
|
|
InitListExpr *ILE, QualType StructTy);
|
|
|
|
static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
|
|
|
|
const APValue &Value, QualType ValTy);
|
|
|
|
static bool UpdateStruct(ConstantEmitter &Emitter,
|
|
|
|
ConstantAggregateBuilder &Const, CharUnits Offset,
|
|
|
|
InitListExpr *Updater);
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
private:
|
|
|
|
ConstStructBuilder(ConstantEmitter &Emitter,
|
|
|
|
ConstantAggregateBuilder &Builder, CharUnits StartOffset)
|
|
|
|
: CGM(Emitter.CGM), Emitter(Emitter), Builder(Builder),
|
|
|
|
StartOffset(StartOffset) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
|
|
|
|
llvm::Constant *InitExpr, bool AllowOverwrite = false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst,
|
|
|
|
bool AllowOverwrite = false);
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
|
|
|
|
llvm::ConstantInt *InitExpr, bool AllowOverwrite = false);
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool Build(InitListExpr *ILE, bool AllowOverwrite);
|
|
|
|
bool Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
|
|
|
|
const CXXRecordDecl *VTableClass, CharUnits BaseOffset);
|
|
|
|
llvm::Constant *Finalize(QualType Ty);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ConstStructBuilder::AppendField(
|
|
|
|
const FieldDecl *Field, uint64_t FieldOffset, llvm::Constant *InitCst,
|
|
|
|
bool AllowOverwrite) {
|
|
|
|
const ASTContext &Context = CGM.getContext();
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
|
|
|
|
|
|
|
|
return AppendBytes(FieldOffsetInChars, InitCst, AllowOverwrite);
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool ConstStructBuilder::AppendBytes(CharUnits FieldOffsetInChars,
|
|
|
|
llvm::Constant *InitCst,
|
|
|
|
bool AllowOverwrite) {
|
|
|
|
return Builder.add(InitCst, StartOffset + FieldOffsetInChars, AllowOverwrite);
|
|
|
|
}
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool ConstStructBuilder::AppendBitField(
|
|
|
|
const FieldDecl *Field, uint64_t FieldOffset, llvm::ConstantInt *CI,
|
|
|
|
bool AllowOverwrite) {
|
|
|
|
uint64_t FieldSize = Field->getBitWidthValue(CGM.getContext());
|
|
|
|
llvm::APInt FieldValue = CI->getValue();
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// Promote the size of FieldValue if necessary
|
|
|
|
// FIXME: This should never occur, but currently it can because initializer
|
|
|
|
// constants are cast to bool, and because clang is not enforcing bitfield
|
|
|
|
// width limits.
|
|
|
|
if (FieldSize > FieldValue.getBitWidth())
|
|
|
|
FieldValue = FieldValue.zext(FieldSize);
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// Truncate the size of FieldValue to the bit field size.
|
|
|
|
if (FieldSize < FieldValue.getBitWidth())
|
|
|
|
FieldValue = FieldValue.trunc(FieldSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
return Builder.addBits(FieldValue,
|
|
|
|
CGM.getContext().toBits(StartOffset) + FieldOffset,
|
|
|
|
AllowOverwrite);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter,
|
|
|
|
ConstantAggregateBuilder &Const,
|
|
|
|
CharUnits Offset, QualType Type,
|
|
|
|
InitListExpr *Updater) {
|
|
|
|
if (Type->isRecordType())
|
|
|
|
return ConstStructBuilder::UpdateStruct(Emitter, Const, Offset, Updater);
|
|
|
|
|
|
|
|
auto CAT = Emitter.CGM.getContext().getAsConstantArrayType(Type);
|
|
|
|
if (!CAT)
|
|
|
|
return false;
|
|
|
|
QualType ElemType = CAT->getElementType();
|
|
|
|
CharUnits ElemSize = Emitter.CGM.getContext().getTypeSizeInChars(ElemType);
|
|
|
|
llvm::Type *ElemTy = Emitter.CGM.getTypes().ConvertTypeForMem(ElemType);
|
|
|
|
|
|
|
|
llvm::Constant *FillC = nullptr;
|
|
|
|
if (Expr *Filler = Updater->getArrayFiller()) {
|
|
|
|
if (!isa<NoInitExpr>(Filler)) {
|
|
|
|
FillC = Emitter.tryEmitAbstractForMemory(Filler, ElemType);
|
|
|
|
if (!FillC)
|
|
|
|
return false;
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
unsigned NumElementsToUpdate =
|
|
|
|
FillC ? CAT->getSize().getZExtValue() : Updater->getNumInits();
|
|
|
|
for (unsigned I = 0; I != NumElementsToUpdate; ++I, Offset += ElemSize) {
|
|
|
|
Expr *Init = nullptr;
|
|
|
|
if (I < Updater->getNumInits())
|
|
|
|
Init = Updater->getInit(I);
|
|
|
|
|
|
|
|
if (!Init && FillC) {
|
|
|
|
if (!Const.add(FillC, Offset, true))
|
|
|
|
return false;
|
|
|
|
} else if (!Init || isa<NoInitExpr>(Init)) {
|
|
|
|
continue;
|
|
|
|
} else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init)) {
|
|
|
|
if (!EmitDesignatedInitUpdater(Emitter, Const, Offset, ElemType,
|
|
|
|
ChildILE))
|
|
|
|
return false;
|
|
|
|
// Attempt to reduce the array element to a single constant if necessary.
|
|
|
|
Const.condense(Offset, ElemTy);
|
|
|
|
} else {
|
|
|
|
llvm::Constant *Val = Emitter.tryEmitPrivateForMemory(Init, ElemType);
|
|
|
|
if (!Const.add(Val, Offset, true))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-04-14 01:45:57 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
return true;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) {
|
2019-10-03 19:22:48 +08:00
|
|
|
RecordDecl *RD = ILE->getType()->castAs<RecordType>()->getDecl();
|
2010-04-14 01:45:57 +08:00
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
unsigned FieldNo = -1;
|
2010-04-14 01:45:57 +08:00
|
|
|
unsigned ElementNo = 0;
|
2016-03-09 06:17:41 +08:00
|
|
|
|
|
|
|
// Bail out if we have base classes. We could support these, but they only
|
|
|
|
// arise in C++1z where we will have already constant folded most interesting
|
|
|
|
// cases. FIXME: There are still a few more cases we can handle this way.
|
|
|
|
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
|
|
|
|
if (CXXRD->getNumBases())
|
|
|
|
return false;
|
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
for (FieldDecl *Field : RD->fields()) {
|
|
|
|
++FieldNo;
|
|
|
|
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
// If this is a union, skip all the fields that aren't being initialized.
|
2019-06-21 04:44:45 +08:00
|
|
|
if (RD->isUnion() &&
|
|
|
|
!declaresSameEntity(ILE->getInitializedFieldInUnion(), Field))
|
2010-04-14 01:45:57 +08:00
|
|
|
continue;
|
|
|
|
|
2019-06-21 04:44:45 +08:00
|
|
|
// Don't emit anonymous bitfields or zero-sized fields.
|
|
|
|
if (Field->isUnnamedBitfield() || Field->isZeroSize(CGM.getContext()))
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
// Get the initializer. A struct can include fields without initializers,
|
|
|
|
// we just use explicit null values for them.
|
2019-06-18 05:08:30 +08:00
|
|
|
Expr *Init = nullptr;
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
if (ElementNo < ILE->getNumInits())
|
2019-06-18 05:08:30 +08:00
|
|
|
Init = ILE->getInit(ElementNo++);
|
|
|
|
if (Init && isa<NoInitExpr>(Init))
|
|
|
|
continue;
|
2010-07-18 07:55:01 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
// When emitting a DesignatedInitUpdateExpr, a nested InitListExpr
|
|
|
|
// represents additional overwriting of our current constant value, and not
|
|
|
|
// a new constant to emit independently.
|
|
|
|
if (AllowOverwrite &&
|
|
|
|
(Field->getType()->isArrayType() || Field->getType()->isRecordType())) {
|
|
|
|
if (auto *SubILE = dyn_cast<InitListExpr>(Init)) {
|
|
|
|
CharUnits Offset = CGM.getContext().toCharUnitsFromBits(
|
|
|
|
Layout.getFieldOffset(FieldNo));
|
|
|
|
if (!EmitDesignatedInitUpdater(Emitter, Builder, StartOffset + Offset,
|
|
|
|
Field->getType(), SubILE))
|
|
|
|
return false;
|
|
|
|
// If we split apart the field's value, try to collapse it down to a
|
|
|
|
// single value now.
|
|
|
|
Builder.condense(StartOffset + Offset,
|
|
|
|
CGM.getTypes().ConvertTypeForMem(Field->getType()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *EltInit =
|
|
|
|
Init ? Emitter.tryEmitPrivateForMemory(Init, Field->getType())
|
|
|
|
: Emitter.emitNullForMemory(Field->getType());
|
2010-07-18 07:55:01 +08:00
|
|
|
if (!EltInit)
|
|
|
|
return false;
|
2015-03-15 06:24:38 +08:00
|
|
|
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
if (!Field->isBitField()) {
|
|
|
|
// Handle non-bitfield members.
|
2019-06-18 05:08:30 +08:00
|
|
|
if (!AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit,
|
|
|
|
AllowOverwrite))
|
|
|
|
return false;
|
2019-06-21 04:44:45 +08:00
|
|
|
// After emitting a non-empty field with [[no_unique_address]], we may
|
|
|
|
// need to overwrite its tail padding.
|
|
|
|
if (Field->hasAttr<NoUniqueAddressAttr>())
|
|
|
|
AllowOverwrite = true;
|
2012-01-14 12:30:29 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise we have a bitfield.
|
2015-03-15 06:24:38 +08:00
|
|
|
if (auto *CI = dyn_cast<llvm::ConstantInt>(EltInit)) {
|
2019-06-18 05:08:30 +08:00
|
|
|
if (!AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI,
|
|
|
|
AllowOverwrite))
|
|
|
|
return false;
|
2015-03-15 06:24:38 +08:00
|
|
|
} else {
|
|
|
|
// We are trying to initialize a bitfield with a non-trivial constant,
|
|
|
|
// this must require run-time code.
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
namespace {
|
|
|
|
struct BaseInfo {
|
|
|
|
BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
|
|
|
|
: Decl(Decl), Offset(Offset), Index(Index) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *Decl;
|
|
|
|
CharUnits Offset;
|
|
|
|
unsigned Index;
|
|
|
|
|
|
|
|
bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-02-23 16:33:23 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
|
2013-09-27 22:48:01 +08:00
|
|
|
bool IsPrimaryBase,
|
2012-02-23 16:33:23 +08:00
|
|
|
const CXXRecordDecl *VTableClass,
|
|
|
|
CharUnits Offset) {
|
2012-01-14 12:30:29 +08:00
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
|
|
|
|
// Add a vtable pointer, if we need one and it hasn't already been added.
|
2013-09-27 22:48:01 +08:00
|
|
|
if (CD->isDynamicClass() && !IsPrimaryBase) {
|
|
|
|
llvm::Constant *VTableAddressPoint =
|
|
|
|
CGM.getCXXABI().getVTableAddressPointForConstExpr(
|
|
|
|
BaseSubobject(CD, Offset), VTableClass);
|
2019-06-18 05:08:30 +08:00
|
|
|
if (!AppendBytes(Offset, VTableAddressPoint))
|
|
|
|
return false;
|
2013-09-27 22:48:01 +08:00
|
|
|
}
|
2012-02-23 16:33:23 +08:00
|
|
|
|
|
|
|
// Accumulate and sort bases, in order to visit them in address order, which
|
|
|
|
// may not be the same as declaration order.
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<BaseInfo, 8> Bases;
|
2012-02-23 16:33:23 +08:00
|
|
|
Bases.reserve(CD->getNumBases());
|
2012-01-14 12:30:29 +08:00
|
|
|
unsigned BaseNo = 0;
|
2012-02-23 16:33:23 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
|
2012-01-14 12:30:29 +08:00
|
|
|
BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
|
2012-02-23 16:33:23 +08:00
|
|
|
assert(!Base->isVirtual() && "should not have virtual bases here");
|
2012-01-14 12:30:29 +08:00
|
|
|
const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
|
|
|
|
CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
|
2012-02-23 16:33:23 +08:00
|
|
|
Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
|
|
|
|
}
|
2019-04-24 22:43:05 +08:00
|
|
|
llvm::stable_sort(Bases);
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
for (unsigned I = 0, N = Bases.size(); I != N; ++I) {
|
|
|
|
BaseInfo &Base = Bases[I];
|
|
|
|
|
|
|
|
bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
|
|
|
|
Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
|
2013-09-27 22:48:01 +08:00
|
|
|
VTableClass, Offset + Base.Offset);
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FieldNo = 0;
|
2012-03-30 11:55:31 +08:00
|
|
|
uint64_t OffsetBits = CGM.getContext().toBits(Offset);
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2019-06-21 04:44:45 +08:00
|
|
|
bool AllowOverwrite = false;
|
2012-01-14 12:30:29 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
|
|
|
|
// If this is a union, skip all the fields that aren't being initialized.
|
2019-05-23 04:36:06 +08:00
|
|
|
if (RD->isUnion() && !declaresSameEntity(Val.getUnionField(), *Field))
|
2012-01-14 12:30:29 +08:00
|
|
|
continue;
|
|
|
|
|
2019-06-21 04:44:45 +08:00
|
|
|
// Don't emit anonymous bitfields or zero-sized fields.
|
|
|
|
if (Field->isUnnamedBitfield() || Field->isZeroSize(CGM.getContext()))
|
2012-01-14 12:30:29 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Emit the value of the initializer.
|
|
|
|
const APValue &FieldValue =
|
|
|
|
RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
|
|
|
|
llvm::Constant *EltInit =
|
2017-08-16 05:42:52 +08:00
|
|
|
Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType());
|
|
|
|
if (!EltInit)
|
|
|
|
return false;
|
2012-01-14 12:30:29 +08:00
|
|
|
|
|
|
|
if (!Field->isBitField()) {
|
|
|
|
// Handle non-bitfield members.
|
2019-06-18 05:08:30 +08:00
|
|
|
if (!AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
|
2019-06-21 04:44:45 +08:00
|
|
|
EltInit, AllowOverwrite))
|
2019-06-18 05:08:30 +08:00
|
|
|
return false;
|
2019-06-21 04:44:45 +08:00
|
|
|
// After emitting a non-empty field with [[no_unique_address]], we may
|
|
|
|
// need to overwrite its tail padding.
|
|
|
|
if (Field->hasAttr<NoUniqueAddressAttr>())
|
|
|
|
AllowOverwrite = true;
|
2010-04-14 01:45:57 +08:00
|
|
|
} else {
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
// Otherwise we have a bitfield.
|
2019-06-18 05:08:30 +08:00
|
|
|
if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
|
2019-06-21 04:44:45 +08:00
|
|
|
cast<llvm::ConstantInt>(EltInit), AllowOverwrite))
|
2019-06-18 05:08:30 +08:00
|
|
|
return false;
|
2009-10-02 10:15:20 +08:00
|
|
|
}
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2017-08-16 05:42:52 +08:00
|
|
|
|
|
|
|
return true;
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::Constant *ConstStructBuilder::Finalize(QualType Type) {
|
2019-10-03 19:22:48 +08:00
|
|
|
RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::Type *ValTy = CGM.getTypes().ConvertType(Type);
|
|
|
|
return Builder.build(ValTy, RD->hasFlexibleArrayMember());
|
2015-06-10 08:27:52 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
|
|
|
|
InitListExpr *ILE,
|
|
|
|
QualType ValTy) {
|
2019-06-18 05:08:30 +08:00
|
|
|
ConstantAggregateBuilder Const(Emitter.CGM);
|
|
|
|
ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero());
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
if (!Builder.Build(ILE, /*AllowOverwrite*/false))
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
return Builder.Finalize(ValTy);
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
|
2012-01-14 12:30:29 +08:00
|
|
|
const APValue &Val,
|
|
|
|
QualType ValTy) {
|
2019-06-18 05:08:30 +08:00
|
|
|
ConstantAggregateBuilder Const(Emitter.CGM);
|
|
|
|
ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero());
|
2012-02-23 16:33:23 +08:00
|
|
|
|
|
|
|
const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl();
|
|
|
|
const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
|
2017-08-16 05:42:52 +08:00
|
|
|
if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero()))
|
|
|
|
return nullptr;
|
2012-02-23 16:33:23 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
return Builder.Finalize(ValTy);
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
bool ConstStructBuilder::UpdateStruct(ConstantEmitter &Emitter,
|
|
|
|
ConstantAggregateBuilder &Const,
|
|
|
|
CharUnits Offset, InitListExpr *Updater) {
|
|
|
|
return ConstStructBuilder(Emitter, Const, Offset)
|
|
|
|
.Build(Updater, /*AllowOverwrite*/ true);
|
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConstExprEmitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-01-18 05:42:19 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
static ConstantAddress tryEmitGlobalCompoundLiteral(CodeGenModule &CGM,
|
|
|
|
CodeGenFunction *CGF,
|
|
|
|
const CompoundLiteralExpr *E) {
|
|
|
|
CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType());
|
|
|
|
if (llvm::GlobalVariable *Addr =
|
|
|
|
CGM.getAddrOfConstantCompoundLiteralIfEmitted(E))
|
|
|
|
return ConstantAddress(Addr, Align);
|
|
|
|
|
Convert clang::LangAS to a strongly typed enum
Summary:
Convert clang::LangAS to a strongly typed enum
Currently both clang AST address spaces and target specific address spaces
are represented as unsigned which can lead to subtle errors if the wrong
type is passed. It is especially confusing in the CodeGen files as it is
not possible to see what kind of address space should be passed to a
function without looking at the implementation.
I originally made this change for our LLVM fork for the CHERI architecture
where we make extensive use of address spaces to differentiate between
capabilities and pointers. When merging the upstream changes I usually
run into some test failures or runtime crashes because the wrong kind of
address space is passed to a function. By converting the LangAS enum to a
C++11 we can catch these errors at compile time. Additionally, it is now
obvious from the function signature which kind of address space it expects.
I found the following errors while writing this patch:
- ItaniumRecordLayoutBuilder::LayoutField was passing a clang AST address
space to TargetInfo::getPointer{Width,Align}()
- TypePrinter::printAttributedAfter() prints the numeric value of the
clang AST address space instead of the target address space.
However, this code is not used so I kept the current behaviour
- initializeForBlockHeader() in CGBlocks.cpp was passing
LangAS::opencl_generic to TargetInfo::getPointer{Width,Align}()
- CodeGenFunction::EmitBlockLiteral() was passing a AST address space to
TargetInfo::getPointerWidth()
- CGOpenMPRuntimeNVPTX::translateParameter() passed a target address space
to Qualifiers::addAddressSpace()
- CGOpenMPRuntimeNVPTX::getParameterAddress() was using
llvm::Type::getPointerTo() with a AST address space
- clang_getAddressSpace() returns either a LangAS or a target address
space. As this is exposed to C I have kept the current behaviour and
added a comment stating that it is probably not correct.
Other than this the patch should not cause any functional changes.
Reviewers: yaxunl, pcc, bader
Reviewed By: yaxunl, bader
Subscribers: jlebar, jholewinski, nhaehnle, Anastasia, cfe-commits
Differential Revision: https://reviews.llvm.org/D38816
llvm-svn: 315871
2017-10-16 02:48:14 +08:00
|
|
|
LangAS addressSpace = E->getType().getAddressSpace();
|
2017-08-16 05:42:52 +08:00
|
|
|
|
|
|
|
ConstantEmitter emitter(CGM, CGF);
|
|
|
|
llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(),
|
|
|
|
addressSpace, E->getType());
|
|
|
|
if (!C) {
|
|
|
|
assert(!E->isFileScope() &&
|
|
|
|
"file-scope compound literal did not have constant initializer!");
|
|
|
|
return ConstantAddress::invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
|
|
|
|
CGM.isTypeConstant(E->getType(), true),
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
|
|
|
C, ".compoundliteral", nullptr,
|
|
|
|
llvm::GlobalVariable::NotThreadLocal,
|
|
|
|
CGM.getContext().getTargetAddressSpace(addressSpace));
|
|
|
|
emitter.finalize(GV);
|
2019-10-03 21:00:29 +08:00
|
|
|
GV->setAlignment(Align.getAsAlign());
|
2017-08-16 05:42:52 +08:00
|
|
|
CGM.setAddrOfConstantCompoundLiteral(E, GV);
|
|
|
|
return ConstantAddress(GV, Align);
|
|
|
|
}
|
|
|
|
|
2018-05-24 07:41:38 +08:00
|
|
|
static llvm::Constant *
|
2019-06-18 05:08:30 +08:00
|
|
|
EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
|
2018-05-24 07:41:38 +08:00
|
|
|
llvm::Type *CommonElementType, unsigned ArrayBound,
|
|
|
|
SmallVectorImpl<llvm::Constant *> &Elements,
|
|
|
|
llvm::Constant *Filler) {
|
|
|
|
// Figure out how long the initial prefix of non-zero elements is.
|
|
|
|
unsigned NonzeroLength = ArrayBound;
|
|
|
|
if (Elements.size() < NonzeroLength && Filler->isNullValue())
|
|
|
|
NonzeroLength = Elements.size();
|
|
|
|
if (NonzeroLength == Elements.size()) {
|
|
|
|
while (NonzeroLength > 0 && Elements[NonzeroLength - 1]->isNullValue())
|
|
|
|
--NonzeroLength;
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
if (NonzeroLength == 0)
|
|
|
|
return llvm::ConstantAggregateZero::get(DesiredType);
|
2018-05-24 07:41:38 +08:00
|
|
|
|
|
|
|
// Add a zeroinitializer array filler if we have lots of trailing zeroes.
|
|
|
|
unsigned TrailingZeroes = ArrayBound - NonzeroLength;
|
|
|
|
if (TrailingZeroes >= 8) {
|
|
|
|
assert(Elements.size() >= NonzeroLength &&
|
|
|
|
"missing initializer for non-zero element");
|
2018-07-20 05:38:56 +08:00
|
|
|
|
|
|
|
// If all the elements had the same type up to the trailing zeroes, emit a
|
|
|
|
// struct of two arrays (the nonzero data and the zeroinitializer).
|
|
|
|
if (CommonElementType && NonzeroLength >= 8) {
|
|
|
|
llvm::Constant *Initial = llvm::ConstantArray::get(
|
2018-07-20 07:24:41 +08:00
|
|
|
llvm::ArrayType::get(CommonElementType, NonzeroLength),
|
2018-07-20 05:38:56 +08:00
|
|
|
makeArrayRef(Elements).take_front(NonzeroLength));
|
|
|
|
Elements.resize(2);
|
|
|
|
Elements[0] = Initial;
|
|
|
|
} else {
|
|
|
|
Elements.resize(NonzeroLength + 1);
|
|
|
|
}
|
|
|
|
|
2018-05-24 07:41:38 +08:00
|
|
|
auto *FillerType =
|
2019-06-18 05:08:30 +08:00
|
|
|
CommonElementType ? CommonElementType : DesiredType->getElementType();
|
2018-05-24 07:41:38 +08:00
|
|
|
FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes);
|
|
|
|
Elements.back() = llvm::ConstantAggregateZero::get(FillerType);
|
|
|
|
CommonElementType = nullptr;
|
|
|
|
} else if (Elements.size() != ArrayBound) {
|
|
|
|
// Otherwise pad to the right size with the filler if necessary.
|
|
|
|
Elements.resize(ArrayBound, Filler);
|
|
|
|
if (Filler->getType() != CommonElementType)
|
|
|
|
CommonElementType = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all elements have the same type, just emit an array constant.
|
|
|
|
if (CommonElementType)
|
|
|
|
return llvm::ConstantArray::get(
|
|
|
|
llvm::ArrayType::get(CommonElementType, ArrayBound), Elements);
|
|
|
|
|
|
|
|
// We have mixed types. Use a packed struct.
|
|
|
|
llvm::SmallVector<llvm::Type *, 16> Types;
|
|
|
|
Types.reserve(Elements.size());
|
|
|
|
for (llvm::Constant *Elt : Elements)
|
|
|
|
Types.push_back(Elt->getType());
|
|
|
|
llvm::StructType *SType =
|
|
|
|
llvm::StructType::get(CGM.getLLVMContext(), Types, true);
|
|
|
|
return llvm::ConstantStruct::get(SType, Elements);
|
|
|
|
}
|
|
|
|
|
2019-02-09 05:36:04 +08:00
|
|
|
// This class only needs to handle arrays, structs and unions. Outside C++11
|
|
|
|
// mode, we don't currently constant fold those types. All other types are
|
|
|
|
// handled by constant folding.
|
|
|
|
//
|
|
|
|
// Constant folding is currently missing support for a few features supported
|
|
|
|
// here: CK_ToUnion, CK_ReinterpretMemberPointer, and DesignatedInitUpdateExpr.
|
2009-11-29 03:45:26 +08:00
|
|
|
class ConstExprEmitter :
|
2017-08-16 05:42:52 +08:00
|
|
|
public StmtVisitor<ConstExprEmitter, llvm::Constant*, QualType> {
|
2008-01-26 09:36:00 +08:00
|
|
|
CodeGenModule &CGM;
|
2017-08-16 05:42:52 +08:00
|
|
|
ConstantEmitter &Emitter;
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::LLVMContext &VMContext;
|
2008-01-26 09:36:00 +08:00
|
|
|
public:
|
2017-08-16 05:42:52 +08:00
|
|
|
ConstExprEmitter(ConstantEmitter &emitter)
|
|
|
|
: CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) {
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitStmt(Stmt *S, QualType T) {
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2018-11-09 08:41:36 +08:00
|
|
|
llvm::Constant *VisitConstantExpr(ConstantExpr *CE, QualType T) {
|
|
|
|
return Visit(CE->getSubExpr(), T);
|
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitParenExpr(ParenExpr *PE, QualType T) {
|
|
|
|
return Visit(PE->getSubExpr(), T);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-15 13:09:51 +08:00
|
|
|
llvm::Constant *
|
2017-08-16 05:42:52 +08:00
|
|
|
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE,
|
|
|
|
QualType T) {
|
|
|
|
return Visit(PE->getReplacement(), T);
|
2011-07-15 13:09:51 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE,
|
|
|
|
QualType T) {
|
|
|
|
return Visit(GE->getResultExpr(), T);
|
2011-04-15 08:35:48 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitChooseExpr(ChooseExpr *CE, QualType T) {
|
|
|
|
return Visit(CE->getChosenSubExpr(), T);
|
2013-07-17 06:40:53 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E, QualType T) {
|
|
|
|
return Visit(E->getInitializer(), T);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2011-02-03 16:15:49 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitCastExpr(CastExpr *E, QualType destType) {
|
2015-10-20 12:24:12 +08:00
|
|
|
if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
|
2017-08-16 05:42:52 +08:00
|
|
|
CGM.EmitExplicitCastExprType(ECE, Emitter.CGF);
|
2011-03-16 05:17:48 +08:00
|
|
|
Expr *subExpr = E->getSubExpr();
|
|
|
|
|
2009-08-23 07:54:44 +08:00
|
|
|
switch (E->getCastKind()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ToUnion: {
|
2009-08-23 07:54:44 +08:00
|
|
|
// GCC cast to union extension
|
|
|
|
assert(E->getType()->isUnionType() &&
|
|
|
|
"Destination type is not union type!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
auto field = E->getTargetUnionField();
|
|
|
|
|
|
|
|
auto C = Emitter.tryEmitPrivateForMemory(subExpr, field->getType());
|
|
|
|
if (!C) return nullptr;
|
|
|
|
|
|
|
|
auto destTy = ConvertType(destType);
|
|
|
|
if (C->getType() == destTy) return C;
|
|
|
|
|
2009-08-01 05:38:39 +08:00
|
|
|
// Build a struct with the union sub-element as the first member,
|
2017-08-16 05:42:52 +08:00
|
|
|
// and padded to the appropriate size.
|
2012-02-07 08:13:27 +08:00
|
|
|
SmallVector<llvm::Constant*, 2> Elts;
|
|
|
|
SmallVector<llvm::Type*, 2> Types;
|
2009-08-01 05:38:39 +08:00
|
|
|
Elts.push_back(C);
|
|
|
|
Types.push_back(C->getType());
|
2012-10-09 00:25:52 +08:00
|
|
|
unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
|
2017-08-16 05:42:52 +08:00
|
|
|
unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 05:38:39 +08:00
|
|
|
assert(CurSize <= TotalSize && "Union size mismatch!");
|
|
|
|
if (unsigned NumPadBytes = TotalSize - CurSize) {
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Type *Ty = CGM.Int8Ty;
|
2009-08-01 05:38:39 +08:00
|
|
|
if (NumPadBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumPadBytes);
|
2009-08-23 07:54:44 +08:00
|
|
|
|
2010-04-17 04:56:35 +08:00
|
|
|
Elts.push_back(llvm::UndefValue::get(Ty));
|
2009-08-01 05:38:39 +08:00
|
|
|
Types.push_back(Ty);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false);
|
2009-08-01 05:38:39 +08:00
|
|
|
return llvm::ConstantStruct::get(STy, Elts);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
2009-10-19 04:31:03 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
case CK_AddressSpaceConversion: {
|
|
|
|
auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
|
|
|
|
if (!C) return nullptr;
|
Convert clang::LangAS to a strongly typed enum
Summary:
Convert clang::LangAS to a strongly typed enum
Currently both clang AST address spaces and target specific address spaces
are represented as unsigned which can lead to subtle errors if the wrong
type is passed. It is especially confusing in the CodeGen files as it is
not possible to see what kind of address space should be passed to a
function without looking at the implementation.
I originally made this change for our LLVM fork for the CHERI architecture
where we make extensive use of address spaces to differentiate between
capabilities and pointers. When merging the upstream changes I usually
run into some test failures or runtime crashes because the wrong kind of
address space is passed to a function. By converting the LangAS enum to a
C++11 we can catch these errors at compile time. Additionally, it is now
obvious from the function signature which kind of address space it expects.
I found the following errors while writing this patch:
- ItaniumRecordLayoutBuilder::LayoutField was passing a clang AST address
space to TargetInfo::getPointer{Width,Align}()
- TypePrinter::printAttributedAfter() prints the numeric value of the
clang AST address space instead of the target address space.
However, this code is not used so I kept the current behaviour
- initializeForBlockHeader() in CGBlocks.cpp was passing
LangAS::opencl_generic to TargetInfo::getPointer{Width,Align}()
- CodeGenFunction::EmitBlockLiteral() was passing a AST address space to
TargetInfo::getPointerWidth()
- CGOpenMPRuntimeNVPTX::translateParameter() passed a target address space
to Qualifiers::addAddressSpace()
- CGOpenMPRuntimeNVPTX::getParameterAddress() was using
llvm::Type::getPointerTo() with a AST address space
- clang_getAddressSpace() returns either a LangAS or a target address
space. As this is exposed to C I have kept the current behaviour and
added a comment stating that it is probably not correct.
Other than this the patch should not cause any functional changes.
Reviewers: yaxunl, pcc, bader
Reviewed By: yaxunl, bader
Subscribers: jlebar, jholewinski, nhaehnle, Anastasia, cfe-commits
Differential Revision: https://reviews.llvm.org/D38816
llvm-svn: 315871
2017-10-16 02:48:14 +08:00
|
|
|
LangAS destAS = E->getType()->getPointeeType().getAddressSpace();
|
|
|
|
LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace();
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Type *destTy = ConvertType(E->getType());
|
|
|
|
return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS,
|
|
|
|
destAS, destTy);
|
|
|
|
}
|
2013-12-11 21:39:46 +08:00
|
|
|
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_LValueToRValue:
|
2012-01-17 01:27:18 +08:00
|
|
|
case CK_AtomicToNonAtomic:
|
|
|
|
case CK_NonAtomicToAtomic:
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_NoOp:
|
2013-07-17 06:40:53 +08:00
|
|
|
case CK_ConstructorConversion:
|
2017-08-16 05:42:52 +08:00
|
|
|
return Visit(subExpr, destType);
|
2011-03-16 05:17:48 +08:00
|
|
|
|
2016-07-29 03:26:30 +08:00
|
|
|
case CK_IntToOCLSampler:
|
|
|
|
llvm_unreachable("global sampler variables are not generated");
|
|
|
|
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_Dependent: llvm_unreachable("saw dependent cast!");
|
|
|
|
|
2012-08-31 08:14:07 +08:00
|
|
|
case CK_BuiltinFnToFnPtr:
|
|
|
|
llvm_unreachable("builtin functions are handled elsewhere");
|
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
case CK_ReinterpretMemberPointer:
|
|
|
|
case CK_DerivedToBaseMemberPointer:
|
2017-08-16 05:42:52 +08:00
|
|
|
case CK_BaseToDerivedMemberPointer: {
|
|
|
|
auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
|
|
|
|
if (!C) return nullptr;
|
2012-02-15 09:22:51 +08:00
|
|
|
return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
|
2017-08-16 05:42:52 +08:00
|
|
|
}
|
2012-02-15 09:22:51 +08:00
|
|
|
|
2011-03-16 05:17:48 +08:00
|
|
|
// These will never be supported.
|
|
|
|
case CK_ObjCObjectLValueCast:
|
2011-09-10 14:18:15 +08:00
|
|
|
case CK_ARCProduceObject:
|
|
|
|
case CK_ARCConsumeObject:
|
|
|
|
case CK_ARCReclaimReturnedObject:
|
|
|
|
case CK_ARCExtendBlockObject:
|
2012-02-22 13:02:47 +08:00
|
|
|
case CK_CopyAndAutoreleaseBlockObject:
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2011-03-16 05:17:48 +08:00
|
|
|
|
2012-01-18 05:42:19 +08:00
|
|
|
// These don't need to be handled here because Evaluate knows how to
|
|
|
|
// evaluate them in the cases where they can be folded.
|
2012-02-15 09:22:51 +08:00
|
|
|
case CK_BitCast:
|
2012-01-18 05:42:19 +08:00
|
|
|
case CK_ToVoid:
|
|
|
|
case CK_Dynamic:
|
|
|
|
case CK_LValueBitCast:
|
2019-07-03 02:28:13 +08:00
|
|
|
case CK_LValueToRValueBitCast:
|
2012-01-18 05:42:19 +08:00
|
|
|
case CK_NullToMemberPointer:
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_UserDefinedConversion:
|
2012-01-05 07:13:47 +08:00
|
|
|
case CK_CPointerToObjCPointerCast:
|
|
|
|
case CK_BlockPointerToObjCPointerCast:
|
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
|
|
|
case CK_BaseToDerived:
|
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_UncheckedDerivedToBase:
|
|
|
|
case CK_MemberPointerToBoolean:
|
|
|
|
case CK_VectorSplat:
|
|
|
|
case CK_FloatingRealToComplex:
|
|
|
|
case CK_FloatingComplexToReal:
|
|
|
|
case CK_FloatingComplexToBoolean:
|
|
|
|
case CK_FloatingComplexCast:
|
|
|
|
case CK_FloatingComplexToIntegralComplex:
|
|
|
|
case CK_IntegralRealToComplex:
|
|
|
|
case CK_IntegralComplexToReal:
|
|
|
|
case CK_IntegralComplexToBoolean:
|
|
|
|
case CK_IntegralComplexCast:
|
|
|
|
case CK_IntegralComplexToFloatingComplex:
|
|
|
|
case CK_PointerToIntegral:
|
|
|
|
case CK_PointerToBoolean:
|
|
|
|
case CK_NullToPointer:
|
2012-01-05 07:13:47 +08:00
|
|
|
case CK_IntegralCast:
|
2016-01-13 09:52:39 +08:00
|
|
|
case CK_BooleanToSignedIntegral:
|
2012-01-05 07:13:47 +08:00
|
|
|
case CK_IntegralToPointer:
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_IntegralToBoolean:
|
|
|
|
case CK_IntegralToFloating:
|
|
|
|
case CK_FloatingToIntegral:
|
|
|
|
case CK_FloatingToBoolean:
|
|
|
|
case CK_FloatingCast:
|
2018-10-16 00:07:02 +08:00
|
|
|
case CK_FixedPointCast:
|
2018-10-24 01:55:35 +08:00
|
|
|
case CK_FixedPointToBoolean:
|
2019-03-06 08:28:43 +08:00
|
|
|
case CK_FixedPointToIntegral:
|
|
|
|
case CK_IntegralToFixedPoint:
|
2018-10-23 23:19:20 +08:00
|
|
|
case CK_ZeroToOCLOpaqueType:
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2009-02-22 15:29:04 +08:00
|
|
|
}
|
2011-03-17 08:46:34 +08:00
|
|
|
llvm_unreachable("Invalid CastKind");
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-01-29 09:15:48 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE, QualType T) {
|
2013-04-21 06:23:05 +08:00
|
|
|
// No need for a DefaultInitExprScope: we don't handle 'this' in a
|
|
|
|
// constant expression.
|
2017-08-16 05:42:52 +08:00
|
|
|
return Visit(DIE->getExpr(), T);
|
2013-04-21 06:23:05 +08:00
|
|
|
}
|
2008-04-08 12:40:51 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitExprWithCleanups(ExprWithCleanups *E, QualType T) {
|
2016-06-22 04:29:17 +08:00
|
|
|
if (!E->cleanupsHaveSideEffects())
|
2017-08-16 05:42:52 +08:00
|
|
|
return Visit(E->getSubExpr(), T);
|
2016-06-22 04:29:17 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E,
|
|
|
|
QualType T) {
|
2019-11-17 15:09:25 +08:00
|
|
|
return Visit(E->GetTemporaryExpr(), T);
|
2011-06-22 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *EmitArrayInitialization(InitListExpr *ILE, QualType T) {
|
2018-05-24 07:41:38 +08:00
|
|
|
auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType());
|
|
|
|
assert(CAT && "can't emit array init for non-constant-bound array");
|
2012-04-15 10:50:59 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
2018-05-24 07:41:38 +08:00
|
|
|
unsigned NumElements = CAT->getSize().getZExtValue();
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Initialising an array requires us to automatically
|
2008-02-05 10:39:50 +08:00
|
|
|
// initialise any elements that have not been initialised explicitly
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
|
|
|
|
2018-05-24 07:41:38 +08:00
|
|
|
QualType EltType = CAT->getElementType();
|
2017-08-16 05:42:52 +08:00
|
|
|
|
2014-12-29 07:46:59 +08:00
|
|
|
// Initialize remaining array elements.
|
2018-05-24 07:41:38 +08:00
|
|
|
llvm::Constant *fillC = nullptr;
|
|
|
|
if (Expr *filler = ILE->getArrayFiller()) {
|
2017-08-16 05:42:52 +08:00
|
|
|
fillC = Emitter.tryEmitAbstractForMemory(filler, EltType);
|
2018-05-24 07:41:38 +08:00
|
|
|
if (!fillC)
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-12-29 07:46:59 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
// Copy initializer elements.
|
2017-08-16 05:42:52 +08:00
|
|
|
SmallVector<llvm::Constant*, 16> Elts;
|
2018-05-24 07:41:38 +08:00
|
|
|
if (fillC && fillC->isNullValue())
|
|
|
|
Elts.reserve(NumInitableElts + 1);
|
|
|
|
else
|
|
|
|
Elts.reserve(NumElements);
|
2012-02-14 20:06:21 +08:00
|
|
|
|
2018-05-24 07:41:38 +08:00
|
|
|
llvm::Type *CommonElementType = nullptr;
|
2012-02-14 20:06:21 +08:00
|
|
|
for (unsigned i = 0; i < NumInitableElts; ++i) {
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *Init = ILE->getInit(i);
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *C = Emitter.tryEmitPrivateForMemory(Init, EltType);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2018-05-24 07:41:38 +08:00
|
|
|
if (i == 0)
|
|
|
|
CommonElementType = C->getType();
|
|
|
|
else if (C->getType() != CommonElementType)
|
|
|
|
CommonElementType = nullptr;
|
2008-02-05 10:39:50 +08:00
|
|
|
Elts.push_back(C);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-05-31 03:58:50 +08:00
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::ArrayType *Desired =
|
|
|
|
cast<llvm::ArrayType>(CGM.getTypes().ConvertType(ILE->getType()));
|
|
|
|
return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
|
2018-05-24 07:41:38 +08:00
|
|
|
fillC);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *EmitRecordInitialization(InitListExpr *ILE, QualType T) {
|
|
|
|
return ConstStructBuilder::BuildStruct(Emitter, ILE, T);
|
2008-05-30 18:24:46 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E,
|
|
|
|
QualType T) {
|
|
|
|
return CGM.EmitNullConstant(T);
|
2009-01-30 14:13:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitInitListExpr(InitListExpr *ILE, QualType T) {
|
2016-12-07 07:52:28 +08:00
|
|
|
if (ILE->isTransparent())
|
2017-08-16 05:42:52 +08:00
|
|
|
return Visit(ILE->getInit(0), T);
|
2016-12-07 07:52:28 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isArrayType())
|
2017-08-16 05:42:52 +08:00
|
|
|
return EmitArrayInitialization(ILE, T);
|
2008-05-30 18:24:46 +08:00
|
|
|
|
2012-09-05 16:37:43 +08:00
|
|
|
if (ILE->getType()->isRecordType())
|
2017-08-16 05:42:52 +08:00
|
|
|
return EmitRecordInitialization(ILE, T);
|
2012-09-05 16:37:43 +08:00
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-02-22 01:57:49 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E,
|
|
|
|
QualType destType) {
|
|
|
|
auto C = Visit(E->getBase(), destType);
|
2019-06-18 05:08:30 +08:00
|
|
|
if (!C)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
ConstantAggregateBuilder Const(CGM);
|
|
|
|
Const.add(C, CharUnits::Zero(), false);
|
|
|
|
|
|
|
|
if (!EmitDesignatedInitUpdater(Emitter, Const, CharUnits::Zero(), destType,
|
|
|
|
E->getUpdater()))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
llvm::Type *ValTy = CGM.getTypes().ConvertType(destType);
|
|
|
|
bool HasFlexibleArray = false;
|
|
|
|
if (auto *RT = destType->getAs<RecordType>())
|
|
|
|
HasFlexibleArray = RT->getDecl()->hasFlexibleArrayMember();
|
|
|
|
return Const.build(ValTy, HasFlexibleArray);
|
2018-07-31 03:24:48 +08:00
|
|
|
}
|
2015-06-10 08:27:52 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E, QualType Ty) {
|
2010-02-02 16:02:49 +08:00
|
|
|
if (!E->getConstructor()->isTrivial())
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2010-02-02 16:02:49 +08:00
|
|
|
|
2010-02-06 02:38:45 +08:00
|
|
|
// FIXME: We should not have to call getBaseElementType here.
|
2019-10-16 18:38:40 +08:00
|
|
|
const auto *RT =
|
|
|
|
CGM.getContext().getBaseElementType(Ty)->castAs<RecordType>();
|
2010-02-06 02:38:45 +08:00
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-02-06 02:38:45 +08:00
|
|
|
// If the class doesn't have a trivial destructor, we can't emit it as a
|
|
|
|
// constant expr.
|
|
|
|
if (!RD->hasTrivialDestructor())
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2010-02-02 16:02:49 +08:00
|
|
|
// Only copy and default constructors can be trivial.
|
|
|
|
|
|
|
|
|
|
|
|
if (E->getNumArgs()) {
|
|
|
|
assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
|
2011-08-31 03:58:05 +08:00
|
|
|
assert(E->getConstructor()->isCopyOrMoveConstructor() &&
|
|
|
|
"trivial ctor has argument but isn't a copy/move ctor");
|
2010-02-02 16:02:49 +08:00
|
|
|
|
|
|
|
Expr *Arg = E->getArg(0);
|
|
|
|
assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
|
|
|
|
"argument to copy ctor is of wrong type");
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
return Visit(Arg, Ty);
|
2010-02-02 16:02:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return CGM.EmitNullConstant(Ty);
|
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitStringLiteral(StringLiteral *E, QualType T) {
|
2019-02-09 05:36:04 +08:00
|
|
|
// This is a string literal initializing an array in an initializer.
|
2011-11-01 10:23:42 +08:00
|
|
|
return CGM.GetConstantArrayFromStringLiteral(E);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E, QualType T) {
|
2009-02-25 06:18:39 +08:00
|
|
|
// This must be an @encode initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
|
|
|
std::string Str;
|
|
|
|
CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
|
2017-08-16 05:42:52 +08:00
|
|
|
const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
// Resize the string to the right size, adding zeros at the end, or
|
|
|
|
// truncating as needed.
|
|
|
|
Str.resize(CAT->getSize().getZExtValue(), '\0');
|
2012-02-05 10:30:40 +08:00
|
|
|
return llvm::ConstantDataArray::getString(VMContext, Str, false);
|
2009-02-25 06:18:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) {
|
|
|
|
return Visit(E->getSubExpr(), T);
|
2008-05-29 19:22:45 +08:00
|
|
|
}
|
2009-02-20 06:01:56 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
// Utility methods
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ConvertType(QualType T) {
|
2008-01-26 09:36:00 +08:00
|
|
|
return CGM.getTypes().ConvertType(T);
|
|
|
|
}
|
2008-01-26 10:08:50 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *ConstantEmitter::validateAndPopAbstract(llvm::Constant *C,
|
|
|
|
AbstractState saved) {
|
|
|
|
Abstract = saved.OldValue;
|
|
|
|
|
|
|
|
assert(saved.OldPlaceholdersSize == PlaceholderAddresses.size() &&
|
|
|
|
"created a placeholder while doing an abstract emission?");
|
|
|
|
|
|
|
|
// No validation necessary for now.
|
|
|
|
// No cleanup to do for now.
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) {
|
|
|
|
auto state = pushAbstract();
|
|
|
|
auto C = tryEmitPrivateForVarInit(D);
|
|
|
|
return validateAndPopAbstract(C, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
ConstantEmitter::tryEmitAbstract(const Expr *E, QualType destType) {
|
|
|
|
auto state = pushAbstract();
|
|
|
|
auto C = tryEmitPrivate(E, destType);
|
|
|
|
return validateAndPopAbstract(C, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
ConstantEmitter::tryEmitAbstract(const APValue &value, QualType destType) {
|
|
|
|
auto state = pushAbstract();
|
|
|
|
auto C = tryEmitPrivate(value, destType);
|
|
|
|
return validateAndPopAbstract(C, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
ConstantEmitter::emitAbstract(const Expr *E, QualType destType) {
|
|
|
|
auto state = pushAbstract();
|
|
|
|
auto C = tryEmitPrivate(E, destType);
|
|
|
|
C = validateAndPopAbstract(C, state);
|
|
|
|
if (!C) {
|
|
|
|
CGM.Error(E->getExprLoc(),
|
|
|
|
"internal error: could not emit constant value \"abstractly\"");
|
|
|
|
C = CGM.EmitNullConstant(destType);
|
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value,
|
|
|
|
QualType destType) {
|
|
|
|
auto state = pushAbstract();
|
|
|
|
auto C = tryEmitPrivate(value, destType);
|
|
|
|
C = validateAndPopAbstract(C, state);
|
|
|
|
if (!C) {
|
|
|
|
CGM.Error(loc,
|
|
|
|
"internal error: could not emit constant value \"abstractly\"");
|
|
|
|
C = CGM.EmitNullConstant(destType);
|
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) {
|
|
|
|
initializeNonAbstract(D.getType().getAddressSpace());
|
|
|
|
return markIfFailed(tryEmitPrivateForVarInit(D));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E,
|
Convert clang::LangAS to a strongly typed enum
Summary:
Convert clang::LangAS to a strongly typed enum
Currently both clang AST address spaces and target specific address spaces
are represented as unsigned which can lead to subtle errors if the wrong
type is passed. It is especially confusing in the CodeGen files as it is
not possible to see what kind of address space should be passed to a
function without looking at the implementation.
I originally made this change for our LLVM fork for the CHERI architecture
where we make extensive use of address spaces to differentiate between
capabilities and pointers. When merging the upstream changes I usually
run into some test failures or runtime crashes because the wrong kind of
address space is passed to a function. By converting the LangAS enum to a
C++11 we can catch these errors at compile time. Additionally, it is now
obvious from the function signature which kind of address space it expects.
I found the following errors while writing this patch:
- ItaniumRecordLayoutBuilder::LayoutField was passing a clang AST address
space to TargetInfo::getPointer{Width,Align}()
- TypePrinter::printAttributedAfter() prints the numeric value of the
clang AST address space instead of the target address space.
However, this code is not used so I kept the current behaviour
- initializeForBlockHeader() in CGBlocks.cpp was passing
LangAS::opencl_generic to TargetInfo::getPointer{Width,Align}()
- CodeGenFunction::EmitBlockLiteral() was passing a AST address space to
TargetInfo::getPointerWidth()
- CGOpenMPRuntimeNVPTX::translateParameter() passed a target address space
to Qualifiers::addAddressSpace()
- CGOpenMPRuntimeNVPTX::getParameterAddress() was using
llvm::Type::getPointerTo() with a AST address space
- clang_getAddressSpace() returns either a LangAS or a target address
space. As this is exposed to C I have kept the current behaviour and
added a comment stating that it is probably not correct.
Other than this the patch should not cause any functional changes.
Reviewers: yaxunl, pcc, bader
Reviewed By: yaxunl, bader
Subscribers: jlebar, jholewinski, nhaehnle, Anastasia, cfe-commits
Differential Revision: https://reviews.llvm.org/D38816
llvm-svn: 315871
2017-10-16 02:48:14 +08:00
|
|
|
LangAS destAddrSpace,
|
2017-08-16 05:42:52 +08:00
|
|
|
QualType destType) {
|
|
|
|
initializeNonAbstract(destAddrSpace);
|
|
|
|
return markIfFailed(tryEmitPrivateForMemory(E, destType));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value,
|
Convert clang::LangAS to a strongly typed enum
Summary:
Convert clang::LangAS to a strongly typed enum
Currently both clang AST address spaces and target specific address spaces
are represented as unsigned which can lead to subtle errors if the wrong
type is passed. It is especially confusing in the CodeGen files as it is
not possible to see what kind of address space should be passed to a
function without looking at the implementation.
I originally made this change for our LLVM fork for the CHERI architecture
where we make extensive use of address spaces to differentiate between
capabilities and pointers. When merging the upstream changes I usually
run into some test failures or runtime crashes because the wrong kind of
address space is passed to a function. By converting the LangAS enum to a
C++11 we can catch these errors at compile time. Additionally, it is now
obvious from the function signature which kind of address space it expects.
I found the following errors while writing this patch:
- ItaniumRecordLayoutBuilder::LayoutField was passing a clang AST address
space to TargetInfo::getPointer{Width,Align}()
- TypePrinter::printAttributedAfter() prints the numeric value of the
clang AST address space instead of the target address space.
However, this code is not used so I kept the current behaviour
- initializeForBlockHeader() in CGBlocks.cpp was passing
LangAS::opencl_generic to TargetInfo::getPointer{Width,Align}()
- CodeGenFunction::EmitBlockLiteral() was passing a AST address space to
TargetInfo::getPointerWidth()
- CGOpenMPRuntimeNVPTX::translateParameter() passed a target address space
to Qualifiers::addAddressSpace()
- CGOpenMPRuntimeNVPTX::getParameterAddress() was using
llvm::Type::getPointerTo() with a AST address space
- clang_getAddressSpace() returns either a LangAS or a target address
space. As this is exposed to C I have kept the current behaviour and
added a comment stating that it is probably not correct.
Other than this the patch should not cause any functional changes.
Reviewers: yaxunl, pcc, bader
Reviewed By: yaxunl, bader
Subscribers: jlebar, jholewinski, nhaehnle, Anastasia, cfe-commits
Differential Revision: https://reviews.llvm.org/D38816
llvm-svn: 315871
2017-10-16 02:48:14 +08:00
|
|
|
LangAS destAddrSpace,
|
2017-08-16 05:42:52 +08:00
|
|
|
QualType destType) {
|
|
|
|
initializeNonAbstract(destAddrSpace);
|
|
|
|
auto C = tryEmitPrivateForMemory(value, destType);
|
|
|
|
assert(C && "couldn't emit constant value non-abstractly?");
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalValue *ConstantEmitter::getCurrentAddrPrivate() {
|
|
|
|
assert(!Abstract && "cannot get current address for abstract constant");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Make an obviously ill-formed global that should blow up compilation
|
|
|
|
// if it survives.
|
|
|
|
auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true,
|
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
|
|
|
/*init*/ nullptr,
|
|
|
|
/*name*/ "",
|
|
|
|
/*before*/ nullptr,
|
|
|
|
llvm::GlobalVariable::NotThreadLocal,
|
|
|
|
CGM.getContext().getTargetAddressSpace(DestAddressSpace));
|
|
|
|
|
|
|
|
PlaceholderAddresses.push_back(std::make_pair(nullptr, global));
|
|
|
|
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstantEmitter::registerCurrentAddrPrivate(llvm::Constant *signal,
|
|
|
|
llvm::GlobalValue *placeholder) {
|
|
|
|
assert(!PlaceholderAddresses.empty());
|
|
|
|
assert(PlaceholderAddresses.back().first == nullptr);
|
|
|
|
assert(PlaceholderAddresses.back().second == placeholder);
|
|
|
|
PlaceholderAddresses.back().first = signal;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ReplacePlaceholders {
|
|
|
|
CodeGenModule &CGM;
|
|
|
|
|
|
|
|
/// The base address of the global.
|
|
|
|
llvm::Constant *Base;
|
|
|
|
llvm::Type *BaseValueTy = nullptr;
|
|
|
|
|
|
|
|
/// The placeholder addresses that were registered during emission.
|
|
|
|
llvm::DenseMap<llvm::Constant*, llvm::GlobalVariable*> PlaceholderAddresses;
|
|
|
|
|
|
|
|
/// The locations of the placeholder signals.
|
|
|
|
llvm::DenseMap<llvm::GlobalVariable*, llvm::Constant*> Locations;
|
|
|
|
|
|
|
|
/// The current index stack. We use a simple unsigned stack because
|
|
|
|
/// we assume that placeholders will be relatively sparse in the
|
|
|
|
/// initializer, but we cache the index values we find just in case.
|
|
|
|
llvm::SmallVector<unsigned, 8> Indices;
|
|
|
|
llvm::SmallVector<llvm::Constant*, 8> IndexValues;
|
|
|
|
|
|
|
|
ReplacePlaceholders(CodeGenModule &CGM, llvm::Constant *base,
|
|
|
|
ArrayRef<std::pair<llvm::Constant*,
|
|
|
|
llvm::GlobalVariable*>> addresses)
|
|
|
|
: CGM(CGM), Base(base),
|
|
|
|
PlaceholderAddresses(addresses.begin(), addresses.end()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void replaceInInitializer(llvm::Constant *init) {
|
|
|
|
// Remember the type of the top-most initializer.
|
|
|
|
BaseValueTy = init->getType();
|
|
|
|
|
|
|
|
// Initialize the stack.
|
|
|
|
Indices.push_back(0);
|
|
|
|
IndexValues.push_back(nullptr);
|
|
|
|
|
|
|
|
// Recurse into the initializer.
|
|
|
|
findLocations(init);
|
|
|
|
|
|
|
|
// Check invariants.
|
|
|
|
assert(IndexValues.size() == Indices.size() && "mismatch");
|
|
|
|
assert(Indices.size() == 1 && "didn't pop all indices");
|
|
|
|
|
|
|
|
// Do the replacement; this basically invalidates 'init'.
|
|
|
|
assert(Locations.size() == PlaceholderAddresses.size() &&
|
|
|
|
"missed a placeholder?");
|
|
|
|
|
|
|
|
// We're iterating over a hashtable, so this would be a source of
|
|
|
|
// non-determinism in compiler output *except* that we're just
|
|
|
|
// messing around with llvm::Constant structures, which never itself
|
|
|
|
// does anything that should be visible in compiler output.
|
|
|
|
for (auto &entry : Locations) {
|
|
|
|
assert(entry.first->getParent() == nullptr && "not a placeholder!");
|
|
|
|
entry.first->replaceAllUsesWith(entry.second);
|
|
|
|
entry.first->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void findLocations(llvm::Constant *init) {
|
|
|
|
// Recurse into aggregates.
|
|
|
|
if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) {
|
|
|
|
for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) {
|
|
|
|
Indices.push_back(i);
|
|
|
|
IndexValues.push_back(nullptr);
|
|
|
|
|
|
|
|
findLocations(agg->getOperand(i));
|
|
|
|
|
|
|
|
IndexValues.pop_back();
|
|
|
|
Indices.pop_back();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, check for registered constants.
|
|
|
|
while (true) {
|
|
|
|
auto it = PlaceholderAddresses.find(init);
|
|
|
|
if (it != PlaceholderAddresses.end()) {
|
|
|
|
setLocation(it->second);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look through bitcasts or other expressions.
|
|
|
|
if (auto expr = dyn_cast<llvm::ConstantExpr>(init)) {
|
|
|
|
init = expr->getOperand(0);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLocation(llvm::GlobalVariable *placeholder) {
|
|
|
|
assert(Locations.find(placeholder) == Locations.end() &&
|
|
|
|
"already found location for placeholder!");
|
|
|
|
|
|
|
|
// Lazily fill in IndexValues with the values from Indices.
|
|
|
|
// We do this in reverse because we should always have a strict
|
|
|
|
// prefix of indices from the start.
|
|
|
|
assert(Indices.size() == IndexValues.size());
|
|
|
|
for (size_t i = Indices.size() - 1; i != size_t(-1); --i) {
|
|
|
|
if (IndexValues[i]) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
for (size_t j = 0; j != i + 1; ++j) {
|
|
|
|
assert(IndexValues[j] &&
|
|
|
|
isa<llvm::ConstantInt>(IndexValues[j]) &&
|
|
|
|
cast<llvm::ConstantInt>(IndexValues[j])->getZExtValue()
|
|
|
|
== Indices[j]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexValues[i] = llvm::ConstantInt::get(CGM.Int32Ty, Indices[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Form a GEP and then bitcast to the placeholder type so that the
|
|
|
|
// replacement will succeed.
|
|
|
|
llvm::Constant *location =
|
|
|
|
llvm::ConstantExpr::getInBoundsGetElementPtr(BaseValueTy,
|
|
|
|
Base, IndexValues);
|
|
|
|
location = llvm::ConstantExpr::getBitCast(location,
|
|
|
|
placeholder->getType());
|
|
|
|
|
|
|
|
Locations.insert({placeholder, location});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstantEmitter::finalize(llvm::GlobalVariable *global) {
|
|
|
|
assert(InitializedNonAbstract &&
|
|
|
|
"finalizing emitter that was used for abstract emission?");
|
|
|
|
assert(!Finalized && "finalizing emitter multiple times");
|
|
|
|
assert(global->getInitializer());
|
|
|
|
|
|
|
|
// Note that we might also be Failed.
|
|
|
|
Finalized = true;
|
|
|
|
|
|
|
|
if (!PlaceholderAddresses.empty()) {
|
|
|
|
ReplacePlaceholders(CGM, global, PlaceholderAddresses)
|
|
|
|
.replaceInInitializer(global->getInitializer());
|
|
|
|
PlaceholderAddresses.clear(); // satisfy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantEmitter::~ConstantEmitter() {
|
|
|
|
assert((!InitializedNonAbstract || Finalized || Failed) &&
|
|
|
|
"not finalized after being initialized for non-abstract emission");
|
|
|
|
assert(PlaceholderAddresses.empty() && "unhandled placeholders");
|
|
|
|
}
|
|
|
|
|
|
|
|
static QualType getNonMemoryType(CodeGenModule &CGM, QualType type) {
|
|
|
|
if (auto AT = type->getAs<AtomicType>()) {
|
|
|
|
return CGM.getContext().getQualifiedType(AT->getValueType(),
|
|
|
|
type.getQualifiers());
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
|
2013-01-11 07:28:43 +08:00
|
|
|
// Make a quick check if variable can be default NULL initialized
|
|
|
|
// and avoid going through rest of code which may do, for c++11,
|
|
|
|
// initialization of memory to all NULLs.
|
2018-05-22 04:36:58 +08:00
|
|
|
if (!D.hasLocalStorage()) {
|
|
|
|
QualType Ty = CGM.getContext().getBaseElementType(D.getType());
|
|
|
|
if (Ty->isRecordType())
|
|
|
|
if (const CXXConstructExpr *E =
|
|
|
|
dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
|
|
|
|
const CXXConstructorDecl *CD = E->getConstructor();
|
|
|
|
if (CD->isTrivial() && CD->isDefaultConstructor())
|
|
|
|
return CGM.EmitNullConstant(D.getType());
|
|
|
|
}
|
2018-12-01 17:06:26 +08:00
|
|
|
InConstantContext = true;
|
2018-05-22 04:36:58 +08:00
|
|
|
}
|
2017-08-16 05:42:52 +08:00
|
|
|
|
|
|
|
QualType destType = D.getType();
|
|
|
|
|
|
|
|
// Try to emit the initializer. Note that this can allow some things that
|
|
|
|
// are not allowed by tryEmitPrivateForMemory alone.
|
|
|
|
if (auto value = D.evaluateValue()) {
|
|
|
|
return tryEmitPrivateForMemory(*value, destType);
|
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2012-02-14 06:16:19 +08:00
|
|
|
// FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
|
|
|
|
// reference is a constant expression, and the reference binds to a temporary,
|
|
|
|
// then constant initialization is performed. ConstExprEmitter will
|
|
|
|
// incorrectly emit a prvalue constant in this case, and the calling code
|
|
|
|
// interprets that as the (pointer) value of the reference, rather than the
|
|
|
|
// desired value of the referee.
|
2017-08-16 05:42:52 +08:00
|
|
|
if (destType->isReferenceType())
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2012-02-14 06:16:19 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
const Expr *E = D.getInit();
|
|
|
|
assert(E && "No initializer to emit");
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
auto nonMemoryDestType = getNonMemoryType(CGM, destType);
|
|
|
|
auto C =
|
|
|
|
ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), nonMemoryDestType);
|
|
|
|
return (C ? emitForMemory(C, destType) : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
ConstantEmitter::tryEmitAbstractForMemory(const Expr *E, QualType destType) {
|
|
|
|
auto nonMemoryDestType = getNonMemoryType(CGM, destType);
|
|
|
|
auto C = tryEmitAbstract(E, nonMemoryDestType);
|
2018-07-31 03:24:48 +08:00
|
|
|
return (C ? emitForMemory(C, destType) : nullptr);
|
2017-08-16 05:42:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
ConstantEmitter::tryEmitAbstractForMemory(const APValue &value,
|
|
|
|
QualType destType) {
|
|
|
|
auto nonMemoryDestType = getNonMemoryType(CGM, destType);
|
|
|
|
auto C = tryEmitAbstract(value, nonMemoryDestType);
|
2018-07-31 03:24:48 +08:00
|
|
|
return (C ? emitForMemory(C, destType) : nullptr);
|
2017-08-16 05:42:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const Expr *E,
|
|
|
|
QualType destType) {
|
|
|
|
auto nonMemoryDestType = getNonMemoryType(CGM, destType);
|
|
|
|
llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType);
|
|
|
|
return (C ? emitForMemory(C, destType) : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const APValue &value,
|
|
|
|
QualType destType) {
|
|
|
|
auto nonMemoryDestType = getNonMemoryType(CGM, destType);
|
|
|
|
auto C = tryEmitPrivate(value, nonMemoryDestType);
|
|
|
|
return (C ? emitForMemory(C, destType) : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM,
|
|
|
|
llvm::Constant *C,
|
|
|
|
QualType destType) {
|
|
|
|
// For an _Atomic-qualified constant, we may need to add tail padding.
|
|
|
|
if (auto AT = destType->getAs<AtomicType>()) {
|
|
|
|
QualType destValueType = AT->getValueType();
|
|
|
|
C = emitForMemory(CGM, C, destValueType);
|
|
|
|
|
|
|
|
uint64_t innerSize = CGM.getContext().getTypeSize(destValueType);
|
|
|
|
uint64_t outerSize = CGM.getContext().getTypeSize(destType);
|
|
|
|
if (innerSize == outerSize)
|
|
|
|
return C;
|
|
|
|
|
|
|
|
assert(innerSize < outerSize && "emitted over-large constant for atomic");
|
|
|
|
llvm::Constant *elts[] = {
|
|
|
|
C,
|
|
|
|
llvm::ConstantAggregateZero::get(
|
|
|
|
llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8))
|
|
|
|
};
|
|
|
|
return llvm::ConstantStruct::getAnon(elts);
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
2017-08-16 05:42:52 +08:00
|
|
|
|
|
|
|
// Zero-extend bool.
|
|
|
|
if (C->getType()->isIntegerTy(1)) {
|
|
|
|
llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType);
|
|
|
|
return llvm::ConstantExpr::getZExt(C, boolTy);
|
|
|
|
}
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
|
|
|
|
QualType destType) {
|
2008-12-01 10:42:14 +08:00
|
|
|
Expr::EvalResult Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 09:08:03 +08:00
|
|
|
bool Success = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
if (destType->isReferenceType())
|
|
|
|
Success = E->EvaluateAsLValue(Result, CGM.getContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2018-12-01 16:29:36 +08:00
|
|
|
Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *C;
|
2012-01-14 12:30:29 +08:00
|
|
|
if (Success && !Result.HasSideEffects)
|
2017-08-16 05:42:52 +08:00
|
|
|
C = tryEmitPrivate(Result.Val, destType);
|
2012-03-03 07:27:11 +08:00
|
|
|
else
|
2017-08-16 05:42:52 +08:00
|
|
|
C = ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), destType);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
return C;
|
|
|
|
}
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2016-12-15 16:09:08 +08:00
|
|
|
llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) {
|
|
|
|
return getTargetCodeGenInfo().getNullPointer(*this, T, QT);
|
|
|
|
}
|
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
namespace {
|
|
|
|
/// A struct which can be used to peephole certain kinds of finalization
|
|
|
|
/// that normally happen during l-value emission.
|
|
|
|
struct ConstantLValue {
|
|
|
|
llvm::Constant *Value;
|
|
|
|
bool HasOffsetApplied;
|
|
|
|
|
|
|
|
/*implicit*/ ConstantLValue(llvm::Constant *value,
|
|
|
|
bool hasOffsetApplied = false)
|
|
|
|
: Value(value), HasOffsetApplied(false) {}
|
|
|
|
|
|
|
|
/*implicit*/ ConstantLValue(ConstantAddress address)
|
|
|
|
: ConstantLValue(address.getPointer()) {}
|
|
|
|
};
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
/// A helper class for emitting constant l-values.
|
|
|
|
class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
|
|
|
|
ConstantLValue> {
|
|
|
|
CodeGenModule &CGM;
|
|
|
|
ConstantEmitter &Emitter;
|
|
|
|
const APValue &Value;
|
|
|
|
QualType DestType;
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
// Befriend StmtVisitorBase so that we don't have to expose Visit*.
|
|
|
|
friend StmtVisitorBase;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
public:
|
|
|
|
ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value,
|
|
|
|
QualType destType)
|
|
|
|
: CGM(emitter.CGM), Emitter(emitter), Value(value), DestType(destType) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
llvm::Constant *tryEmit();
|
2011-03-27 17:32:40 +08:00
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
private:
|
|
|
|
llvm::Constant *tryEmitAbsolute(llvm::Type *destTy);
|
|
|
|
ConstantLValue tryEmitBase(const APValue::LValueBase &base);
|
|
|
|
|
|
|
|
ConstantLValue VisitStmt(const Stmt *S) { return nullptr; }
|
2018-11-09 08:41:36 +08:00
|
|
|
ConstantLValue VisitConstantExpr(const ConstantExpr *E);
|
2017-08-17 13:03:55 +08:00
|
|
|
ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
|
|
|
|
ConstantLValue VisitStringLiteral(const StringLiteral *E);
|
2019-03-08 12:45:37 +08:00
|
|
|
ConstantLValue VisitObjCBoxedExpr(const ObjCBoxedExpr *E);
|
2017-08-17 13:03:55 +08:00
|
|
|
ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
|
|
|
|
ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *E);
|
|
|
|
ConstantLValue VisitPredefinedExpr(const PredefinedExpr *E);
|
|
|
|
ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *E);
|
|
|
|
ConstantLValue VisitCallExpr(const CallExpr *E);
|
|
|
|
ConstantLValue VisitBlockExpr(const BlockExpr *E);
|
|
|
|
ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *E);
|
|
|
|
ConstantLValue VisitCXXUuidofExpr(const CXXUuidofExpr *E);
|
|
|
|
ConstantLValue VisitMaterializeTemporaryExpr(
|
|
|
|
const MaterializeTemporaryExpr *E);
|
|
|
|
|
|
|
|
bool hasNonZeroOffset() const {
|
|
|
|
return !Value.getLValueOffset().isZero();
|
|
|
|
}
|
2011-03-27 17:32:40 +08:00
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
/// Return the value offset.
|
|
|
|
llvm::Constant *getOffset() {
|
|
|
|
return llvm::ConstantInt::get(CGM.Int64Ty,
|
|
|
|
Value.getLValueOffset().getQuantity());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply the value offset to the given constant.
|
|
|
|
llvm::Constant *applyOffset(llvm::Constant *C) {
|
|
|
|
if (!hasNonZeroOffset())
|
2012-01-14 12:30:29 +08:00
|
|
|
return C;
|
2017-08-17 13:03:55 +08:00
|
|
|
|
|
|
|
llvm::Type *origPtrTy = C->getType();
|
|
|
|
unsigned AS = origPtrTy->getPointerAddressSpace();
|
|
|
|
llvm::Type *charPtrTy = CGM.Int8Ty->getPointerTo(AS);
|
|
|
|
C = llvm::ConstantExpr::getBitCast(C, charPtrTy);
|
|
|
|
C = llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset());
|
|
|
|
C = llvm::ConstantExpr::getPointerCast(C, origPtrTy);
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantLValueEmitter::tryEmit() {
|
|
|
|
const APValue::LValueBase &base = Value.getLValueBase();
|
|
|
|
|
2019-02-09 05:36:04 +08:00
|
|
|
// The destination type should be a pointer or reference
|
2017-08-17 13:03:55 +08:00
|
|
|
// type, but it might also be a cast thereof.
|
|
|
|
//
|
|
|
|
// FIXME: the chain of casts required should be reflected in the APValue.
|
|
|
|
// We need this in order to correctly handle things like a ptrtoint of a
|
|
|
|
// non-zero null pointer and addrspace casts that aren't trivially
|
|
|
|
// represented in LLVM IR.
|
|
|
|
auto destTy = CGM.getTypes().ConvertTypeForMem(DestType);
|
|
|
|
assert(isa<llvm::IntegerType>(destTy) || isa<llvm::PointerType>(destTy));
|
|
|
|
|
|
|
|
// If there's no base at all, this is a null or absolute pointer,
|
|
|
|
// possibly cast back to an integer type.
|
|
|
|
if (!base) {
|
|
|
|
return tryEmitAbsolute(destTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, try to emit the base.
|
|
|
|
ConstantLValue result = tryEmitBase(base);
|
|
|
|
|
|
|
|
// If that failed, we're done.
|
|
|
|
llvm::Constant *value = result.Value;
|
|
|
|
if (!value) return nullptr;
|
|
|
|
|
|
|
|
// Apply the offset if necessary and not already done.
|
|
|
|
if (!result.HasOffsetApplied) {
|
|
|
|
value = applyOffset(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer. FIXME: performAddrSpaceCast
|
|
|
|
if (isa<llvm::PointerType>(destTy))
|
|
|
|
return llvm::ConstantExpr::getPointerCast(value, destTy);
|
|
|
|
|
|
|
|
return llvm::ConstantExpr::getPtrToInt(value, destTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to emit an absolute l-value, such as a null pointer or an integer
|
|
|
|
/// bitcast to pointer type.
|
|
|
|
llvm::Constant *
|
|
|
|
ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
|
|
|
|
// If we're producing a pointer, this is easy.
|
2019-03-19 14:14:14 +08:00
|
|
|
auto destPtrTy = cast<llvm::PointerType>(destTy);
|
|
|
|
if (Value.isNullPointer()) {
|
|
|
|
// FIXME: integer offsets from non-zero null pointers.
|
|
|
|
return CGM.getNullPointer(destPtrTy, DestType);
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
2017-08-17 13:03:55 +08:00
|
|
|
|
2019-03-19 14:14:14 +08:00
|
|
|
// Convert the integer to a pointer-sized integer before converting it
|
|
|
|
// to a pointer.
|
|
|
|
// FIXME: signedness depends on the original integer type.
|
|
|
|
auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy);
|
2019-05-11 19:01:46 +08:00
|
|
|
llvm::Constant *C;
|
2019-03-19 14:14:14 +08:00
|
|
|
C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy,
|
|
|
|
/*isSigned*/ false);
|
|
|
|
C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy);
|
2017-08-17 13:03:55 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
|
|
|
|
// Handle values.
|
|
|
|
if (const ValueDecl *D = base.dyn_cast<const ValueDecl*>()) {
|
|
|
|
if (D->hasAttr<WeakRefAttr>())
|
|
|
|
return CGM.GetWeakRefReference(D).getPointer();
|
|
|
|
|
|
|
|
if (auto FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
return CGM.GetAddrOfFunction(FD);
|
|
|
|
|
|
|
|
if (auto VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
// We can never refer to a variable with local storage.
|
|
|
|
if (!VD->hasLocalStorage()) {
|
|
|
|
if (VD->isFileVarDecl() || VD->hasExternalStorage())
|
|
|
|
return CGM.GetAddrOfGlobalVar(VD);
|
|
|
|
|
|
|
|
if (VD->isLocalVarDecl()) {
|
|
|
|
return CGM.getOrCreateStaticVarDecl(
|
2019-07-16 12:46:31 +08:00
|
|
|
*VD, CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false));
|
2017-08-17 13:03:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:06:46 +08:00
|
|
|
// Handle typeid(T).
|
|
|
|
if (TypeInfoLValue TI = base.dyn_cast<TypeInfoLValue>()) {
|
|
|
|
llvm::Type *StdTypeInfoPtrTy =
|
|
|
|
CGM.getTypes().ConvertType(base.getTypeInfoType())->getPointerTo();
|
|
|
|
llvm::Constant *TypeInfo =
|
|
|
|
CGM.GetAddrOfRTTIDescriptor(QualType(TI.getType(), 0));
|
|
|
|
if (TypeInfo->getType() != StdTypeInfoPtrTy)
|
|
|
|
TypeInfo = llvm::ConstantExpr::getBitCast(TypeInfo, StdTypeInfoPtrTy);
|
|
|
|
return TypeInfo;
|
|
|
|
}
|
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
// Otherwise, it must be an expression.
|
|
|
|
return Visit(base.get<const Expr*>());
|
|
|
|
}
|
|
|
|
|
2018-11-09 08:41:36 +08:00
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *E) {
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
|
|
|
|
return tryEmitGlobalCompoundLiteral(CGM, Emitter.CGF, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) {
|
|
|
|
return CGM.GetAddrOfConstantStringFromLiteral(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
|
|
|
|
return CGM.GetAddrOfConstantStringFromObjCEncode(E);
|
|
|
|
}
|
|
|
|
|
2019-03-08 12:45:37 +08:00
|
|
|
static ConstantLValue emitConstantObjCStringLiteral(const StringLiteral *S,
|
|
|
|
QualType T,
|
|
|
|
CodeGenModule &CGM) {
|
|
|
|
auto C = CGM.getObjCRuntime().GenerateConstantString(S);
|
|
|
|
return C.getElementBitCast(CGM.getTypes().ConvertTypeForMem(T));
|
|
|
|
}
|
|
|
|
|
2017-08-17 13:03:55 +08:00
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
|
2019-03-08 12:45:37 +08:00
|
|
|
return emitConstantObjCStringLiteral(E->getString(), E->getType(), CGM);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
|
|
|
|
assert(E->isExpressibleAsConstantInitializer() &&
|
|
|
|
"this boxed expression can't be emitted as a compile-time constant");
|
|
|
|
auto *SL = cast<StringLiteral>(E->getSubExpr()->IgnoreParenCasts());
|
|
|
|
return emitConstantObjCStringLiteral(SL, E->getType(), CGM);
|
2017-08-17 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) {
|
2019-01-22 08:11:17 +08:00
|
|
|
return CGM.GetAddrOfConstantStringFromLiteral(E->getFunctionName());
|
2017-08-17 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) {
|
|
|
|
assert(Emitter.CGF && "Invalid address of label expression outside function");
|
|
|
|
llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel());
|
|
|
|
Ptr = llvm::ConstantExpr::getBitCast(Ptr,
|
|
|
|
CGM.getTypes().ConvertType(E->getType()));
|
|
|
|
return Ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
|
|
|
|
unsigned builtin = E->getBuiltinCallee();
|
|
|
|
if (builtin != Builtin::BI__builtin___CFStringMakeConstantString &&
|
|
|
|
builtin != Builtin::BI__builtin___NSStringMakeConstantString)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts());
|
|
|
|
if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) {
|
|
|
|
return CGM.getObjCRuntime().GenerateConstantString(literal);
|
|
|
|
} else {
|
|
|
|
// FIXME: need to deal with UCN conversion issues.
|
|
|
|
return CGM.GetAddrOfConstantCFString(literal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *E) {
|
|
|
|
StringRef functionName;
|
|
|
|
if (auto CGF = Emitter.CGF)
|
|
|
|
functionName = CGF->CurFn->getName();
|
|
|
|
else
|
|
|
|
functionName = "global";
|
|
|
|
|
|
|
|
return CGM.GetAddrOfGlobalBlock(E, functionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
|
|
|
|
QualType T;
|
|
|
|
if (E->isTypeOperand())
|
|
|
|
T = E->getTypeOperand(CGM.getContext());
|
|
|
|
else
|
|
|
|
T = E->getExprOperand()->getType();
|
|
|
|
return CGM.GetAddrOfRTTIDescriptor(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
|
|
|
|
return CGM.GetAddrOfUuidDescriptor(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantLValue
|
|
|
|
ConstantLValueEmitter::VisitMaterializeTemporaryExpr(
|
|
|
|
const MaterializeTemporaryExpr *E) {
|
|
|
|
assert(E->getStorageDuration() == SD_Static);
|
|
|
|
SmallVector<const Expr *, 2> CommaLHSs;
|
|
|
|
SmallVector<SubobjectAdjustment, 2> Adjustments;
|
2019-11-17 15:09:25 +08:00
|
|
|
const Expr *Inner = E->GetTemporaryExpr()
|
|
|
|
->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
|
2017-08-17 13:03:55 +08:00
|
|
|
return CGM.GetAddrOfGlobalTemporary(E, Inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value,
|
|
|
|
QualType DestType) {
|
|
|
|
switch (Value.getKind()) {
|
2019-05-22 07:15:18 +08:00
|
|
|
case APValue::None:
|
|
|
|
case APValue::Indeterminate:
|
|
|
|
// Out-of-lifetime and indeterminate values can be modeled as 'undef'.
|
|
|
|
return llvm::UndefValue::get(CGM.getTypes().ConvertType(DestType));
|
2017-08-17 13:03:55 +08:00
|
|
|
case APValue::LValue:
|
|
|
|
return ConstantLValueEmitter(*this, Value, DestType).tryEmit();
|
2012-03-03 07:27:11 +08:00
|
|
|
case APValue::Int:
|
2017-08-16 05:42:52 +08:00
|
|
|
return llvm::ConstantInt::get(CGM.getLLVMContext(), Value.getInt());
|
2019-01-17 02:53:05 +08:00
|
|
|
case APValue::FixedPoint:
|
|
|
|
return llvm::ConstantInt::get(CGM.getLLVMContext(),
|
|
|
|
Value.getFixedPoint().getValue());
|
2012-01-14 12:30:29 +08:00
|
|
|
case APValue::ComplexInt: {
|
|
|
|
llvm::Constant *Complex[2];
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(),
|
2012-01-14 12:30:29 +08:00
|
|
|
Value.getComplexIntReal());
|
2017-08-16 05:42:52 +08:00
|
|
|
Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(),
|
2012-01-14 12:30:29 +08:00
|
|
|
Value.getComplexIntImag());
|
|
|
|
|
|
|
|
// FIXME: the target may want to specify that this is packed.
|
2017-05-10 03:31:30 +08:00
|
|
|
llvm::StructType *STy =
|
|
|
|
llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
|
2012-01-14 12:30:29 +08:00
|
|
|
return llvm::ConstantStruct::get(STy, Complex);
|
|
|
|
}
|
|
|
|
case APValue::Float: {
|
|
|
|
const llvm::APFloat &Init = Value.getFloat();
|
2016-12-14 19:57:17 +08:00
|
|
|
if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf() &&
|
2017-08-16 05:42:52 +08:00
|
|
|
!CGM.getContext().getLangOpts().NativeHalfType &&
|
[CodeGen][X86] Fix handling of __fp16 vectors.
This commit fixes a bug in IRGen where it generates completely broken
code for __fp16 vectors on X86. For example when the following code is
compiled:
half4 hv0, hv1, hv2; // these are vectors of __fp16.
void foo221() {
hv0 = hv1 + hv2;
}
clang generates the following IR, in which two i16 vectors are added:
@hv1 = common global <4 x i16> zeroinitializer, align 8
@hv2 = common global <4 x i16> zeroinitializer, align 8
@hv0 = common global <4 x i16> zeroinitializer, align 8
define void @foo221() {
%0 = load <4 x i16>, <4 x i16>* @hv1, align 8
%1 = load <4 x i16>, <4 x i16>* @hv2, align 8
%add = add <4 x i16> %0, %1
store <4 x i16> %add, <4 x i16>* @hv0, align 8
ret void
}
To fix the bug, this commit uses the code committed in r314056, which
modified clang to promote and truncate __fp16 vectors to and from float
vectors in the AST. It also fixes another IRGen bug where a short value
is assigned to an __fp16 variable without any integer-to-floating-point
conversion, as shown in the following example:
__fp16 a;
short b;
void foo1() {
a = b;
}
@b = common global i16 0, align 2
@a = common global i16 0, align 2
define void @foo1() #0 {
%0 = load i16, i16* @b, align 2
store i16 %0, i16* @a, align 2
ret void
}
rdar://problem/20625184
Differential Revision: https://reviews.llvm.org/D40112
llvm-svn: 320215
2017-12-09 08:02:37 +08:00
|
|
|
CGM.getContext().getTargetInfo().useFP16ConversionIntrinsics())
|
2017-08-16 05:42:52 +08:00
|
|
|
return llvm::ConstantInt::get(CGM.getLLVMContext(),
|
|
|
|
Init.bitcastToAPInt());
|
2012-01-14 12:30:29 +08:00
|
|
|
else
|
2017-08-16 05:42:52 +08:00
|
|
|
return llvm::ConstantFP::get(CGM.getLLVMContext(), Init);
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
case APValue::ComplexFloat: {
|
|
|
|
llvm::Constant *Complex[2];
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(),
|
2012-01-14 12:30:29 +08:00
|
|
|
Value.getComplexFloatReal());
|
2017-08-16 05:42:52 +08:00
|
|
|
Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(),
|
2012-01-14 12:30:29 +08:00
|
|
|
Value.getComplexFloatImag());
|
|
|
|
|
|
|
|
// FIXME: the target may want to specify that this is packed.
|
2017-05-10 03:31:30 +08:00
|
|
|
llvm::StructType *STy =
|
|
|
|
llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
|
2012-01-14 12:30:29 +08:00
|
|
|
return llvm::ConstantStruct::get(STy, Complex);
|
|
|
|
}
|
|
|
|
case APValue::Vector: {
|
|
|
|
unsigned NumElts = Value.getVectorLength();
|
2015-12-11 08:23:35 +08:00
|
|
|
SmallVector<llvm::Constant *, 4> Inits(NumElts);
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2015-12-11 08:23:35 +08:00
|
|
|
for (unsigned I = 0; I != NumElts; ++I) {
|
|
|
|
const APValue &Elt = Value.getVectorElt(I);
|
2012-01-14 12:30:29 +08:00
|
|
|
if (Elt.isInt())
|
2017-08-16 05:42:52 +08:00
|
|
|
Inits[I] = llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt());
|
2015-12-11 08:23:35 +08:00
|
|
|
else if (Elt.isFloat())
|
2017-08-16 05:42:52 +08:00
|
|
|
Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat());
|
2012-01-14 12:30:29 +08:00
|
|
|
else
|
2015-12-11 08:23:35 +08:00
|
|
|
llvm_unreachable("unsupported vector element type");
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
return llvm::ConstantVector::get(Inits);
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
case APValue::AddrLabelDiff: {
|
|
|
|
const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
|
|
|
|
const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType());
|
|
|
|
llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType());
|
|
|
|
if (!LHS || !RHS) return nullptr;
|
2012-01-14 12:30:29 +08:00
|
|
|
|
|
|
|
// Compute difference
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType);
|
|
|
|
LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy);
|
|
|
|
RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy);
|
2012-01-14 12:30:29 +08:00
|
|
|
llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
|
|
|
|
|
|
|
|
// LLVM is a bit sensitive about the exact format of the
|
|
|
|
// address-of-label difference; make sure to truncate after
|
|
|
|
// the subtraction.
|
|
|
|
return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
|
|
|
|
}
|
|
|
|
case APValue::Struct:
|
|
|
|
case APValue::Union:
|
2017-08-16 05:42:52 +08:00
|
|
|
return ConstStructBuilder::BuildStruct(*this, Value, DestType);
|
2012-01-14 12:30:29 +08:00
|
|
|
case APValue::Array: {
|
2018-05-24 07:41:38 +08:00
|
|
|
const ConstantArrayType *CAT =
|
|
|
|
CGM.getContext().getAsConstantArrayType(DestType);
|
2012-01-14 12:30:29 +08:00
|
|
|
unsigned NumElements = Value.getArraySize();
|
|
|
|
unsigned NumInitElts = Value.getArrayInitializedElts();
|
2008-06-01 23:31:44 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
// Emit array filler, if there is one.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Constant *Filler = nullptr;
|
2018-05-24 07:41:38 +08:00
|
|
|
if (Value.hasArrayFiller()) {
|
2017-08-16 05:42:52 +08:00
|
|
|
Filler = tryEmitAbstractForMemory(Value.getArrayFiller(),
|
|
|
|
CAT->getElementType());
|
2018-05-24 07:41:38 +08:00
|
|
|
if (!Filler)
|
|
|
|
return nullptr;
|
2018-05-23 16:24:01 +08:00
|
|
|
}
|
2018-05-23 08:09:29 +08:00
|
|
|
|
2018-05-24 07:41:38 +08:00
|
|
|
// Emit initializer elements.
|
2018-05-23 16:24:01 +08:00
|
|
|
SmallVector<llvm::Constant*, 16> Elts;
|
2018-05-24 07:41:38 +08:00
|
|
|
if (Filler && Filler->isNullValue())
|
|
|
|
Elts.reserve(NumInitElts + 1);
|
|
|
|
else
|
|
|
|
Elts.reserve(NumElements);
|
|
|
|
|
|
|
|
llvm::Type *CommonElementType = nullptr;
|
|
|
|
for (unsigned I = 0; I < NumInitElts; ++I) {
|
|
|
|
llvm::Constant *C = tryEmitPrivateForMemory(
|
|
|
|
Value.getArrayInitializedElt(I), CAT->getElementType());
|
2017-08-16 05:42:52 +08:00
|
|
|
if (!C) return nullptr;
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
if (I == 0)
|
|
|
|
CommonElementType = C->getType();
|
|
|
|
else if (C->getType() != CommonElementType)
|
2014-05-21 13:09:00 +08:00
|
|
|
CommonElementType = nullptr;
|
2012-01-14 12:30:29 +08:00
|
|
|
Elts.push_back(C);
|
|
|
|
}
|
|
|
|
|
2018-08-08 08:01:21 +08:00
|
|
|
// This means that the array type is probably "IncompleteType" or some
|
|
|
|
// type that is not ConstantArray.
|
|
|
|
if (CAT == nullptr && CommonElementType == nullptr && !NumInitElts) {
|
|
|
|
const ArrayType *AT = CGM.getContext().getAsArrayType(DestType);
|
|
|
|
CommonElementType = CGM.getTypes().ConvertType(AT->getElementType());
|
|
|
|
llvm::ArrayType *AType = llvm::ArrayType::get(CommonElementType,
|
|
|
|
NumElements);
|
|
|
|
return llvm::ConstantAggregateZero::get(AType);
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:08:30 +08:00
|
|
|
llvm::ArrayType *Desired =
|
|
|
|
cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType));
|
|
|
|
return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
|
2018-05-24 07:41:38 +08:00
|
|
|
Filler);
|
2008-06-01 23:31:44 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
case APValue::MemberPointer:
|
2017-08-16 05:42:52 +08:00
|
|
|
return CGM.getCXXABI().EmitMemberPointer(Value, DestType);
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown APValue kind");
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
|
2016-12-28 15:27:40 +08:00
|
|
|
llvm::GlobalVariable *CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted(
|
|
|
|
const CompoundLiteralExpr *E) {
|
|
|
|
return EmittedCompoundLiterals.lookup(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::setAddrOfConstantCompoundLiteral(
|
|
|
|
const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) {
|
|
|
|
bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second;
|
|
|
|
(void)Ok;
|
|
|
|
assert(Ok && "CLE has already been emitted!");
|
|
|
|
}
|
|
|
|
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
ConstantAddress
|
2011-11-23 06:48:32 +08:00
|
|
|
CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
|
|
|
|
assert(E->isFileScope() && "not a file-scope compound literal expr");
|
2017-08-16 05:42:52 +08:00
|
|
|
return tryEmitGlobalCompoundLiteral(*this, nullptr, E);
|
2011-11-23 06:48:32 +08:00
|
|
|
}
|
|
|
|
|
2011-02-03 16:15:49 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
|
|
|
|
// Member pointer constants always have a very particular form.
|
|
|
|
const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
|
|
|
|
const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
|
|
|
|
|
|
|
|
// A member function pointer.
|
|
|
|
if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
|
2015-06-23 15:31:01 +08:00
|
|
|
return getCXXABI().EmitMemberFunctionPointer(method);
|
2011-02-03 16:15:49 +08:00
|
|
|
|
|
|
|
// Otherwise, a member data pointer.
|
2012-01-14 12:30:29 +08:00
|
|
|
uint64_t fieldOffset = getContext().getFieldOffset(decl);
|
2011-02-03 16:15:49 +08:00
|
|
|
CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
|
|
|
|
return getCXXABI().EmitMemberDataPointer(type, chars);
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *baseType,
|
2011-02-15 14:40:56 +08:00
|
|
|
const CXXRecordDecl *base);
|
2010-11-23 02:42:14 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
|
2016-12-15 16:09:08 +08:00
|
|
|
const RecordDecl *record,
|
2011-02-15 14:40:56 +08:00
|
|
|
bool asCompleteObject) {
|
|
|
|
const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType *structure =
|
2011-02-15 14:40:56 +08:00
|
|
|
(asCompleteObject ? layout.getLLVMType()
|
|
|
|
: layout.getBaseSubobjectLLVMType());
|
|
|
|
|
|
|
|
unsigned numElements = structure->getNumElements();
|
|
|
|
std::vector<llvm::Constant *> elements(numElements);
|
|
|
|
|
2016-12-15 16:09:08 +08:00
|
|
|
auto CXXR = dyn_cast<CXXRecordDecl>(record);
|
2011-02-15 14:40:56 +08:00
|
|
|
// Fill in all the bases.
|
2016-12-15 16:09:08 +08:00
|
|
|
if (CXXR) {
|
|
|
|
for (const auto &I : CXXR->bases()) {
|
|
|
|
if (I.isVirtual()) {
|
|
|
|
// Ignore virtual bases; if we're laying out for a complete
|
|
|
|
// object, we'll lay these out later.
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-23 02:42:14 +08:00
|
|
|
|
2016-12-15 16:09:08 +08:00
|
|
|
const CXXRecordDecl *base =
|
|
|
|
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
|
2010-11-23 02:42:14 +08:00
|
|
|
|
2016-12-15 16:09:08 +08:00
|
|
|
// Ignore empty bases.
|
|
|
|
if (base->isEmpty() ||
|
|
|
|
CGM.getContext().getASTRecordLayout(base).getNonVirtualSize()
|
|
|
|
.isZero())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
|
|
|
|
llvm::Type *baseType = structure->getElementType(fieldIndex);
|
|
|
|
elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
|
|
|
|
}
|
2010-11-23 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
// Fill in all the fields.
|
2014-03-09 04:12:42 +08:00
|
|
|
for (const auto *Field : record->fields()) {
|
2011-12-07 09:30:11 +08:00
|
|
|
// Fill in non-bitfields. (Bitfields always use a zero pattern, which we
|
|
|
|
// will fill in later.)
|
2019-06-21 04:44:45 +08:00
|
|
|
if (!Field->isBitField() && !Field->isZeroSize(CGM.getContext())) {
|
2014-03-09 04:12:42 +08:00
|
|
|
unsigned fieldIndex = layout.getLLVMFieldNo(Field);
|
|
|
|
elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
|
2011-12-07 09:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For unions, stop after the first named field.
|
2015-05-30 17:12:07 +08:00
|
|
|
if (record->isUnion()) {
|
|
|
|
if (Field->getIdentifier())
|
|
|
|
break;
|
2018-07-28 10:16:13 +08:00
|
|
|
if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
|
2015-05-30 17:12:07 +08:00
|
|
|
if (FieldRD->findFirstNamedDataMember())
|
|
|
|
break;
|
|
|
|
}
|
2011-02-15 14:40:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the virtual bases, if we're working with the complete object.
|
2016-12-15 16:09:08 +08:00
|
|
|
if (CXXR && asCompleteObject) {
|
|
|
|
for (const auto &I : CXXR->vbases()) {
|
2018-07-31 03:24:48 +08:00
|
|
|
const CXXRecordDecl *base =
|
2014-03-14 00:15:17 +08:00
|
|
|
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
|
2011-02-15 14:40:56 +08:00
|
|
|
|
|
|
|
// Ignore empty bases.
|
|
|
|
if (base->isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned fieldIndex = layout.getVirtualBaseIndex(base);
|
|
|
|
|
|
|
|
// We might have already laid this field out.
|
|
|
|
if (elements[fieldIndex]) continue;
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *baseType = structure->getElementType(fieldIndex);
|
2011-02-15 14:40:56 +08:00
|
|
|
elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
|
|
|
|
}
|
2010-11-23 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through all other fields and zero them out.
|
2011-02-15 14:40:56 +08:00
|
|
|
for (unsigned i = 0; i != numElements; ++i) {
|
|
|
|
if (!elements[i])
|
|
|
|
elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
|
2010-11-23 02:42:14 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
return llvm::ConstantStruct::get(structure, elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the null constant for a base subobject.
|
|
|
|
static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *baseType,
|
2011-02-15 14:40:56 +08:00
|
|
|
const CXXRecordDecl *base) {
|
|
|
|
const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
|
|
|
|
|
|
|
|
// Just zero out bases that don't have any pointer to data members.
|
|
|
|
if (baseLayout.isZeroInitializableAsBase())
|
|
|
|
return llvm::Constant::getNullValue(baseType);
|
|
|
|
|
2014-10-17 09:00:43 +08:00
|
|
|
// Otherwise, we can just use its null constant.
|
|
|
|
return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
|
2010-11-23 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *ConstantEmitter::emitNullForMemory(CodeGenModule &CGM,
|
|
|
|
QualType T) {
|
|
|
|
return emitForMemory(CGM, CGM.EmitNullConstant(T), T);
|
|
|
|
}
|
|
|
|
|
2009-04-14 05:47:26 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
|
2016-12-15 16:09:08 +08:00
|
|
|
if (T->getAs<PointerType>())
|
|
|
|
return getNullPointer(
|
|
|
|
cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T);
|
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
if (getTypes().isZeroInitializable(T))
|
2009-08-25 01:16:23 +08:00
|
|
|
return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
|
2012-02-07 06:00:56 +08:00
|
|
|
llvm::ArrayType *ATy =
|
|
|
|
cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
QualType ElementTy = CAT->getElementType();
|
|
|
|
|
2017-08-16 05:42:52 +08:00
|
|
|
llvm::Constant *Element =
|
|
|
|
ConstantEmitter::emitNullForMemory(*this, ElementTy);
|
2010-02-02 13:17:25 +08:00
|
|
|
unsigned NumElements = CAT->getSize().getZExtValue();
|
2012-02-07 06:00:56 +08:00
|
|
|
SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
|
2010-02-02 13:17:25 +08:00
|
|
|
return llvm::ConstantArray::get(ATy, Array);
|
2009-08-10 02:26:27 +08:00
|
|
|
}
|
2009-08-23 09:25:01 +08:00
|
|
|
|
2016-12-15 16:09:08 +08:00
|
|
|
if (const RecordType *RT = T->getAs<RecordType>())
|
|
|
|
return ::EmitNullConstant(*this, RT->getDecl(), /*complete object*/ true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-04-24 09:25:08 +08:00
|
|
|
assert(T->isMemberDataPointerType() &&
|
2010-02-02 13:17:25 +08:00
|
|
|
"Should only see pointers to data members here!");
|
2014-10-17 09:00:43 +08:00
|
|
|
|
2011-02-03 16:15:49 +08:00
|
|
|
return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
|
2009-04-14 05:47:26 +08:00
|
|
|
}
|
2011-10-14 10:27:24 +08:00
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
|
|
|
|
return ::EmitNullConstant(*this, Record, false);
|
|
|
|
}
|