/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata /// information reachable from the given MDNode. void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) { assert(N->isFunctionLocal() && N->getFunction() && "EnumerateFunctionLocalMetadata called on non-function-local mdnode!"); // Enumerate the type of this value. EnumerateType(N->getType()); // Check to see if it's already in! unsigned &MDValueID = MDValueMap[N]; if (MDValueID) { // Increment use count. MDValues[MDValueID-1].second++; return; } MDValues.push_back(std::make_pair(N, 1U)); MDValueID = MDValues.size(); // To incoroporate function-local information visit all function-local // MDNodes and all function-local values they reference. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) if (Value *V = N->getOperand(i)) { if (MDNode *O = dyn_cast<MDNode>(V)) { if (O->isFunctionLocal() && O->getFunction()) EnumerateFunctionLocalMetadata(O); } else if (isa<Instruction>(V) || isa<Argument>(V)) EnumerateValue(V); } // Also, collect all function-local MDNodes for easy access. FunctionLocalMDs.push_back(N); }
void ValueEnumerator::EnumerateMetadata(const llvm::Metadata *MD) { assert( (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) && "Invalid metadata kind"); // Insert a dummy ID to block the co-recursive call to // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph. // // Return early if there's already an ID. if (!MDValueMap.insert(std::make_pair(MD, 0)).second) return; // Visit operands first to minimize RAUW. if (auto *N = dyn_cast<MDNode>(MD)) EnumerateMDNodeOperands(N); else if (auto *C = dyn_cast<ConstantAsMetadata>(MD)) EnumerateValue(C->getValue()); HasMDString |= isa<MDString>(MD); #if LLVM_VERSION >= 37 HasDILocation |= isa<DILocation>(MD); #else HasDILocation |= isa<MDLocation>(MD); #endif // Replace the dummy ID inserted above with the correct one. MDValueMap may // have changed by inserting operands, so we need a fresh lookup here. MDs.push_back(MD); MDValueMap[MD] = MDs.size(); }
/// EnumerateMDNodeOperands - Enumerate all non-function-local values /// and types referenced by the given MDNode. void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { if (Value *V = N->getOperand(i)) { if (isa<MDNode>(V) || isa<MDString>(V)) EnumerateMetadata(V); else if (!isa<Instruction>(V) && !isa<Argument>(V)) EnumerateValue(V); } else EnumerateType(Type::getVoidTy(N->getContext())); } }
/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata /// information reachable from the metadata. void ValueEnumerator::EnumerateFunctionLocalMetadata( const llvm::LocalAsMetadata *Local) { // Check to see if it's already in! unsigned &MDValueID = MDValueMap[Local]; if (MDValueID) return; MDs.push_back(Local); MDValueID = MDs.size(); EnumerateValue(Local->getValue()); // Also, collect all function-local metadata for easy access. FunctionLocalMDs.push_back(Local); }
void ValueEnumerator::incorporateFunction(const Function &F) { InstructionCount = 0; NumModuleValues = Values.size(); NumModuleMDValues = MDValues.size(); // Adding function arguments to the value table. for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) EnumerateValue(I); FirstFuncConstantID = Values.size(); // Add all function-level constants to the value table. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) { if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || isa<InlineAsm>(*OI)) EnumerateValue(*OI); } BasicBlocks.push_back(BB); ValueMap[BB] = BasicBlocks.size(); } // Optimize the constant layout. OptimizeConstants(FirstFuncConstantID, Values.size()); // Add the function's parameter attributes so they are available for use in // the function's instruction. EnumerateAttributes(F.getAttributes()); FirstInstID = Values.size(); SmallVector<MDNode *, 8> FnLocalMDVector; // Add all of the instructions. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) { if (MDNode *MD = dyn_cast<MDNode>(*OI)) if (MD->isFunctionLocal() && MD->getFunction()) // Enumerate metadata after the instructions they might refer to. FnLocalMDVector.push_back(MD); } SmallVector<std::pair<unsigned, MDNode*>, 8> MDs; I->getAllMetadataOtherThanDebugLoc(MDs); for (unsigned i = 0, e = MDs.size(); i != e; ++i) { MDNode *N = MDs[i].second; if (N->isFunctionLocal() && N->getFunction()) FnLocalMDVector.push_back(N); } if (!I->getType()->isVoidTy()) EnumerateValue(I); } } // Add all of the function-local metadata. for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) EnumerateFunctionLocalMetadata(FnLocalMDVector[i]); }
/// ValueEnumerator - Enumerate module-level information. ValueEnumerator::ValueEnumerator(const Module *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()); // 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 (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) EnumerateType(I->getType()); for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){ for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) { if (MDNode *MD = dyn_cast<MDNode>(*OI)) if (MD->isFunctionLocal() && MD->getFunction()) // These will get enumerated during function-incorporation. continue; EnumerateOperandType(*OI); } 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); if (!I->getDebugLoc().isUnknown()) { MDNode *Scope, *IA; I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext()); if (Scope) EnumerateMetadata(Scope); if (IA) EnumerateMetadata(IA); } } } // Optimize constant ordering. OptimizeConstants(FirstConstant, Values.size()); }
/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol /// table into the values table. void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); VI != VE; ++VI) EnumerateValue(VI->getValue()); }
/// ValueEnumerator - Enumerate module-level information. ValueEnumerator::ValueEnumerator(const llvm::Module &M, bool ShouldPreserveUseListOrder) : HasMDString(false), HasDILocation(false), ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) { assert(!ShouldPreserveUseListOrder && "not supported UseListOrders = predictUseListOrder(M)"); // Enumerate the global variables. for (llvm::Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) EnumerateValue(I); // Enumerate the functions. for (llvm::Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) { EnumerateValue(I); EnumerateAttributes(cast<Function>(I)->getAttributes()); } // Enumerate the aliases. for (llvm::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 (llvm::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 (llvm::Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E; ++I) EnumerateValue(I->getAliasee()); // 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()); 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); #if LLVM_VERSION >= 37 if (I.getDebugLoc()) { MDNode* Scope = I.getDebugLoc().getScope(); if (Scope) EnumerateMetadata(Scope); DILocation *IA = I.getDebugLoc().getInlinedAt(); if (IA) EnumerateMetadata(IA); } #else if (!I.getDebugLoc().isUnknown()) { MDNode *Scope, *IA; I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext()); if (Scope) EnumerateMetadata(Scope); if (IA) EnumerateMetadata(IA); } #endif } } // Optimize constant ordering. OptimizeConstants(FirstConstant, Values.size()); }