static bool replaceModuleFlagsEntry(llvm::LLVMContext &Ctx, llvm::Module &Module, StringRef EntryName, llvm::Module::ModFlagBehavior Behavior, llvm::Metadata *Val) { auto *ModuleFlags = Module.getModuleFlagsMetadata(); for (unsigned I = 0, E = ModuleFlags->getNumOperands(); I != E; ++I) { llvm::MDNode *Op = ModuleFlags->getOperand(I); llvm::MDString *ID = cast<llvm::MDString>(Op->getOperand(1)); if (ID->getString().equals(EntryName)) { // Create the new entry. llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); llvm::Metadata *Ops[3] = {llvm::ConstantAsMetadata::get( llvm::ConstantInt::get(Int32Ty, Behavior)), llvm::MDString::get(Ctx, EntryName), Val}; ModuleFlags->setOperand(I, llvm::MDNode::get(Ctx, Ops)); return true; } } llvm_unreachable("Could not replace old linker options entry?"); }
bool swift::immediate::IRGenImportedModules( CompilerInstance &CI, llvm::Module &Module, llvm::SmallPtrSet<swift::Module *, 8> &ImportedModules, SmallVectorImpl<llvm::Function*> &InitFns, IRGenOptions &IRGenOpts, const SILOptions &SILOpts) { swift::Module *M = CI.getMainModule(); // Perform autolinking. SmallVector<LinkLibrary, 4> AllLinkLibraries(IRGenOpts.LinkLibraries); auto addLinkLibrary = [&](LinkLibrary linkLib) { AllLinkLibraries.push_back(linkLib); }; M->forAllVisibleModules({}, /*includePrivateTopLevel=*/true, [&](Module::ImportedModule import) { import.second->collectLinkLibraries(addLinkLibrary); }); // Hack to handle thunks eagerly synthesized by the Clang importer. swift::Module *prev = nullptr; for (auto external : CI.getASTContext().ExternalDefinitions) { swift::Module *next = external->getModuleContext(); if (next == prev) continue; next->collectLinkLibraries(addLinkLibrary); prev = next; } tryLoadLibraries(AllLinkLibraries, CI.getASTContext().SearchPathOpts, CI.getDiags()); ImportedModules.insert(M); if (!CI.hasSourceImport()) return false; // IRGen the modules this module depends on. This is only really necessary // for imported source, but that's a very convenient thing to do in -i mode. // FIXME: Crawling all loaded modules is a hack. // FIXME: And re-doing SILGen, SIL-linking, SIL diagnostics, and IRGen is // expensive, because it's not properly being limited to new things right now. bool hadError = false; for (auto &entry : CI.getASTContext().LoadedModules) { swift::Module *import = entry.second; if (!ImportedModules.insert(import).second) continue; std::unique_ptr<SILModule> SILMod = performSILGeneration(import, CI.getSILOptions()); performSILLinking(SILMod.get()); if (runSILDiagnosticPasses(*SILMod)) { hadError = true; break; } // FIXME: We shouldn't need to use the global context here, but // something is persisting across calls to performIRGeneration. auto SubModule = performIRGeneration(IRGenOpts, import, SILMod.get(), import->getName().str(), llvm::getGlobalContext()); if (CI.getASTContext().hadError()) { hadError = true; break; } if (!linkLLVMModules(&Module, std::move(SubModule) // TODO: reactivate the linker mode if it is // supported in llvm again. Otherwise remove the // commented code completely. /*, llvm::Linker::DestroySource */)) { hadError = true; break; } // FIXME: This is an ugly hack; need to figure out how this should // actually work. SmallVector<char, 20> NameBuf; StringRef InitFnName = (import->getName().str() + ".init").toStringRef(NameBuf); llvm::Function *InitFn = Module.getFunction(InitFnName); if (InitFn) InitFns.push_back(InitFn); } return hadError; }
void ClangInternalState::printLLVMModule(llvm::raw_ostream& Out, llvm::Module& M, CodeGenerator& CG) { M.print(Out, /*AssemblyAnnotationWriter*/ 0); CG.print(Out); }
CodeGenASTVisitor::CodeGenASTVisitor(llvm::Module& targetModule) : Context(targetModule.getContext()) , TargetModule(targetModule) , Builder(TargetModule.getContext()) { }
/*! * This method identify which is value sym and which is object sym */ void SymbolTableInfo::buildMemModel(llvm::Module& module) { analysisUtil::increaseStackSize(); prePassSchedule(module); mod = &module; maxFieldLimit = maxFieldNumLimit; // Object #0 is black hole the object that may point to any object assert(totalSymNum == BlackHole && "Something changed!"); symTyMap.insert(std::make_pair(totalSymNum++, BlackHole)); createBlkOrConstantObj(BlackHole); // Object #1 always represents the constant assert(totalSymNum == ConstantObj && "Something changed!"); symTyMap.insert(std::make_pair(totalSymNum++, ConstantObj)); createBlkOrConstantObj(ConstantObj); // Pointer #2 always represents the pointer points-to black hole. assert(totalSymNum == BlkPtr && "Something changed!"); symTyMap.insert(std::make_pair(totalSymNum++, BlkPtr)); // Pointer #3 always represents the null pointer. assert(totalSymNum == NullPtr && "Something changed!"); symTyMap.insert(std::make_pair(totalSymNum, NullPtr)); // Add symbols for all the globals . for (Module::global_iterator I = module.global_begin(), E = module.global_end(); I != E; ++I) { collectSym(&*I); } // Add symbols for all the global aliases for (Module::alias_iterator I = module.alias_begin(), E = module.alias_end(); I != E; I++) { collectSym(&*I); } // Add symbols for all of the functions and the instructions in them. for (Module::iterator F = module.begin(), E = module.end(); F != E; ++F) { collectSym(&*F); collectRet(&*F); if (F->getFunctionType()->isVarArg()) collectVararg(&*F); // Add symbols for all formal parameters. for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) { collectSym(&*I); } // collect and create symbols inside the function body for (inst_iterator II = inst_begin(&*F), E = inst_end(&*F); II != E; ++II) { const Instruction *inst = &*II; collectSym(inst); // initialization for some special instructions //{@ if (const StoreInst *st = dyn_cast<StoreInst>(inst)) { collectSym(st->getPointerOperand()); collectSym(st->getValueOperand()); } else if (const LoadInst *ld = dyn_cast<LoadInst>(inst)) { collectSym(ld->getPointerOperand()); } else if (const PHINode *phi = dyn_cast<PHINode>(inst)) { for (u32_t i = 0; i < phi->getNumIncomingValues(); ++i) { collectSym(phi->getIncomingValue(i)); } } else if (const GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>( inst)) { collectSym(gep->getPointerOperand()); } else if (const SelectInst *sel = dyn_cast<SelectInst>(inst)) { collectSym(sel->getTrueValue()); collectSym(sel->getFalseValue()); } else if (const CastInst *cast = dyn_cast<CastInst>(inst)) { collectSym(cast->getOperand(0)); } else if (const ReturnInst *ret = dyn_cast<ReturnInst>(inst)) { if(ret->getReturnValue()) collectSym(ret->getReturnValue()); } else if (isCallSite(inst) && isInstrinsicDbgInst(inst)==false) { CallSite cs = analysisUtil::getLLVMCallSite(inst); callSiteSet.insert(cs); for (CallSite::arg_iterator it = cs.arg_begin(); it != cs.arg_end(); ++it) { collectSym(*it); } // Calls to inline asm need to be added as well because the callee isn't // referenced anywhere else. const Value *Callee = cs.getCalledValue(); collectSym(Callee); //TODO handle inlineAsm ///if (isa<InlineAsm>(Callee)) } //@} } } }
void SmackModuleGenerator::generateProgram(llvm::Module& M) { Naming naming; SmackRep rep(M.getDataLayout(), naming, program, getAnalysis<Regions>()); std::list<Decl*>& decls = program.getDeclarations(); DEBUG(errs() << "Analyzing globals...\n"); for (auto& G : M.globals()) { auto ds = rep.globalDecl(&G); decls.insert(decls.end(), ds.begin(), ds.end()); } DEBUG(errs() << "Analyzing functions...\n"); for (auto& F : M) { // Reset the counters for per-function names naming.reset(); DEBUG(errs() << "Analyzing function: " << naming.get(F) << "\n"); auto ds = rep.globalDecl(&F); decls.insert(decls.end(), ds.begin(), ds.end()); auto procs = rep.procedure(&F); assert(procs.size() > 0); if (naming.get(F) != Naming::DECLARATIONS_PROC) decls.insert(decls.end(), procs.begin(), procs.end()); if (F.isDeclaration()) continue; if (!F.empty() && !F.getEntryBlock().empty()) { DEBUG(errs() << "Analyzing function body: " << naming.get(F) << "\n"); for (auto P : procs) { SmackInstGenerator igen(getAnalysis<LoopInfo>(F), rep, *P, naming); DEBUG(errs() << "Generating body for " << naming.get(F) << "\n"); igen.visit(F); DEBUG(errs() << "\n"); // First execute static initializers, in the main procedure. if (F.hasName() && SmackOptions::isEntryPoint(F.getName())) { P->insert(Stmt::call(Naming::INITIALIZE_PROC)); } else if (naming.get(F).find(Naming::INIT_FUNC_PREFIX) == 0) rep.addInitFunc(&F); } DEBUG(errs() << "Finished analyzing function: " << naming.get(F) << "\n\n"); } // MODIFIES // ... to do below, after memory splitting is determined. } auto ds = rep.auxiliaryDeclarations(); decls.insert(decls.end(), ds.begin(), ds.end()); decls.insert(decls.end(), rep.getInitFuncs()); // NOTE we must do this after instruction generation, since we would not // otherwise know how many regions to declare. program.appendPrelude(rep.getPrelude()); std::list<Decl*> kill_list; for (auto D : program) { if (auto P = dyn_cast<ProcDecl>(D)) { if (D->getName().find(Naming::CONTRACT_EXPR) != std::string::npos) { decls.insert(decls.end(), Decl::code(P)); kill_list.push_back(P); } } } for (auto D : kill_list) decls.erase(std::remove(decls.begin(), decls.end(), D), decls.end()); }
bool CheckCandidateMap(llvm::Module &Mod, CandidateMap &M, Solver *S, InstContext &IC) { if (!S) { llvm::errs() << "Solver required in -check mode\n"; return false; } unsigned ExpectedID = Mod.getContext().getMDKindID("expected"); bool OK = true; for (auto &Cand : M) { Inst *RHS; if (std::error_code EC = S->infer(Cand.PCs, Cand.Mapping.LHS, RHS, &Cand.Origin, IC)) { llvm::errs() << "Unable to query solver: " << EC.message() << '\n'; return false; } if (RHS) { Cand.Mapping.RHS = RHS; if (Cand.Mapping.RHS->K != Inst::Const) { llvm::errs() << "found replacement:\n"; Cand.printFunction(llvm::errs()); Cand.print(llvm::errs()); llvm::errs() << "but cannot yet analyze non-constant replacements\n"; OK = false; continue; } llvm::APInt ActualVal = Cand.Mapping.RHS->Val; llvm::Instruction *Inst = Cand.Origin.getInstruction(); llvm::MDNode *ExpectedMD = Inst->getMetadata(ExpectedID); if (!ExpectedMD) { llvm::errs() << "instruction:\n"; Inst->dump(); llvm::errs() << "unexpected simplification:\n"; Cand.printFunction(llvm::errs()); Cand.print(llvm::errs()); OK = false; continue; } if (ExpectedMD->getNumOperands() != 1 || !isa<llvm::ConstantInt>(ExpectedMD->getOperand(0))) { llvm::errs() << "instruction:\n"; Inst->dump(); llvm::errs() << "invalid metadata\n"; OK = false; continue; } llvm::APInt ExpectedVal = cast<llvm::ConstantInt>(ExpectedMD->getOperand(0))->getValue(); Inst->setMetadata(ExpectedID, 0); if (ExpectedVal != ActualVal) { llvm::errs() << "instruction:\n"; Inst->dump(); llvm::errs() << "unexpected simplification, wanted " << ExpectedVal << ":\n"; Cand.printFunction(llvm::errs()); Cand.print(llvm::errs()); OK = false; continue; } } } for (const auto &F : Mod) { for (const auto &BB : F) { for (const auto &Inst : BB) { llvm::MDNode *ExpectedMD = Inst.getMetadata(ExpectedID); if (ExpectedMD) { llvm::errs() << "instruction:\n"; Inst.dump(); llvm::errs() << "expected simplification, none found\n"; OK = false; continue; } } } } return OK; }
Compiler(llvm::Module& module_) : module(module_), builder(module_.getContext()), endLoopBlock(nullptr), result(nullptr) {}
/// 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()); }
/// EnumerateNamedMetadata - Insert all of the values referenced by /// named metadata in the specified module. void ValueEnumerator::EnumerateNamedMetadata(const llvm::Module &M) { for (llvm::Module::const_named_metadata_iterator I = M.named_metadata_begin(), E = M.named_metadata_end(); I != E; ++I) EnumerateNamedMDNode(I); }
virtual llvm::Value* materializeValueFor(llvm::Value* v) { if (llvm::ConstantExpr* ce = llvm::dyn_cast<llvm::ConstantExpr>(v)) { llvm::PointerType* pt = llvm::dyn_cast<llvm::PointerType>(ce->getType()); if (ce->isCast() && ce->getOpcode() == llvm::Instruction::IntToPtr && pt) { llvm::ConstantInt* addr_const = llvm::cast<llvm::ConstantInt>(ce->getOperand(0)); void* addr = (void*)addr_const->getSExtValue(); bool lookup_success = true; std::string name; if (addr == (void*)None) { name = "None"; } else { name = g.func_addr_registry.getFuncNameAtAddress(addr, true, &lookup_success); } if (!lookup_success) return v; return module->getOrInsertGlobal(name, pt->getElementType()); } } if (llvm::IntrinsicInst* ii = llvm::dyn_cast<llvm::IntrinsicInst>(v)) { if (ii->getIntrinsicID() == llvm::Intrinsic::experimental_patchpoint_i64 || ii->getIntrinsicID() == llvm::Intrinsic::experimental_patchpoint_void || ii->getIntrinsicID() == llvm::Intrinsic::experimental_patchpoint_double) { int pp_id = -1; for (int i = 0; i < ii->getNumArgOperands(); i++) { llvm::Value* op = ii->getArgOperand(i); if (i != 2) { if (i == 0) { llvm::ConstantInt* l_pp_id = llvm::cast<llvm::ConstantInt>(op); pp_id = l_pp_id->getSExtValue(); } ii->setArgOperand(i, llvm::MapValue(op, VMap, flags, type_remapper, this)); continue; } else { assert(pp_id != -1); void* addr = PatchpointInfo::getSlowpathAddr(pp_id); bool lookup_success = true; std::string name; if (addr == (void*)None) { name = "None"; } else { name = g.func_addr_registry.getFuncNameAtAddress(addr, true, &lookup_success); } if (!lookup_success) { llvm::Constant* int_val = llvm::ConstantInt::get(g.i64, reinterpret_cast<uintptr_t>(addr), false); llvm::Constant* ptr_val = llvm::ConstantExpr::getIntToPtr(int_val, g.i8); ii->setArgOperand(i, ptr_val); continue; } else { ii->setArgOperand(i, module->getOrInsertGlobal(name, g.i8)); } } } return ii; } } return v; }
bool RenderScriptRuntimeModulePass::runOnModule(llvm::Module &module) { bool changed_module = false; Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_EXPRESSIONS)); std::string err; llvm::StringRef real_triple = m_process_ptr->GetTarget().GetArchitecture().GetTriple().getTriple(); const llvm::Target *target_info = llvm::TargetRegistry::lookupTarget(real_triple, err); if (!target_info) { if (log) log->Warning("couldn't determine real target architecture: '%s'", err.c_str()); return false; } llvm::Optional<llvm::Reloc::Model> reloc_model = llvm::None; assert(m_process_ptr && "no available lldb process"); switch (m_process_ptr->GetTarget().GetArchitecture().GetMachine()) { case llvm::Triple::ArchType::x86: changed_module |= fixupX86FunctionCalls(module); // For some reason this triple gets totally missed by the backend, and must be set manually. // There a reference in bcc/Main.cpp about auto feature-detection being removed from LLVM3.5, but I can't // see that discussion anywhere public. real_triple = "i686--linux-android"; break; case llvm::Triple::ArchType::x86_64: changed_module |= fixupX86_64FunctionCalls(module); break; case llvm::Triple::ArchType::mipsel: case llvm::Triple::ArchType::mips64el: // No actual IR fixup pass is needed on MIPS, but the datalayout // and targetmachine do need to be explicitly set. // bcc explicitly compiles MIPS code to use the static relocation // model due to an issue with relocations in mclinker. // see libbcc/support/CompilerConfig.cpp for details reloc_model = llvm::Reloc::Static; changed_module = true; break; case llvm::Triple::ArchType::arm: case llvm::Triple::ArchType::aarch64: // ARM subtargets need no fixup passes as they are the initial target as generated by the // slang compiler frontend. break; default: if (log) log->Warning("Ignoring unknown renderscript target"); return false; } if (changed_module) { llvm::TargetOptions options; llvm::TargetMachine *target_machine = target_info->createTargetMachine(real_triple, "", "", options, reloc_model); assert(target_machine && "failed to identify RenderScriptRuntime target machine"); // We've been using a triple and datalayout of some ARM variant all along, so // we need to let the backend know that this is no longer the case. if (log) { log->Printf("%s - Changing RS target triple to '%s'", __FUNCTION__, real_triple.str().c_str()); log->Printf("%s - Changing RS datalayout to '%s'", __FUNCTION__, target_machine->createDataLayout().getStringRepresentation().c_str()); } module.setTargetTriple(real_triple); module.setDataLayout(target_machine->createDataLayout()); } return changed_module; }
std::string Aa::get_aa_type_name(const llvm::Type* ptr, llvm::Module& tst) { if(ptr->isVoidTy()) return("$void"); std::string ret_string = tst.getTypeName(ptr); if(ret_string != "") return(to_aa(ret_string)); if(isa<PointerType>(ptr)) { ret_string = "$pointer< "; const llvm::PointerType *ptr_pointer = dyn_cast<llvm::PointerType>(ptr); const llvm::Type* el_type = ptr_pointer->getElementType(); ret_string += get_aa_type_name(el_type,tst); ret_string += " >"; } else if(isa<ArrayType>(ptr) || isa<VectorType>(ptr)) { const llvm::SequentialType *ptr_seq = dyn_cast<llvm::SequentialType>(ptr); const llvm::Type* el_type = ptr_seq->getElementType(); int dim = 0; const llvm::ArrayType* ptr_array = dyn_cast<llvm::ArrayType>(ptr); if(ptr_array != NULL) dim = ptr_array->getNumElements(); else { const llvm::VectorType* ptr_vec = dyn_cast<llvm::VectorType>(ptr); dim = ptr_vec->getNumElements(); } ret_string = "$array [" + int_to_str(dim) + "] $of "; ret_string += get_aa_type_name(el_type,tst); } else if(isa<StructType>(ptr)) { const llvm::StructType *ptr_struct = dyn_cast<llvm::StructType>(ptr); ret_string = "$record "; for(int idx = 0; idx < ptr_struct->getNumElements(); idx++) { ret_string += "< " ; ret_string += get_aa_type_name(ptr_struct->getElementType(idx),tst); ret_string += " > "; } } else if(isa<IntegerType>(ptr)) { ret_string = "$uint<"; const llvm::IntegerType* ptr_int = dyn_cast<llvm::IntegerType>(ptr); ret_string += int_to_str(ptr_int->getBitWidth()) + ">"; } else if(ptr->isFloatTy()) { ret_string = "$float<8,23>"; } else if(ptr->isDoubleTy()) { ret_string = "$float<11,52>"; } else if(ptr->isVoidTy()) { ret_string = "$void"; } else if(ptr->isFunctionTy()) { std::cerr << "Warning: function type converted to void*" << std::endl; ret_string = "$pointer < $void > "; } else { std::cerr << "Error: unsupported type" << std::endl; ret_string = "Unsupported_Type"; } return(ret_string); }
void compile_llvm_module_to_llvm_assembly(llvm::Module &module, Internal::LLVMOStream& out) { module.print(out, nullptr); }
void Simulator::Simulate() { llvm::SMDiagnostic error; //std::unique_ptr<llvm::Module> M = llvm::ParseIRFile("Behavior.bc", error, context); //MyModule = M.get(); MyModule = llvm::ParseIRFile("Behavior.bc", error, context); string err_str; /* OwningPtr<MemoryBuffer> result; MemoryBuffer *mb; llvm::error_code ec = MemoryBuffer::getFile("Behavior.bc", result); mb = result.take(); err_str = ec.message(); if (!mb) { error() <<"Cannot open \"" <<bitcode_file() <<"\": " <<err_str <<endl; exit(1); } MyModule = llvm::ParseBitcodeFile(mb,context,&err_str); if (!MyModule) { error() <<"Failed to load module from bitcode file: " <<err_str <<endl; exit(1); } delete mb; */ /* for (llvm::Module::iterator f = MyModule->begin(), ef = MyModule->end(); f!=ef; ++f) { f->addFnAttr(Attributes::AlwaysInline); } */ for (int32_t i = 0; i < (int32_t)OPCODE::INVALID; i++) { OPCODE op = (OPCODE) i; llvm::Function *func = MyModule->getFunction(GetFuncName(op)); func->addFnAttr(llvm::Attribute::AlwaysInline); OpFunctionMap[op] = func; } test_type = MyModule->getFunction("test")->getFunctionType(); std::string ErrStr; passManager = new llvm::PassManager(); passManager->add(llvm::createAlwaysInlinerPass()); fPassManager = new llvm::FunctionPassManager(MyModule); //fPassManager->add(new DataLayout(*EE->getDataLayout())); //fPassManager->add(new DataLayout(*EE->getDataLayout())); fPassManager->add(llvm::createGVNPass()); fPassManager->add(llvm::createInstructionCombiningPass()); fPassManager->add(llvm::createCFGSimplificationPass()); fPassManager->add(llvm::createDeadStoreEliminationPass()); /* llvm::EngineBuilder builder(MyModule); builder.setErrorStr(&ErrStr); builder.setEngineKind(llvm::EngineKind::JIT); builder.setOptLevel(llvm::CodeGenOpt::Default); // None/Less/Default/Aggressive //llvm::TargetOptions options; //options.JITExceptionHandling = 1; //builder.setTargetOptions(options); EE = builder.create(); */ //StringRef MCPU = llvm::sys::getHostCPUName() EE = llvm::EngineBuilder(MyModule).create(); /* EE = llvm::EngineBuilder(std::move(M)) .setErrorStr(&ErrStr) .setMCJITMemoryManager(llvm::make_unique<llvm::SectionMemoryManager>()) .create(); */ /* EE = llvm::EngineBuilder(M) .setErrorStr(&ErrStr) .setMCJITMemoryManager(llvm::SectionMemoryManager()) .create(); EE = llvm::EngineBuilder(std::move(M)).setErrorStr(&ErrStr).create(); */ if (!EE) { fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str()); exit(1); } EE->DisableLazyCompilation(true); /* //atexit(llvm_shutdown); // Call llvm_shutdown() on exit. //llvm::EngineBuilder builder(MyModule); llvm::EngineBuilder builder(std::move(M)); //llvm::EngineBuilder builder(MyModule); builder.setErrorStr(&err_str); builder.setEngineKind(llvm::EngineKind::JIT); builder.setOptLevel(llvm::CodeGenOpt::Default); // None/Less/Default/Aggressive //TargetOptions options; //options.JITExceptionHandling = 1; //builder.setTargetOptions(options); EE = builder.create(); if (!EE) { std::cout <<"failed to create execution engine: " <<err_str <<"\n"; exit(1); } */ /* //MyModule->dump(); EE = llvm::EngineBuilder(std::move(M)).create(); */ //string ErrStr; //EE = llvm::EngineBuilder(std::move(M)).setErrorStr(&ErrStr).setMCPU("i386").create(); //context = llvm::getGlobalContext(); int32_t index = 0; Blocks = new HostBlock[1000]; int32_t block_index = 0; while(index < total_inst) { Blocks[block_index].pc = index; ostringstream f_name; f_name << "func"; f_name << block_index; //string f_name = string("func");// + string(index); /* llvm::Function *func = llvm::cast<llvm::Function>(MyModule->getOrInsertFunction(f_name.str(), llvm::Type::getVoidTy(context), (llvm::Type *)0)); */ // Make the function type: double(double,double) etc. //std::vector<Type*> Doubles(Args.size(), // Type::getDoubleTy(getGlobalContext())); llvm::FunctionType *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(context), false); llvm::Function *func = llvm::Function::Create(FT, llvm::Function::ExternalLinkage, f_name.str(), MyModule); //Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M); // Add a basic block to the function. As before, it automatically inserts // because of the last argument. llvm::BasicBlock *BB = llvm::BasicBlock::Create(context, "EntryBlock", func); // Create a basic block builder with default parameters. The builder will // automatically append instructions to the basic block `BB'. IRB.SetInsertPoint(BB); llvm::Function *sim_func; CInst *inst; int b_size = 0; do { inst = &instructions[index]; llvm::Value *dst = IRB.getInt32(inst->dst_reg); llvm::Value *src0 = IRB.getInt32(inst->src0_reg); llvm::Value *src1 = IRB.getInt32(inst->src1_reg); sim_func = OpFunctionMap[inst->opcode]; llvm::Value *oprnds[] = {dst, src0, src1}; llvm::ArrayRef <llvm::Value *> ref(oprnds, 3); IRB.CreateCall(sim_func, ref); index++; b_size++; //cout << "Index " << index << endl; //} while(b_size < 10 || inst->opcode != OPCODE::JMP); } while(b_size < 10 && index < total_inst); IRB.CreateRetVoid(); passManager->run(*MyModule); fPassManager->run(*func); EE->finalizeObject(); //std::vector<llvm::GenericValue> noargs; //std::cout << "calling " << f_name.str() << endl; //EE->runFunction(func, noargs); //Blocks[block_index].func_ptr = reinterpret_cast<HostFunction>(EE->getPointerToFunction(func)); Blocks[block_index].func_ptr = (void *)(EE->getPointerToFunction(func)); //Blocks[block_index].func_ptr = reinterpret_cast<decltype(HostFunction())>(EE->getPointerToNamedFunction(f_name.str())); //(Blocks[block_index].func_ptr)(); block_index++; //cout << "BlockIndex " << block_index << endl; } total_blocks = block_index; MyModule->dump(); /* cout << "calling add32" << endl; void (*add32)(int32_t, int32_t, int32_t) = reinterpret_cast<void (*)(int32_t, int32_t, int32_t)>(EE->getFunctionAddress("add32")); add32(0, 2, 3); */ /* void (*func0)() = reinterpret_cast<void (*)()>(EE->getFunctionAddress("func1")); func0(); */ /* struct timeval tp; gettimeofday(&tp, NULL); long int start = tp.tv_sec * 1000 + tp.tv_usec / 1000; for (int32_t i = 0; i < 100; i++) { for (int32_t j = 0 ; j < total_blocks; j++) { ((HostFunction)(Blocks[j].func_ptr))(); } } gettimeofday(&tp, NULL); long int end = tp.tv_sec * 1000 + tp.tv_usec / 1000; cout << "Time = " << end - start << endl; */ }
bool TargetInfo::doInitialization(llvm::Module &m) { dl = &m.getDataLayout(); return ImmutablePass::doInitialization(m); }
void ClangInternalState::printLLVMModule(llvm::raw_ostream& Out, llvm::Module& M) { M.print(Out, /*AssemblyAnnotationWriter*/ 0); }
void Prepare::deleteAsmBodies(llvm::Module &M) { static const char *toDelete[] = { "atomic_inc", "atomic_dec", "atomic_add", "atomic_sub", "atomic_dec_and_test", "atomic_add_return", "atomic64_inc", "atomic64_dec", "atomic64_add", "atomic64_sub", "local_inc", "local_dec", "__fswab16", "__fswab32", "__fswab64", "__xchg", "__cmpxchg", "__set_bit", "__clear_bit", "set_bit", "clear_bit", "variable_test_bit", "__test_and_set_bit", "__test_and_clear_bit", "test_and_set_bit", "test_and_clear_bit", "__fls", "fls", "__ffs", "ffs", "ffz", "inb", "inw", "inl", "insb", "insw", "insl", "inb_p", "inw_p", "inl_p", "readb", "readw", "readl", "readq", "__readb", "__readw", "__readl", "__readq", "___arch__swab32", "___arch__swab64" // generates false positives in stats }; static const char *_makeNop[] = { "pagefault_disable", "__raw_local_save_flags", "raw_local_irq_restore", "raw_local_irq_enable", "raw_local_irq_disable", "__raw_spin_is_contended", "local_bh_enable", "local_bh_disable", "schedule", "schedule_timeout", "schedule_timeout_interruptible", "schedule_timeout_uninterruptible", "preempt_schedule", "msleep", "msleep_interruptible", "__udelay", "__const_udelay", "printk_ratelimit", "warn_slowpath", "warn_on_slowpath", "dump_stack", "printk", "vprintk", "snd_verbose_printk", "rep_nop", "outb", "outw", "outl", "outsb", "outsw", "outsl", "outb_p", "outw_p", "outl_p", "writeb", "writew", "writel", "writeq", "__writeb", "__writew", "__writel", "__writeq", "mod_timer", "__mod_timer", "del_timer", "del_timer_sync", "complete", "wait_for_completion", "interruptible_sleep_on", "add_wait_queue", "remove_wait_queue", "prepare_to_wait", "finish_wait", "__tasklet_schedule", "queue_work", "schedule_work", "flush_scheduled_work", "schedule_delayed_work", "__wake_up", "wake_up_process", "wake_up_state", "kill_fasync" }; unsigned int i; for (i = 0; i < ARRAY_SIZE(toDelete); i++) { Function *F = M.getFunction(toDelete[i]); if (F) F->deleteBody(); } for (i = 0; i < ARRAY_SIZE(_makeNop); i++) { Function *F = M.getFunction(_makeNop[i]); if (F) makeNop(F); } }
void CloneSubModule(llvm::Module &Dst, const Module &Src, HandleGlobalVariableFtor HandleGlobalVariable, HandleFunctionFtor HandleFunction, bool CloneInlineAsm) { ValueToValueMapTy VMap; if (CloneInlineAsm) Dst.appendModuleInlineAsm(Src.getModuleInlineAsm()); // Copy global variables (but not initializers, yet). for (Module::const_global_iterator I = Src.global_begin(), E = Src.global_end(); I != E; ++I) { GlobalVariable *GV = new GlobalVariable( Dst, I->getType()->getElementType(), I->isConstant(), I->getLinkage(), (Constant *)nullptr, I->getName(), (GlobalVariable *)nullptr, I->getThreadLocalMode(), I->getType()->getAddressSpace()); GV->copyAttributesFrom(I); VMap[I] = GV; } // Loop over the functions in the module, making external functions as before for (Module::const_iterator I = Src.begin(), E = Src.end(); I != E; ++I) { Function *NF = Function::Create(cast<FunctionType>(I->getType()->getElementType()), I->getLinkage(), I->getName(), &Dst); NF->copyAttributesFrom(I); VMap[I] = NF; } // Loop over the aliases in the module for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end(); I != E; ++I) { auto *PTy = cast<PointerType>(I->getType()); auto *GA = GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), I->getLinkage(), I->getName(), &Dst); GA->copyAttributesFrom(I); VMap[I] = GA; } // Now that all of the things that global variable initializer can refer to // have been created, loop through and copy the global variable referrers // over... We also set the attributes on the global now. for (Module::const_global_iterator I = Src.global_begin(), E = Src.global_end(); I != E; ++I) { GlobalVariable &GV = *cast<GlobalVariable>(VMap[I]); HandleGlobalVariable(GV, *I, VMap); } // Similarly, copy over function bodies now... // for (Module::const_iterator I = Src.begin(), E = Src.end(); I != E; ++I) { Function &F = *cast<Function>(VMap[I]); HandleFunction(F, *I, VMap); } // And aliases for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end(); I != E; ++I) { GlobalAlias *GA = cast<GlobalAlias>(VMap[I]); if (const Constant *C = I->getAliasee()) GA->setAliasee(MapValue(C, VMap)); } // And named metadata.... for (Module::const_named_metadata_iterator I = Src.named_metadata_begin(), E = Src.named_metadata_end(); I != E; ++I) { const NamedMDNode &NMD = *I; NamedMDNode *NewNMD = Dst.getOrInsertNamedMetadata(NMD.getName()); for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap)); } }