llvm libLLVMCore源码分析 25 - GVMaterializer Class

源码路径

llvm\include\llvm\IR\GVMaterializer.h

llvm\include\llvm\IR\AutoUpgrade.h

llvm\Bitcode\BitcodeReader.h

llvm\lib\Bitcode\Reader\BitcodeReader.cpp

llvm GVMaterializer Class

llvm中使用GVMaterializer类作为加载Module的类的抽象接口,比如llvm的BitcodeReader就是该类的实体类:

class GVMaterializer {
public:
  virtual Error materialize(GlobalValue *GV) = 0;
  virtual Error materializeModule() = 0;
  virtual Error materializeMetadata() = 0;
  virtual void setStripDebugInfo() = 0;
  virtual std::vector<StructType *> getIdentifiedStructTypes() const = 0;
};

以BitcodeReader类为例,加载Module的实现如下:

Error BitcodeReader::materializeModule() {
  if (Error Err = materializeMetadata())
    return Err;

  // Promise to materialize all forward references.
  WillMaterializeAllForwardRefs = true;

  // Iterate over the module, deserializing any functions that are still on
  // disk.
  for (Function &F : *TheModule) {
    if (Error Err = materialize(&F))
      return Err;
  }
  // At this point, if there are any function bodies, parse the rest of
  // the bits in the module past the last function block we have recorded
  // through either lazy scanning or the VST.
  if (LastFunctionBlockBit || NextUnreadBit)
    if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit
                                    ? LastFunctionBlockBit
                                    : NextUnreadBit))
      return Err;

  // Check that all block address forward references got resolved (as we
  // promised above).
  if (!BasicBlockFwdRefs.empty())
    return error("Never resolved function from blockaddress");

  // Upgrade any intrinsic calls that slipped through (should not happen!) and
  // delete the old functions to clean up. We can't do this unless the entire
  // module is materialized because there could always be another function body
  // with calls to the old function.
  for (auto &I : UpgradedIntrinsics) {
    for (auto *U : I.first->users()) {
      if (CallInst *CI = dyn_cast<CallInst>(U))
        UpgradeIntrinsicCall(CI, I.second);
    }
    if (!I.first->use_empty())
      I.first->replaceAllUsesWith(I.second);
    I.first->eraseFromParent();
  }
  UpgradedIntrinsics.clear();
  // Do the same for remangled intrinsics
  for (auto &I : RemangledIntrinsics) {
    I.first->replaceAllUsesWith(I.second);
    I.first->eraseFromParent();
  }
  RemangledIntrinsics.clear();

  UpgradeDebugInfo(*TheModule);

  UpgradeModuleFlags(*TheModule);

  UpgradeARCRuntime(*TheModule);

  return Error::success();
}

Module的加载过程如下(更新的工作由AutoUpgrade类实现):

  • 加载Metadata。
  • 加载Function。
  • Resolve前向引用。
  • 更新Intrinsic Call(兼容老的IR)。
  • 更新调试信息
  • 更新Module Flags
  • 更新ARC(ObjectC中的Automatic Reference Counting)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值