Address.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //===-- Address.h - An aligned address -------------------------*- C++ -*-===//
  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. // This class provides a simple wrapper for a pair of a pointer and an
  10. // alignment.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
  14. #define LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
  15. #include "llvm/IR/Constants.h"
  16. #include "clang/AST/CharUnits.h"
  17. namespace clang {
  18. namespace CodeGen {
  19. /// An aligned address.
  20. class Address {
  21. llvm::Value *Pointer;
  22. CharUnits Alignment;
  23. public:
  24. Address(llvm::Value *pointer, CharUnits alignment)
  25. : Pointer(pointer), Alignment(alignment) {
  26. assert((!alignment.isZero() || pointer == nullptr) &&
  27. "creating valid address with invalid alignment");
  28. }
  29. static Address invalid() { return Address(nullptr, CharUnits()); }
  30. bool isValid() const { return Pointer != nullptr; }
  31. llvm::Value *getPointer() const {
  32. assert(isValid());
  33. return Pointer;
  34. }
  35. /// Return the type of the pointer value.
  36. llvm::PointerType *getType() const {
  37. return llvm::cast<llvm::PointerType>(getPointer()->getType());
  38. }
  39. /// Return the type of the values stored in this address.
  40. ///
  41. /// When IR pointer types lose their element type, we should simply
  42. /// store it in Address instead for the convenience of writing code.
  43. llvm::Type *getElementType() const {
  44. return getType()->getElementType();
  45. }
  46. /// Return the address space that this address resides in.
  47. unsigned getAddressSpace() const {
  48. return getType()->getAddressSpace();
  49. }
  50. /// Return the IR name of the pointer value.
  51. llvm::StringRef getName() const {
  52. return getPointer()->getName();
  53. }
  54. /// Return the alignment of this pointer.
  55. CharUnits getAlignment() const {
  56. assert(isValid());
  57. return Alignment;
  58. }
  59. };
  60. /// A specialization of Address that requires the address to be an
  61. /// LLVM Constant.
  62. class ConstantAddress : public Address {
  63. public:
  64. ConstantAddress(llvm::Constant *pointer, CharUnits alignment)
  65. : Address(pointer, alignment) {}
  66. static ConstantAddress invalid() {
  67. return ConstantAddress(nullptr, CharUnits());
  68. }
  69. llvm::Constant *getPointer() const {
  70. return llvm::cast<llvm::Constant>(Address::getPointer());
  71. }
  72. ConstantAddress getBitCast(llvm::Type *ty) const {
  73. return ConstantAddress(llvm::ConstantExpr::getBitCast(getPointer(), ty),
  74. getAlignment());
  75. }
  76. ConstantAddress getElementBitCast(llvm::Type *ty) const {
  77. return getBitCast(ty->getPointerTo(getAddressSpace()));
  78. }
  79. static bool isaImpl(Address addr) {
  80. return llvm::isa<llvm::Constant>(addr.getPointer());
  81. }
  82. static ConstantAddress castImpl(Address addr) {
  83. return ConstantAddress(llvm::cast<llvm::Constant>(addr.getPointer()),
  84. addr.getAlignment());
  85. }
  86. };
  87. }
  88. // Present a minimal LLVM-like casting interface.
  89. template <class U> inline U cast(CodeGen::Address addr) {
  90. return U::castImpl(addr);
  91. }
  92. template <class U> inline bool isa(CodeGen::Address addr) {
  93. return U::isaImpl(addr);
  94. }
  95. }
  96. #endif