66#include " llvm/IR/Function.h"
77#include " llvm/IR/IRBuilder.h"
88#include " llvm/IR/LLVMContext.h"
9+ #include " llvm/IR/LegacyPassManager.h"
910#include " llvm/IR/Module.h"
1011#include " llvm/IR/Type.h"
1112#include " llvm/IR/Verifier.h"
13+ #include " llvm/Support/TargetSelect.h"
14+ #include " llvm/Target/TargetMachine.h"
15+ #include " llvm/Transforms/InstCombine/InstCombine.h"
16+ #include " llvm/Transforms/Scalar.h"
17+ #include " llvm/Transforms/Scalar/GVN.h"
1218#include < iostream>
1319#include < string>
1420#include < vector>
1824#include < cstdio>
1925#include < cstdlib>
2026#include < unordered_map>
27+ #include " ./KaleidoscopeJIT.h"
2128
2229using namespace llvm ;
2330
@@ -107,9 +114,10 @@ static int gettok(void){
107114
108115
109116// State variables
110- static std::unique_ptr<IRBuilder<>> Builder; // for creating instructions, constants, etc
111- static std::unique_ptr<Module> TheModule; // to hold blocks, definitions? (TODO), TODO: why does this have to be a pointer?
112117static std::unique_ptr<LLVMContext> TheContext;
118+ static std::unique_ptr<Module> TheModule; // to hold blocks, definitions? (TODO), TODO: why does this have to be a pointer?
119+ static std::unique_ptr<IRBuilder<>> Builder; // for creating instructions, constants, etc
120+ static std::unique_ptr<legacy::FunctionPassManager> TheFPM; // Function pass manager
113121static std::unordered_map<std::string, Value *> Symbols; // Maps names inside function context to LLVM "values"
114122
115123
@@ -679,12 +687,6 @@ Value* VariableExprAST::codegen() {
679687// Generates code for function call, returns `Value` of function call
680688Value* CallExprAST::codegen () {
681689
682- // Generate code for arguments, and get their values
683- std::vector<Value *> Argvec;
684- for (const auto & arg: Args) {
685- Argvec.push_back (arg->codegen ());
686- }
687-
688690 // Obtain function with name `Callee` from Module
689691 Function *func = TheModule->getFunction (Callee);
690692 if (!func) {
@@ -699,16 +701,24 @@ Value* CallExprAST::codegen() {
699701 );
700702 }
701703
704+ // Generate code for arguments, and get their values
705+ std::vector<Value *> Argvec;
706+ for (const auto & arg: Args) {
707+ Argvec.push_back (arg->codegen ());
708+ }
709+
710+
711+
702712 // TODO: why do I need to provide TheContext to getDoubleTy?
703- std::vector<Type *> ArgTypes = std::vector<Type *>(Args.size (), Builder->getDoubleTy ());
713+ // std::vector<Type *> ArgTypes = std::vector<Type *>(Args.size(), Builder->getDoubleTy());
704714
705715 // Create type for call
706716 // TODO: this is not needed: CreateCall can be called without a type
707717 // Why is this so? Is it because the function does not take variable arguments?
708718 // FunctionType *func_type = FunctionType::get(Builder->getDoubleTy(), ArgTypes, false);
709719
710720 // Create call
711- return Builder->CreateCall (func, Argvec, Callee );
721+ return Builder->CreateCall (func, Argvec, " call " );
712722}
713723
714724Value* BinaryExprAST::codegen () {
@@ -742,6 +752,7 @@ Value* BinaryExprAST::codegen() {
742752 return Builder->CreateUIToFP (L, Builder->getDoubleTy (), " booltofp" );
743753 default :
744754 return LogErrorV (" Invalid Operator" );
755+ break ;
745756 }
746757}
747758
@@ -797,6 +808,9 @@ Function* FunctionAST::codegen() {
797808 // TODO: What if this check fails? Do I still continue?
798809 verifyFunction (*func);
799810
811+ // Run passes on function
812+ TheFPM->run (*func);
813+
800814 return func;
801815 }
802816
@@ -873,15 +887,32 @@ static void HandleTopLevelExpression() {
873887 }
874888}
875889
876- static void InitializeModule () {
890+ static void InitializeModuleAndPassManager () {
877891 TheContext = std::make_unique<LLVMContext>();
878892 TheModule = std::make_unique<Module>(" kaleidoscope" , *TheContext);
893+ // Why .get? Ahh- I want to pass a pointer. What about uniqueness?
894+ TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get ());
879895 Builder = std::make_unique<IRBuilder<>>(*TheContext);
896+
897+ // Peephole optimizations
898+ TheFPM->add (createInstructionCombiningPass ());
899+
900+ // ?
901+ TheFPM->add (createReassociatePass ());
902+
903+ // Global value numbering-> common subexpression elimination. Global is actually per-function
904+ TheFPM->add (createGVNPass ());
905+
906+ // Dead code elimination pass;
907+ TheFPM->add (createCFGSimplificationPass ());
908+
909+ // Run initalizers for all passes added to pass manager
910+ TheFPM->doInitialization ();
880911}
881912
882913// top = definition | expression | external | ;
883914static void MainLoop () {
884- while (1 ) {
915+ while (true ) {
885916 fprintf (stderr, " ready>" );
886917 switch (CurTok) {
887918 case tok_eof:
@@ -940,13 +971,13 @@ int main(void) {
940971 BinopPrecedence[' *' ] = 40 ;
941972 BinopPrecedence[' /' ] = 40 ;
942973
943- #if IRGEN
944- InitializeModule ();
945- #endif
946-
947974 fprintf (stderr, " ready>" );
948975 getNextToken ();
949976
977+ #if IRGEN
978+ InitializeModuleAndPassManager ();
979+ #endif
980+
950981 MainLoop ();
951982 // oldmain();
952983
0 commit comments