GlobalObject.h revision ebe69fe11e48d322045d5949c83283927a0d790b
1//===-- llvm/GlobalObject.h - Class to represent global objects -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This represents an independent object. That is, a function or a global
11// variable, but not an alias.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_GLOBALOBJECT_H
16#define LLVM_IR_GLOBALOBJECT_H
17
18#include "llvm/IR/Constant.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/GlobalValue.h"
21
22namespace llvm {
23class Comdat;
24class Module;
25
26class GlobalObject : public GlobalValue {
27  GlobalObject(const GlobalObject &) = delete;
28
29protected:
30  GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
31               LinkageTypes Linkage, const Twine &Name)
32      : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name), ObjComdat(nullptr) {
33    setGlobalValueSubClassData(0);
34  }
35
36  std::string Section;     // Section to emit this into, empty means default
37  Comdat *ObjComdat;
38  static const unsigned AlignmentBits = 5;
39  static const unsigned GlobalObjectSubClassDataBits =
40      GlobalValueSubClassDataBits - AlignmentBits;
41
42private:
43  static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
44
45public:
46  unsigned getAlignment() const {
47    unsigned Data = getGlobalValueSubClassData();
48    unsigned AlignmentData = Data & AlignmentMask;
49    return (1u << AlignmentData) >> 1;
50  }
51  void setAlignment(unsigned Align);
52
53  unsigned getGlobalObjectSubClassData() const;
54  void setGlobalObjectSubClassData(unsigned Val);
55
56  bool hasSection() const { return !StringRef(getSection()).empty(); }
57  const char *getSection() const { return Section.c_str(); }
58  void setSection(StringRef S);
59
60  bool hasComdat() const { return getComdat() != nullptr; }
61  const Comdat *getComdat() const { return ObjComdat; }
62  Comdat *getComdat() { return ObjComdat; }
63  void setComdat(Comdat *C) { ObjComdat = C; }
64
65  void copyAttributesFrom(const GlobalValue *Src) override;
66
67  // Methods for support type inquiry through isa, cast, and dyn_cast:
68  static inline bool classof(const Value *V) {
69    return V->getValueID() == Value::FunctionVal ||
70           V->getValueID() == Value::GlobalVariableVal;
71  }
72};
73
74} // End llvm namespace
75
76#endif
77