PatternInit.cpp 4.2 KB

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