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