-//===----------------------------------------------------------------------===//
-// Helper class for JIT execution engine
-//===----------------------------------------------------------------------===//
-
-class JITHelper : public BaseHelper {
-public:
- JITHelper(LLVMContext &Context) {
- // Make the module, which holds all the code.
- if (!InputIR.empty()) {
- TheModule = parseInputIR(InputIR, Context);
- } else {
- TheModule = new Module("my cool jit", Context);
- }
-
- // Create the JIT. This takes ownership of the module.
- std::string ErrStr;
- TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
- if (!TheExecutionEngine) {
- fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
- exit(1);
- }
-
- TheFPM = new FunctionPassManager(TheModule);
-
- // Set up the optimizer pipeline. Start with registering info about how the
- // target lays out data structures.
- TheFPM->add(new DataLayout(*TheExecutionEngine->getDataLayout()));
- // Provide basic AliasAnalysis support for GVN.
- TheFPM->add(createBasicAliasAnalysisPass());
- // Promote allocas to registers.
- TheFPM->add(createPromoteMemoryToRegisterPass());
- // Do simple "peephole" optimizations and bit-twiddling optzns.
- TheFPM->add(createInstructionCombiningPass());
- // Reassociate expressions.
- TheFPM->add(createReassociatePass());
- // Eliminate Common SubExpressions.
- TheFPM->add(createGVNPass());
- // Simplify the control flow graph (deleting unreachable blocks, etc).
- TheFPM->add(createCFGSimplificationPass());
-
- TheFPM->doInitialization();
- }
-
- virtual ~JITHelper() {
- if (TheFPM)
- delete TheFPM;
- if (TheExecutionEngine)
- delete TheExecutionEngine;
- }
-
- virtual Function *getFunction(const std::string FnName) {
- assert(TheModule);
- return TheModule->getFunction(FnName);
- }
-
- virtual Module *getModuleForNewFunction() {
- assert(TheModule);
- return TheModule;
- }
-
- virtual void *getPointerToFunction(Function* F) {
- assert(TheExecutionEngine);
- return TheExecutionEngine->getPointerToFunction(F);
- }
-
- virtual void *getPointerToNamedFunction(const std::string &Name) {
- return TheExecutionEngine->getPointerToNamedFunction(Name);
- }
-
- virtual void runFPM(Function &F) {
- assert(TheFPM);
- TheFPM->run(F);
- }
-
- virtual void closeCurrentModule() {
- // This should never be called for JIT
- assert(false);
- }
-
- virtual void dump() {
- assert(TheModule);
- TheModule->dump();
- }
-
-private:
- Module *TheModule;
- ExecutionEngine *TheExecutionEngine;
- FunctionPassManager *TheFPM;
-};
-