#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
+#include "llvm/IR/LeakDetector.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/LeakDetector.h"
using namespace llvm;
//===----------------------------------------------------------------------===//
getParent()->Dematerialize(this);
}
+const DataLayout *GlobalValue::getDataLayout() const {
+ return getParent()->getDataLayout();
+}
+
/// Override destroyConstant to make sure it doesn't get called on
/// GlobalValue's because they shouldn't be treated like other constants.
void GlobalValue::destroyConstant() {
/// copyAttributesFrom - copy all additional attributes (those not needed to
/// create a GlobalValue) from the GlobalValue Src to this one.
void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
- setAlignment(Src->getAlignment());
- setSection(Src->getSection());
setVisibility(Src->getVisibility());
setUnnamedAddr(Src->hasUnnamedAddr());
+ setDLLStorageClass(Src->getDLLStorageClass());
+}
+
+unsigned GlobalValue::getAlignment() const {
+ if (auto *GA = dyn_cast<GlobalAlias>(this))
+ return GA->getAliasedGlobal()->getAlignment();
+
+ return cast<GlobalObject>(this)->getAlignment();
}
-void GlobalValue::setAlignment(unsigned Align) {
+void GlobalObject::setAlignment(unsigned Align) {
assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
assert(Align <= MaximumAlignment &&
"Alignment is greater than MaximumAlignment!");
- Alignment = Log2_32(Align) + 1;
+ setGlobalValueSubClassData(Log2_32(Align) + 1);
assert(getAlignment() == Align && "Alignment representation error!");
}
+void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
+ const auto *GV = cast<GlobalObject>(Src);
+ GlobalValue::copyAttributesFrom(GV);
+ setAlignment(GV->getAlignment());
+ setSection(GV->getSection());
+}
+
+const std::string &GlobalValue::getSection() const {
+ if (auto *GA = dyn_cast<GlobalAlias>(this))
+ return GA->getAliasedGlobal()->getSection();
+ return cast<GlobalObject>(this)->getSection();
+}
+
+void GlobalObject::setSection(StringRef S) { Section = S; }
+
bool GlobalValue::isDeclaration() const {
// Globals are definitions if they have an initializer.
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
assert(isa<GlobalAlias>(this));
return false;
}
-
+
//===----------------------------------------------------------------------===//
// GlobalVariable Implementation
//===----------------------------------------------------------------------===//
GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
Constant *InitVal, const Twine &Name,
- ThreadLocalMode TLMode, unsigned AddressSpace)
- : GlobalValue(PointerType::get(Ty, AddressSpace),
- Value::GlobalVariableVal,
- OperandTraits<GlobalVariable>::op_begin(this),
- InitVal != 0, Link, Name),
- isConstantGlobal(constant), threadLocalMode(TLMode) {
+ ThreadLocalMode TLMode, unsigned AddressSpace,
+ bool isExternallyInitialized)
+ : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
+ OperandTraits<GlobalVariable>::op_begin(this),
+ InitVal != nullptr, Link, Name),
+ isConstantGlobal(constant), threadLocalMode(TLMode),
+ isExternallyInitializedConstant(isExternallyInitialized) {
if (InitVal) {
assert(InitVal->getType() == Ty &&
"Initializer should be the same type as the GlobalVariable!");
GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
LinkageTypes Link, Constant *InitVal,
- const Twine &Name,
- GlobalVariable *Before, ThreadLocalMode TLMode,
- unsigned AddressSpace)
- : GlobalValue(PointerType::get(Ty, AddressSpace),
- Value::GlobalVariableVal,
- OperandTraits<GlobalVariable>::op_begin(this),
- InitVal != 0, Link, Name),
- isConstantGlobal(constant), threadLocalMode(TLMode) {
+ const Twine &Name, GlobalVariable *Before,
+ ThreadLocalMode TLMode, unsigned AddressSpace,
+ bool isExternallyInitialized)
+ : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
+ OperandTraits<GlobalVariable>::op_begin(this),
+ InitVal != nullptr, Link, Name),
+ isConstantGlobal(constant), threadLocalMode(TLMode),
+ isExternallyInitializedConstant(isExternallyInitialized) {
if (InitVal) {
assert(InitVal->getType() == Ty &&
"Initializer should be the same type as the GlobalVariable!");
Op<0>() = InitVal;
}
-
+
LeakDetector::addGarbageObject(this);
-
+
if (Before)
Before->getParent()->getGlobalList().insert(Before, this);
else
}
void GlobalVariable::setInitializer(Constant *InitVal) {
- if (InitVal == 0) {
+ if (!InitVal) {
if (hasInitializer()) {
- Op<0>().set(0);
+ Op<0>().set(nullptr);
NumOperands = 0;
}
} else {
/// create a GlobalVariable) from the GlobalVariable Src to this one.
void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
- GlobalValue::copyAttributesFrom(Src);
+ GlobalObject::copyAttributesFrom(Src);
const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
- setThreadLocal(SrcVar->isThreadLocal());
+ setThreadLocalMode(SrcVar->getThreadLocalMode());
}
void GlobalAlias::setAliasee(Constant *Aliasee) {
assert((!Aliasee || Aliasee->getType() == getType()) &&
"Alias and aliasee types should match!");
-
+
setOperand(0, Aliasee);
}
-const GlobalValue *GlobalAlias::getAliasedGlobal() const {
- const Constant *C = getAliasee();
- if (C == 0) return 0;
-
- if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
+static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
+ Constant *C = GA->getAliasee();
+ assert(C && "Must alias something");
+
+ if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
return GV;
- const ConstantExpr *CE = cast<ConstantExpr>(C);
- assert((CE->getOpcode() == Instruction::BitCast ||
+ ConstantExpr *CE = cast<ConstantExpr>(C);
+ assert((CE->getOpcode() == Instruction::BitCast ||
+ CE->getOpcode() == Instruction::AddrSpaceCast ||
CE->getOpcode() == Instruction::GetElementPtr) &&
"Unsupported aliasee");
-
+
return cast<GlobalValue>(CE->getOperand(0));
}
-const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
- SmallPtrSet<const GlobalValue*, 3> Visited;
+GlobalObject *GlobalAlias::getAliasedGlobal() {
+ SmallPtrSet<GlobalValue*, 3> Visited;
- // Check if we need to stop early.
- if (stopOnWeak && mayBeOverridden())
- return this;
-
- const GlobalValue *GV = getAliasedGlobal();
- Visited.insert(GV);
-
- // Iterate over aliasing chain, stopping on weak alias if necessary.
- while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
- if (stopOnWeak && GA->mayBeOverridden())
- break;
-
- GV = GA->getAliasedGlobal();
+ GlobalAlias *GA = this;
+ for (;;) {
+ GlobalValue *GV = getAliaseeGV(GA);
if (!Visited.insert(GV))
- return 0;
- }
+ return nullptr;
- return GV;
+ // Iterate over aliasing chain.
+ GA = dyn_cast<GlobalAlias>(GV);
+ if (!GA)
+ return cast<GlobalObject>(GV);
+ }
}