PatternInit.cpp 4.1 KB

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