/// isDeclaration - Return 'true' if the global value is a declaration. static bool isDeclaration(const GlobalValue &V) { if (V.hasAvailableExternallyLinkage()) return true; if (V.isMaterializable()) return false; return V.isDeclaration(); }
static void raiseVisibilityOnValue(GlobalValue &V, GlobalRenamer &R) { if (V.hasLocalLinkage()) { if (R.needsRenaming(V)) V.setName(R.getRename(V)); V.setLinkage(GlobalValue::ExternalLinkage); V.setVisibility(GlobalValue::HiddenVisibility); } V.setUnnamedAddr(GlobalValue::UnnamedAddr::None); assert(!R.needsRenaming(V) && "Invalid global name."); }
Module * llvmutil_extractmodule(Module * OrigMod, TargetMachine * TM, std::vector<llvm::GlobalValue*> * livevalues, std::vector<std::string> * symbolnames, bool internalizeandoptimize) { assert(symbolnames == NULL || livevalues->size() == symbolnames->size()); ValueToValueMapTy VMap; #if LLVM_VERSION >= 34 Module * M = llvmutil_extractmodulewithproperties(OrigMod->getModuleIdentifier(), OrigMod, (llvm::GlobalValue **)&(*livevalues)[0], livevalues->size(), AlwaysCopy, NULL, VMap); #else Module * M = CloneModule(OrigMod, VMap); internalizeandoptimize = true; //we need to do this regardless of the input because it is the only way we can extract just the needed functions from the module #endif //rename values to symbolsnames std::vector<const char *> names; for(size_t i = 0; i < livevalues->size(); i++) { GlobalValue * fn = cast<GlobalValue>(VMap[(*livevalues)[i]]); const std::string & name = (*symbolnames)[i]; GlobalValue * gv = M->getNamedValue(name); if(gv) { //if there is already a symbol with this name, rename it gv->setName(Twine((*symbolnames)[i],"_renamed")); } fn->setName(name); //and set our function to this name assert(fn->getName() == name); names.push_back(name.c_str()); //internalize pass has weird interface, so we need to copy the names here } if (!internalizeandoptimize) return M; //at this point we run optimizations on the module //first internalize all functions not mentioned in "names" using an internalize pass and then perform //standard optimizations PassManager MPM; llvmutil_addtargetspecificpasses(&MPM, TM); MPM.add(createVerifierPass()); //make sure we haven't messed stuff up yet MPM.add(createInternalizePass(names)); MPM.add(createGlobalDCEPass()); //run this early since anything not in the table of exported functions is still in this module //this will remove dead functions PassManagerBuilder PMB; PMB.OptLevel = 3; PMB.SizeLevel = 0; #if LLVM_VERSION >= 35 PMB.LoopVectorize = true; PMB.SLPVectorize = true; #endif PMB.populateModulePassManager(MPM); MPM.run(*M); return M; }
bool FunctionImportGlobalProcessing::isNonRenamableLocal( const GlobalValue &GV) const { if (!GV.hasLocalLinkage()) return false; // This needs to stay in sync with the logic in buildModuleSummaryIndex. if (GV.hasSection()) return true; if (Used.count(const_cast<GlobalValue *>(&GV))) return true; return false; }
void Variables::changeGlobal(Change* change, Module &module) { GlobalValue* oldTarget = dyn_cast<GlobalValue>(change->getValue()); Type* oldType = oldTarget->getType()->getElementType(); Type* newType = change->getType()[0]; errs() << "Changing the precision of variable \"" << oldTarget->getName() << "\" from " << *oldType << " to " << *newType << ".\n"; if (diffTypes(oldType, newType)) { Constant *initializer; GlobalVariable* newTarget; if (PointerType *newPointerType = dyn_cast<PointerType>(newType)) { initializer = ConstantPointerNull::get(newPointerType); newTarget = new GlobalVariable(module, newType, false, GlobalValue::CommonLinkage, initializer, ""); } else if (ArrayType * atype = dyn_cast<ArrayType>(newType)) { // preparing initializer Type *temp = Type::getFloatTy(module.getContext()); vector<Constant*> operands; operands.push_back(ConstantFP::get(temp, 0)); ArrayRef<Constant*> *arrayRef = new ArrayRef<Constant*>(operands); initializer = ConstantArray::get(atype, *arrayRef); newTarget = new GlobalVariable(module, newType, false, GlobalValue::CommonLinkage, initializer, ""); } else { initializer = ConstantFP::get(newType, 0); newTarget = new GlobalVariable(module, newType, false, GlobalValue::CommonLinkage, initializer, ""); } /* GlobalVariable* newTarget = new GlobalVariable(module, newType, false, GlobalValue::CommonLinkage, initializer, ""); */ unsigned alignment = getAlignment(newType); newTarget->setAlignment(alignment); newTarget->takeName(oldTarget); // iterating through instructions using old AllocaInst Value::use_iterator it = oldTarget->use_begin(); for(; it != oldTarget->use_end(); it++) { Transformer::transform(it, newTarget, oldTarget, newType, oldType, alignment); } //oldTarget->eraseFromParent(); } else { errs() << "No changes required.\n"; } return; }
// llvm_mark_decl_weak - Used by varasm.c, called when a decl is found to be // weak, but it already had an llvm object created for it. This marks the LLVM // object weak as well. void llvm_mark_decl_weak(tree decl) { assert(DECL_LLVM_SET_P(decl) && DECL_WEAK(decl) && isa<GlobalValue>(DECL_LLVM(decl)) && "Decl isn't marked weak!"); GlobalValue *GV = cast<GlobalValue>(DECL_LLVM(decl)); // Do not mark something that is already known to be linkonce or internal. if (GV->hasExternalLinkage()) { if (GV->isDeclaration()) GV->setLinkage(GlobalValue::ExternalWeakLinkage); else GV->setLinkage(GlobalValue::WeakLinkage); } }
static void DumpSymbolNameForGlobalValue(GlobalValue &GV) { // Private linkage and available_externally linkage don't exist in symtab. if (GV.hasPrivateLinkage() || GV.hasLinkerPrivateLinkage() || GV.hasLinkerPrivateWeakLinkage() || GV.hasAvailableExternallyLinkage()) return; const std::string SymbolAddrStr = " "; // Not used yet... char TypeChar = TypeCharForSymbol(GV); if ((TypeChar != 'U') && UndefinedOnly) return; if ((TypeChar == 'U') && DefinedOnly) return; if (GV.hasLocalLinkage () && ExternalOnly) return; if (OutputFormat == posix) { outs() << GV.getName () << " " << TypeCharForSymbol(GV) << " " << SymbolAddrStr << "\n"; } else if (OutputFormat == bsd) { outs() << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " " << GV.getName () << "\n"; } else if (OutputFormat == sysv) { std::string PaddedName (GV.getName ()); while (PaddedName.length () < 20) PaddedName += " "; outs() << PaddedName << "|" << SymbolAddrStr << "| " << TypeCharForSymbol(GV) << " | | | |\n"; } }
/// Make sure GV is visible from both modules. Delete is true if it is /// being deleted from this module. /// This also makes sure GV cannot be dropped so that references from /// the split module remain valid. static void makeVisible(GlobalValue &GV, bool Delete, bool IsDeletePass) { bool Local = GV.hasLocalLinkage(); if (Local || Delete) { // This changes members from private -> hidden -> causes linker errors when using llvm-link if (!IsDeletePass) GV.setLinkage(GlobalValue::ExternalLinkage); if (Local) GV.setVisibility(GlobalValue::HiddenVisibility); return; } if (!GV.hasLinkOnceLinkage()) { assert(!GV.isDiscardableIfUnused()); return; } // Map linkonce* to weak* so that llvm doesn't drop this GV. switch(GV.getLinkage()) { default: llvm_unreachable("Unexpected linkage"); case GlobalValue::LinkOnceAnyLinkage: GV.setLinkage(GlobalValue::WeakAnyLinkage); return; case GlobalValue::LinkOnceODRLinkage: GV.setLinkage(GlobalValue::WeakODRLinkage); return; } }
/// Make sure GV is visible from both modules. Delete is true if it is /// being deleted from this module. /// This also makes sure GV cannot be dropped so that references from /// the split module remain valid. static void makeVisible(GlobalValue &GV, bool Delete) { bool Local = GV.hasLocalLinkage(); if (Local) GV.setVisibility(GlobalValue::HiddenVisibility); if (Local || Delete) { GV.setLinkage(GlobalValue::ExternalLinkage); return; } if (!GV.hasLinkOnceLinkage()) { assert(!GV.isDiscardableIfUnused()); return; } // Map linkonce* to weak* so that llvm doesn't drop this GV. switch(GV.getLinkage()) { default: llvm_unreachable("Unexpected linkage"); case GlobalValue::LinkOnceAnyLinkage: GV.setLinkage(GlobalValue::WeakAnyLinkage); return; case GlobalValue::LinkOnceODRLinkage: GV.setLinkage(GlobalValue::WeakODRLinkage); return; } }
void Module::wrapSymbol(StringRef symName) { std::string wrapSymName("__wrap_"); wrapSymName += symName; std::string realSymName("__real_"); realSymName += symName; GlobalValue *SymGV = getNamedValue(symName); GlobalValue *WrapGV = getNamedValue(wrapSymName); GlobalValue *RealGV = getNamedValue(realSymName); // Replace uses of "sym" with __wrap_sym. if (SymGV) { if (!WrapGV) WrapGV = cast<GlobalValue>(getOrInsertGlobal(wrapSymName, SymGV->getType())); SymGV->replaceAllUsesWith(ConstantExpr::getBitCast(WrapGV, SymGV->getType())); } // Replace uses of "__real_sym" with "sym". if (RealGV) { if (!SymGV) SymGV = cast<GlobalValue>(getOrInsertGlobal(symName, RealGV->getType())); RealGV->replaceAllUsesWith(ConstantExpr::getBitCast(SymGV, RealGV->getType())); } }
void LTOCodeGenerator:: applyRestriction(GlobalValue &GV, std::vector<const char*> &mustPreserveList, SmallPtrSet<GlobalValue*, 8> &asmUsed, Mangler &mangler) { SmallString<64> Buffer; mangler.getNameWithPrefix(Buffer, &GV, false); if (GV.isDeclaration()) return; if (_mustPreserveSymbols.count(Buffer)) mustPreserveList.push_back(GV.getName().data()); if (_asmUndefinedRefs.count(Buffer)) asmUsed.insert(&GV); }
bool EliminateAvailableExternally::runOnModule(Module &M) { bool Changed = false; // Convert any aliases that alias with an available externally // value (which will be turned into declarations later on in this routine) // into declarations themselves. All aliases must be definitions, and // must alias with a definition. So this involves creating a declaration // equivalent to the alias's base object. for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;) { // Increment the iterator first since we may delete the current alias. GlobalAlias &GA = *(I++); GlobalValue *GVal = GA.getBaseObject(); if (!GVal->hasAvailableExternallyLinkage()) continue; convertAliasToDeclaration(GA, M); Changed = true; } // Drop initializers of available externally global variables. for (GlobalVariable &GV : M.globals()) { if (!GV.hasAvailableExternallyLinkage()) continue; if (GV.hasInitializer()) { Constant *Init = GV.getInitializer(); GV.setInitializer(nullptr); if (isSafeToDestroyConstant(Init)) Init->destroyConstant(); } GV.removeDeadConstantUsers(); GV.setLinkage(GlobalValue::ExternalLinkage); NumVariables++; Changed = true; } // Drop the bodies of available externally functions. for (Function &F : M) { if (!F.hasAvailableExternallyLinkage()) continue; if (!F.isDeclaration()) // This will set the linkage to external F.deleteBody(); F.removeDeadConstantUsers(); NumFunctions++; Changed = true; } return Changed; }
static char getSymbolNMTypeChar(const GlobalValue &GV) { if (GV.getType()->getElementType()->isFunctionTy()) return 't'; // FIXME: should we print 'b'? At the IR level we cannot be sure if this // will be in bss or not, but we could approximate. return 'd'; }
void AMDGPUAlwaysInline::recursivelyVisitUsers( GlobalValue &GV, SmallPtrSetImpl<Function *> &FuncsToAlwaysInline) { SmallVector<User *, 16> Stack; SmallPtrSet<const Value *, 8> Visited; for (User *U : GV.users()) Stack.push_back(U); while (!Stack.empty()) { User *U = Stack.pop_back_val(); if (!Visited.insert(U).second) continue; if (Instruction *I = dyn_cast<Instruction>(U)) { Function *F = I->getParent()->getParent(); if (!AMDGPU::isEntryFunctionCC(F->getCallingConv())) { FuncsToAlwaysInline.insert(F); Stack.push_back(F); } // No need to look at further users, but we do need to inline any callers. continue; } for (User *UU : U->users()) Stack.push_back(UU); } }
static bool useExistingDest(GlobalValue &SGV, GlobalValue *DGV, bool ShouldLink) { if (!DGV) return false; if (SGV.isDeclaration()) return true; if (DGV->isDeclarationForLinker() && !SGV.isDeclarationForLinker()) return false; if (ShouldLink) return false; return true; }
JITSymbolFlags llvm::JITSymbolFlags::fromGlobalValue(const GlobalValue &GV) { JITSymbolFlags Flags = JITSymbolFlags::None; if (GV.hasWeakLinkage() || GV.hasLinkOnceLinkage()) Flags |= JITSymbolFlags::Weak; if (GV.hasCommonLinkage()) Flags |= JITSymbolFlags::Common; if (!GV.hasLocalLinkage() && !GV.hasHiddenVisibility()) Flags |= JITSymbolFlags::Exported; if (isa<Function>(GV)) Flags |= JITSymbolFlags::Callable; else if (isa<GlobalAlias>(GV) && isa<Function>(cast<GlobalAlias>(GV).getAliasee())) Flags |= JITSymbolFlags::Callable; return Flags; }
static bool shouldInternalize(const GlobalValue &GV, const std::set<std::string> &ExternalNames, const std::set<std::string> &DSONames) { // Function must be defined here if (GV.isDeclaration()) return false; // Available externally is really just a "declaration with a body". if (GV.hasAvailableExternallyLinkage()) return false; // Already has internal linkage if (GV.hasLocalLinkage()) return false; // Marked to keep external? if (ExternalNames.count(GV.getName())) return false; // Not needed for the symbol table? if (!DSONames.count(GV.getName())) return true; // Not a linkonce. Someone can depend on it being on the symbol table. if (!GV.hasLinkOnceLinkage()) return false; // The address is not important, we can hide it. if (GV.hasUnnamedAddr()) return true; // FIXME: Check if the address is used. return false; }
void ModuleLinker::addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add) { // Add these to the internalize list if (!GV.hasLinkOnceLinkage()) return; if (shouldInternalizeLinkedSymbols()) Internalize.insert(GV.getName()); Add(GV); const Comdat *SC = GV.getComdat(); if (!SC) return; for (GlobalValue *GV2 : ComdatMembers[SC]) { if (!GV2->hasLocalLinkage() && shouldInternalizeLinkedSymbols()) Internalize.insert(GV2->getName()); Add(*GV2); } }
void GlobalDCEPass::UpdateGVDependencies(GlobalValue &GV) { SmallPtrSet<GlobalValue *, 8> Deps; for (User *User : GV.users()) ComputeDependencies(User, Deps); Deps.erase(&GV); // Remove self-reference. for (GlobalValue *GVU : Deps) { GVDependencies.insert(std::make_pair(GVU, &GV)); } }
void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) { const TargetRegisterInfo &RI = *TM.getRegisterInfo(); switch (MO.getType()) { case MachineOperand::MO_Register: O << RI.get(MO.getReg()).AsmName; return; case MachineOperand::MO_Immediate: cerr << "printOp() does not handle immediate values\n"; abort(); return; case MachineOperand::MO_MachineBasicBlock: printBasicBlockLabel(MO.getMBB()); return; case MachineOperand::MO_ConstantPoolIndex: O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_" << MO.getIndex(); return; case MachineOperand::MO_ExternalSymbol: O << MO.getSymbolName(); return; case MachineOperand::MO_GlobalAddress: { GlobalValue *GV = MO.getGlobal(); O << Mang->getValueName(GV); if (GV->isDeclaration() && GV->hasExternalWeakLinkage()) ExtWeakSymbols.insert(GV); return; } case MachineOperand::MO_JumpTableIndex: O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_' << MO.getIndex(); return; default: O << "<unknown operand type: " << MO.getType() << ">"; return; } }
bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) { if (ValuesToLink.count(&SGV)) return true; if (SGV.hasLocalLinkage()) return true; if (DGV && !DGV->isDeclaration()) return false; if (SGV.hasAvailableExternallyLinkage()) return true; if (DoneLinkingBodies) return false; AddLazyFor(SGV, [this](GlobalValue &GV) { maybeAdd(&GV); }); return ValuesToLink.count(&SGV); }
GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition) { GlobalValue *NewGV; if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) { NewGV = copyGlobalVariableProto(SGVar); } else if (auto *SF = dyn_cast<Function>(SGV)) { NewGV = copyFunctionProto(SF); } else { if (ForDefinition) NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV)); else NewGV = new GlobalVariable( DstM, TypeMap.get(SGV->getValueType()), /*isConstant*/ false, GlobalValue::ExternalLinkage, /*init*/ nullptr, SGV->getName(), /*insertbefore*/ nullptr, SGV->getThreadLocalMode(), SGV->getType()->getAddressSpace()); } if (ForDefinition) NewGV->setLinkage(SGV->getLinkage()); else if (SGV->hasExternalWeakLinkage()) NewGV->setLinkage(GlobalValue::ExternalWeakLinkage); NewGV->copyAttributesFrom(SGV); if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) { // Metadata for global variables and function declarations is copied eagerly. if (isa<GlobalVariable>(SGV) || SGV->isDeclaration()) NewGO->copyMetadata(cast<GlobalObject>(SGV), 0); } // Remove these copied constants in case this stays a declaration, since // they point to the source module. If the def is linked the values will // be mapped in during linkFunctionBody. if (auto *NewF = dyn_cast<Function>(NewGV)) { NewF->setPersonalityFn(nullptr); NewF->setPrefixData(nullptr); NewF->setPrologueData(nullptr); } return NewGV; }
static void keepGlobalValue(GlobalValue &GV, std::vector<GlobalAlias *> &KeptAliases) { assert(!GV.hasLocalLinkage()); if (auto *GA = dyn_cast<GlobalAlias>(&GV)) KeptAliases.push_back(GA); switch (GV.getLinkage()) { default: break; case GlobalValue::LinkOnceAnyLinkage: GV.setLinkage(GlobalValue::WeakAnyLinkage); break; case GlobalValue::LinkOnceODRLinkage: GV.setLinkage(GlobalValue::WeakODRLinkage); break; } assert(!GV.isDiscardableIfUnused()); }
static void forceImport(Module *m, const char *name, const Type *retType, ...) { // If module lacks an externally visible symbol for the name then we // need to create one. We have to look in the symbol table because // we want to check everything (global variables, functions, and // aliases). Value *v = m->getValueSymbolTable().lookup(name); GlobalValue *gv = dyn_cast_or_null<GlobalValue>(v); if (!gv || gv->hasInternalLinkage()) { va_list ap; va_start(ap, retType); std::vector<const Type *> argTypes; while (const Type *t = va_arg(ap, const Type*)) argTypes.push_back(t); va_end(ap); m->getOrInsertFunction(name, FunctionType::get(retType, argTypes, false)); }
static void convertAliasToDeclaration(GlobalAlias &GA, Module &M) { GlobalValue *GVal = GA.getBaseObject(); GlobalValue *NewGV; if (auto *GVar = dyn_cast<GlobalVariable>(GVal)) { GlobalVariable *NewGVar = new GlobalVariable( M, GVar->getType()->getElementType(), GVar->isConstant(), GVar->getLinkage(), /*init*/ nullptr, GA.getName(), GVar, GVar->getThreadLocalMode(), GVar->getType()->getAddressSpace()); NewGV = NewGVar; NewGV->copyAttributesFrom(GVar); } else { auto *F = dyn_cast<Function>(GVal); assert(F); Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(), GA.getName(), &M); NewGV = NewF; NewGV->copyAttributesFrom(F); } GA.replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, GA.getType())); GA.eraseFromParent(); }
void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { // Check the summaries to see if the symbol gets resolved to a known local // definition. if (GV.hasName()) { ValueInfo VI = ImportIndex.getValueInfo(GV.getGUID()); if (VI) { // Need to check all summaries are local in case of hash collisions. bool IsLocal = VI.getSummaryList().size() && llvm::all_of(VI.getSummaryList(), [](const std::unique_ptr<GlobalValueSummary> &Summary) { return Summary->isDSOLocal(); }); if (IsLocal) GV.setDSOLocal(true); } } bool DoPromote = false; if (GV.hasLocalLinkage() && ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) { // Once we change the name or linkage it is difficult to determine // again whether we should promote since shouldPromoteLocalToGlobal needs // to locate the summary (based on GUID from name and linkage). Therefore, // use DoPromote result saved above. GV.setName(getName(&GV, DoPromote)); GV.setLinkage(getLinkage(&GV, DoPromote)); if (!GV.hasLocalLinkage()) GV.setVisibility(GlobalValue::HiddenVisibility); } else GV.setLinkage(getLinkage(&GV, /* DoPromote */ false)); // Remove functions imported as available externally defs from comdats, // as this is a declaration for the linker, and will be dropped eventually. // It is illegal for comdats to contain declarations. auto *GO = dyn_cast_or_null<GlobalObject>(&GV); if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { // The IRMover should not have placed any imported declarations in // a comdat, so the only declaration that should be in a comdat // at this point would be a definition imported as available_externally. assert(GO->hasAvailableExternallyLinkage() && "Expected comdat on definition (possibly available external)"); GO->setComdat(nullptr); } }
static bool shouldInternalize(const GlobalValue &GV, const std::set<std::string> &ExternalNames, bool OnlyHidden) { if (OnlyHidden && !GV.hasHiddenVisibility()) return false; // Function must be defined here if (GV.isDeclaration()) return false; // Available externally is really just a "declaration with a body". if (GV.hasAvailableExternallyLinkage()) return false; // Assume that dllexported symbols are referenced elsewhere if (GV.hasDLLExportStorageClass()) return false; // Already has internal linkage if (GV.hasLocalLinkage()) return false; // Marked to keep external? if (ExternalNames.count(GV.getName())) return false; return true; }
void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { bool DoPromote = false; if (GV.hasLocalLinkage() && ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) { // Once we change the name or linkage it is difficult to determine // again whether we should promote since shouldPromoteLocalToGlobal needs // to locate the summary (based on GUID from name and linkage). Therefore, // use DoPromote result saved above. GV.setName(getName(&GV, DoPromote)); GV.setLinkage(getLinkage(&GV, DoPromote)); if (!GV.hasLocalLinkage()) GV.setVisibility(GlobalValue::HiddenVisibility); } else GV.setLinkage(getLinkage(&GV, /* DoPromote */ false)); // Remove functions imported as available externally defs from comdats, // as this is a declaration for the linker, and will be dropped eventually. // It is illegal for comdats to contain declarations. auto *GO = dyn_cast_or_null<GlobalObject>(&GV); if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { // The IRMover should not have placed any imported declarations in // a comdat, so the only declaration that should be in a comdat // at this point would be a definition imported as available_externally. assert(GO->hasAvailableExternallyLinkage() && "Expected comdat on definition (possibly available external)"); GO->setComdat(nullptr); } }
// getOrInsertFunction - Look up the specified function in the module symbol // table. If it does not exist, add a prototype for the function and return // it. This is nice because it allows most passes to get away with not handling // the symbol table directly for this common task. // Constant *Module::getOrInsertFunction(StringRef Name, FunctionType *Ty, AttributeSet AttributeList) { // See if we have a definition for the specified function already. GlobalValue *F = getNamedValue(Name); if (!F) { // Nope, add it Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); if (!New->isIntrinsic()) // Intrinsics get attrs set on construction New->setAttributes(AttributeList); FunctionList.push_back(New); return New; // Return the new prototype. } // If the function exists but has the wrong type, return a bitcast to the // right type. if (F->getType() != PointerType::getUnqual(Ty)) return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); // Otherwise, we just found the existing function or a prototype. return F; }
bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) { if (ValuesToLink.count(&SGV) || SGV.hasLocalLinkage()) return true; if (DGV && !DGV->isDeclarationForLinker()) return false; if (SGV.hasAvailableExternallyLinkage()) return true; if (SGV.isDeclaration() || DoneLinkingBodies) return false; // Callback to the client to give a chance to lazily add the Global to the // list of value to link. bool LazilyAdded = false; AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) { maybeAdd(&GV); LazilyAdded = true; }); return LazilyAdded; }