示例#1
0
void TypeFinder::run(const Module &M, bool onlyNamed) {
    OnlyNamed = onlyNamed;

    // Get types from global variables.
    for (Module::const_global_iterator I = M.global_begin(),
            E = M.global_end(); I != E; ++I) {
        incorporateType(I->getType());
        if (I->hasInitializer())
            incorporateValue(I->getInitializer());
    }

    // Get types from aliases.
    for (Module::const_alias_iterator I = M.alias_begin(),
            E = M.alias_end(); I != E; ++I) {
        incorporateType(I->getType());
        if (const Value *Aliasee = I->getAliasee())
            incorporateValue(Aliasee);
    }

    // Get types from functions.
    SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst;
    for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
        incorporateType(FI->getType());

        if (FI->hasPrefixData())
            incorporateValue(FI->getPrefixData());

        if (FI->hasPrologueData())
            incorporateValue(FI->getPrologueData());

        if (FI->hasPersonalityFn())
            incorporateValue(FI->getPersonalityFn());

        // First incorporate the arguments.
        for (Function::const_arg_iterator AI = FI->arg_begin(),
                AE = FI->arg_end(); AI != AE; ++AI)
            incorporateValue(AI);

        for (Function::const_iterator BB = FI->begin(), E = FI->end();
                BB != E; ++BB)
            for (BasicBlock::const_iterator II = BB->begin(),
                    E = BB->end(); II != E; ++II) {
                const Instruction &I = *II;

                // Incorporate the type of the instruction.
                incorporateType(I.getType());

                // Incorporate non-instruction operand types. (We are incorporating all
                // instructions with this loop.)
                for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
                        OI != OE; ++OI)
                    if (*OI && !isa<Instruction>(OI))
                        incorporateValue(*OI);

                // Incorporate types hiding in metadata.
                I.getAllMetadataOtherThanDebugLoc(MDForInst);
                for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
                    incorporateMDNode(MDForInst[i].second);

                MDForInst.clear();
            }
    }

    for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
            E = M.named_metadata_end(); I != E; ++I) {
        const NamedMDNode *NMD = I;
        for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
            incorporateMDNode(NMD->getOperand(i));
    }
}
ValueEnumerator::ValueEnumerator(const Module &M,
                                 bool ShouldPreserveUseListOrder)
    : HasMDString(false), HasDILocation(false), HasGenericDINode(false),
      ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
  if (ShouldPreserveUseListOrder)
    UseListOrders = predictUseListOrder(M);

  // Enumerate the global variables.
  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
       I != E; ++I)
    EnumerateValue(I);

  // Enumerate the functions.
  for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
    EnumerateValue(I);
    EnumerateAttributes(cast<Function>(I)->getAttributes());
  }

  // Enumerate the aliases.
  for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
       I != E; ++I)
    EnumerateValue(I);

  // Remember what is the cutoff between globalvalue's and other constants.
  unsigned FirstConstant = Values.size();

  // Enumerate the global variable initializers.
  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
       I != E; ++I)
    if (I->hasInitializer())
      EnumerateValue(I->getInitializer());

  // Enumerate the aliasees.
  for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
       I != E; ++I)
    EnumerateValue(I->getAliasee());

  // Enumerate the prefix data constants.
  for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
    if (I->hasPrefixData())
      EnumerateValue(I->getPrefixData());

  // Enumerate the prologue data constants.
  for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
    if (I->hasPrologueData())
      EnumerateValue(I->getPrologueData());

  // Enumerate the metadata type.
  //
  // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
  // only encodes the metadata type when it's used as a value.
  EnumerateType(Type::getMetadataTy(M.getContext()));

  // Insert constants and metadata that are named at module level into the slot
  // pool so that the module symbol table can refer to them...
  EnumerateValueSymbolTable(M.getValueSymbolTable());
  EnumerateNamedMetadata(M);

  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;

  // Enumerate types used by function bodies and argument lists.
  for (const Function &F : M) {
    for (const Argument &A : F.args())
      EnumerateType(A.getType());

    // Enumerate metadata attached to this function.
    F.getAllMetadata(MDs);
    for (const auto &I : MDs)
      EnumerateMetadata(I.second);

    for (const BasicBlock &BB : F)
      for (const Instruction &I : BB) {
        for (const Use &Op : I.operands()) {
          auto *MD = dyn_cast<MetadataAsValue>(&Op);
          if (!MD) {
            EnumerateOperandType(Op);
            continue;
          }

          // Local metadata is enumerated during function-incorporation.
          if (isa<LocalAsMetadata>(MD->getMetadata()))
            continue;

          EnumerateMetadata(MD->getMetadata());
        }
        EnumerateType(I.getType());
        if (const CallInst *CI = dyn_cast<CallInst>(&I))
          EnumerateAttributes(CI->getAttributes());
        else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
          EnumerateAttributes(II->getAttributes());

        // Enumerate metadata attached with this instruction.
        MDs.clear();
        I.getAllMetadataOtherThanDebugLoc(MDs);
        for (unsigned i = 0, e = MDs.size(); i != e; ++i)
          EnumerateMetadata(MDs[i].second);

        // Don't enumerate the location directly -- it has a special record
        // type -- but enumerate its operands.
        if (DILocation *L = I.getDebugLoc())
          EnumerateMDNodeOperands(L);
      }
  }

  // Optimize constant ordering.
  OptimizeConstants(FirstConstant, Values.size());
}