LLVM  16.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"
20 #include "llvm/Support/Error.h"
22 #include <cstddef>
23 
24 using namespace llvm;
25 
27  unsigned TypeID) {
28  if (Idx == size()) {
29  push_back(V, TypeID);
30  return Error::success();
31  }
32 
33  if (Idx >= size())
34  resize(Idx + 1);
35 
36  auto &Old = ValuePtrs[Idx];
37  if (!Old.first) {
38  Old.first = V;
39  Old.second = TypeID;
40  return Error::success();
41  }
42 
43  assert(!isa<Constant>(&*Old.first) && "Shouldn't update constant");
44  // If there was a forward reference to this value, replace it.
45  Value *PrevVal = Old.first;
46  if (PrevVal->getType() != V->getType())
47  return createStringError(
48  std::errc::illegal_byte_sequence,
49  "Assigned value does not match type of forward declaration");
50  Old.first->replaceAllUsesWith(V);
51  PrevVal->deleteValue();
52  return Error::success();
53 }
54 
56  unsigned TyID,
57  BasicBlock *ConstExprInsertBB) {
58  // Bail out for a clearly invalid value.
59  if (Idx >= RefsUpperBound)
60  return nullptr;
61 
62  if (Idx >= size())
63  resize(Idx + 1);
64 
65  if (Value *V = ValuePtrs[Idx].first) {
66  // If the types don't match, it's invalid.
67  if (Ty && Ty != V->getType())
68  return nullptr;
69 
70  Expected<Value *> MaybeV = MaterializeValueFn(Idx, ConstExprInsertBB);
71  if (!MaybeV) {
72  // TODO: We might want to propagate the precise error message here.
73  consumeError(MaybeV.takeError());
74  return nullptr;
75  }
76  return MaybeV.get();
77  }
78 
79  // No type specified, must be invalid reference.
80  if (!Ty)
81  return nullptr;
82 
83  // Create and return a placeholder, which will later be RAUW'd.
84  Value *V = new Argument(Ty);
85  ValuePtrs[Idx] = {V, TyID};
86  return V;
87 }
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ValueList.h
ErrorHandling.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
Error.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
Instruction.h
GlobalValue.h
Constants.h
Type.h
TypeID
Type::TypeID TypeID
Definition: Mips16HardFloat.cpp:102
llvm::BitcodeReaderValueList::push_back
void push_back(Value *V, unsigned TypeID)
Definition: ValueList.h:52
llvm::BitcodeReaderValueList::assignValue
Error assignValue(unsigned Idx, Value *V, unsigned TypeID)
Definition: ValueList.cpp:26
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::BitcodeReaderValueList::resize
void resize(unsigned N)
Definition: ValueList.h:49
llvm::BitcodeReaderValueList::size
unsigned size() const
Definition: ValueList.h:48
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:567
Argument.h
Constant.h
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1239
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
Casting.h
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
SmallVector.h
User.h
llvm::Value::deleteValue
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:109
llvm::Type::TypeID
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
llvm::BitcodeReaderValueList::getValueFwdRef
Value * getValueFwdRef(unsigned Idx, Type *Ty, unsigned TyID, BasicBlock *ConstExprInsertBB)
Definition: ValueList.cpp:55
Value.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74