blob: 0e8227bdebc29f2345808749d20ce152c7f045ee [file] [log] [blame]
Reid Spencer3d169b12004-07-18 00:06:26 +00001//===-- Globals.cpp - Implement the Global object classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the GlobalValue & GlobalVariable classes for the VMCore
11// library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/DerivedTypes.h"
16#include "llvm/GlobalVariable.h"
17#include "llvm/Module.h"
18#include "llvm/SymbolTable.h"
19#include "Support/LeakDetector.h"
20
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// GlobalValue Class
25//===----------------------------------------------------------------------===//
26
27/// This could be named "SafeToDestroyGlobalValue". It just makes sure that
28/// there are no non-constant uses of this GlobalValue. If there aren't then
29/// this and the transitive closure of the constants can be deleted. See the
30/// destructor for details.
31namespace {
32bool removeDeadConstantUsers(Constant* C) {
33 while (!C->use_empty()) {
34 if (Constant *User = dyn_cast<Constant>(C->use_back())) {
35 if (!removeDeadConstantUsers(User))
36 return false; // Constant wasn't dead
37 } else {
38 return false; // Non-constant usage;
39 }
40 }
41 if (!isa<GlobalValue>(C))
42 C->destroyConstant();
43 return true;
44}
45}
46
47/// removeDeadConstantUsers - If there are any dead constant users dangling
48/// off of this global value, remove them. This method is useful for clients
49/// that want to check to see if a global is unused, but don't want to deal
50/// with potentially dead constants hanging off of the globals.
51///
52/// This function returns true if the global value is now dead. If all
53/// users of this global are not dead, this method may return false and
54/// leave some of them around.
55bool GlobalValue::removeDeadConstantUsers() {
56 while(!use_empty()) {
57 if (Constant* User = dyn_cast<Constant>(use_back())) {
58 if (!::removeDeadConstantUsers(User))
59 return false; // Constant wasn't dead
60 } else {
61 return false; // Non-constant usage;
62 }
63 }
64 return true;
65}
66
67/// This virtual destructor is responsible for deleting any transitively dead
68/// Constants that are using the GlobalValue.
69GlobalValue::~GlobalValue() {
70 // Its an error to attempt destruction with non-constant uses remaining.
71 bool okay_to_destruct = removeDeadConstantUsers();
72 assert(okay_to_destruct &&
73 "Can't destroy GlobalValue with non-constant uses.");
74}
75
76/// Override destroyConstant to make sure it doesn't get called on
77/// GlobalValue's because they shouldn't be treated like other constants.
78void GlobalValue::destroyConstant() {
79 assert(0 && "You can't GV->destroyConstant()!");
80 abort();
81}
82//===----------------------------------------------------------------------===//
83// GlobalVariable Implementation
84//===----------------------------------------------------------------------===//
85
86GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
87 Constant *Initializer,
88 const std::string &Name, Module *ParentModule)
89 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
90 isConstantGlobal(constant) {
91 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
92
93 LeakDetector::addGarbageObject(this);
94
95 if (ParentModule)
96 ParentModule->getGlobalList().push_back(this);
97}
98
99void GlobalVariable::setParent(Module *parent) {
100 if (getParent())
101 LeakDetector::addGarbageObject(this);
102 Parent = parent;
103 if (getParent())
104 LeakDetector::removeGarbageObject(this);
105}
106
107// Specialize setName to take care of symbol table majik
108void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
109 Module *P;
110 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
111 "Invalid symtab argument!");
112 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
113 Value::setName(name);
114 if (P && hasName()) P->getSymbolTable().insert(this);
115}
116
117void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
118 bool DisableChecking )
119{
120 // If you call this, then you better know this GVar has a constant
121 // initializer worth replacing. Enforce that here.
122 assert(getNumOperands() == 1 &&
123 "Attempt to replace uses of Constants on a GVar with no initializer");
124
125 // And, since you know it has an initializer, the From value better be
126 // the initializer :)
127 assert(getOperand(0) == From &&
128 "Attempt to replace wrong constant initializer in GVar");
129
130 // And, you better have a constant for the replacement value
131 assert(isa<Constant>(To) &&
132 "Attempt to replace GVar initializer with non-constant");
133
134 // Okay, preconditions out of the way, replace the constant initializer.
135 this->setOperand(0,To);
136}
137
138// vim: sw=2 ai
139