Globals.cpp revision da6cdfa9e885ab8e1eb53b984c0ee77d0d64cd5e
1//===-- 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"
20using 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.
30static bool removeDeadConstantUsers(Constant* C) {
31  if (isa<GlobalValue>(C)) return false; // Cannot remove this
32
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  C->destroyConstant();
42  return true;
43}
44
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.
53bool GlobalValue::removeDeadConstantUsers() {
54  while(!use_empty()) {
55    if (Constant* User = dyn_cast<Constant>(use_back())) {
56      if (!::removeDeadConstantUsers(User))
57        return false; // Constant wasn't dead
58    } else {
59      return false; // Non-constant usage;
60    }
61  }
62  return true;
63}
64
65/// This virtual destructor is responsible for deleting any transitively dead
66/// Constants that are using the GlobalValue.
67GlobalValue::~GlobalValue() {
68  // Its an error to attempt destruction with non-constant uses remaining.
69  bool okay_to_destruct = removeDeadConstantUsers();
70  assert(okay_to_destruct &&
71         "Can't destroy GlobalValue with non-constant uses.");
72}
73
74/// Override destroyConstant to make sure it doesn't get called on
75/// GlobalValue's because they shouldn't be treated like other constants.
76void GlobalValue::destroyConstant() {
77  assert(0 && "You can't GV->destroyConstant()!");
78  abort();
79}
80//===----------------------------------------------------------------------===//
81// GlobalVariable Implementation
82//===----------------------------------------------------------------------===//
83
84GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
85                               Constant *Initializer,
86                               const std::string &Name, Module *ParentModule)
87  : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
88    isConstantGlobal(constant) {
89  if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
90
91  LeakDetector::addGarbageObject(this);
92
93  if (ParentModule)
94    ParentModule->getGlobalList().push_back(this);
95}
96
97void GlobalVariable::setParent(Module *parent) {
98  if (getParent())
99    LeakDetector::addGarbageObject(this);
100  Parent = parent;
101  if (getParent())
102    LeakDetector::removeGarbageObject(this);
103}
104
105// Specialize setName to take care of symbol table majik
106void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
107  Module *P;
108  assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
109         "Invalid symtab argument!");
110  if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
111  Value::setName(name);
112  if (P && hasName()) P->getSymbolTable().insert(this);
113}
114
115void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
116                                                 bool DisableChecking )
117{
118  // If you call this, then you better know this GVar has a constant
119  // initializer worth replacing. Enforce that here.
120  assert(getNumOperands() == 1 &&
121         "Attempt to replace uses of Constants on a GVar with no initializer");
122
123  // And, since you know it has an initializer, the From value better be
124  // the initializer :)
125  assert(getOperand(0) == From &&
126         "Attempt to replace wrong constant initializer in GVar");
127
128  // And, you better have a constant for the replacement value
129  assert(isa<Constant>(To) &&
130         "Attempt to replace GVar initializer with non-constant");
131
132  // Okay, preconditions out of the way, replace the constant initializer.
133  this->setOperand(0,To);
134}
135
136// vim: sw=2 ai
137
138