blob: d65935204771d2bfa7aaf087a8f484b859b31346 [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 {
Pete Cooper87b925b2015-06-10 22:38:30 +000016class BasicBlock;
Jay Foad59809c72011-01-16 08:10:57 +000017
18//===----------------------------------------------------------------------===//
19// User Class
20//===----------------------------------------------------------------------===//
21
David Blaikie3a15e142011-12-01 08:00:17 +000022void User::anchor() {}
23
Jay Foad59809c72011-01-16 08:10:57 +000024void User::replaceUsesOfWith(Value *From, Value *To) {
25 if (From == To) return; // Duh what?
26
27 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
28 "Cannot call User::replaceUsesOfWith on a constant!");
29
30 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
31 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
32 // The side effects of this setOperand call include linking to
33 // "To", adding "this" to the uses list of To, and
34 // most importantly, removing "this" from the use list of "From".
35 setOperand(i, To); // Fix it now...
36 }
37}
38
39//===----------------------------------------------------------------------===//
40// User allocHungoffUses Implementation
41//===----------------------------------------------------------------------===//
42
Pete Cooper3fc30402015-06-10 22:38:46 +000043void User::allocHungoffUses(unsigned N, bool IsPhi) {
Jay Foad61ea0e42011-06-23 09:09:15 +000044 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
45 // the User.
46 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
Pete Cooper87b925b2015-06-10 22:38:30 +000047 if (IsPhi)
48 size += N * sizeof(BasicBlock *);
Jay Foad61ea0e42011-06-23 09:09:15 +000049 Use *Begin = static_cast<Use*>(::operator new(size));
Jay Foad59809c72011-01-16 08:10:57 +000050 Use *End = Begin + N;
Jay Foad5c54d752011-06-20 14:12:33 +000051 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
Pete Cooper3fc30402015-06-10 22:38:46 +000052 OperandList = Use::initTags(Begin, End);
Pete Cooperc6c04392015-06-10 22:38:34 +000053 // Tag this operand list as being a hung off.
54 HasHungOffUses = true;
Jay Foad59809c72011-01-16 08:10:57 +000055}
56
Pete Cooper93f9ff52015-06-10 22:38:41 +000057void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
58 assert(HasHungOffUses && "realloc must have hung off uses");
59
60 unsigned OldNumUses = getNumOperands();
61
62 // We don't support shrinking the number of uses. We wouldn't have enough
63 // space to copy the old uses in to the new space.
64 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
65
66 Use *OldOps = OperandList;
67 allocHungoffUses(NewNumUses, IsPhi);
68 Use *NewOps = OperandList;
69
70 // Now copy from the old operands list to the new one.
71 std::copy(OldOps, OldOps + OldNumUses, NewOps);
72
73 // If this is a Phi, then we need to copy the BB pointers too.
74 if (IsPhi) {
75 auto *OldPtr =
76 reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
77 auto *NewPtr =
78 reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
79 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
80 }
81 Use::zap(OldOps, OldOps + OldNumUses, true);
82}
83
Jay Foad59809c72011-01-16 08:10:57 +000084//===----------------------------------------------------------------------===//
85// User operator new Implementations
86//===----------------------------------------------------------------------===//
87
88void *User::operator new(size_t s, unsigned Us) {
89 void *Storage = ::operator new(s + sizeof(Use) * Us);
90 Use *Start = static_cast<Use*>(Storage);
91 Use *End = Start + Us;
92 User *Obj = reinterpret_cast<User*>(End);
93 Obj->OperandList = Start;
Pete Cooperc6c04392015-06-10 22:38:34 +000094 Obj->HasHungOffUses = false;
Jay Foad59809c72011-01-16 08:10:57 +000095 Obj->NumOperands = Us;
96 Use::initTags(Start, End);
97 return Obj;
98}
99
100//===----------------------------------------------------------------------===//
101// User operator delete Implementation
102//===----------------------------------------------------------------------===//
103
104void User::operator delete(void *Usr) {
105 User *Start = static_cast<User*>(Usr);
106 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
Jay Foadb0c5e352011-01-26 21:56:10 +0000107 // If there were hung-off uses, they will have been freed already and
108 // NumOperands reset to 0, so here we just free the User itself.
109 ::operator delete(Storage);
Jay Foad59809c72011-01-16 08:10:57 +0000110}
111
Richard Smith1f6f4552012-10-24 00:30:41 +0000112//===----------------------------------------------------------------------===//
113// Operator Class
114//===----------------------------------------------------------------------===//
115
116Operator::~Operator() {
117 llvm_unreachable("should never destroy an Operator");
118}
119
Jay Foad59809c72011-01-16 08:10:57 +0000120} // End llvm namespace