PatternInit.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. constexpr uint64_t LargeValue = 0xAAAAAAAAAAAAAAAAull;
  19. // For 32-bit platforms it's a bit trickier because, across systems, only the
  20. // zero page can reasonably be expected to be unmapped, and even then we need
  21. // a very low address. We use a smaller value, and that value sadly doesn't
  22. // have a repeated byte-pattern. We don't use it for integers.
  23. constexpr uint32_t SmallValue = 0x000000AA;
  24. // Floating-point values are initialized as NaNs because they propagate. Using
  25. // a repeated byte pattern means that it will be easier to initialize
  26. // all-floating-point aggregates and arrays with memset. Further, aggregates
  27. // which mix integral and a few floats might also initialize with memset
  28. // followed by a handful of stores for the floats. Using fairly unique NaNs
  29. // also means they'll be easier to distinguish in a crash.
  30. constexpr bool NegativeNaN = true;
  31. constexpr uint64_t NaNPayload = 0xFFFFFFFFFFFFFFFFull;
  32. if (Ty->isIntOrIntVectorTy()) {
  33. unsigned BitWidth = cast<llvm::IntegerType>(
  34. Ty->isVectorTy() ? Ty->getVectorElementType() : Ty)
  35. ->getBitWidth();
  36. if (BitWidth <= 64)
  37. return llvm::ConstantInt::get(Ty, LargeValue);
  38. return llvm::ConstantInt::get(
  39. Ty, llvm::APInt::getSplat(BitWidth, llvm::APInt(64, LargeValue)));
  40. }
  41. if (Ty->isPtrOrPtrVectorTy()) {
  42. auto *PtrTy = cast<llvm::PointerType>(
  43. Ty->isVectorTy() ? Ty->getVectorElementType() : Ty);
  44. unsigned PtrWidth = CGM.getContext().getTargetInfo().getPointerWidth(
  45. PtrTy->getAddressSpace());
  46. llvm::Type *IntTy = llvm::IntegerType::get(CGM.getLLVMContext(), PtrWidth);
  47. uint64_t IntValue;
  48. switch (PtrWidth) {
  49. default:
  50. llvm_unreachable("pattern initialization of unsupported pointer width");
  51. case 64:
  52. IntValue = LargeValue;
  53. break;
  54. case 32:
  55. IntValue = SmallValue;
  56. break;
  57. }
  58. auto *Int = llvm::ConstantInt::get(IntTy, IntValue);
  59. return llvm::ConstantExpr::getIntToPtr(Int, PtrTy);
  60. }
  61. if (Ty->isFPOrFPVectorTy()) {
  62. unsigned BitWidth = llvm::APFloat::semanticsSizeInBits(
  63. (Ty->isVectorTy() ? Ty->getVectorElementType() : Ty)
  64. ->getFltSemantics());
  65. llvm::APInt Payload(64, NaNPayload);
  66. if (BitWidth >= 64)
  67. Payload = llvm::APInt::getSplat(BitWidth, Payload);
  68. return llvm::ConstantFP::getQNaN(Ty, NegativeNaN, &Payload);
  69. }
  70. if (Ty->isArrayTy()) {
  71. // Note: this doesn't touch tail padding (at the end of an object, before
  72. // the next array object). It is instead handled by replaceUndef.
  73. auto *ArrTy = cast<llvm::ArrayType>(Ty);
  74. llvm::SmallVector<llvm::Constant *, 8> Element(
  75. ArrTy->getNumElements(),
  76. initializationPatternFor(CGM, ArrTy->getElementType()));
  77. return llvm::ConstantArray::get(ArrTy, Element);
  78. }
  79. // Note: this doesn't touch struct padding. It will initialize as much union
  80. // padding as is required for the largest type in the union. Padding is
  81. // instead handled by replaceUndef. Stores to structs with volatile members
  82. // don't have a volatile qualifier when initialized according to C++. This is
  83. // fine because stack-based volatiles don't really have volatile semantics
  84. // anyways, and the initialization shouldn't be observable.
  85. auto *StructTy = cast<llvm::StructType>(Ty);
  86. llvm::SmallVector<llvm::Constant *, 8> Struct(StructTy->getNumElements());
  87. for (unsigned El = 0; El != Struct.size(); ++El)
  88. Struct[El] = initializationPatternFor(CGM, StructTy->getElementType(El));
  89. return llvm::ConstantStruct::get(StructTy, Struct);
  90. }