Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/Address.h
Line
Count
Source (jump to first uncovered line)
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
  llvm::Type *ElementType;
27
  CharUnits Alignment;
28
29
protected:
30
10.7M
  Address(std::nullptr_t) : Pointer(nullptr), ElementType(nullptr) {}
31
32
public:
33
  Address(llvm::Value *pointer, llvm::Type *elementType, CharUnits alignment)
34
6.91M
      : Pointer(pointer), ElementType(elementType), Alignment(alignment) {
35
6.91M
    assert(pointer != nullptr && "Pointer cannot be null");
36
0
    assert(elementType != nullptr && "Element type cannot be null");
37
0
    assert(llvm::cast<llvm::PointerType>(pointer->getType())
38
6.91M
               ->isOpaqueOrPointeeTypeMatches(elementType) &&
39
6.91M
           "Incorrect pointer element type");
40
0
    assert(!alignment.isZero() && "Alignment cannot be zero");
41
6.91M
  }
42
43
  // Deprecated: Use constructor with explicit element type instead.
44
  Address(llvm::Value *Pointer, CharUnits Alignment)
45
      : Address(Pointer, Pointer->getType()->getPointerElementType(),
46
155k
                Alignment) {}
clang::CodeGen::Address::Address(llvm::Value*, clang::CharUnits)
Line
Count
Source
46
155k
                Alignment) {}
Unexecuted instantiation: clang::CodeGen::Address::Address(llvm::Value*, clang::CharUnits)
47
48
10.7M
  static Address invalid() { return Address(nullptr); }
49
38.0M
  bool isValid() const { return Pointer != nullptr; }
50
51
17.6M
  llvm::Value *getPointer() const {
52
17.6M
    assert(isValid());
53
0
    return Pointer;
54
17.6M
  }
55
56
  /// Return the type of the pointer value.
57
617k
  llvm::PointerType *getType() const {
58
617k
    return llvm::cast<llvm::PointerType>(getPointer()->getType());
59
617k
  }
60
61
  /// Return the type of the values stored in this address.
62
7.91M
  llvm::Type *getElementType() const {
63
7.91M
    assert(isValid());
64
0
    return ElementType;
65
7.91M
  }
66
67
  /// Return the address space that this address resides in.
68
548k
  unsigned getAddressSpace() const {
69
548k
    return getType()->getAddressSpace();
70
548k
  }
71
72
  /// Return the IR name of the pointer value.
73
11.9k
  llvm::StringRef getName() const {
74
11.9k
    return getPointer()->getName();
75
11.9k
  }
76
77
  /// Return the alignment of this pointer.
78
8.98M
  CharUnits getAlignment() const {
79
8.98M
    assert(isValid());
80
0
    return Alignment;
81
8.98M
  }
82
83
  /// Return address with different pointer, but same element type and
84
  /// alignment.
85
1.37k
  Address withPointer(llvm::Value *NewPointer) const {
86
1.37k
    return Address(NewPointer, ElementType, Alignment);
87
1.37k
  }
88
89
  /// Return address with different alignment, but same pointer and element
90
  /// type.
91
741
  Address withAlignment(CharUnits NewAlignment) const {
92
741
    return Address(Pointer, ElementType, NewAlignment);
93
741
  }
94
};
95
96
/// A specialization of Address that requires the address to be an
97
/// LLVM Constant.
98
class ConstantAddress : public Address {
99
1.99k
  ConstantAddress(std::nullptr_t) : Address(nullptr) {}
100
101
public:
102
  ConstantAddress(llvm::Constant *pointer, llvm::Type *elementType,
103
                  CharUnits alignment)
104
108k
      : Address(pointer, elementType, alignment) {}
105
106
1.99k
  static ConstantAddress invalid() {
107
1.99k
    return ConstantAddress(nullptr);
108
1.99k
  }
109
110
17.5k
  llvm::Constant *getPointer() const {
111
17.5k
    return llvm::cast<llvm::Constant>(Address::getPointer());
112
17.5k
  }
113
114
630
  ConstantAddress getElementBitCast(llvm::Type *ElemTy) const {
115
630
    llvm::Constant *BitCast = llvm::ConstantExpr::getBitCast(
116
630
        getPointer(), ElemTy->getPointerTo(getAddressSpace()));
117
630
    return ConstantAddress(BitCast, ElemTy, getAlignment());
118
630
  }
119
120
0
  static bool isaImpl(Address addr) {
121
0
    return llvm::isa<llvm::Constant>(addr.getPointer());
122
0
  }
123
0
  static ConstantAddress castImpl(Address addr) {
124
0
    return ConstantAddress(llvm::cast<llvm::Constant>(addr.getPointer()),
125
0
                           addr.getElementType(), addr.getAlignment());
126
0
  }
127
};
128
129
}
130
131
// Present a minimal LLVM-like casting interface.
132
template <class U> inline U cast(CodeGen::Address addr) {
133
  return U::castImpl(addr);
134
}
135
template <class U> inline bool isa(CodeGen::Address addr) {
136
  return U::isaImpl(addr);
137
}
138
139
}
140
141
#endif