1351280Sdim//===--- PatternInit.cpp - Pattern Initialization -------------------------===// 2351280Sdim// 3351280Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4351280Sdim// See https://llvm.org/LICENSE.txt for license information. 5351280Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6351280Sdim// 7351280Sdim//===----------------------------------------------------------------------===// 8351280Sdim 9351280Sdim#include "PatternInit.h" 10351280Sdim#include "CodeGenModule.h" 11351280Sdim#include "llvm/IR/Constant.h" 12351280Sdim#include "llvm/IR/Type.h" 13351280Sdim 14351280Sdimllvm::Constant *clang::CodeGen::initializationPatternFor(CodeGenModule &CGM, 15351280Sdim llvm::Type *Ty) { 16351280Sdim // The following value is a guaranteed unmappable pointer value and has a 17351280Sdim // repeated byte-pattern which makes it easier to synthesize. We use it for 18351280Sdim // pointers as well as integers so that aggregates are likely to be 19351280Sdim // initialized with this repeated value. 20351280Sdim // For 32-bit platforms it's a bit trickier because, across systems, only the 21351280Sdim // zero page can reasonably be expected to be unmapped. We use max 0xFFFFFFFF 22351280Sdim // assuming that memory access will overlap into zero page. 23351280Sdim const uint64_t IntValue = 24351280Sdim CGM.getContext().getTargetInfo().getMaxPointerWidth() < 64 25351280Sdim ? 0xFFFFFFFFFFFFFFFFull 26351280Sdim : 0xAAAAAAAAAAAAAAAAull; 27351280Sdim // Floating-point values are initialized as NaNs because they propagate. Using 28351280Sdim // a repeated byte pattern means that it will be easier to initialize 29351280Sdim // all-floating-point aggregates and arrays with memset. Further, aggregates 30351280Sdim // which mix integral and a few floats might also initialize with memset 31351280Sdim // followed by a handful of stores for the floats. Using fairly unique NaNs 32351280Sdim // also means they'll be easier to distinguish in a crash. 33351280Sdim constexpr bool NegativeNaN = true; 34351280Sdim constexpr uint64_t NaNPayload = 0xFFFFFFFFFFFFFFFFull; 35351280Sdim if (Ty->isIntOrIntVectorTy()) { 36351280Sdim unsigned BitWidth = cast<llvm::IntegerType>( 37351280Sdim Ty->isVectorTy() ? Ty->getVectorElementType() : Ty) 38351280Sdim ->getBitWidth(); 39351280Sdim if (BitWidth <= 64) 40351280Sdim return llvm::ConstantInt::get(Ty, IntValue); 41351280Sdim return llvm::ConstantInt::get( 42351280Sdim Ty, llvm::APInt::getSplat(BitWidth, llvm::APInt(64, IntValue))); 43351280Sdim } 44351280Sdim if (Ty->isPtrOrPtrVectorTy()) { 45351280Sdim auto *PtrTy = cast<llvm::PointerType>( 46351280Sdim Ty->isVectorTy() ? Ty->getVectorElementType() : Ty); 47351280Sdim unsigned PtrWidth = CGM.getContext().getTargetInfo().getPointerWidth( 48351280Sdim PtrTy->getAddressSpace()); 49351280Sdim if (PtrWidth > 64) 50351280Sdim llvm_unreachable("pattern initialization of unsupported pointer width"); 51351280Sdim llvm::Type *IntTy = llvm::IntegerType::get(CGM.getLLVMContext(), PtrWidth); 52351280Sdim auto *Int = llvm::ConstantInt::get(IntTy, IntValue); 53351280Sdim return llvm::ConstantExpr::getIntToPtr(Int, PtrTy); 54351280Sdim } 55351280Sdim if (Ty->isFPOrFPVectorTy()) { 56351280Sdim unsigned BitWidth = llvm::APFloat::semanticsSizeInBits( 57351280Sdim (Ty->isVectorTy() ? Ty->getVectorElementType() : Ty) 58351280Sdim ->getFltSemantics()); 59351280Sdim llvm::APInt Payload(64, NaNPayload); 60351280Sdim if (BitWidth >= 64) 61351280Sdim Payload = llvm::APInt::getSplat(BitWidth, Payload); 62351280Sdim return llvm::ConstantFP::getQNaN(Ty, NegativeNaN, &Payload); 63351280Sdim } 64351280Sdim if (Ty->isArrayTy()) { 65351280Sdim // Note: this doesn't touch tail padding (at the end of an object, before 66351280Sdim // the next array object). It is instead handled by replaceUndef. 67351280Sdim auto *ArrTy = cast<llvm::ArrayType>(Ty); 68351280Sdim llvm::SmallVector<llvm::Constant *, 8> Element( 69351280Sdim ArrTy->getNumElements(), 70351280Sdim initializationPatternFor(CGM, ArrTy->getElementType())); 71351280Sdim return llvm::ConstantArray::get(ArrTy, Element); 72351280Sdim } 73351280Sdim 74351280Sdim // Note: this doesn't touch struct padding. It will initialize as much union 75351280Sdim // padding as is required for the largest type in the union. Padding is 76351280Sdim // instead handled by replaceUndef. Stores to structs with volatile members 77351280Sdim // don't have a volatile qualifier when initialized according to C++. This is 78351280Sdim // fine because stack-based volatiles don't really have volatile semantics 79351280Sdim // anyways, and the initialization shouldn't be observable. 80351280Sdim auto *StructTy = cast<llvm::StructType>(Ty); 81351280Sdim llvm::SmallVector<llvm::Constant *, 8> Struct(StructTy->getNumElements()); 82351280Sdim for (unsigned El = 0; El != Struct.size(); ++El) 83351280Sdim Struct[El] = initializationPatternFor(CGM, StructTy->getElementType(El)); 84351280Sdim return llvm::ConstantStruct::get(StructTy, Struct); 85351280Sdim} 86