今天找了一天的在LLVM中如何进行代码优化的方法。
一开始在谷歌搜索LLVM JIT
,看到了LLVM的官方教程:
1. Building a JIT: Starting out with KaleidoscopeJIT
1.构建JIT:从KaleidoscopeJIT开始
然后我看得头都要大了,而且仍然不知道改怎么进行中间代码优化。
我看到了另一篇博客:编译器架构的王者LLVM——(12)使用JIT引擎,写得不错,但帮助不大。
随后我看了stackoverflow上的 一个提问:Call LLVM Jit from c program
里面的问题是如何在LLVM中读取bc文件,也就是LLVM 中间代码(LLVM ir bitcode),然后进行运行。
示例代码如下:
#include <string>
#include <memory>
#include <iostream>
#include <llvm/LLVMContext.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ModuleProvider.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/JIT.h>
using namespace std;
using namespace llvm;
int main()
{
InitializeNativeTarget();
llvm_start_multithreaded();
LLVMContext context;
string error;
Module *m = ParseBitcodeFile(MemoryBuffer::getFile("tst.bc"), context, &error);
ExecutionEngine *ee = ExecutionEngine::create(m);