blob: 2369d1c8eaa11d4f4e903f1a589d32fe64bda36e [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"
Reid Spencer3d169b12004-07-18 00:06:26 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// GlobalValue Class
24//===----------------------------------------------------------------------===//
25
26/// This could be named "SafeToDestroyGlobalValue". It just makes sure that
27/// there are no non-constant uses of this GlobalValue. If there aren't then
28/// this and the transitive closure of the constants can be deleted. See the
29/// destructor for details.
Chris Lattner0145eb72004-07-18 08:12:57 +000030static bool removeDeadConstantUsers(Constant* C) {
31 if (isa<GlobalValue>(C)) return false; // Cannot remove this
32
33 while (!C->use_empty())
Reid Spencer3d169b12004-07-18 00:06:26 +000034 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 }
Chris Lattner0145eb72004-07-18 08:12:57 +000040
41 C->destroyConstant();
Reid Spencer3d169b12004-07-18 00:06:26 +000042 return true;
43}
Reid Spencer3d169b12004-07-18 00:06:26 +000044
45/// removeDeadConstantUsers - If there are any dead constant users dangling
46/// off of this global value, remove them. This method is useful for clients
47/// that want to check to see if a global is unused, but don't want to deal
48/// with potentially dead constants hanging off of the globals.
49///
50/// This function returns true if the global value is now dead. If all
51/// users of this global are not dead, this method may return false and
52/// leave some of them around.
Chris Lattner2730e6a2004-07-19 00:55:35 +000053void GlobalValue::removeDeadConstantUsers() {
Reid Spencer3d169b12004-07-18 00:06:26 +000054 while(!use_empty()) {
55 if (Constant* User = dyn_cast<Constant>(use_back())) {
56 if (!::removeDeadConstantUsers(User))
Chris Lattner2730e6a2004-07-19 00:55:35 +000057 return; // Constant wasn't dead
Reid Spencer3d169b12004-07-18 00:06:26 +000058 } else {
Chris Lattner2730e6a2004-07-19 00:55:35 +000059 return; // Non-constant usage;
Reid Spencer3d169b12004-07-18 00:06:26 +000060 }
61 }
Reid Spencer3d169b12004-07-18 00:06:26 +000062}
63
64/// Override destroyConstant to make sure it doesn't get called on
65/// GlobalValue's because they shouldn't be treated like other constants.
66void GlobalValue::destroyConstant() {
67 assert(0 && "You can't GV->destroyConstant()!");
68 abort();
69}
70//===----------------------------------------------------------------------===//
71// GlobalVariable Implementation
72//===----------------------------------------------------------------------===//
73
74GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
75 Constant *Initializer,
76 const std::string &Name, Module *ParentModule)
77 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
78 isConstantGlobal(constant) {
79 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
80
81 LeakDetector::addGarbageObject(this);
82
83 if (ParentModule)
84 ParentModule->getGlobalList().push_back(this);
85}
86
87void GlobalVariable::setParent(Module *parent) {
88 if (getParent())
89 LeakDetector::addGarbageObject(this);
90 Parent = parent;
91 if (getParent())
92 LeakDetector::removeGarbageObject(this);
93}
94
95// Specialize setName to take care of symbol table majik
96void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
97 Module *P;
98 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
99 "Invalid symtab argument!");
100 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
101 Value::setName(name);
102 if (P && hasName()) P->getSymbolTable().insert(this);
103}
104
105void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
106 bool DisableChecking )
107{
108 // If you call this, then you better know this GVar has a constant
109 // initializer worth replacing. Enforce that here.
110 assert(getNumOperands() == 1 &&
111 "Attempt to replace uses of Constants on a GVar with no initializer");
112
113 // And, since you know it has an initializer, the From value better be
114 // the initializer :)
115 assert(getOperand(0) == From &&
116 "Attempt to replace wrong constant initializer in GVar");
117
118 // And, you better have a constant for the replacement value
119 assert(isa<Constant>(To) &&
120 "Attempt to replace GVar initializer with non-constant");
121
122 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000123 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000124}
125
126// vim: sw=2 ai
127