xref: /freebsd-src/contrib/llvm-project/clang/lib/CodeGen/PatternInit.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===--- PatternInit.cpp - Pattern Initialization -------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "PatternInit.h"
100b57cec5SDimitry Andric #include "CodeGenModule.h"
115ffd83dbSDimitry Andric #include "clang/Basic/TargetInfo.h"
120b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
130b57cec5SDimitry Andric #include "llvm/IR/Type.h"
140b57cec5SDimitry Andric 
initializationPatternFor(CodeGenModule & CGM,llvm::Type * Ty)150b57cec5SDimitry Andric llvm::Constant *clang::CodeGen::initializationPatternFor(CodeGenModule &CGM,
160b57cec5SDimitry Andric                                                          llvm::Type *Ty) {
170b57cec5SDimitry Andric   // The following value is a guaranteed unmappable pointer value and has a
180b57cec5SDimitry Andric   // repeated byte-pattern which makes it easier to synthesize. We use it for
190b57cec5SDimitry Andric   // pointers as well as integers so that aggregates are likely to be
200b57cec5SDimitry Andric   // initialized with this repeated value.
210b57cec5SDimitry Andric   // For 32-bit platforms it's a bit trickier because, across systems, only the
220b57cec5SDimitry Andric   // zero page can reasonably be expected to be unmapped. We use max 0xFFFFFFFF
230b57cec5SDimitry Andric   // assuming that memory access will overlap into zero page.
240b57cec5SDimitry Andric   const uint64_t IntValue =
250b57cec5SDimitry Andric       CGM.getContext().getTargetInfo().getMaxPointerWidth() < 64
260b57cec5SDimitry Andric           ? 0xFFFFFFFFFFFFFFFFull
270b57cec5SDimitry Andric           : 0xAAAAAAAAAAAAAAAAull;
280b57cec5SDimitry Andric   // Floating-point values are initialized as NaNs because they propagate. Using
290b57cec5SDimitry Andric   // a repeated byte pattern means that it will be easier to initialize
300b57cec5SDimitry Andric   // all-floating-point aggregates and arrays with memset. Further, aggregates
310b57cec5SDimitry Andric   // which mix integral and a few floats might also initialize with memset
320b57cec5SDimitry Andric   // followed by a handful of stores for the floats. Using fairly unique NaNs
330b57cec5SDimitry Andric   // also means they'll be easier to distinguish in a crash.
340b57cec5SDimitry Andric   constexpr bool NegativeNaN = true;
350b57cec5SDimitry Andric   constexpr uint64_t NaNPayload = 0xFFFFFFFFFFFFFFFFull;
360b57cec5SDimitry Andric   if (Ty->isIntOrIntVectorTy()) {
375ffd83dbSDimitry Andric     unsigned BitWidth =
385ffd83dbSDimitry Andric         cast<llvm::IntegerType>(Ty->getScalarType())->getBitWidth();
390b57cec5SDimitry Andric     if (BitWidth <= 64)
400b57cec5SDimitry Andric       return llvm::ConstantInt::get(Ty, IntValue);
410b57cec5SDimitry Andric     return llvm::ConstantInt::get(
420b57cec5SDimitry Andric         Ty, llvm::APInt::getSplat(BitWidth, llvm::APInt(64, IntValue)));
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric   if (Ty->isPtrOrPtrVectorTy()) {
455ffd83dbSDimitry Andric     auto *PtrTy = cast<llvm::PointerType>(Ty->getScalarType());
46*bdd1243dSDimitry Andric     unsigned PtrWidth =
47*bdd1243dSDimitry Andric         CGM.getDataLayout().getPointerSizeInBits(PtrTy->getAddressSpace());
480b57cec5SDimitry Andric     if (PtrWidth > 64)
490b57cec5SDimitry Andric       llvm_unreachable("pattern initialization of unsupported pointer width");
500b57cec5SDimitry Andric     llvm::Type *IntTy = llvm::IntegerType::get(CGM.getLLVMContext(), PtrWidth);
510b57cec5SDimitry Andric     auto *Int = llvm::ConstantInt::get(IntTy, IntValue);
520b57cec5SDimitry Andric     return llvm::ConstantExpr::getIntToPtr(Int, PtrTy);
530b57cec5SDimitry Andric   }
540b57cec5SDimitry Andric   if (Ty->isFPOrFPVectorTy()) {
550b57cec5SDimitry Andric     unsigned BitWidth = llvm::APFloat::semanticsSizeInBits(
565ffd83dbSDimitry Andric         Ty->getScalarType()->getFltSemantics());
570b57cec5SDimitry Andric     llvm::APInt Payload(64, NaNPayload);
580b57cec5SDimitry Andric     if (BitWidth >= 64)
590b57cec5SDimitry Andric       Payload = llvm::APInt::getSplat(BitWidth, Payload);
600b57cec5SDimitry Andric     return llvm::ConstantFP::getQNaN(Ty, NegativeNaN, &Payload);
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric   if (Ty->isArrayTy()) {
630b57cec5SDimitry Andric     // Note: this doesn't touch tail padding (at the end of an object, before
640b57cec5SDimitry Andric     // the next array object). It is instead handled by replaceUndef.
650b57cec5SDimitry Andric     auto *ArrTy = cast<llvm::ArrayType>(Ty);
660b57cec5SDimitry Andric     llvm::SmallVector<llvm::Constant *, 8> Element(
670b57cec5SDimitry Andric         ArrTy->getNumElements(),
680b57cec5SDimitry Andric         initializationPatternFor(CGM, ArrTy->getElementType()));
690b57cec5SDimitry Andric     return llvm::ConstantArray::get(ArrTy, Element);
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   // Note: this doesn't touch struct padding. It will initialize as much union
730b57cec5SDimitry Andric   // padding as is required for the largest type in the union. Padding is
740b57cec5SDimitry Andric   // instead handled by replaceUndef. Stores to structs with volatile members
750b57cec5SDimitry Andric   // don't have a volatile qualifier when initialized according to C++. This is
760b57cec5SDimitry Andric   // fine because stack-based volatiles don't really have volatile semantics
770b57cec5SDimitry Andric   // anyways, and the initialization shouldn't be observable.
780b57cec5SDimitry Andric   auto *StructTy = cast<llvm::StructType>(Ty);
790b57cec5SDimitry Andric   llvm::SmallVector<llvm::Constant *, 8> Struct(StructTy->getNumElements());
800b57cec5SDimitry Andric   for (unsigned El = 0; El != Struct.size(); ++El)
810b57cec5SDimitry Andric     Struct[El] = initializationPatternFor(CGM, StructTy->getElementType(El));
820b57cec5SDimitry Andric   return llvm::ConstantStruct::get(StructTy, Struct);
830b57cec5SDimitry Andric }
84