blob: 0478a2ffb61bb547fda73f8386b5d973283f41b8 [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"
Chris Lattner184b2982002-09-08 18:59:35 +000018#include "Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Chris Lattner2f7c9632001-06-06 20:29:01 +000022//===----------------------------------------------------------------------===//
23// Value Class
24//===----------------------------------------------------------------------===//
25
Chris Lattner25450e32001-12-13 00:41:27 +000026static inline const Type *checkType(const Type *Ty) {
27 assert(Ty && "Value defined with a null type: Error!");
28 return Ty;
29}
30
Chris Lattnerd0b0b452004-06-26 20:33:39 +000031Value::Value(const Type *ty, unsigned scid, const std::string &name)
32 : SubclassID(scid), Ty(checkType(ty)), Name(name) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000033}
34
35Value::~Value() {
36#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000037 // Check to make sure that there are no uses of this value that are still
38 // around when the value is destroyed. If there are, then we have a dangling
39 // reference and something is wrong. This code is here to print out what is
40 // still being referenced. The value in question should be printed as
41 // a <badref>
42 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000043 if (Uses.begin() != Uses.end()) {
Chris Lattner22e4ca82003-10-02 19:44:40 +000044 std::cerr << "While deleting: " << *Ty << "%" << Name << "\n";
Chris Lattner079edeb2003-10-16 16:53:07 +000045 for (use_const_iterator I = Uses.begin(), E = Uses.end(); I != E; ++I)
Chris Lattnera82ee2d2002-07-24 22:08:53 +000046 std::cerr << "Use still stuck around after Def is destroyed:"
47 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000048 }
49#endif
Chris Lattner8bd8bc82003-06-24 22:20:19 +000050 assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000051
52 // There should be no uses of this object anymore, remove it.
53 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000054}
55
Chris Lattner2c6abea2002-10-09 23:12:59 +000056
Chris Lattner9f158122003-08-29 05:09:37 +000057// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
58// except that it doesn't have all of the asserts. The asserts fail because we
59// are half-way done resolving types, which causes some types to exist as two
60// different Type*'s at the same time. This is a sledgehammer to work around
61// this problem.
62//
63void Value::uncheckedReplaceAllUsesWith(Value *New) {
64 while (!Uses.empty()) {
Chris Lattner079edeb2003-10-16 16:53:07 +000065 Use &U = Uses.back();
Chris Lattner9f158122003-08-29 05:09:37 +000066 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
67 // constant!
Chris Lattner079edeb2003-10-16 16:53:07 +000068 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattnere2255072003-11-05 19:09:40 +000069 C->replaceUsesOfWithOnConstant(this, New, true);
Chris Lattner9f158122003-08-29 05:09:37 +000070 } else {
Chris Lattner079edeb2003-10-16 16:53:07 +000071 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +000072 }
73 }
74}
75
Chris Lattner079edeb2003-10-16 16:53:07 +000076void Value::replaceAllUsesWith(Value *New) {
77 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
78 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
79 assert(New->getType() == getType() &&
80 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +000081
Chris Lattner079edeb2003-10-16 16:53:07 +000082 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +000083}
84
Chris Lattner2f7c9632001-06-06 20:29:01 +000085//===----------------------------------------------------------------------===//
86// User Class
87//===----------------------------------------------------------------------===//
88
Chris Lattner7f74a562002-01-20 22:54:45 +000089User::User(const Type *Ty, ValueTy vty, const std::string &name)
Chris Lattner2f7c9632001-06-06 20:29:01 +000090 : Value(Ty, vty, name) {
91}
92
93// replaceUsesOfWith - Replaces all references to the "From" definition with
94// references to the "To" definition.
95//
96void User::replaceUsesOfWith(Value *From, Value *To) {
97 if (From == To) return; // Duh what?
98
Chris Lattner2c6abea2002-10-09 23:12:59 +000099 assert(!isa<Constant>(this) &&
100 "Cannot call User::replaceUsesofWith on a constant!");
101
Chris Lattnera073acb2001-07-07 08:36:50 +0000102 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
103 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 // The side effects of this setOperand call include linking to
105 // "To", adding "this" to the uses list of To, and
106 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000107 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000109}