clang  9.0.0svn
Address.h
Go to the documentation of this file.
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 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
15 #define LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
16 
17 #include "llvm/IR/Constants.h"
18 #include "clang/AST/CharUnits.h"
19 
20 namespace clang {
21 namespace CodeGen {
22 
23 /// An aligned address.
24 class Address {
25  llvm::Value *Pointer;
26  CharUnits Alignment;
27 public:
28  Address(llvm::Value *pointer, CharUnits alignment)
29  : Pointer(pointer), Alignment(alignment) {
30  assert((!alignment.isZero() || pointer == nullptr) &&
31  "creating valid address with invalid alignment");
32  }
33 
34  static Address invalid() { return Address(nullptr, CharUnits()); }
35  bool isValid() const { return Pointer != nullptr; }
36 
38  assert(isValid());
39  return Pointer;
40  }
41 
42  /// Return the type of the pointer value.
43  llvm::PointerType *getType() const {
44  return llvm::cast<llvm::PointerType>(getPointer()->getType());
45  }
46 
47  /// Return the type of the values stored in this address.
48  ///
49  /// When IR pointer types lose their element type, we should simply
50  /// store it in Address instead for the convenience of writing code.
52  return getType()->getElementType();
53  }
54 
55  /// Return the address space that this address resides in.
56  unsigned getAddressSpace() const {
57  return getType()->getAddressSpace();
58  }
59 
60  /// Return the IR name of the pointer value.
61  llvm::StringRef getName() const {
62  return getPointer()->getName();
63  }
64 
65  /// Return the alignment of this pointer.
67  assert(isValid());
68  return Alignment;
69  }
70 };
71 
72 /// A specialization of Address that requires the address to be an
73 /// LLVM Constant.
74 class ConstantAddress : public Address {
75 public:
76  ConstantAddress(llvm::Constant *pointer, CharUnits alignment)
77  : Address(pointer, alignment) {}
78 
80  return ConstantAddress(nullptr, CharUnits());
81  }
82 
83  llvm::Constant *getPointer() const {
84  return llvm::cast<llvm::Constant>(Address::getPointer());
85  }
86 
88  return ConstantAddress(llvm::ConstantExpr::getBitCast(getPointer(), ty),
89  getAlignment());
90  }
91 
93  return getBitCast(ty->getPointerTo(getAddressSpace()));
94  }
95 
96  static bool isaImpl(Address addr) {
97  return llvm::isa<llvm::Constant>(addr.getPointer());
98  }
100  return ConstantAddress(llvm::cast<llvm::Constant>(addr.getPointer()),
101  addr.getAlignment());
102  }
103 };
104 
105 }
106 
107 // Present a minimal LLVM-like casting interface.
108 template <class U> inline U cast(CodeGen::Address addr) {
109  return U::castImpl(addr);
110 }
111 template <class U> inline bool isa(CodeGen::Address addr) {
112  return U::isaImpl(addr);
113 }
114 
115 }
116 
117 #endif
ConstantAddress(llvm::Constant *pointer, CharUnits alignment)
Definition: Address.h:76
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:115
llvm::Value * getPointer() const
Definition: Address.h:37
unsigned getAddressSpace() const
Return the address space that this address resides in.
Definition: Address.h:56
static bool isaImpl(Address addr)
Definition: Address.h:96
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:66
bool isValid() const
Definition: Address.h:35
ConstantAddress getElementBitCast(llvm::Type *ty) const
Definition: Address.h:92
static ConstantAddress castImpl(Address addr)
Definition: Address.h:99
static Address invalid()
Definition: Address.h:34
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:43
bool isa(CodeGen::Address addr)
Definition: Address.h:111
Address(llvm::Value *pointer, CharUnits alignment)
Definition: Address.h:28
The l-value was considered opaque, so the alignment was determined from a type.
ConstantAddress getBitCast(llvm::Type *ty) const
Definition: Address.h:87
An aligned address.
Definition: Address.h:24
Dataflow Directional Tag Classes.
llvm::Constant * getPointer() const
Definition: Address.h:83
U cast(CodeGen::Address addr)
Definition: Address.h:108
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:51
llvm::StringRef getName() const
Return the IR name of the pointer value.
Definition: Address.h:61
A specialization of Address that requires the address to be an LLVM Constant.
Definition: Address.h:74
static ConstantAddress invalid()
Definition: Address.h:79