1e5dd7070Spatrick //===--- PatternInit.cpp - Pattern Initialization -------------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick
9e5dd7070Spatrick #include "PatternInit.h"
10e5dd7070Spatrick #include "CodeGenModule.h"
11ec727ea7Spatrick #include "clang/Basic/TargetInfo.h"
12e5dd7070Spatrick #include "llvm/IR/Constant.h"
13e5dd7070Spatrick #include "llvm/IR/Type.h"
14e5dd7070Spatrick
initializationPatternFor(CodeGenModule & CGM,llvm::Type * Ty)15e5dd7070Spatrick llvm::Constant *clang::CodeGen::initializationPatternFor(CodeGenModule &CGM,
16e5dd7070Spatrick llvm::Type *Ty) {
17e5dd7070Spatrick // The following value is a guaranteed unmappable pointer value and has a
18e5dd7070Spatrick // repeated byte-pattern which makes it easier to synthesize. We use it for
19e5dd7070Spatrick // pointers as well as integers so that aggregates are likely to be
20e5dd7070Spatrick // initialized with this repeated value.
21e5dd7070Spatrick // For 32-bit platforms it's a bit trickier because, across systems, only the
22e5dd7070Spatrick // zero page can reasonably be expected to be unmapped. We use max 0xFFFFFFFF
23e5dd7070Spatrick // assuming that memory access will overlap into zero page.
24e5dd7070Spatrick const uint64_t IntValue =
25e5dd7070Spatrick CGM.getContext().getTargetInfo().getMaxPointerWidth() < 64
26e5dd7070Spatrick ? 0xFFFFFFFFFFFFFFFFull
27e5dd7070Spatrick : 0xAAAAAAAAAAAAAAAAull;
28e5dd7070Spatrick // Floating-point values are initialized as NaNs because they propagate. Using
29e5dd7070Spatrick // a repeated byte pattern means that it will be easier to initialize
30e5dd7070Spatrick // all-floating-point aggregates and arrays with memset. Further, aggregates
31e5dd7070Spatrick // which mix integral and a few floats might also initialize with memset
32e5dd7070Spatrick // followed by a handful of stores for the floats. Using fairly unique NaNs
33e5dd7070Spatrick // also means they'll be easier to distinguish in a crash.
34e5dd7070Spatrick constexpr bool NegativeNaN = true;
35e5dd7070Spatrick constexpr uint64_t NaNPayload = 0xFFFFFFFFFFFFFFFFull;
36e5dd7070Spatrick if (Ty->isIntOrIntVectorTy()) {
37ec727ea7Spatrick unsigned BitWidth =
38ec727ea7Spatrick cast<llvm::IntegerType>(Ty->getScalarType())->getBitWidth();
39e5dd7070Spatrick if (BitWidth <= 64)
40e5dd7070Spatrick return llvm::ConstantInt::get(Ty, IntValue);
41e5dd7070Spatrick return llvm::ConstantInt::get(
42e5dd7070Spatrick Ty, llvm::APInt::getSplat(BitWidth, llvm::APInt(64, IntValue)));
43e5dd7070Spatrick }
44e5dd7070Spatrick if (Ty->isPtrOrPtrVectorTy()) {
45ec727ea7Spatrick auto *PtrTy = cast<llvm::PointerType>(Ty->getScalarType());
46*12c85518Srobert unsigned PtrWidth =
47*12c85518Srobert CGM.getDataLayout().getPointerSizeInBits(PtrTy->getAddressSpace());
48e5dd7070Spatrick if (PtrWidth > 64)
49e5dd7070Spatrick llvm_unreachable("pattern initialization of unsupported pointer width");
50e5dd7070Spatrick llvm::Type *IntTy = llvm::IntegerType::get(CGM.getLLVMContext(), PtrWidth);
51e5dd7070Spatrick auto *Int = llvm::ConstantInt::get(IntTy, IntValue);
52e5dd7070Spatrick return llvm::ConstantExpr::getIntToPtr(Int, PtrTy);
53e5dd7070Spatrick }
54e5dd7070Spatrick if (Ty->isFPOrFPVectorTy()) {
55e5dd7070Spatrick unsigned BitWidth = llvm::APFloat::semanticsSizeInBits(
56ec727ea7Spatrick Ty->getScalarType()->getFltSemantics());
57e5dd7070Spatrick llvm::APInt Payload(64, NaNPayload);
58e5dd7070Spatrick if (BitWidth >= 64)
59e5dd7070Spatrick Payload = llvm::APInt::getSplat(BitWidth, Payload);
60e5dd7070Spatrick return llvm::ConstantFP::getQNaN(Ty, NegativeNaN, &Payload);
61e5dd7070Spatrick }
62e5dd7070Spatrick if (Ty->isArrayTy()) {
63e5dd7070Spatrick // Note: this doesn't touch tail padding (at the end of an object, before
64e5dd7070Spatrick // the next array object). It is instead handled by replaceUndef.
65e5dd7070Spatrick auto *ArrTy = cast<llvm::ArrayType>(Ty);
66e5dd7070Spatrick llvm::SmallVector<llvm::Constant *, 8> Element(
67e5dd7070Spatrick ArrTy->getNumElements(),
68e5dd7070Spatrick initializationPatternFor(CGM, ArrTy->getElementType()));
69e5dd7070Spatrick return llvm::ConstantArray::get(ArrTy, Element);
70e5dd7070Spatrick }
71e5dd7070Spatrick
72e5dd7070Spatrick // Note: this doesn't touch struct padding. It will initialize as much union
73e5dd7070Spatrick // padding as is required for the largest type in the union. Padding is
74e5dd7070Spatrick // instead handled by replaceUndef. Stores to structs with volatile members
75e5dd7070Spatrick // don't have a volatile qualifier when initialized according to C++. This is
76e5dd7070Spatrick // fine because stack-based volatiles don't really have volatile semantics
77e5dd7070Spatrick // anyways, and the initialization shouldn't be observable.
78e5dd7070Spatrick auto *StructTy = cast<llvm::StructType>(Ty);
79e5dd7070Spatrick llvm::SmallVector<llvm::Constant *, 8> Struct(StructTy->getNumElements());
80e5dd7070Spatrick for (unsigned El = 0; El != Struct.size(); ++El)
81e5dd7070Spatrick Struct[El] = initializationPatternFor(CGM, StructTy->getElementType(El));
82e5dd7070Spatrick return llvm::ConstantStruct::get(StructTy, Struct);
83e5dd7070Spatrick }
84