blob: ee83eacf2b2b23d0a9290667004087bf40d0248f [file] [log] [blame]
Jay Foad59809c72011-01-16 08:10:57 +00001//===-- User.cpp - Implement the User class -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth9fb823b2013-01-02 11:36:10 +000010#include "llvm/IR/User.h"
11#include "llvm/IR/Constant.h"
12#include "llvm/IR/GlobalValue.h"
13#include "llvm/IR/Operator.h"
Jay Foad59809c72011-01-16 08:10:57 +000014
15namespace llvm {
16
17//===----------------------------------------------------------------------===//
18// User Class
19//===----------------------------------------------------------------------===//
20
David Blaikie3a15e142011-12-01 08:00:17 +000021void User::anchor() {}
22
Jay Foad59809c72011-01-16 08:10:57 +000023void User::replaceUsesOfWith(Value *From, Value *To) {
24 if (From == To) return; // Duh what?
25
26 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
27 "Cannot call User::replaceUsesOfWith on a constant!");
28
29 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
30 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
31 // The side effects of this setOperand call include linking to
32 // "To", adding "this" to the uses list of To, and
33 // most importantly, removing "this" from the use list of "From".
34 setOperand(i, To); // Fix it now...
35 }
36}
37
38//===----------------------------------------------------------------------===//
39// User allocHungoffUses Implementation
40//===----------------------------------------------------------------------===//
41
42Use *User::allocHungoffUses(unsigned N) const {
Jay Foad61ea0e42011-06-23 09:09:15 +000043 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
44 // the User.
45 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
46 Use *Begin = static_cast<Use*>(::operator new(size));
Jay Foad59809c72011-01-16 08:10:57 +000047 Use *End = Begin + N;
Jay Foad5c54d752011-06-20 14:12:33 +000048 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
Jay Foad59809c72011-01-16 08:10:57 +000049 return Use::initTags(Begin, End);
50}
51
52//===----------------------------------------------------------------------===//
53// User operator new Implementations
54//===----------------------------------------------------------------------===//
55
56void *User::operator new(size_t s, unsigned Us) {
57 void *Storage = ::operator new(s + sizeof(Use) * Us);
58 Use *Start = static_cast<Use*>(Storage);
59 Use *End = Start + Us;
60 User *Obj = reinterpret_cast<User*>(End);
61 Obj->OperandList = Start;
62 Obj->NumOperands = Us;
63 Use::initTags(Start, End);
64 return Obj;
65}
66
67//===----------------------------------------------------------------------===//
68// User operator delete Implementation
69//===----------------------------------------------------------------------===//
70
71void User::operator delete(void *Usr) {
72 User *Start = static_cast<User*>(Usr);
73 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
Jay Foadb0c5e352011-01-26 21:56:10 +000074 // If there were hung-off uses, they will have been freed already and
75 // NumOperands reset to 0, so here we just free the User itself.
76 ::operator delete(Storage);
Jay Foad59809c72011-01-16 08:10:57 +000077}
78
Richard Smith1f6f4552012-10-24 00:30:41 +000079//===----------------------------------------------------------------------===//
80// Operator Class
81//===----------------------------------------------------------------------===//
82
83Operator::~Operator() {
84 llvm_unreachable("should never destroy an Operator");
85}
86
Jay Foad59809c72011-01-16 08:10:57 +000087} // End llvm namespace