122989816SDimitry Andric //===--- PatternInit.cpp - Pattern Initialization -------------------------===//
222989816SDimitry Andric //
322989816SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
422989816SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
522989816SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
622989816SDimitry Andric //
722989816SDimitry Andric //===----------------------------------------------------------------------===//
822989816SDimitry Andric
922989816SDimitry Andric #include "PatternInit.h"
1022989816SDimitry Andric #include "CodeGenModule.h"
11cfca06d7SDimitry Andric #include "clang/Basic/TargetInfo.h"
1222989816SDimitry Andric #include "llvm/IR/Constant.h"
1322989816SDimitry Andric #include "llvm/IR/Type.h"
1422989816SDimitry Andric
initializationPatternFor(CodeGenModule & CGM,llvm::Type * Ty)1522989816SDimitry Andric llvm::Constant *clang::CodeGen::initializationPatternFor(CodeGenModule &CGM,
1622989816SDimitry Andric llvm::Type *Ty) {
1722989816SDimitry Andric // The following value is a guaranteed unmappable pointer value and has a
1822989816SDimitry Andric // repeated byte-pattern which makes it easier to synthesize. We use it for
1922989816SDimitry Andric // pointers as well as integers so that aggregates are likely to be
2022989816SDimitry Andric // initialized with this repeated value.
2122989816SDimitry Andric // For 32-bit platforms it's a bit trickier because, across systems, only the
2222989816SDimitry Andric // zero page can reasonably be expected to be unmapped. We use max 0xFFFFFFFF
2322989816SDimitry Andric // assuming that memory access will overlap into zero page.
2422989816SDimitry Andric const uint64_t IntValue =
2522989816SDimitry Andric CGM.getContext().getTargetInfo().getMaxPointerWidth() < 64
2622989816SDimitry Andric ? 0xFFFFFFFFFFFFFFFFull
2722989816SDimitry Andric : 0xAAAAAAAAAAAAAAAAull;
2822989816SDimitry Andric // Floating-point values are initialized as NaNs because they propagate. Using
2922989816SDimitry Andric // a repeated byte pattern means that it will be easier to initialize
3022989816SDimitry Andric // all-floating-point aggregates and arrays with memset. Further, aggregates
3122989816SDimitry Andric // which mix integral and a few floats might also initialize with memset
3222989816SDimitry Andric // followed by a handful of stores for the floats. Using fairly unique NaNs
3322989816SDimitry Andric // also means they'll be easier to distinguish in a crash.
3422989816SDimitry Andric constexpr bool NegativeNaN = true;
3522989816SDimitry Andric constexpr uint64_t NaNPayload = 0xFFFFFFFFFFFFFFFFull;
3622989816SDimitry Andric if (Ty->isIntOrIntVectorTy()) {
37cfca06d7SDimitry Andric unsigned BitWidth =
38cfca06d7SDimitry Andric cast<llvm::IntegerType>(Ty->getScalarType())->getBitWidth();
3922989816SDimitry Andric if (BitWidth <= 64)
4022989816SDimitry Andric return llvm::ConstantInt::get(Ty, IntValue);
4122989816SDimitry Andric return llvm::ConstantInt::get(
4222989816SDimitry Andric Ty, llvm::APInt::getSplat(BitWidth, llvm::APInt(64, IntValue)));
4322989816SDimitry Andric }
4422989816SDimitry Andric if (Ty->isPtrOrPtrVectorTy()) {
45cfca06d7SDimitry Andric auto *PtrTy = cast<llvm::PointerType>(Ty->getScalarType());
46e3b55780SDimitry Andric unsigned PtrWidth =
47e3b55780SDimitry Andric CGM.getDataLayout().getPointerSizeInBits(PtrTy->getAddressSpace());
4822989816SDimitry Andric if (PtrWidth > 64)
4922989816SDimitry Andric llvm_unreachable("pattern initialization of unsupported pointer width");
5022989816SDimitry Andric llvm::Type *IntTy = llvm::IntegerType::get(CGM.getLLVMContext(), PtrWidth);
5122989816SDimitry Andric auto *Int = llvm::ConstantInt::get(IntTy, IntValue);
5222989816SDimitry Andric return llvm::ConstantExpr::getIntToPtr(Int, PtrTy);
5322989816SDimitry Andric }
5422989816SDimitry Andric if (Ty->isFPOrFPVectorTy()) {
5522989816SDimitry Andric unsigned BitWidth = llvm::APFloat::semanticsSizeInBits(
56cfca06d7SDimitry Andric Ty->getScalarType()->getFltSemantics());
5722989816SDimitry Andric llvm::APInt Payload(64, NaNPayload);
5822989816SDimitry Andric if (BitWidth >= 64)
5922989816SDimitry Andric Payload = llvm::APInt::getSplat(BitWidth, Payload);
6022989816SDimitry Andric return llvm::ConstantFP::getQNaN(Ty, NegativeNaN, &Payload);
6122989816SDimitry Andric }
6222989816SDimitry Andric if (Ty->isArrayTy()) {
6322989816SDimitry Andric // Note: this doesn't touch tail padding (at the end of an object, before
6422989816SDimitry Andric // the next array object). It is instead handled by replaceUndef.
6522989816SDimitry Andric auto *ArrTy = cast<llvm::ArrayType>(Ty);
6622989816SDimitry Andric llvm::SmallVector<llvm::Constant *, 8> Element(
6722989816SDimitry Andric ArrTy->getNumElements(),
6822989816SDimitry Andric initializationPatternFor(CGM, ArrTy->getElementType()));
6922989816SDimitry Andric return llvm::ConstantArray::get(ArrTy, Element);
7022989816SDimitry Andric }
7122989816SDimitry Andric
7222989816SDimitry Andric // Note: this doesn't touch struct padding. It will initialize as much union
7322989816SDimitry Andric // padding as is required for the largest type in the union. Padding is
7422989816SDimitry Andric // instead handled by replaceUndef. Stores to structs with volatile members
7522989816SDimitry Andric // don't have a volatile qualifier when initialized according to C++. This is
7622989816SDimitry Andric // fine because stack-based volatiles don't really have volatile semantics
7722989816SDimitry Andric // anyways, and the initialization shouldn't be observable.
7822989816SDimitry Andric auto *StructTy = cast<llvm::StructType>(Ty);
7922989816SDimitry Andric llvm::SmallVector<llvm::Constant *, 8> Struct(StructTy->getNumElements());
8022989816SDimitry Andric for (unsigned El = 0; El != Struct.size(); ++El)
8122989816SDimitry Andric Struct[El] = initializationPatternFor(CGM, StructTy->getElementType(El));
8222989816SDimitry Andric return llvm::ConstantStruct::get(StructTy, Struct);
8322989816SDimitry Andric }
84