LLVM  14.0.0git
ValueList.cpp
Go to the documentation of this file.
1 //===- ValueList.cpp - Internal BitcodeReader implementation --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ValueList.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/IR/Argument.h"
12 #include "llvm/IR/Constant.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/GlobalValue.h"
15 #include "llvm/IR/Instruction.h"
16 #include "llvm/IR/Type.h"
17 #include "llvm/IR/User.h"
18 #include "llvm/IR/Value.h"
19 #include "llvm/Support/Casting.h"
21 #include <algorithm>
22 #include <cstddef>
23 #include <limits>
24 
25 using namespace llvm;
26 
27 namespace llvm {
28 
29 namespace {
30 
31 /// A class for maintaining the slot number definition
32 /// as a placeholder for the actual definition for forward constants defs.
33 class ConstantPlaceHolder : public ConstantExpr {
34 public:
35  explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context)
36  : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
38  }
39 
40  ConstantPlaceHolder &operator=(const ConstantPlaceHolder &) = delete;
41 
42  // allocate space for exactly one operand
43  void *operator new(size_t s) { return User::operator new(s, 1); }
44 
45  /// Methods to support type inquiry through isa, cast, and dyn_cast.
46  static bool classof(const Value *V) {
47  return isa<ConstantExpr>(V) &&
48  cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
49  }
50 
51  /// Provide fast operand accessors
53 };
54 
55 } // end anonymous namespace
56 
57 // FIXME: can we inherit this from ConstantExpr?
58 template <>
59 struct OperandTraits<ConstantPlaceHolder>
60  : public FixedNumOperandTraits<ConstantPlaceHolder, 1> {};
61 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
62 
63 } // end namespace llvm
64 
66  if (Idx == size()) {
67  push_back(V);
68  return;
69  }
70 
71  if (Idx >= size())
72  resize(Idx + 1);
73 
74  WeakTrackingVH &OldV = ValuePtrs[Idx];
75  if (!OldV) {
76  OldV = V;
77  return;
78  }
79 
80  // Handle constants and non-constants (e.g. instrs) differently for
81  // efficiency.
82  if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
83  ResolveConstants.push_back(std::make_pair(PHC, Idx));
84  OldV = V;
85  } else {
86  // If there was a forward reference to this value, replace it.
87  Value *PrevVal = OldV;
88  OldV->replaceAllUsesWith(V);
89  PrevVal->deleteValue();
90  }
91 }
92 
94  // Bail out for a clearly invalid value.
95  if (Idx >= RefsUpperBound)
96  return nullptr;
97 
98  if (Idx >= size())
99  resize(Idx + 1);
100 
101  if (Value *V = ValuePtrs[Idx]) {
102  if (Ty != V->getType())
103  report_fatal_error("Type mismatch in constant table!");
104  return cast<Constant>(V);
105  }
106 
107  // Create and return a placeholder, which will later be RAUW'd.
108  Constant *C = new ConstantPlaceHolder(Ty, Context);
109  ValuePtrs[Idx] = C;
110  return C;
111 }
112 
114  // Bail out for a clearly invalid value.
115  if (Idx >= RefsUpperBound)
116  return nullptr;
117 
118  if (Idx >= size())
119  resize(Idx + 1);
120 
121  if (Value *V = ValuePtrs[Idx]) {
122  // If the types don't match, it's invalid.
123  if (Ty && Ty != V->getType())
124  return nullptr;
125  return V;
126  }
127 
128  // No type specified, must be invalid reference.
129  if (!Ty)
130  return nullptr;
131 
132  // Create and return a placeholder, which will later be RAUW'd.
133  Value *V = new Argument(Ty);
134  ValuePtrs[Idx] = V;
135  return V;
136 }
137 
138 /// Once all constants are read, this method bulk resolves any forward
139 /// references. The idea behind this is that we sometimes get constants (such
140 /// as large arrays) which reference *many* forward ref constants. Replacing
141 /// each of these causes a lot of thrashing when building/reuniquing the
142 /// constant. Instead of doing this, we look at all the uses and rewrite all
143 /// the place holders at once for any constant that uses a placeholder.
145  // Sort the values by-pointer so that they are efficient to look up with a
146  // binary search.
147  llvm::sort(ResolveConstants);
148 
150 
151  while (!ResolveConstants.empty()) {
152  Value *RealVal = operator[](ResolveConstants.back().second);
153  Constant *Placeholder = ResolveConstants.back().first;
154  ResolveConstants.pop_back();
155 
156  // Loop over all users of the placeholder, updating them to reference the
157  // new value. If they reference more than one placeholder, update them all
158  // at once.
159  while (!Placeholder->use_empty()) {
160  auto UI = Placeholder->user_begin();
161  User *U = *UI;
162 
163  // If the using object isn't uniqued, just update the operands. This
164  // handles instructions and initializers for global variables.
165  if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
166  UI.getUse().set(RealVal);
167  continue;
168  }
169 
170  // Otherwise, we have a constant that uses the placeholder. Replace that
171  // constant with a new constant that has *all* placeholder uses updated.
172  Constant *UserC = cast<Constant>(U);
173  for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); I != E;
174  ++I) {
175  Value *NewOp;
176  if (!isa<ConstantPlaceHolder>(*I)) {
177  // Not a placeholder reference.
178  NewOp = *I;
179  } else if (*I == Placeholder) {
180  // Common case is that it just references this one placeholder.
181  NewOp = RealVal;
182  } else {
183  // Otherwise, look up the placeholder in ResolveConstants.
184  ResolveConstantsTy::iterator It = llvm::lower_bound(
185  ResolveConstants,
186  std::pair<Constant *, unsigned>(cast<Constant>(*I), 0));
187  assert(It != ResolveConstants.end() && It->first == *I);
188  NewOp = operator[](It->second);
189  }
190 
191  NewOps.push_back(cast<Constant>(NewOp));
192  }
193 
194  // Make the new constant.
195  Constant *NewC;
196  if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
197  NewC = ConstantArray::get(UserCA->getType(), NewOps);
198  } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
199  NewC = ConstantStruct::get(UserCS->getType(), NewOps);
200  } else if (isa<ConstantVector>(UserC)) {
201  NewC = ConstantVector::get(NewOps);
202  } else {
203  assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
204  NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
205  }
206 
207  UserC->replaceAllUsesWith(NewC);
208  UserC->destroyConstant();
209  NewOps.clear();
210  }
211 
212  // Update all ValueHandles, they should be the only users at this point.
213  Placeholder->replaceAllUsesWith(RealVal);
214  delete cast<ConstantPlaceHolder>(Placeholder);
215  }
216 }
llvm::BitcodeReaderValueList::operator[]
Value * operator[](unsigned i) const
Definition: ValueList.h:68
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::WeakTrackingVH
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:204
ValueList.h
llvm::lower_bound
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1657
llvm::ConstantStruct::get
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1327
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
ErrorHandling.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
DEFINE_TRANSPARENT_OPERAND_ACCESSORS
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
Definition: OperandTraits.h:125
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:203
llvm::BitcodeReaderValueList::getConstantFwdRef
Constant * getConstantFwdRef(unsigned Idx, Type *Ty)
Definition: ValueList.cpp:93
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::BitcodeReaderValueList::push_back
void push_back(Value *V)
Definition: ValueList.h:61
Instruction.h
GlobalValue.h
llvm::OperandTraits
Compile-time customization of User operands.
Definition: User.h:42
Constants.h
llvm::BitcodeReaderValueList::getValueFwdRef
Value * getValueFwdRef(unsigned Idx, Type *Ty)
Definition: ValueList.cpp:113
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::User
Definition: User.h:44
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::ConstantArray
ConstantArray - Constant Array Declarations.
Definition: Constants.h:409
llvm::Instruction
Definition: Instruction.h:45
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1771
Type.h
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::BitcodeReaderValueList::assignValue
void assignValue(Value *V, unsigned Idx)
Definition: ValueList.cpp:65
s
multiplies can be turned into SHL s
Definition: README.txt:370
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
I
#define I(x, y, z)
Definition: MD5.cpp:59
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::BitcodeReaderValueList::resize
void resize(unsigned N)
Definition: ValueList.h:58
llvm::BitcodeReaderValueList::resolveConstantForwardRefs
void resolveConstantForwardRefs()
Once all constants are read, this method bulk resolves any forward references.
Definition: ValueList.cpp:144
llvm::BitcodeReaderValueList::size
unsigned size() const
Definition: ValueList.h:57
llvm::ConstantStruct
Definition: Constants.h:441
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:520
llvm::ConstantVector::get
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1369
Argument.h
Constant.h
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:936
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:321
Casting.h
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1488
llvm::ConstantArray::get
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1263
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:585
DECLARE_TRANSPARENT_OPERAND_ACCESSORS
#define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS)
Macro for generating in-class operand accessor declarations.
Definition: OperandTraits.h:110
SmallVector.h
User.h
llvm::User::op_begin
op_iterator op_begin()
Definition: User.h:234
llvm::Value::deleteValue
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:110
llvm::FixedNumOperandTraits
FixedNumOperandTraits - determine the allocation regime of the Use array when it is a prefix to the U...
Definition: OperandTraits.h:30
Value.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::Constant::destroyConstant
void destroyConstant()
Called if some element of this constant is no longer valid.
Definition: Constants.cpp:461
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
llvm::User::op_end
op_iterator op_end()
Definition: User.h:236