blob: e41c7fd1366f5b0ffccefe12664681f4cf5fa60d [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner30ddf7f2002-04-28 04:52:28 +000010// This file implements the Value and User classes.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/InstrTypes.h"
15#include "llvm/SymbolTable.h"
Chris Lattner67e5c292002-01-25 03:45:27 +000016#include "llvm/DerivedTypes.h"
Chris Lattner2c6abea2002-10-09 23:12:59 +000017#include "llvm/Constant.h"
Reid Spencerbbddbf32004-07-18 00:01:50 +000018#include "llvm/GlobalValue.h"
Chris Lattner184b2982002-09-08 18:59:35 +000019#include "Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include <algorithm>
Reid Spencer8baf8e22004-07-04 11:55:37 +000021#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner2f7c9632001-06-06 20:29:01 +000024//===----------------------------------------------------------------------===//
25// Value Class
26//===----------------------------------------------------------------------===//
27
Chris Lattner25450e32001-12-13 00:41:27 +000028static inline const Type *checkType(const Type *Ty) {
29 assert(Ty && "Value defined with a null type: Error!");
30 return Ty;
31}
32
Chris Lattnerd0b0b452004-06-26 20:33:39 +000033Value::Value(const Type *ty, unsigned scid, const std::string &name)
34 : SubclassID(scid), Ty(checkType(ty)), Name(name) {
Chris Lattnerbea72472004-07-06 17:44:17 +000035 if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Chris Lattner9df9afd2004-07-07 18:07:46 +000036 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
37 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000038 "Cannot create non-first-class values except for constants!");
Alkis Evlogimenos2f130282004-07-25 06:07:15 +000039 if (ty == Type::VoidTy)
Alkis Evlogimenos74614b02004-07-25 06:16:52 +000040 assert(name.empty() && "Cannot have named void values!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000041}
42
43Value::~Value() {
44#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000045 // Check to make sure that there are no uses of this value that are still
46 // around when the value is destroyed. If there are, then we have a dangling
47 // reference and something is wrong. This code is here to print out what is
48 // still being referenced. The value in question should be printed as
49 // a <badref>
50 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000051 if (Uses.begin() != Uses.end()) {
Chris Lattner22e4ca82003-10-02 19:44:40 +000052 std::cerr << "While deleting: " << *Ty << "%" << Name << "\n";
Chris Lattner079edeb2003-10-16 16:53:07 +000053 for (use_const_iterator I = Uses.begin(), E = Uses.end(); I != E; ++I)
Chris Lattnera82ee2d2002-07-24 22:08:53 +000054 std::cerr << "Use still stuck around after Def is destroyed:"
55 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000056 }
57#endif
Chris Lattner8bd8bc82003-06-24 22:20:19 +000058 assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000059
60 // There should be no uses of this object anymore, remove it.
61 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000062}
63
Chris Lattner2c6abea2002-10-09 23:12:59 +000064
Chris Lattner9f158122003-08-29 05:09:37 +000065// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
66// except that it doesn't have all of the asserts. The asserts fail because we
67// are half-way done resolving types, which causes some types to exist as two
68// different Type*'s at the same time. This is a sledgehammer to work around
69// this problem.
70//
71void Value::uncheckedReplaceAllUsesWith(Value *New) {
72 while (!Uses.empty()) {
Chris Lattner079edeb2003-10-16 16:53:07 +000073 Use &U = Uses.back();
Chris Lattner9f158122003-08-29 05:09:37 +000074 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
75 // constant!
Chris Lattner079edeb2003-10-16 16:53:07 +000076 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Reid Spencerbbddbf32004-07-18 00:01:50 +000077 if (!isa<GlobalValue>(C))
78 C->replaceUsesOfWithOnConstant(this, New, true);
79 else
80 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +000081 } else {
Chris Lattner079edeb2003-10-16 16:53:07 +000082 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +000083 }
84 }
85}
86
Chris Lattner079edeb2003-10-16 16:53:07 +000087void Value::replaceAllUsesWith(Value *New) {
88 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
89 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
90 assert(New->getType() == getType() &&
91 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +000092
Chris Lattner079edeb2003-10-16 16:53:07 +000093 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +000094}
95
Chris Lattner2f7c9632001-06-06 20:29:01 +000096//===----------------------------------------------------------------------===//
97// User Class
98//===----------------------------------------------------------------------===//
99
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100// replaceUsesOfWith - Replaces all references to the "From" definition with
101// references to the "To" definition.
102//
103void User::replaceUsesOfWith(Value *From, Value *To) {
104 if (From == To) return; // Duh what?
105
Reid Spencerbbddbf32004-07-18 00:01:50 +0000106 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000107 "Cannot call User::replaceUsesofWith on a constant!");
108
Chris Lattnera073acb2001-07-07 08:36:50 +0000109 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
110 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111 // The side effects of this setOperand call include linking to
112 // "To", adding "this" to the uses list of To, and
113 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000114 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116}
Reid Spencerbbddbf32004-07-18 00:01:50 +0000117