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.
| 142 | // the symbol table directly for this common task. |
| 143 | // |
| 144 | Constant *Module::getOrInsertFunction(StringRef Name, FunctionType *Ty, |
| 145 | AttributeList AttributeList) { |
| 146 | // See if we have a definition for the specified function already. |
| 147 | GlobalValue *F = getNamedValue(Name); |
| 148 | if (!F) { |
| 149 | // Nope, add it |
| 150 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, |
| 151 | DL.getProgramAddressSpace(), Name); |
| 152 | if (!New->isIntrinsic()) // Intrinsics get attrs set on construction |
| 153 | New->setAttributes(AttributeList); |
| 154 | FunctionList.push_back(New); |
| 155 | return New; // Return the new prototype. |
| 156 | } |
| 157 | |
| 158 | // If the function exists but has the wrong type, return a bitcast to the |
| 159 | // right type. |
| 160 | auto *PTy = PointerType::get(Ty, F->getAddressSpace()); |
| 161 | if (F->getType() != PTy) |
| 162 | return ConstantExpr::getBitCast(F, PTy); |
| 163 | |
| 164 | // Otherwise, we just found the existing function or a prototype. |
| 165 | return F; |
| 166 | } |
| 167 | |
| 168 | Constant *Module::getOrInsertFunction(StringRef Name, |
| 169 | FunctionType *Ty) { |