12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- //LLVM includes
- #include "llvm/Analysis/Passes.h"
- // Local includes
- #include "JIT.h"
- using namespace ast;
- using namespace llvm;
- using namespace llvm::orc;
- namespace jit{
- std::unique_ptr<llvm::legacy::FunctionPassManager> JITObjects::TheFPM =
- std::make_unique<llvm::legacy::FunctionPassManager>(AstObjects::TheModule.get());
- std::unique_ptr<llvm::orc::KaleidoscopeJIT> JITObjects::TheJIT =
- std::unique_ptr<llvm::orc::KaleidoscopeJIT>(nullptr);
- std::map<std::string, std::unique_ptr<ast::PrototypeAST>> JITObjects::FunctionProtos{};
- void InitializeModuleAndPassManager(void) {
- // Open a new module.
- AstObjects::TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext());
- AstObjects::TheModule->setDataLayout(JITObjects::TheJIT->getTargetMachine().createDataLayout());
- // Create a new pass manager attached to it.
- JITObjects::TheFPM = llvm::make_unique<legacy::FunctionPassManager>(AstObjects::TheModule.get());
- // HACK:: removed because not present in the full code listing and because of
- // errors in linking phase (libLLVMTransformUtils)
- // Promote allocas to registers.
- JITObjects::TheFPM->add(createPromoteMemoryToRegisterPass());
- // Do simple "peephole" optimizations and bit-twiddling optzns.
- JITObjects::TheFPM->add(createInstructionCombiningPass());
- // Reassociate expressions.
- JITObjects::TheFPM->add(createReassociatePass());
- // Eliminate Common SubExpressions.
- JITObjects::TheFPM->add(createGVNPass());
- // Simplify the control flow graph (deleting unreachable blocks, etc).
- JITObjects::TheFPM->add(createCFGSimplificationPass());
- JITObjects::TheFPM->doInitialization();
- }
- Function *getFunction(std::string Name) {
- // First, see if the function has already been added to the current module.
- if (auto *F = AstObjects::TheModule->getFunction(Name))
- return F;
- // If not, check whether we can codegen the declaration from some existing
- // prototype.
- auto FI = JITObjects::FunctionProtos.find(Name);
- if (FI != JITObjects::FunctionProtos.end())
- return FI->second->codegen();
- // If no existing prototype exists, return null.
- return nullptr;
- }
- }
|