1
1
#include " ../include/KaleidoscopeJIT.h"
2
2
#include " llvm/ADT/APFloat.h"
3
3
#include " llvm/ADT/STLExtras.h"
4
+ #include " llvm/Analysis/AssumptionCache.h"
5
+ #include " llvm/Analysis/BasicAliasAnalysis.h"
6
+ #include " llvm/Analysis/MemoryDependenceAnalysis.h"
7
+ #include " llvm/Analysis/MemorySSA.h"
8
+ #include " llvm/Analysis/OptimizationRemarkEmitter.h"
9
+ #include " llvm/Analysis/ProfileSummaryInfo.h"
10
+ #include " llvm/Analysis/TargetTransformInfo.h"
4
11
#include " llvm/IR/BasicBlock.h"
5
12
#include " llvm/IR/Constants.h"
6
13
#include " llvm/IR/DerivedTypes.h"
7
14
#include " llvm/IR/Function.h"
8
15
#include " llvm/IR/IRBuilder.h"
9
16
#include " llvm/IR/LLVMContext.h"
10
- #include " llvm/IR/LegacyPassManager.h"
11
17
#include " llvm/IR/Module.h"
18
+ #include " llvm/IR/PassManager.h"
12
19
#include " llvm/IR/Type.h"
13
20
#include " llvm/IR/Verifier.h"
21
+ #include " llvm/Passes/PassBuilder.h"
22
+ #include " llvm/Passes/StandardInstrumentations.h"
14
23
#include " llvm/Support/TargetSelect.h"
15
24
#include " llvm/Target/TargetMachine.h"
16
25
#include " llvm/Transforms/InstCombine/InstCombine.h"
17
26
#include " llvm/Transforms/Scalar.h"
18
27
#include " llvm/Transforms/Scalar/GVN.h"
28
+ #include " llvm/Transforms/Scalar/Reassociate.h"
29
+ #include " llvm/Transforms/Scalar/SimplifyCFG.h"
19
30
#include < algorithm>
20
31
#include < cassert>
21
32
#include < cctype>
@@ -413,8 +424,12 @@ static std::unique_ptr<LLVMContext> TheContext;
413
424
static std::unique_ptr<Module> TheModule;
414
425
static std::unique_ptr<IRBuilder<>> Builder;
415
426
static std::map<std::string, Value *> NamedValues;
416
- static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
417
427
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
428
+ static std::unique_ptr<FunctionPassManager> TheFPM;
429
+ static std::unique_ptr<FunctionAnalysisManager> TheFAM;
430
+ static std::unique_ptr<ModuleAnalysisManager> TheMAM;
431
+ static std::unique_ptr<PassInstrumentationCallbacks> ThePIC;
432
+ static std::unique_ptr<StandardInstrumentations> TheSI;
418
433
static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
419
434
static ExitOnError ExitOnErr;
420
435
@@ -535,7 +550,7 @@ Function *FunctionAST::codegen() {
535
550
verifyFunction (*TheFunction);
536
551
537
552
// Run the optimizer on the function.
538
- TheFPM->run (*TheFunction);
553
+ TheFPM->run (*TheFunction, *TheFAM );
539
554
540
555
return TheFunction;
541
556
}
@@ -549,28 +564,51 @@ Function *FunctionAST::codegen() {
549
564
// Top-Level parsing and JIT Driver
550
565
// ===----------------------------------------------------------------------===//
551
566
552
- static void InitializeModuleAndPassManager () {
567
+ static void InitializeModuleAndManagers () {
553
568
// Open a new context and module.
554
569
TheContext = std::make_unique<LLVMContext>();
555
- TheModule = std::make_unique<Module>(" my cool jit " , *TheContext);
570
+ TheModule = std::make_unique<Module>(" KaleidoscopeJIT " , *TheContext);
556
571
TheModule->setDataLayout (TheJIT->getDataLayout ());
557
572
558
573
// Create a new builder for the module.
559
574
Builder = std::make_unique<IRBuilder<>>(*TheContext);
560
575
561
- // Create a new pass manager attached to it.
562
- TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get ());
576
+ // Create new pass and analysis managers.
577
+ TheFPM = std::make_unique<FunctionPassManager>();
578
+ TheFAM = std::make_unique<FunctionAnalysisManager>();
579
+ TheMAM = std::make_unique<ModuleAnalysisManager>();
580
+ ThePIC = std::make_unique<PassInstrumentationCallbacks>();
581
+ TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
582
+ /* DebugLogging*/ true );
583
+ TheSI->registerCallbacks (*ThePIC, TheMAM.get ());
563
584
585
+ // Add transform passes.
564
586
// Do simple "peephole" optimizations and bit-twiddling optzns.
565
- TheFPM->add ( createInstructionCombiningPass ());
587
+ TheFPM->addPass ( InstCombinePass ());
566
588
// Reassociate expressions.
567
- TheFPM->add ( createReassociatePass ());
589
+ TheFPM->addPass ( ReassociatePass ());
568
590
// Eliminate Common SubExpressions.
569
- TheFPM->add ( createGVNPass ());
591
+ TheFPM->addPass ( GVNPass ());
570
592
// Simplify the control flow graph (deleting unreachable blocks, etc).
571
- TheFPM->add (createCFGSimplificationPass ());
572
-
573
- TheFPM->doInitialization ();
593
+ TheFPM->addPass (SimplifyCFGPass ());
594
+
595
+ // Register analysis passes used in these transform passes.
596
+ TheFAM->registerPass ([&] { return AAManager (); });
597
+ TheFAM->registerPass ([&] { return AssumptionAnalysis (); });
598
+ TheFAM->registerPass ([&] { return DominatorTreeAnalysis (); });
599
+ TheFAM->registerPass ([&] { return LoopAnalysis (); });
600
+ TheFAM->registerPass ([&] { return MemoryDependenceAnalysis (); });
601
+ TheFAM->registerPass ([&] { return MemorySSAAnalysis (); });
602
+ TheFAM->registerPass ([&] { return OptimizationRemarkEmitterAnalysis (); });
603
+ TheFAM->registerPass ([&] {
604
+ return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
605
+ });
606
+ TheFAM->registerPass (
607
+ [&] { return PassInstrumentationAnalysis (ThePIC.get ()); });
608
+ TheFAM->registerPass ([&] { return TargetIRAnalysis (); });
609
+ TheFAM->registerPass ([&] { return TargetLibraryAnalysis (); });
610
+
611
+ TheMAM->registerPass ([&] { return ProfileSummaryAnalysis (); });
574
612
}
575
613
576
614
static void HandleDefinition () {
@@ -581,7 +619,7 @@ static void HandleDefinition() {
581
619
fprintf (stderr, " \n " );
582
620
ExitOnErr (TheJIT->addModule (
583
621
ThreadSafeModule (std::move (TheModule), std::move (TheContext))));
584
- InitializeModuleAndPassManager ();
622
+ InitializeModuleAndManagers ();
585
623
}
586
624
} else {
587
625
// Skip token for error recovery.
@@ -613,7 +651,7 @@ static void HandleTopLevelExpression() {
613
651
614
652
auto TSM = ThreadSafeModule (std::move (TheModule), std::move (TheContext));
615
653
ExitOnErr (TheJIT->addModule (std::move (TSM), RT));
616
- InitializeModuleAndPassManager ();
654
+ InitializeModuleAndManagers ();
617
655
618
656
// Search the JIT for the __anon_expr symbol.
619
657
auto ExprSymbol = ExitOnErr (TheJIT->lookup (" __anon_expr" ));
@@ -699,7 +737,7 @@ int main() {
699
737
700
738
TheJIT = ExitOnErr (KaleidoscopeJIT::Create ());
701
739
702
- InitializeModuleAndPassManager ();
740
+ InitializeModuleAndManagers ();
703
741
704
742
// Run the main "interpreter loop" now.
705
743
MainLoop ();
0 commit comments