17330f729Sjoerg //===--- PatternInit.cpp - Pattern Initialization -------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg
97330f729Sjoerg #include "PatternInit.h"
107330f729Sjoerg #include "CodeGenModule.h"
11*e038c9c4Sjoerg #include "clang/Basic/TargetInfo.h"
127330f729Sjoerg #include "llvm/IR/Constant.h"
137330f729Sjoerg #include "llvm/IR/Type.h"
147330f729Sjoerg
initializationPatternFor(CodeGenModule & CGM,llvm::Type * Ty)157330f729Sjoerg llvm::Constant *clang::CodeGen::initializationPatternFor(CodeGenModule &CGM,
167330f729Sjoerg llvm::Type *Ty) {
177330f729Sjoerg // The following value is a guaranteed unmappable pointer value and has a
187330f729Sjoerg // repeated byte-pattern which makes it easier to synthesize. We use it for
197330f729Sjoerg // pointers as well as integers so that aggregates are likely to be
207330f729Sjoerg // initialized with this repeated value.
217330f729Sjoerg // For 32-bit platforms it's a bit trickier because, across systems, only the
227330f729Sjoerg // zero page can reasonably be expected to be unmapped. We use max 0xFFFFFFFF
237330f729Sjoerg // assuming that memory access will overlap into zero page.
247330f729Sjoerg const uint64_t IntValue =
257330f729Sjoerg CGM.getContext().getTargetInfo().getMaxPointerWidth() < 64
267330f729Sjoerg ? 0xFFFFFFFFFFFFFFFFull
277330f729Sjoerg : 0xAAAAAAAAAAAAAAAAull;
287330f729Sjoerg // Floating-point values are initialized as NaNs because they propagate. Using
297330f729Sjoerg // a repeated byte pattern means that it will be easier to initialize
307330f729Sjoerg // all-floating-point aggregates and arrays with memset. Further, aggregates
317330f729Sjoerg // which mix integral and a few floats might also initialize with memset
327330f729Sjoerg // followed by a handful of stores for the floats. Using fairly unique NaNs
337330f729Sjoerg // also means they'll be easier to distinguish in a crash.
347330f729Sjoerg constexpr bool NegativeNaN = true;
357330f729Sjoerg constexpr uint64_t NaNPayload = 0xFFFFFFFFFFFFFFFFull;
367330f729Sjoerg if (Ty->isIntOrIntVectorTy()) {
37*e038c9c4Sjoerg unsigned BitWidth =
38*e038c9c4Sjoerg cast<llvm::IntegerType>(Ty->getScalarType())->getBitWidth();
397330f729Sjoerg if (BitWidth <= 64)
407330f729Sjoerg return llvm::ConstantInt::get(Ty, IntValue);
417330f729Sjoerg return llvm::ConstantInt::get(
427330f729Sjoerg Ty, llvm::APInt::getSplat(BitWidth, llvm::APInt(64, IntValue)));
437330f729Sjoerg }
447330f729Sjoerg if (Ty->isPtrOrPtrVectorTy()) {
45*e038c9c4Sjoerg auto *PtrTy = cast<llvm::PointerType>(Ty->getScalarType());
467330f729Sjoerg unsigned PtrWidth = CGM.getContext().getTargetInfo().getPointerWidth(
477330f729Sjoerg PtrTy->getAddressSpace());
487330f729Sjoerg if (PtrWidth > 64)
497330f729Sjoerg llvm_unreachable("pattern initialization of unsupported pointer width");
507330f729Sjoerg llvm::Type *IntTy = llvm::IntegerType::get(CGM.getLLVMContext(), PtrWidth);
517330f729Sjoerg auto *Int = llvm::ConstantInt::get(IntTy, IntValue);
527330f729Sjoerg return llvm::ConstantExpr::getIntToPtr(Int, PtrTy);
537330f729Sjoerg }
547330f729Sjoerg if (Ty->isFPOrFPVectorTy()) {
557330f729Sjoerg unsigned BitWidth = llvm::APFloat::semanticsSizeInBits(
56*e038c9c4Sjoerg Ty->getScalarType()->getFltSemantics());
577330f729Sjoerg llvm::APInt Payload(64, NaNPayload);
587330f729Sjoerg if (BitWidth >= 64)
597330f729Sjoerg Payload = llvm::APInt::getSplat(BitWidth, Payload);
607330f729Sjoerg return llvm::ConstantFP::getQNaN(Ty, NegativeNaN, &Payload);
617330f729Sjoerg }
627330f729Sjoerg if (Ty->isArrayTy()) {
637330f729Sjoerg // Note: this doesn't touch tail padding (at the end of an object, before
647330f729Sjoerg // the next array object). It is instead handled by replaceUndef.
657330f729Sjoerg auto *ArrTy = cast<llvm::ArrayType>(Ty);
667330f729Sjoerg llvm::SmallVector<llvm::Constant *, 8> Element(
677330f729Sjoerg ArrTy->getNumElements(),
687330f729Sjoerg initializationPatternFor(CGM, ArrTy->getElementType()));
697330f729Sjoerg return llvm::ConstantArray::get(ArrTy, Element);
707330f729Sjoerg }
717330f729Sjoerg
727330f729Sjoerg // Note: this doesn't touch struct padding. It will initialize as much union
737330f729Sjoerg // padding as is required for the largest type in the union. Padding is
747330f729Sjoerg // instead handled by replaceUndef. Stores to structs with volatile members
757330f729Sjoerg // don't have a volatile qualifier when initialized according to C++. This is
767330f729Sjoerg // fine because stack-based volatiles don't really have volatile semantics
777330f729Sjoerg // anyways, and the initialization shouldn't be observable.
787330f729Sjoerg auto *StructTy = cast<llvm::StructType>(Ty);
797330f729Sjoerg llvm::SmallVector<llvm::Constant *, 8> Struct(StructTy->getNumElements());
807330f729Sjoerg for (unsigned El = 0; El != Struct.size(); ++El)
817330f729Sjoerg Struct[El] = initializationPatternFor(CGM, StructTy->getElementType(El));
827330f729Sjoerg return llvm::ConstantStruct::get(StructTy, Struct);
837330f729Sjoerg }
84