LLVM API Documentation

User.cpp
Go to the documentation of this file.
00001 //===-- User.cpp - Implement the User class -------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/IR/User.h"
00011 #include "llvm/IR/Constant.h"
00012 #include "llvm/IR/GlobalValue.h"
00013 #include "llvm/IR/Operator.h"
00014 
00015 namespace llvm {
00016 
00017 //===----------------------------------------------------------------------===//
00018 //                                 User Class
00019 //===----------------------------------------------------------------------===//
00020 
00021 void User::anchor() {}
00022 
00023 // replaceUsesOfWith - Replaces all references to the "From" definition with
00024 // references to the "To" definition.
00025 //
00026 void User::replaceUsesOfWith(Value *From, Value *To) {
00027   if (From == To) return;   // Duh what?
00028 
00029   assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
00030          "Cannot call User::replaceUsesOfWith on a constant!");
00031 
00032   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
00033     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
00034       // The side effects of this setOperand call include linking to
00035       // "To", adding "this" to the uses list of To, and
00036       // most importantly, removing "this" from the use list of "From".
00037       setOperand(i, To); // Fix it now...
00038     }
00039 }
00040 
00041 //===----------------------------------------------------------------------===//
00042 //                         User allocHungoffUses Implementation
00043 //===----------------------------------------------------------------------===//
00044 
00045 Use *User::allocHungoffUses(unsigned N) const {
00046   // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
00047   // the User.
00048   size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
00049   Use *Begin = static_cast<Use*>(::operator new(size));
00050   Use *End = Begin + N;
00051   (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
00052   return Use::initTags(Begin, End);
00053 }
00054 
00055 //===----------------------------------------------------------------------===//
00056 //                         User operator new Implementations
00057 //===----------------------------------------------------------------------===//
00058 
00059 void *User::operator new(size_t s, unsigned Us) {
00060   void *Storage = ::operator new(s + sizeof(Use) * Us);
00061   Use *Start = static_cast<Use*>(Storage);
00062   Use *End = Start + Us;
00063   User *Obj = reinterpret_cast<User*>(End);
00064   Obj->OperandList = Start;
00065   Obj->NumOperands = Us;
00066   Use::initTags(Start, End);
00067   return Obj;
00068 }
00069 
00070 //===----------------------------------------------------------------------===//
00071 //                         User operator delete Implementation
00072 //===----------------------------------------------------------------------===//
00073 
00074 void User::operator delete(void *Usr) {
00075   User *Start = static_cast<User*>(Usr);
00076   Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
00077   // If there were hung-off uses, they will have been freed already and
00078   // NumOperands reset to 0, so here we just free the User itself.
00079   ::operator delete(Storage);
00080 }
00081 
00082 //===----------------------------------------------------------------------===//
00083 //                             Operator Class
00084 //===----------------------------------------------------------------------===//
00085 
00086 Operator::~Operator() {
00087   llvm_unreachable("should never destroy an Operator");
00088 }
00089 
00090 } // End llvm namespace