ValueList.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "ValueList.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/IR/Argument.h"
  11. #include "llvm/IR/Constant.h"
  12. #include "llvm/IR/Constants.h"
  13. #include "llvm/IR/GlobalValue.h"
  14. #include "llvm/IR/Instruction.h"
  15. #include "llvm/IR/Type.h"
  16. #include "llvm/IR/User.h"
  17. #include "llvm/IR/Value.h"
  18. #include "llvm/Support/Casting.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include <cstddef>
  22. using namespace llvm;
  23. Error BitcodeReaderValueList::assignValue(unsigned Idx, Value *V,
  24. unsigned TypeID) {
  25. if (Idx == size()) {
  26. push_back(V, TypeID);
  27. return Error::success();
  28. }
  29. if (Idx >= size())
  30. resize(Idx + 1);
  31. auto &Old = ValuePtrs[Idx];
  32. if (!Old.first) {
  33. Old.first = V;
  34. Old.second = TypeID;
  35. return Error::success();
  36. }
  37. assert(!isa<Constant>(&*Old.first) && "Shouldn't update constant");
  38. // If there was a forward reference to this value, replace it.
  39. Value *PrevVal = Old.first;
  40. if (PrevVal->getType() != V->getType())
  41. return createStringError(
  42. std::errc::illegal_byte_sequence,
  43. "Assigned value does not match type of forward declaration");
  44. Old.first->replaceAllUsesWith(V);
  45. PrevVal->deleteValue();
  46. return Error::success();
  47. }
  48. Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty,
  49. unsigned TyID,
  50. BasicBlock *ConstExprInsertBB) {
  51. // Bail out for a clearly invalid value.
  52. if (Idx >= RefsUpperBound)
  53. return nullptr;
  54. if (Idx >= size())
  55. resize(Idx + 1);
  56. if (Value *V = ValuePtrs[Idx].first) {
  57. // If the types don't match, it's invalid.
  58. if (Ty && Ty != V->getType())
  59. return nullptr;
  60. Expected<Value *> MaybeV = MaterializeValueFn(Idx, ConstExprInsertBB);
  61. if (!MaybeV) {
  62. // TODO: We might want to propagate the precise error message here.
  63. consumeError(MaybeV.takeError());
  64. return nullptr;
  65. }
  66. return MaybeV.get();
  67. }
  68. // No type specified, must be invalid reference.
  69. if (!Ty)
  70. return nullptr;
  71. // Create and return a placeholder, which will later be RAUW'd.
  72. Value *V = new Argument(Ty);
  73. ValuePtrs[Idx] = {V, TyID};
  74. return V;
  75. }