llvm.org GIT mirror llvm / 8be7707
Remove every uses of getGlobalContext() in LLVM (but the C API) At the same time, fixes InstructionsTest::CastInst unittest: yes you can leave the IR in an invalid state and exit when you don't destroy the context (like the global one), no longer now. This is the first part of http://reviews.llvm.org/D19094 From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266379 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
80 changed file(s) with 1111 addition(s) and 1082 deletion(s). Raw diff Collapse all Expand all
433433 [llvm::LLVMContext::~LLVMContext]. *)
434434 val dispose_context : llcontext -> unit
435435
436 (** See the function [llvm::getGlobalContext]. *)
436 (** See the function [LLVMGetGlobalContext]. *)
437437 val global_context : unit -> llcontext
438438
439439 (** [mdkind_id context name] returns the MDKind ID that corresponds to the
24182418 are adding new entities to LLVM IR, please try to maintain this interface
24192419 design.
24202420
2421 For clients that do *not* require the benefits of isolation, LLVM provides a
2422 convenience API ``getGlobalContext()``. This returns a global, lazily
2423 initialized ``LLVMContext`` that may be used in situations where isolation is
2424 not a concern.
2425
24262421 .. _jitthreading:
24272422
24282423 Threads and the JIT
3636 LLVMContext::discardValueNames()) that can be set to discard Value names
3737 (other than GlobalValue). This is intended to be used in release builds by
3838 clients that are interested in saving CPU/memory as much as possible.
39
40 * There is no longer a "global context" available in LLVM, except for the C API.
3941
4042 * .. note about autoconf build having been removed.
4143
7373 .. code-block:: c++
7474
7575 static std::unique_ptr *TheModule;
76 static IRBuilder<> Builder(getGlobalContext());
76 static IRBuilder<> Builder(LLVMContext);
7777 static std::map NamedValues;
7878
7979 Value *LogErrorV(const char *Str) {
115115 .. code-block:: c++
116116
117117 Value *NumberExprAST::codegen() {
118 return ConstantFP::get(getGlobalContext(), APFloat(Val));
118 return ConstantFP::get(LLVMContext, APFloat(Val));
119119 }
120120
121121 In the LLVM IR, numeric constants are represented with the
164164 case '<':
165165 L = Builder.CreateFCmpULT(L, R, "cmptmp");
166166 // Convert bool 0/1 to double 0.0 or 1.0
167 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
167 return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext),
168168 "booltmp");
169169 default:
170170 return LogErrorV("invalid binary operator");
263263 Function *PrototypeAST::codegen() {
264264 // Make the function type: double(double,double) etc.
265265 std::vector Doubles(Args.size(),
266 Type::getDoubleTy(getGlobalContext()));
266 Type::getDoubleTy(LLVMContext));
267267 FunctionType *FT =
268 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
268 FunctionType::get(Type::getDoubleTy(LLVMContext), Doubles, false);
269269
270270 Function *F =
271271 Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
339339 .. code-block:: c++
340340
341341 // Create a new basic block to start insertion into.
342 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
342 BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction);
343343 Builder.SetInsertPoint(BB);
344344
345345 // Record the function arguments in the NamedValues map.
130130
131131 void InitializeModuleAndPassManager(void) {
132132 // Open a new module.
133 TheModule = llvm::make_unique("my cool jit", getGlobalContext());
133 Context LLVMContext;
134 TheModule = llvm::make_unique("my cool jit", LLVMContext);
134135 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
135136
136137 // Create a new pass manager attached to it.
291291
292292 // Convert condition to a bool by comparing equal to 0.0.
293293 CondV = Builder.CreateFCmpONE(
294 CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
294 CondV, ConstantFP::get(LLVMContext, APFloat(0.0)), "ifcond");
295295
296296 This code is straightforward and similar to what we saw before. We emit
297297 the expression for the condition, then compare that value to zero to get
304304 // Create blocks for the then and else cases. Insert the 'then' block at the
305305 // end of the function.
306306 BasicBlock *ThenBB =
307 BasicBlock::Create(getGlobalContext(), "then", TheFunction);
308 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
309 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
307 BasicBlock::Create(LLVMContext, "then", TheFunction);
308 BasicBlock *ElseBB = BasicBlock::Create(LLVMContext, "else");
309 BasicBlock *MergeBB = BasicBlock::Create(LLVMContext, "ifcont");
310310
311311 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
312312
399399 TheFunction->getBasicBlockList().push_back(MergeBB);
400400 Builder.SetInsertPoint(MergeBB);
401401 PHINode *PN =
402 Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
402 Builder.CreatePHI(Type::getDoubleTy(LLVMContext), 2, "iftmp");
403403
404404 PN->addIncoming(ThenV, ThenBB);
405405 PN->addIncoming(ElseV, ElseBB);
624624 Function *TheFunction = Builder.GetInsertBlock()->getParent();
625625 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
626626 BasicBlock *LoopBB =
627 BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
627 BasicBlock::Create(LLVMContext, "loop", TheFunction);
628628
629629 // Insert an explicit fall through from the current block to the LoopBB.
630630 Builder.CreateBr(LoopBB);
641641 Builder.SetInsertPoint(LoopBB);
642642
643643 // Start the PHI node with an entry for Start.
644 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
644 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(LLVMContext),
645645 2, VarName.c_str());
646646 Variable->addIncoming(StartVal, PreheaderBB);
647647
692692 return nullptr;
693693 } else {
694694 // If not specified, use 1.0.
695 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
695 StepVal = ConstantFP::get(LLVMContext, APFloat(1.0));
696696 }
697697
698698 Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
711711
712712 // Convert condition to a bool by comparing equal to 0.0.
713713 EndCond = Builder.CreateFCmpONE(
714 EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
714 EndCond, ConstantFP::get(LLVMContext, APFloat(0.0)), "loopcond");
715715
716716 Finally, we evaluate the exit value of the loop, to determine whether
717717 the loop should exit. This mirrors the condition evaluation for the
722722 // Create the "after loop" block and insert it.
723723 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
724724 BasicBlock *AfterBB =
725 BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
725 BasicBlock::Create(LLVMContext, "afterloop", TheFunction);
726726
727727 // Insert the conditional branch into the end of LoopEndBB.
728728 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
750750 NamedValues.erase(VarName);
751751
752752 // for expr always returns 0.0.
753 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
753 return Constant::getNullValue(Type::getDoubleTy(LLVMContext));
754754 }
755755
756756 The final code handles various cleanups: now that we have the "NextVar"
250250 case '<':
251251 L = Builder.CreateFCmpULT(L, R, "cmptmp");
252252 // Convert bool 0/1 to double 0.0 or 1.0
253 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
253 return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext),
254254 "booltmp");
255255 default:
256256 break;
287287 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
288288
289289 // Create a new basic block to start insertion into.
290 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
290 BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction);
291291 Builder.SetInsertPoint(BB);
292292
293293 if (Value *RetVal = Body->codegen()) {
338338 const std::string &VarName) {
339339 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
340340 TheFunction->getEntryBlock().begin());
341 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
341 return TmpB.CreateAlloca(Type::getDoubleTy(LLVMContext), 0,
342342 VarName.c_str());
343343 }
344344
811811 if (!InitVal)
812812 return nullptr;
813813 } else { // If not specified, use 0.0.
814 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
814 InitVal = ConstantFP::get(LLVMContext, APFloat(0.0));
815815 }
816816
817817 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
8787 int main(int argc, char **argv) {
8888 cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
8989
90 LLVMContext &Context = getGlobalContext();
90 LLVMContext Context;
9191
9292 if (InputFilename == "") {
9393 errs() << "Error: You must specify the filename of the program to "
19501950
19511951 llvm::InitializeNativeTarget();
19521952 llvm::InitializeNativeTargetAsmPrinter();
1953 llvm::LLVMContext &context = llvm::getGlobalContext();
1954 llvm::IRBuilder<> theBuilder(context);
1953 llvm::LLVMContext Context;
1954 llvm::IRBuilder<> theBuilder(Context);
19551955
19561956 // Make the module, which holds all the code.
19571957 std::unique_ptr Owner =
1958 llvm::make_unique("my cool jit", context);
1958 llvm::make_unique("my cool jit", Context);
19591959 llvm::Module *module = Owner.get();
19601960
19611961 std::unique_ptr MemMgr(new llvm::SectionMemoryManager());
380380 //===----------------------------------------------------------------------===//
381381
382382 static std::unique_ptr TheModule;
383 static IRBuilder<> Builder(getGlobalContext());
383 static LLVMContext TheContext;
384 static IRBuilder<> Builder(TheContext);
384385 static std::map NamedValues;
385386
386387 Value *LogErrorV(const char *Str) {
389390 }
390391
391392 Value *NumberExprAST::codegen() {
392 return ConstantFP::get(getGlobalContext(), APFloat(Val));
393 return ConstantFP::get(TheContext, APFloat(Val));
393394 }
394395
395396 Value *VariableExprAST::codegen() {
416417 case '<':
417418 L = Builder.CreateFCmpULT(L, R, "cmptmp");
418419 // Convert bool 0/1 to double 0.0 or 1.0
419 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
420 "booltmp");
420 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
421421 default:
422422 return LogErrorV("invalid binary operator");
423423 }
445445
446446 Function *PrototypeAST::codegen() {
447447 // Make the function type: double(double,double) etc.
448 std::vector Doubles(Args.size(),
449 Type::getDoubleTy(getGlobalContext()));
448 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
450449 FunctionType *FT =
451 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
450 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
452451
453452 Function *F =
454453 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
472471 return nullptr;
473472
474473 // Create a new basic block to start insertion into.
475 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
474 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
476475 Builder.SetInsertPoint(BB);
477476
478477 // Record the function arguments in the NamedValues map.
576575 getNextToken();
577576
578577 // Make the module, which holds all the code.
579 TheModule = llvm::make_unique("my cool jit", getGlobalContext());
578 TheModule = llvm::make_unique("my cool jit", TheContext);
580579
581580 // Run the main "interpreter loop" now.
582581 MainLoop();
387387 //===----------------------------------------------------------------------===//
388388
389389 static std::unique_ptr TheModule;
390 static IRBuilder<> Builder(getGlobalContext());
390 static LLVMContext TheContext;
391 static IRBuilder<> Builder(TheContext);
391392 static std::map NamedValues;
392393 static std::unique_ptr TheFPM;
393394 static std::unique_ptr TheJIT;
414415 }
415416
416417 Value *NumberExprAST::codegen() {
417 return ConstantFP::get(getGlobalContext(), APFloat(Val));
418 return ConstantFP::get(TheContext, APFloat(Val));
418419 }
419420
420421 Value *VariableExprAST::codegen() {
441442 case '<':
442443 L = Builder.CreateFCmpULT(L, R, "cmptmp");
443444 // Convert bool 0/1 to double 0.0 or 1.0
444 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
445 "booltmp");
445 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
446446 default:
447447 return LogErrorV("invalid binary operator");
448448 }
470470
471471 Function *PrototypeAST::codegen() {
472472 // Make the function type: double(double,double) etc.
473 std::vector Doubles(Args.size(),
474 Type::getDoubleTy(getGlobalContext()));
473 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
475474 FunctionType *FT =
476 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
475 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
477476
478477 Function *F =
479478 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
496495 return nullptr;
497496
498497 // Create a new basic block to start insertion into.
499 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
498 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
500499 Builder.SetInsertPoint(BB);
501500
502501 // Record the function arguments in the NamedValues map.
528527
529528 static void InitializeModuleAndPassManager() {
530529 // Open a new module.
531 TheModule = llvm::make_unique("my cool jit", getGlobalContext());
530 TheModule = llvm::make_unique("my cool jit", TheContext);
532531 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
533532
534533 // Create a new pass manager attached to it.
511511 //===----------------------------------------------------------------------===//
512512
513513 static std::unique_ptr TheModule;
514 static IRBuilder<> Builder(getGlobalContext());
514 static LLVMContext TheContext;
515 static IRBuilder<> Builder(TheContext);
515516 static std::map NamedValues;
516517 static std::unique_ptr TheFPM;
517518 static std::unique_ptr TheJIT;
538539 }
539540
540541 Value *NumberExprAST::codegen() {
541 return ConstantFP::get(getGlobalContext(), APFloat(Val));
542 return ConstantFP::get(TheContext, APFloat(Val));
542543 }
543544
544545 Value *VariableExprAST::codegen() {
565566 case '<':
566567 L = Builder.CreateFCmpULT(L, R, "cmptmp");
567568 // Convert bool 0/1 to double 0.0 or 1.0
568 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
569 "booltmp");
569 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
570570 default:
571571 return LogErrorV("invalid binary operator");
572572 }
599599
600600 // Convert condition to a bool by comparing equal to 0.0.
601601 CondV = Builder.CreateFCmpONE(
602 CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
602 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
603603
604604 Function *TheFunction = Builder.GetInsertBlock()->getParent();
605605
606606 // Create blocks for the then and else cases. Insert the 'then' block at the
607607 // end of the function.
608 BasicBlock *ThenBB =
609 BasicBlock::Create(getGlobalContext(), "then", TheFunction);
610 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
611 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
608 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
609 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
610 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
612611
613612 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
614613
638637 // Emit merge block.
639638 TheFunction->getBasicBlockList().push_back(MergeBB);
640639 Builder.SetInsertPoint(MergeBB);
641 PHINode *PN =
642 Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
640 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
643641
644642 PN->addIncoming(ThenV, ThenBB);
645643 PN->addIncoming(ElseV, ElseBB);
671669 // block.
672670 Function *TheFunction = Builder.GetInsertBlock()->getParent();
673671 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
674 BasicBlock *LoopBB =
675 BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
672 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
676673
677674 // Insert an explicit fall through from the current block to the LoopBB.
678675 Builder.CreateBr(LoopBB);
681678 Builder.SetInsertPoint(LoopBB);
682679
683680 // Start the PHI node with an entry for Start.
684 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
685 2, VarName.c_str());
681 PHINode *Variable =
682 Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, VarName.c_str());
686683 Variable->addIncoming(StartVal, PreheaderBB);
687684
688685 // Within the loop, the variable is defined equal to the PHI node. If it
704701 return nullptr;
705702 } else {
706703 // If not specified, use 1.0.
707 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
704 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
708705 }
709706
710707 Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
716713
717714 // Convert condition to a bool by comparing equal to 0.0.
718715 EndCond = Builder.CreateFCmpONE(
719 EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
716 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
720717
721718 // Create the "after loop" block and insert it.
722719 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
723720 BasicBlock *AfterBB =
724 BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
721 BasicBlock::Create(TheContext, "afterloop", TheFunction);
725722
726723 // Insert the conditional branch into the end of LoopEndBB.
727724 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
739736 NamedValues.erase(VarName);
740737
741738 // for expr always returns 0.0.
742 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
739 return Constant::getNullValue(Type::getDoubleTy(TheContext));
743740 }
744741
745742 Function *PrototypeAST::codegen() {
746743 // Make the function type: double(double,double) etc.
747 std::vector Doubles(Args.size(),
748 Type::getDoubleTy(getGlobalContext()));
744 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
749745 FunctionType *FT =
750 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
746 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
751747
752748 Function *F =
753749 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
770766 return nullptr;
771767
772768 // Create a new basic block to start insertion into.
773 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
769 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
774770 Builder.SetInsertPoint(BB);
775771
776772 // Record the function arguments in the NamedValues map.
802798
803799 static void InitializeModuleAndPassManager() {
804800 // Open a new module.
805 TheModule = llvm::make_unique("my cool jit", getGlobalContext());
801 TheModule = llvm::make_unique("my cool jit", TheContext);
806802 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
807803
808804 // Create a new pass manager attached to it.
602602 //===----------------------------------------------------------------------===//
603603
604604 static std::unique_ptr TheModule;
605 static IRBuilder<> Builder(getGlobalContext());
605 static LLVMContext TheContext;
606 static IRBuilder<> Builder(TheContext);
606607 static std::map NamedValues;
607608 static std::unique_ptr TheFPM;
608609 static std::unique_ptr TheJIT;
629630 }
630631
631632 Value *NumberExprAST::codegen() {
632 return ConstantFP::get(getGlobalContext(), APFloat(Val));
633 return ConstantFP::get(TheContext, APFloat(Val));
633634 }
634635
635636 Value *VariableExprAST::codegen() {
668669 case '<':
669670 L = Builder.CreateFCmpULT(L, R, "cmptmp");
670671 // Convert bool 0/1 to double 0.0 or 1.0
671 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
672 "booltmp");
672 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
673673 default:
674674 break;
675675 }
710710
711711 // Convert condition to a bool by comparing equal to 0.0.
712712 CondV = Builder.CreateFCmpONE(
713 CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
713 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
714714
715715 Function *TheFunction = Builder.GetInsertBlock()->getParent();
716716
717717 // Create blocks for the then and else cases. Insert the 'then' block at the
718718 // end of the function.
719 BasicBlock *ThenBB =
720 BasicBlock::Create(getGlobalContext(), "then", TheFunction);
721 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
722 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
719 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
720 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
721 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
723722
724723 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
725724
749748 // Emit merge block.
750749 TheFunction->getBasicBlockList().push_back(MergeBB);
751750 Builder.SetInsertPoint(MergeBB);
752 PHINode *PN =
753 Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
751 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
754752
755753 PN->addIncoming(ThenV, ThenBB);
756754 PN->addIncoming(ElseV, ElseBB);
782780 // block.
783781 Function *TheFunction = Builder.GetInsertBlock()->getParent();
784782 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
785 BasicBlock *LoopBB =
786 BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
783 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
787784
788785 // Insert an explicit fall through from the current block to the LoopBB.
789786 Builder.CreateBr(LoopBB);
792789 Builder.SetInsertPoint(LoopBB);
793790
794791 // Start the PHI node with an entry for Start.
795 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
796 2, VarName.c_str());
792 PHINode *Variable =
793 Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, VarName.c_str());
797794 Variable->addIncoming(StartVal, PreheaderBB);
798795
799796 // Within the loop, the variable is defined equal to the PHI node. If it
815812 return nullptr;
816813 } else {
817814 // If not specified, use 1.0.
818 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
815 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
819816 }
820817
821818 Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
827824
828825 // Convert condition to a bool by comparing equal to 0.0.
829826 EndCond = Builder.CreateFCmpONE(
830 EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
827 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
831828
832829 // Create the "after loop" block and insert it.
833830 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
834831 BasicBlock *AfterBB =
835 BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
832 BasicBlock::Create(TheContext, "afterloop", TheFunction);
836833
837834 // Insert the conditional branch into the end of LoopEndBB.
838835 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
850847 NamedValues.erase(VarName);
851848
852849 // for expr always returns 0.0.
853 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
850 return Constant::getNullValue(Type::getDoubleTy(TheContext));
854851 }
855852
856853 Function *PrototypeAST::codegen() {
857854 // Make the function type: double(double,double) etc.
858 std::vector Doubles(Args.size(),
859 Type::getDoubleTy(getGlobalContext()));
855 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
860856 FunctionType *FT =
861 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
857 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
862858
863859 Function *F =
864860 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
885881 BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
886882
887883 // Create a new basic block to start insertion into.
888 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
884 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
889885 Builder.SetInsertPoint(BB);
890886
891887 // Record the function arguments in the NamedValues map.
920916
921917 static void InitializeModuleAndPassManager() {
922918 // Open a new module.
923 TheModule = llvm::make_unique("my cool jit", getGlobalContext());
919 TheModule = llvm::make_unique("my cool jit", TheContext);
924920 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
925921
926922 // Create a new pass manager attached to it.
672672 //===----------------------------------------------------------------------===//
673673
674674 static std::unique_ptr TheModule;
675 static IRBuilder<> Builder(getGlobalContext());
675 static LLVMContext TheContext;
676 static IRBuilder<> Builder(TheContext);
676677 static std::map NamedValues;
677678 static std::unique_ptr TheFPM;
678679 static std::unique_ptr TheJIT;
704705 const std::string &VarName) {
705706 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
706707 TheFunction->getEntryBlock().begin());
707 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), nullptr,
708 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), nullptr,
708709 VarName.c_str());
709710 }
710711
711712 Value *NumberExprAST::codegen() {
712 return ConstantFP::get(getGlobalContext(), APFloat(Val));
713 return ConstantFP::get(TheContext, APFloat(Val));
713714 }
714715
715716 Value *VariableExprAST::codegen() {
773774 case '<':
774775 L = Builder.CreateFCmpULT(L, R, "cmptmp");
775776 // Convert bool 0/1 to double 0.0 or 1.0
776 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
777 "booltmp");
777 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
778778 default:
779779 break;
780780 }
815815
816816 // Convert condition to a bool by comparing equal to 0.0.
817817 CondV = Builder.CreateFCmpONE(
818 CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
818 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
819819
820820 Function *TheFunction = Builder.GetInsertBlock()->getParent();
821821
822822 // Create blocks for the then and else cases. Insert the 'then' block at the
823823 // end of the function.
824 BasicBlock *ThenBB =
825 BasicBlock::Create(getGlobalContext(), "then", TheFunction);
826 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
827 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
824 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
825 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
826 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
828827
829828 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
830829
854853 // Emit merge block.
855854 TheFunction->getBasicBlockList().push_back(MergeBB);
856855 Builder.SetInsertPoint(MergeBB);
857 PHINode *PN =
858 Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
856 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
859857
860858 PN->addIncoming(ThenV, ThenBB);
861859 PN->addIncoming(ElseV, ElseBB);
897895
898896 // Make the new basic block for the loop header, inserting after current
899897 // block.
900 BasicBlock *LoopBB =
901 BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
898 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
902899
903900 // Insert an explicit fall through from the current block to the LoopBB.
904901 Builder.CreateBr(LoopBB);
925922 return nullptr;
926923 } else {
927924 // If not specified, use 1.0.
928 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
925 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
929926 }
930927
931928 // Compute the end condition.
941938
942939 // Convert condition to a bool by comparing equal to 0.0.
943940 EndCond = Builder.CreateFCmpONE(
944 EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
941 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
945942
946943 // Create the "after loop" block and insert it.
947944 BasicBlock *AfterBB =
948 BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
945 BasicBlock::Create(TheContext, "afterloop", TheFunction);
949946
950947 // Insert the conditional branch into the end of LoopEndBB.
951948 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
960957 NamedValues.erase(VarName);
961958
962959 // for expr always returns 0.0.
963 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
960 return Constant::getNullValue(Type::getDoubleTy(TheContext));
964961 }
965962
966963 Value *VarExprAST::codegen() {
984981 if (!InitVal)
985982 return nullptr;
986983 } else { // If not specified, use 0.0.
987 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
984 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
988985 }
989986
990987 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
10131010
10141011 Function *PrototypeAST::codegen() {
10151012 // Make the function type: double(double,double) etc.
1016 std::vector Doubles(Args.size(),
1017 Type::getDoubleTy(getGlobalContext()));
1013 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
10181014 FunctionType *FT =
1019 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
1015 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
10201016
10211017 Function *F =
10221018 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
10431039 BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
10441040
10451041 // Create a new basic block to start insertion into.
1046 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1042 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
10471043 Builder.SetInsertPoint(BB);
10481044
10491045 // Record the function arguments in the NamedValues map.
10861082
10871083 static void InitializeModuleAndPassManager() {
10881084 // Open a new module.
1089 TheModule = llvm::make_unique("my cool jit", getGlobalContext());
1085 TheModule = llvm::make_unique("my cool jit", TheContext);
10901086 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
10911087
10921088 // Create a new pass manager attached to it.
8686 class PrototypeAST;
8787 class ExprAST;
8888 }
89 static IRBuilder<> Builder(getGlobalContext());
89 static LLVMContext TheContext;
90 static IRBuilder<> Builder(TheContext);
9091 struct DebugInfo {
9192 DICompileUnit *TheCU;
9293 DIType *DblTy;
885886 const std::string &VarName) {
886887 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
887888 TheFunction->getEntryBlock().begin());
888 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), nullptr,
889 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), nullptr,
889890 VarName.c_str());
890891 }
891892
892893 Value *NumberExprAST::codegen() {
893894 KSDbgInfo.emitLocation(this);
894 return ConstantFP::get(getGlobalContext(), APFloat(Val));
895 return ConstantFP::get(TheContext, APFloat(Val));
895896 }
896897
897898 Value *VariableExprAST::codegen() {
959960 case '<':
960961 L = Builder.CreateFCmpULT(L, R, "cmptmp");
961962 // Convert bool 0/1 to double 0.0 or 1.0
962 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
963 "booltmp");
963 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
964964 default:
965965 break;
966966 }
10051005
10061006 // Convert condition to a bool by comparing equal to 0.0.
10071007 CondV = Builder.CreateFCmpONE(
1008 CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
1008 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
10091009
10101010 Function *TheFunction = Builder.GetInsertBlock()->getParent();
10111011
10121012 // Create blocks for the then and else cases. Insert the 'then' block at the
10131013 // end of the function.
1014 BasicBlock *ThenBB =
1015 BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1016 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1017 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1014 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
1015 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
1016 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
10181017
10191018 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
10201019
10441043 // Emit merge block.
10451044 TheFunction->getBasicBlockList().push_back(MergeBB);
10461045 Builder.SetInsertPoint(MergeBB);
1047 PHINode *PN =
1048 Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
1046 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
10491047
10501048 PN->addIncoming(ThenV, ThenBB);
10511049 PN->addIncoming(ElseV, ElseBB);
10891087
10901088 // Make the new basic block for the loop header, inserting after current
10911089 // block.
1092 BasicBlock *LoopBB =
1093 BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1090 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
10941091
10951092 // Insert an explicit fall through from the current block to the LoopBB.
10961093 Builder.CreateBr(LoopBB);
11171114 return nullptr;
11181115 } else {
11191116 // If not specified, use 1.0.
1120 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1117 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
11211118 }
11221119
11231120 // Compute the end condition.
11331130
11341131 // Convert condition to a bool by comparing equal to 0.0.
11351132 EndCond = Builder.CreateFCmpONE(
1136 EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
1133 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
11371134
11381135 // Create the "after loop" block and insert it.
11391136 BasicBlock *AfterBB =
1140 BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1137 BasicBlock::Create(TheContext, "afterloop", TheFunction);
11411138
11421139 // Insert the conditional branch into the end of LoopEndBB.
11431140 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
11521149 NamedValues.erase(VarName);
11531150
11541151 // for expr always returns 0.0.
1155 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1152 return Constant::getNullValue(Type::getDoubleTy(TheContext));
11561153 }
11571154
11581155 Value *VarExprAST::codegen() {
11761173 if (!InitVal)
11771174 return nullptr;
11781175 } else { // If not specified, use 0.0.
1179 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1176 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
11801177 }
11811178
11821179 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
12071204
12081205 Function *PrototypeAST::codegen() {
12091206 // Make the function type: double(double,double) etc.
1210 std::vector Doubles(Args.size(),
1211 Type::getDoubleTy(getGlobalContext()));
1207 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
12121208 FunctionType *FT =
1213 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
1209 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
12141210
12151211 Function *F =
12161212 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
12371233 BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
12381234
12391235 // Create a new basic block to start insertion into.
1240 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1236 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
12411237 Builder.SetInsertPoint(BB);
12421238
12431239 // Create a subprogram DIE for this function.
13181314
13191315 static void InitializeModule() {
13201316 // Open a new module.
1321 TheModule = llvm::make_unique("my cool jit", getGlobalContext());
1317 TheModule = llvm::make_unique("my cool jit", TheContext);
13221318 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
13231319 }
13241320
622622
623623 static Module *TheModule;
624624 static FunctionPassManager *TheFPM;
625 static IRBuilder<> Builder(getGlobalContext());
625 static LLVMContext TheContext;
626 static IRBuilder<> Builder(TheContext);
626627 static std::map NamedValues;
627628
628629 Value *ErrorV(const char *Str) { Error(Str); return 0; }
633634 const std::string &VarName) {
634635 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
635636 TheFunction->getEntryBlock().begin());
636 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
637 VarName.c_str());
637 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str());
638638 }
639639
640640 Value *NumberExprAST::Codegen() {
641 return ConstantFP::get(getGlobalContext(), APFloat(Val));
641 return ConstantFP::get(TheContext, APFloat(Val));
642642 }
643643
644644 Value *VariableExprAST::Codegen() {
698698 case '<':
699699 L = Builder.CreateFCmpULT(L, R, "cmptmp");
700700 // Convert bool 0/1 to double 0.0 or 1.0
701 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
702 "booltmp");
701 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
703702 default: break;
704703 }
705704
739738 if (CondV == 0) return 0;
740739
741740 // Convert condition to a bool by comparing equal to 0.0.
742 CondV = Builder.CreateFCmpONE(CondV,
743 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
744 "ifcond");
745
741 CondV = Builder.CreateFCmpONE(
742 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
743
746744 Function *TheFunction = Builder.GetInsertBlock()->getParent();
747745
748746 // Create blocks for the then and else cases. Insert the 'then' block at the
749747 // end of the function.
750 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
751 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
752 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
753
748 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
749 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
750 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
751
754752 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
755753
756754 // Emit then value.
777775 // Emit merge block.
778776 TheFunction->getBasicBlockList().push_back(MergeBB);
779777 Builder.SetInsertPoint(MergeBB);
780 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
781 "iftmp");
782
778 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
779
783780 PN->addIncoming(ThenV, ThenBB);
784781 PN->addIncoming(ElseV, ElseBB);
785782 return PN;
820817
821818 // Make the new basic block for the loop header, inserting after current
822819 // block.
823 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
824
820 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
821
825822 // Insert an explicit fall through from the current block to the LoopBB.
826823 Builder.CreateBr(LoopBB);
827824
846843 if (StepVal == 0) return 0;
847844 } else {
848845 // If not specified, use 1.0.
849 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
846 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
850847 }
851848
852849 // Compute the end condition.
860857 Builder.CreateStore(NextVar, Alloca);
861858
862859 // Convert condition to a bool by comparing equal to 0.0.
863 EndCond = Builder.CreateFCmpONE(EndCond,
864 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
865 "loopcond");
866
860 EndCond = Builder.CreateFCmpONE(
861 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
862
867863 // Create the "after loop" block and insert it.
868 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
869
864 BasicBlock *AfterBB =
865 BasicBlock::Create(TheContext, "afterloop", TheFunction);
866
870867 // Insert the conditional branch into the end of LoopEndBB.
871868 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
872869
881878
882879
883880 // for expr always returns 0.0.
884 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
881 return Constant::getNullValue(Type::getDoubleTy(TheContext));
885882 }
886883
887884 Value *VarExprAST::Codegen() {
904901 InitVal = Init->Codegen();
905902 if (InitVal == 0) return 0;
906903 } else { // If not specified, use 0.0.
907 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
904 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
908905 }
909906
910907 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
932929
933930 Function *PrototypeAST::Codegen() {
934931 // Make the function type: double(double,double) etc.
935 std::vector Doubles(Args.size(),
936 Type::getDoubleTy(getGlobalContext()));
937 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
938 Doubles, false);
932 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
933 FunctionType *FT =
934 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
939935
940936 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
941937 // If F conflicted, there was already something named 'Name'. If it has a
993989 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
994990
995991 // Create a new basic block to start insertion into.
996 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
992 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
997993 Builder.SetInsertPoint(BB);
998994
999995 // Add all arguments to the symbol table and create their allocas.
11211117
11221118 Module* parseInputIR(std::string InputFile) {
11231119 SMDiagnostic Err;
1124 Module *M = ParseIRFile(InputFile, Err, getGlobalContext());
1120 Module *M = ParseIRFile(InputFile, Err, TheContext);
11251121 if (!M) {
11261122 Err.print("IR parsing failed: ", errs());
11271123 return NULL;
11361132
11371133 int main(int argc, char **argv) {
11381134 InitializeNativeTarget();
1139 LLVMContext &Context = getGlobalContext();
1135 LLVMContext &Context = TheContext;
11401136
11411137 cl::ParseCommandLineOptions(argc, argv,
11421138 "Kaleidoscope example program\n");
993993 //===----------------------------------------------------------------------===//
994994
995995 static MCJITHelper *TheHelper;
996 static IRBuilder<> Builder(getGlobalContext());
996 static LLVMContext TheContext;
997 static IRBuilder<> Builder(TheContext);
997998 static std::map NamedValues;
998999
9991000 Value *ErrorV(const char *Str) { Error(Str); return 0; }
10041005 const std::string &VarName) {
10051006 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
10061007 TheFunction->getEntryBlock().begin());
1007 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
1008 VarName.c_str());
1008 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str());
10091009 }
10101010
10111011 Value *NumberExprAST::Codegen() {
1012 return ConstantFP::get(getGlobalContext(), APFloat(Val));
1012 return ConstantFP::get(TheContext, APFloat(Val));
10131013 }
10141014
10151015 Value *VariableExprAST::Codegen() {
10651065 case '<':
10661066 L = Builder.CreateFCmpULT(L, R, "cmptmp");
10671067 // Convert bool 0/1 to double 0.0 or 1.0
1068 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
1069 "booltmp");
1068 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
10701069 default: break;
10711070 }
10721071
11031102 if (CondV == 0) return 0;
11041103
11051104 // Convert condition to a bool by comparing equal to 0.0.
1106 CondV = Builder.CreateFCmpONE(CondV,
1107 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1108 "ifcond");
1105 CondV = Builder.CreateFCmpONE(
1106 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
11091107
11101108 Function *TheFunction = Builder.GetInsertBlock()->getParent();
11111109
11121110 // Create blocks for the then and else cases. Insert the 'then' block at the
11131111 // end of the function.
1114 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1115 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1116 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1112 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
1113 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
1114 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
11171115
11181116 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
11191117
11411139 // Emit merge block.
11421140 TheFunction->getBasicBlockList().push_back(MergeBB);
11431141 Builder.SetInsertPoint(MergeBB);
1144 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1145 "iftmp");
1142 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
11461143
11471144 PN->addIncoming(ThenV, ThenBB);
11481145 PN->addIncoming(ElseV, ElseBB);
11841181
11851182 // Make the new basic block for the loop header, inserting after current
11861183 // block.
1187 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1184 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
11881185
11891186 // Insert an explicit fall through from the current block to the LoopBB.
11901187 Builder.CreateBr(LoopBB);
12101207 if (StepVal == 0) return 0;
12111208 } else {
12121209 // If not specified, use 1.0.
1213 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1210 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
12141211 }
12151212
12161213 // Compute the end condition.
12241221 Builder.CreateStore(NextVar, Alloca);
12251222
12261223 // Convert condition to a bool by comparing equal to 0.0.
1227 EndCond = Builder.CreateFCmpONE(EndCond,
1228 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1229 "loopcond");
1224 EndCond = Builder.CreateFCmpONE(
1225 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
12301226
12311227 // Create the "after loop" block and insert it.
1232 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1228 BasicBlock *AfterBB =
1229 BasicBlock::Create(TheContext, "afterloop", TheFunction);
12331230
12341231 // Insert the conditional branch into the end of LoopEndBB.
12351232 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
12451242
12461243
12471244 // for expr always returns 0.0.
1248 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1245 return Constant::getNullValue(Type::getDoubleTy(TheContext));
12491246 }
12501247
12511248 Value *VarExprAST::Codegen() {
12681265 InitVal = Init->Codegen();
12691266 if (InitVal == 0) return 0;
12701267 } else { // If not specified, use 0.0.
1271 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1268 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
12721269 }
12731270
12741271 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
12961293
12971294 Function *PrototypeAST::Codegen() {
12981295 // Make the function type: double(double,double) etc.
1299 std::vector Doubles(Args.size(),
1300 Type::getDoubleTy(getGlobalContext()));
1301 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1302 Doubles, false);
1296 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
1297 FunctionType *FT =
1298 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
13031299
13041300 std::string FnName = MakeLegalFunctionName(Name);
13051301
13641360 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
13651361
13661362 // Create a new basic block to start insertion into.
1367 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1363 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
13681364 Builder.SetInsertPoint(BB);
13691365
13701366 // Add all arguments to the symbol table and create their allocas.
14891485
14901486 Module* parseInputIR(std::string InputFile) {
14911487 SMDiagnostic Err;
1492 Module *M = ParseIRFile(InputFile, Err, getGlobalContext());
1488 Module *M = ParseIRFile(InputFile, Err, TheContext);
14931489 if (!M) {
14941490 Err.print("IR parsing failed: ", errs());
14951491 return NULL;
15111507 InitializeNativeTarget();
15121508 InitializeNativeTargetAsmPrinter();
15131509 InitializeNativeTargetAsmParser();
1514 LLVMContext &Context = getGlobalContext();
1510 LLVMContext &Context = TheContext;
15151511
15161512 cl::ParseCommandLineOptions(argc, argv,
15171513 "Kaleidoscope example program\n");
10651065 //===----------------------------------------------------------------------===//
10661066
10671067 static BaseHelper *TheHelper;
1068 static IRBuilder<> Builder(getGlobalContext());
1068 static LLVMContext TheContext;
1069 static IRBuilder<> Builder(TheContext);
10691070 static std::map NamedValues;
10701071
10711072 Value *ErrorV(const char *Str) { Error(Str); return 0; }
10761077 const std::string &VarName) {
10771078 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
10781079 TheFunction->getEntryBlock().begin());
1079 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
1080 VarName.c_str());
1080 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str());
10811081 }
10821082
10831083 Value *NumberExprAST::Codegen() {
1084 return ConstantFP::get(getGlobalContext(), APFloat(Val));
1084 return ConstantFP::get(TheContext, APFloat(Val));
10851085 }
10861086
10871087 Value *VariableExprAST::Codegen() {
11391139 case '<':
11401140 L = Builder.CreateFCmpULT(L, R, "cmptmp");
11411141 // Convert bool 0/1 to double 0.0 or 1.0
1142 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
1143 "booltmp");
1142 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
11441143 default: break;
11451144 }
11461145
11811180 if (CondV == 0) return 0;
11821181
11831182 // Convert condition to a bool by comparing equal to 0.0.
1184 CondV = Builder.CreateFCmpONE(CondV,
1185 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1186 "ifcond");
1183 CondV = Builder.CreateFCmpONE(
1184 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
11871185
11881186 Function *TheFunction = Builder.GetInsertBlock()->getParent();
11891187
11901188 // Create blocks for the then and else cases. Insert the 'then' block at the
11911189 // end of the function.
1192 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1193 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1194 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1190 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
1191 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
1192 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
11951193
11961194 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
11971195
12191217 // Emit merge block.
12201218 TheFunction->getBasicBlockList().push_back(MergeBB);
12211219 Builder.SetInsertPoint(MergeBB);
1222 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1223 "iftmp");
1220 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
12241221
12251222 PN->addIncoming(ThenV, ThenBB);
12261223 PN->addIncoming(ElseV, ElseBB);
12621259
12631260 // Make the new basic block for the loop header, inserting after current
12641261 // block.
1265 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1262 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
12661263
12671264 // Insert an explicit fall through from the current block to the LoopBB.
12681265 Builder.CreateBr(LoopBB);
12881285 if (StepVal == 0) return 0;
12891286 } else {
12901287 // If not specified, use 1.0.
1291 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1288 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
12921289 }
12931290
12941291 // Compute the end condition.
13021299 Builder.CreateStore(NextVar, Alloca);
13031300
13041301 // Convert condition to a bool by comparing equal to 0.0.
1305 EndCond = Builder.CreateFCmpONE(EndCond,
1306 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1307 "loopcond");
1302 EndCond = Builder.CreateFCmpONE(
1303 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
13081304
13091305 // Create the "after loop" block and insert it.
1310 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1306 BasicBlock *AfterBB =
1307 BasicBlock::Create(TheContext, "afterloop", TheFunction);
13111308
13121309 // Insert the conditional branch into the end of LoopEndBB.
13131310 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
13231320
13241321
13251322 // for expr always returns 0.0.
1326 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1323 return Constant::getNullValue(Type::getDoubleTy(TheContext));
13271324 }
13281325
13291326 Value *VarExprAST::Codegen() {
13461343 InitVal = Init->Codegen();
13471344 if (InitVal == 0) return 0;
13481345 } else { // If not specified, use 0.0.
1349 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1346 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
13501347 }
13511348
13521349 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
13741371
13751372 Function *PrototypeAST::Codegen() {
13761373 // Make the function type: double(double,double) etc.
1377 std::vector Doubles(Args.size(),
1378 Type::getDoubleTy(getGlobalContext()));
1379 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1380 Doubles, false);
1374 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
1375 FunctionType *FT =
1376 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
13811377
13821378 std::string FnName;
13831379 FnName = MakeLegalFunctionName(Name);
14421438 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
14431439
14441440 // Create a new basic block to start insertion into.
1445 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1441 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
14461442 Builder.SetInsertPoint(BB);
14471443
14481444 // Add all arguments to the symbol table and create their allocas.
15641560 InitializeNativeTarget();
15651561 InitializeNativeTargetAsmPrinter();
15661562 InitializeNativeTargetAsmParser();
1567 LLVMContext &Context = getGlobalContext();
1563 LLVMContext &Context = TheContext;
15681564
15691565 cl::ParseCommandLineOptions(argc, argv,
15701566 "Kaleidoscope example program\n");
851851 //===----------------------------------------------------------------------===//
852852
853853 static MCJITHelper *TheHelper;
854 static IRBuilder<> Builder(getGlobalContext());
854 static LLVMContext TheContext;
855 static IRBuilder<> Builder(TheContext);
855856 static std::map NamedValues;
856857
857858 Value *ErrorV(const char *Str) { Error(Str); return 0; }
862863 const std::string &VarName) {
863864 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
864865 TheFunction->getEntryBlock().begin());
865 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
866 VarName.c_str());
866 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str());
867867 }
868868
869869 Value *NumberExprAST::Codegen() {
870 return ConstantFP::get(getGlobalContext(), APFloat(Val));
870 return ConstantFP::get(TheContext, APFloat(Val));
871871 }
872872
873873 Value *VariableExprAST::Codegen() {
923923 case '<':
924924 L = Builder.CreateFCmpULT(L, R, "cmptmp");
925925 // Convert bool 0/1 to double 0.0 or 1.0
926 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
927 "booltmp");
926 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
928927 default: break;
929928 }
930929
961960 if (CondV == 0) return 0;
962961
963962 // Convert condition to a bool by comparing equal to 0.0.
964 CondV = Builder.CreateFCmpONE(CondV,
965 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
966 "ifcond");
963 CondV = Builder.CreateFCmpONE(
964 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
967965
968966 Function *TheFunction = Builder.GetInsertBlock()->getParent();
969967
970968 // Create blocks for the then and else cases. Insert the 'then' block at the
971969 // end of the function.
972 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
973 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
974 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
970 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
971 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
972 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
975973
976974 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
977975
999997 // Emit merge block.
1000998 TheFunction->getBasicBlockList().push_back(MergeBB);
1001999 Builder.SetInsertPoint(MergeBB);
1002 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1003 "iftmp");
1000 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
10041001
10051002 PN->addIncoming(ThenV, ThenBB);
10061003 PN->addIncoming(ElseV, ElseBB);
10421039
10431040 // Make the new basic block for the loop header, inserting after current
10441041 // block.
1045 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1042 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
10461043
10471044 // Insert an explicit fall through from the current block to the LoopBB.
10481045 Builder.CreateBr(LoopBB);
10681065 if (StepVal == 0) return 0;
10691066 } else {
10701067 // If not specified, use 1.0.
1071 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1068 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
10721069 }
10731070
10741071 // Compute the end condition.
10821079 Builder.CreateStore(NextVar, Alloca);
10831080
10841081 // Convert condition to a bool by comparing equal to 0.0.
1085 EndCond = Builder.CreateFCmpONE(EndCond,
1086 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1087 "loopcond");
1082 EndCond = Builder.CreateFCmpONE(
1083 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
10881084
10891085 // Create the "after loop" block and insert it.
1090 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1086 BasicBlock *AfterBB =
1087 BasicBlock::Create(TheContext, "afterloop", TheFunction);
10911088
10921089 // Insert the conditional branch into the end of LoopEndBB.
10931090 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
11031100
11041101
11051102 // for expr always returns 0.0.
1106 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1103 return Constant::getNullValue(Type::getDoubleTy(TheContext));
11071104 }
11081105
11091106 Value *VarExprAST::Codegen() {
11261123 InitVal = Init->Codegen();
11271124 if (InitVal == 0) return 0;
11281125 } else { // If not specified, use 0.0.
1129 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1126 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
11301127 }
11311128
11321129 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
11541151
11551152 Function *PrototypeAST::Codegen() {
11561153 // Make the function type: double(double,double) etc.
1157 std::vector Doubles(Args.size(),
1158 Type::getDoubleTy(getGlobalContext()));
1159 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1160 Doubles, false);
1154 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
1155 FunctionType *FT =
1156 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
11611157
11621158 std::string FnName = MakeLegalFunctionName(Name);
11631159
12221218 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
12231219
12241220 // Create a new basic block to start insertion into.
1225 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1221 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
12261222 Builder.SetInsertPoint(BB);
12271223
12281224 // Add all arguments to the symbol table and create their allocas.
13481344 InitializeNativeTarget();
13491345 InitializeNativeTargetAsmPrinter();
13501346 InitializeNativeTargetAsmParser();
1351 LLVMContext &Context = getGlobalContext();
1347 LLVMContext &Context = TheContext;
13521348
13531349 // Install standard binary operators.
13541350 // 1 is lowest precedence.
607607
608608 static Module *TheModule;
609609 static FunctionPassManager *TheFPM;
610 static IRBuilder<> Builder(getGlobalContext());
610 static LLVMContext TheContext;
611 static IRBuilder<> Builder(TheContext);
611612 static std::map NamedValues;
612613
613614 Value *ErrorV(const char *Str) { Error(Str); return 0; }
618619 const std::string &VarName) {
619620 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
620621 TheFunction->getEntryBlock().begin());
621 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
622 VarName.c_str());
622 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str());
623623 }
624624
625625 Value *NumberExprAST::Codegen() {
626 return ConstantFP::get(getGlobalContext(), APFloat(Val));
626 return ConstantFP::get(TheContext, APFloat(Val));
627627 }
628628
629629 Value *VariableExprAST::Codegen() {
680680 case '<':
681681 L = Builder.CreateFCmpULT(L, R, "cmptmp");
682682 // Convert bool 0/1 to double 0.0 or 1.0
683 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
684 "booltmp");
683 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
685684 default: break;
686685 }
687686
721720 if (CondV == 0) return 0;
722721
723722 // Convert condition to a bool by comparing equal to 0.0.
724 CondV = Builder.CreateFCmpONE(CondV,
725 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
726 "ifcond");
727
723 CondV = Builder.CreateFCmpONE(
724 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
725
728726 Function *TheFunction = Builder.GetInsertBlock()->getParent();
729727
730728 // Create blocks for the then and else cases. Insert the 'then' block at the
731729 // end of the function.
732 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
733 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
734 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
735
730 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
731 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
732 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
733
736734 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
737735
738736 // Emit then value.
759757 // Emit merge block.
760758 TheFunction->getBasicBlockList().push_back(MergeBB);
761759 Builder.SetInsertPoint(MergeBB);
762 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
763 "iftmp");
764
760 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
761
765762 PN->addIncoming(ThenV, ThenBB);
766763 PN->addIncoming(ElseV, ElseBB);
767764 return PN;
802799
803800 // Make the new basic block for the loop header, inserting after current
804801 // block.
805 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
806
802 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
803
807804 // Insert an explicit fall through from the current block to the LoopBB.
808805 Builder.CreateBr(LoopBB);
809806
828825 if (StepVal == 0) return 0;
829826 } else {
830827 // If not specified, use 1.0.
831 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
828 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
832829 }
833830
834831 // Compute the end condition.
842839 Builder.CreateStore(NextVar, Alloca);
843840
844841 // Convert condition to a bool by comparing equal to 0.0.
845 EndCond = Builder.CreateFCmpONE(EndCond,
846 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
847 "loopcond");
848
842 EndCond = Builder.CreateFCmpONE(
843 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
844
849845 // Create the "after loop" block and insert it.
850 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
851
846 BasicBlock *AfterBB =
847 BasicBlock::Create(TheContext, "afterloop", TheFunction);
848
852849 // Insert the conditional branch into the end of LoopEndBB.
853850 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
854851
863860
864861
865862 // for expr always returns 0.0.
866 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
863 return Constant::getNullValue(Type::getDoubleTy(TheContext));
867864 }
868865
869866 Value *VarExprAST::Codegen() {
886883 InitVal = Init->Codegen();
887884 if (InitVal == 0) return 0;
888885 } else { // If not specified, use 0.0.
889 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
886 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
890887 }
891888
892889 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
914911
915912 Function *PrototypeAST::Codegen() {
916913 // Make the function type: double(double,double) etc.
917 std::vector Doubles(Args.size(),
918 Type::getDoubleTy(getGlobalContext()));
919 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
920 Doubles, false);
914 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
915 FunctionType *FT =
916 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
921917
922918 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
923919 // If F conflicted, there was already something named 'Name'. If it has a
975971 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
976972
977973 // Create a new basic block to start insertion into.
978 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
974 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
979975 Builder.SetInsertPoint(BB);
980976
981977 // Add all arguments to the symbol table and create their allocas.
11031099
11041100 int main(int argc, char **argv) {
11051101 InitializeNativeTarget();
1106 LLVMContext &Context = getGlobalContext();
1102 LLVMContext &Context = TheContext;
11071103
11081104 // Install standard binary operators.
11091105 // 1 is lowest precedence.
891891 //===----------------------------------------------------------------------===//
892892
893893 static MCJITHelper *TheHelper;
894 static IRBuilder<> Builder(getGlobalContext());
894 static LLVMContext TheContext;
895 static IRBuilder<> Builder(TheContext);
895896 static std::map NamedValues;
896897
897898 Value *ErrorV(const char *Str) { Error(Str); return 0; }
902903 const std::string &VarName) {
903904 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
904905 TheFunction->getEntryBlock().begin());
905 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
906 VarName.c_str());
906 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str());
907907 }
908908
909909 Value *NumberExprAST::Codegen() {
910 return ConstantFP::get(getGlobalContext(), APFloat(Val));
910 return ConstantFP::get(TheContext, APFloat(Val));
911911 }
912912
913913 Value *VariableExprAST::Codegen() {
963963 case '<':
964964 L = Builder.CreateFCmpULT(L, R, "cmptmp");
965965 // Convert bool 0/1 to double 0.0 or 1.0
966 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
967 "booltmp");
966 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
968967 default: break;
969968 }
970969
10011000 if (CondV == 0) return 0;
10021001
10031002 // Convert condition to a bool by comparing equal to 0.0.
1004 CondV = Builder.CreateFCmpONE(CondV,
1005 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1006 "ifcond");
1003 CondV = Builder.CreateFCmpONE(
1004 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
10071005
10081006 Function *TheFunction = Builder.GetInsertBlock()->getParent();
10091007
10101008 // Create blocks for the then and else cases. Insert the 'then' block at the
10111009 // end of the function.
1012 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1013 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1014 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1010 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
1011 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
1012 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
10151013
10161014 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
10171015
10391037 // Emit merge block.
10401038 TheFunction->getBasicBlockList().push_back(MergeBB);
10411039 Builder.SetInsertPoint(MergeBB);
1042 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1043 "iftmp");
1040 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
10441041
10451042 PN->addIncoming(ThenV, ThenBB);
10461043 PN->addIncoming(ElseV, ElseBB);
10821079
10831080 // Make the new basic block for the loop header, inserting after current
10841081 // block.
1085 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1082 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
10861083
10871084 // Insert an explicit fall through from the current block to the LoopBB.
10881085 Builder.CreateBr(LoopBB);
11081105 if (StepVal == 0) return 0;
11091106 } else {
11101107 // If not specified, use 1.0.
1111 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1108 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
11121109 }
11131110
11141111 // Compute the end condition.
11221119 Builder.CreateStore(NextVar, Alloca);
11231120
11241121 // Convert condition to a bool by comparing equal to 0.0.
1125 EndCond = Builder.CreateFCmpONE(EndCond,
1126 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1127 "loopcond");
1122 EndCond = Builder.CreateFCmpONE(
1123 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
11281124
11291125 // Create the "after loop" block and insert it.
1130 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1126 BasicBlock *AfterBB =
1127 BasicBlock::Create(TheContext, "afterloop", TheFunction);
11311128
11321129 // Insert the conditional branch into the end of LoopEndBB.
11331130 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
11431140
11441141
11451142 // for expr always returns 0.0.
1146 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1143 return Constant::getNullValue(Type::getDoubleTy(TheContext));
11471144 }
11481145
11491146 Value *VarExprAST::Codegen() {
11661163 InitVal = Init->Codegen();
11671164 if (InitVal == 0) return 0;
11681165 } else { // If not specified, use 0.0.
1169 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1166 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
11701167 }
11711168
11721169 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
11941191
11951192 Function *PrototypeAST::Codegen() {
11961193 // Make the function type: double(double,double) etc.
1197 std::vector Doubles(Args.size(),
1198 Type::getDoubleTy(getGlobalContext()));
1199 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1200 Doubles, false);
1194 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
1195 FunctionType *FT =
1196 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
12011197
12021198 std::string FnName = MakeLegalFunctionName(Name);
12031199
12621258 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
12631259
12641260 // Create a new basic block to start insertion into.
1265 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1261 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
12661262 Builder.SetInsertPoint(BB);
12671263
12681264 // Add all arguments to the symbol table and create their allocas.
13891385 InitializeNativeTarget();
13901386 InitializeNativeTargetAsmPrinter();
13911387 InitializeNativeTargetAsmParser();
1392 LLVMContext &Context = getGlobalContext();
1388 LLVMContext &Context = TheContext;
13931389
13941390 // Install standard binary operators.
13951391 // 1 is lowest precedence.
746746 const std::string &VarName) {
747747 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
748748 TheFunction->getEntryBlock().begin());
749 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), nullptr,
750 VarName.c_str());
749 return TmpB.CreateAlloca(Type::getDoubleTy(TheFunction->getContext()),
750 nullptr, VarName.c_str());
751751 }
752752
753753 Value *NumberExprAST::IRGen(IRGenContext &C) const {
806806 case '<':
807807 L = C.getBuilder().CreateFCmpULT(L, R, "cmptmp");
808808 // Convert bool 0/1 to double 0.0 or 1.0
809 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
810 "booltmp");
809 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(C.getLLVMContext()),
810 "booltmp");
811811 default: break;
812812 }
813813
884884 // Emit merge block.
885885 TheFunction->getBasicBlockList().push_back(MergeBB);
886886 C.getBuilder().SetInsertPoint(MergeBB);
887 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
888 "iftmp");
887 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(C.getLLVMContext()),
888 2, "iftmp");
889889
890890 PN->addIncoming(ThenV, ThenBB);
891891 PN->addIncoming(ElseV, ElseBB);
927927
928928 // Make the new basic block for the loop header, inserting after current
929929 // block.
930 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
930 BasicBlock *LoopBB =
931 BasicBlock::Create(C.getLLVMContext(), "loop", TheFunction);
931932
932933 // Insert an explicit fall through from the current block to the LoopBB.
933934 C.getBuilder().CreateBr(LoopBB);
953954 if (!StepVal) return nullptr;
954955 } else {
955956 // If not specified, use 1.0.
956 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
957 StepVal = ConstantFP::get(C.getLLVMContext(), APFloat(1.0));
957958 }
958959
959960 // Compute the end condition.
967968 C.getBuilder().CreateStore(NextVar, Alloca);
968969
969970 // Convert condition to a bool by comparing equal to 0.0.
970 EndCond = C.getBuilder().CreateFCmpONE(EndCond,
971 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
972 "loopcond");
971 EndCond = C.getBuilder().CreateFCmpONE(
972 EndCond, ConstantFP::get(C.getLLVMContext(), APFloat(0.0)), "loopcond");
973973
974974 // Create the "after loop" block and insert it.
975 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
975 BasicBlock *AfterBB =
976 BasicBlock::Create(C.getLLVMContext(), "afterloop", TheFunction);
976977
977978 // Insert the conditional branch into the end of LoopEndBB.
978979 C.getBuilder().CreateCondBr(EndCond, LoopBB, AfterBB);
987988 C.NamedValues.erase(VarName);
988989
989990 // for expr always returns 0.0.
990 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
991 return Constant::getNullValue(Type::getDoubleTy(C.getLLVMContext()));
991992 }
992993
993994 Value *VarExprAST::IRGen(IRGenContext &C) const {
10101011 InitVal = Init->IRGen(C);
10111012 if (!InitVal) return nullptr;
10121013 } else // If not specified, use 0.0.
1013 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1014 InitVal = ConstantFP::get(C.getLLVMContext(), APFloat(0.0));
10141015
10151016 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
10161017 C.getBuilder().CreateStore(InitVal, Alloca);
10391040 std::string FnName = MakeLegalFunctionName(Name);
10401041
10411042 // Make the function type: double(double,double) etc.
1042 std::vector Doubles(Args.size(),
1043 Type::getDoubleTy(getGlobalContext()));
1044 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1045 Doubles, false);
1043 std::vector Doubles(Args.size(),
1044 Type::getDoubleTy(C.getLLVMContext()));
1045 FunctionType *FT =
1046 FunctionType::get(Type::getDoubleTy(C.getLLVMContext()), Doubles, false);
10461047 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName,
10471048 &C.getM());
10481049
11031104 BinopPrecedence[Proto->getOperatorName()] = Proto->Precedence;
11041105
11051106 // Create a new basic block to start insertion into.
1106 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1107 BasicBlock *BB = BasicBlock::Create(C.getLLVMContext(), "entry", TheFunction);
11071108 C.getBuilder().SetInsertPoint(BB);
11081109
11091110 // Add all arguments to the symbol table and create their allocas.
13651366
13661367 /// top ::= definition | external | expression | ';'
13671368 static void MainLoop() {
1368 SessionContext S(getGlobalContext());
1369 LLVMContext TheContext;
1370 SessionContext S(TheContext);
13691371 KaleidoscopeJIT J(S);
13701372
13711373 while (1) {
745745 const std::string &VarName) {
746746 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
747747 TheFunction->getEntryBlock().begin());
748 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), nullptr,
749 VarName.c_str());
748 return TmpB.CreateAlloca(Type::getDoubleTy(TheFunction->getContext()),
749 nullptr, VarName.c_str());
750750 }
751751
752752 Value *NumberExprAST::IRGen(IRGenContext &C) const {
805805 case '<':
806806 L = C.getBuilder().CreateFCmpULT(L, R, "cmptmp");
807807 // Convert bool 0/1 to double 0.0 or 1.0
808 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
809 "booltmp");
808 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(C.getLLVMContext()),
809 "booltmp");
810810 default: break;
811811 }
812812
883883 // Emit merge block.
884884 TheFunction->getBasicBlockList().push_back(MergeBB);
885885 C.getBuilder().SetInsertPoint(MergeBB);
886 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
887 "iftmp");
886 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(C.getLLVMContext()),
887 2, "iftmp");
888888
889889 PN->addIncoming(ThenV, ThenBB);
890890 PN->addIncoming(ElseV, ElseBB);
926926
927927 // Make the new basic block for the loop header, inserting after current
928928 // block.
929 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
929 BasicBlock *LoopBB =
930 BasicBlock::Create(C.getLLVMContext(), "loop", TheFunction);
930931
931932 // Insert an explicit fall through from the current block to the LoopBB.
932933 C.getBuilder().CreateBr(LoopBB);
952953 if (!StepVal) return nullptr;
953954 } else {
954955 // If not specified, use 1.0.
955 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
956 StepVal = ConstantFP::get(C.getLLVMContext(), APFloat(1.0));
956957 }
957958
958959 // Compute the end condition.
966967 C.getBuilder().CreateStore(NextVar, Alloca);
967968
968969 // Convert condition to a bool by comparing equal to 0.0.
969 EndCond = C.getBuilder().CreateFCmpONE(EndCond,
970 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
971 "loopcond");
970 EndCond = C.getBuilder().CreateFCmpONE(
971 EndCond, ConstantFP::get(C.getLLVMContext(), APFloat(0.0)), "loopcond");
972972
973973 // Create the "after loop" block and insert it.
974 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
974 BasicBlock *AfterBB =
975 BasicBlock::Create(C.getLLVMContext(), "afterloop", TheFunction);
975976
976977 // Insert the conditional branch into the end of LoopEndBB.
977978 C.getBuilder().CreateCondBr(EndCond, LoopBB, AfterBB);
986987 C.NamedValues.erase(VarName);
987988
988989 // for expr always returns 0.0.
989 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
990 return Constant::getNullValue(Type::getDoubleTy(C.getLLVMContext()));
990991 }
991992
992993 Value *VarExprAST::IRGen(IRGenContext &C) const {
10091010 InitVal = Init->IRGen(C);
10101011 if (!InitVal) return nullptr;
10111012 } else // If not specified, use 0.0.
1012 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1013 InitVal = ConstantFP::get(C.getLLVMContext(), APFloat(0.0));
10131014
10141015 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
10151016 C.getBuilder().CreateStore(InitVal, Alloca);
10381039 std::string FnName = MakeLegalFunctionName(Name);
10391040
10401041 // Make the function type: double(double,double) etc.
1041 std::vector Doubles(Args.size(),
1042 Type::getDoubleTy(getGlobalContext()));
1043 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1044 Doubles, false);
1042 std::vector Doubles(Args.size(),
1043 Type::getDoubleTy(C.getLLVMContext()));
1044 FunctionType *FT =
1045 FunctionType::get(Type::getDoubleTy(C.getLLVMContext()), Doubles, false);
10451046 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName,
10461047 &C.getM());
10471048
11021103 BinopPrecedence[Proto->getOperatorName()] = Proto->Precedence;
11031104
11041105 // Create a new basic block to start insertion into.
1105 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1106 BasicBlock *BB = BasicBlock::Create(C.getLLVMContext(), "entry", TheFunction);
11061107 C.getBuilder().SetInsertPoint(BB);
11071108
11081109 // Add all arguments to the symbol table and create their allocas.
12601261
12611262 /// top ::= definition | external | expression | ';'
12621263 static void MainLoop() {
1263 SessionContext S(getGlobalContext());
1264 LLVMContext TheContext;
1265 SessionContext S(TheContext);
12641266 KaleidoscopeJIT J(S);
12651267
12661268 while (1) {
745745 const std::string &VarName) {
746746 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
747747 TheFunction->getEntryBlock().begin());
748 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), nullptr,
749 VarName.c_str());
748 return TmpB.CreateAlloca(Type::getDoubleTy(TheFunction->getContext()),
749 nullptr, VarName.c_str());
750750 }
751751
752752 Value *NumberExprAST::IRGen(IRGenContext &C) const {
805805 case '<':
806806 L = C.getBuilder().CreateFCmpULT(L, R, "cmptmp");
807807 // Convert bool 0/1 to double 0.0 or 1.0
808 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
809 "booltmp");
808 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(C.getLLVMContext()),
809 "booltmp");
810810 default: break;
811811 }
812812
883883 // Emit merge block.
884884 TheFunction->getBasicBlockList().push_back(MergeBB);
885885 C.getBuilder().SetInsertPoint(MergeBB);
886 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
887 "iftmp");
886 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(C.getLLVMContext()),
887 2, "iftmp");
888888
889889 PN->addIncoming(ThenV, ThenBB);
890890 PN->addIncoming(ElseV, ElseBB);
926926
927927 // Make the new basic block for the loop header, inserting after current
928928 // block.
929 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
929 BasicBlock *LoopBB =
930 BasicBlock::Create(C.getLLVMContext(), "loop", TheFunction);
930931
931932 // Insert an explicit fall through from the current block to the LoopBB.
932933 C.getBuilder().CreateBr(LoopBB);
952953 if (!StepVal) return nullptr;
953954 } else {
954955 // If not specified, use 1.0.
955 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
956 StepVal = ConstantFP::get(C.getLLVMContext(), APFloat(1.0));
956957 }
957958
958959 // Compute the end condition.
966967 C.getBuilder().CreateStore(NextVar, Alloca);
967968
968969 // Convert condition to a bool by comparing equal to 0.0.
969 EndCond = C.getBuilder().CreateFCmpONE(EndCond,
970 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
971 "loopcond");
970 EndCond = C.getBuilder().CreateFCmpONE(
971 EndCond, ConstantFP::get(C.getLLVMContext(), APFloat(0.0)), "loopcond");
972972
973973 // Create the "after loop" block and insert it.
974 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
974 BasicBlock *AfterBB =
975 BasicBlock::Create(C.getLLVMContext(), "afterloop", TheFunction);
975976
976977 // Insert the conditional branch into the end of LoopEndBB.
977978 C.getBuilder().CreateCondBr(EndCond, LoopBB, AfterBB);
986987 C.NamedValues.erase(VarName);
987988
988989 // for expr always returns 0.0.
989 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
990 return Constant::getNullValue(Type::getDoubleTy(C.getLLVMContext()));
990991 }
991992
992993 Value *VarExprAST::IRGen(IRGenContext &C) const {
10091010 InitVal = Init->IRGen(C);
10101011 if (!InitVal) return nullptr;
10111012 } else // If not specified, use 0.0.
1012 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1013 InitVal = ConstantFP::get(C.getLLVMContext(), APFloat(0.0));
10131014
10141015 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
10151016 C.getBuilder().CreateStore(InitVal, Alloca);
10381039 std::string FnName = MakeLegalFunctionName(Name);
10391040
10401041 // Make the function type: double(double,double) etc.
1041 std::vector Doubles(Args.size(),
1042 Type::getDoubleTy(getGlobalContext()));
1043 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1044 Doubles, false);
1042 std::vector Doubles(Args.size(),
1043 Type::getDoubleTy(C.getLLVMContext()));
1044 FunctionType *FT =
1045 FunctionType::get(Type::getDoubleTy(C.getLLVMContext()), Doubles, false);
10451046 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName,
10461047 &C.getM());
10471048
11021103 BinopPrecedence[Proto->getOperatorName()] = Proto->Precedence;
11031104
11041105 // Create a new basic block to start insertion into.
1105 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1106 BasicBlock *BB = BasicBlock::Create(C.getLLVMContext(), "entry", TheFunction);
11061107 C.getBuilder().SetInsertPoint(BB);
11071108
11081109 // Add all arguments to the symbol table and create their allocas.
12641265
12651266 /// top ::= definition | external | expression | ';'
12661267 static void MainLoop() {
1267 SessionContext S(getGlobalContext());
1268 LLVMContext TheContext;
1269 SessionContext S(TheContext);
12681270 KaleidoscopeJIT J(S);
12691271
12701272 while (1) {
745745 const std::string &VarName) {
746746 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
747747 TheFunction->getEntryBlock().begin());
748 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), nullptr,
749 VarName.c_str());
748 return TmpB.CreateAlloca(Type::getDoubleTy(TheFunction->getContext()),
749 nullptr, VarName.c_str());
750750 }
751751
752752 Value *NumberExprAST::IRGen(IRGenContext &C) const {
805805 case '<':
806806 L = C.getBuilder().CreateFCmpULT(L, R, "cmptmp");
807807 // Convert bool 0/1 to double 0.0 or 1.0
808 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
809 "booltmp");
808 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(C.getLLVMContext()),
809 "booltmp");
810810 default: break;
811811 }
812812
883883 // Emit merge block.
884884 TheFunction->getBasicBlockList().push_back(MergeBB);
885885 C.getBuilder().SetInsertPoint(MergeBB);
886 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
887 "iftmp");
886 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(C.getLLVMContext()),
887 2, "iftmp");
888888
889889 PN->addIncoming(ThenV, ThenBB);
890890 PN->addIncoming(ElseV, ElseBB);
926926
927927 // Make the new basic block for the loop header, inserting after current
928928 // block.
929 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
929 BasicBlock *LoopBB =
930 BasicBlock::Create(C.getLLVMContext(), "loop", TheFunction);
930931
931932 // Insert an explicit fall through from the current block to the LoopBB.
932933 C.getBuilder().CreateBr(LoopBB);
952953 if (!StepVal) return nullptr;
953954 } else {
954955 // If not specified, use 1.0.
955 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
956 StepVal = ConstantFP::get(C.getLLVMContext(), APFloat(1.0));
956957 }
957958
958959 // Compute the end condition.
966967 C.getBuilder().CreateStore(NextVar, Alloca);
967968
968969 // Convert condition to a bool by comparing equal to 0.0.
969 EndCond = C.getBuilder().CreateFCmpONE(EndCond,
970 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
971 "loopcond");
970 EndCond = C.getBuilder().CreateFCmpONE(
971 EndCond, ConstantFP::get(C.getLLVMContext(), APFloat(0.0)), "loopcond");
972972
973973 // Create the "after loop" block and insert it.
974 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
974 BasicBlock *AfterBB =
975 BasicBlock::Create(C.getLLVMContext(), "afterloop", TheFunction);
975976
976977 // Insert the conditional branch into the end of LoopEndBB.
977978 C.getBuilder().CreateCondBr(EndCond, LoopBB, AfterBB);
986987 C.NamedValues.erase(VarName);
987988
988989 // for expr always returns 0.0.
989 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
990 return Constant::getNullValue(Type::getDoubleTy(C.getLLVMContext()));
990991 }
991992
992993 Value *VarExprAST::IRGen(IRGenContext &C) const {
10091010 InitVal = Init->IRGen(C);
10101011 if (!InitVal) return nullptr;
10111012 } else // If not specified, use 0.0.
1012 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1013 InitVal = ConstantFP::get(C.getLLVMContext(), APFloat(0.0));
10131014
10141015 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
10151016 C.getBuilder().CreateStore(InitVal, Alloca);
10381039 std::string FnName = MakeLegalFunctionName(Name);
10391040
10401041 // Make the function type: double(double,double) etc.
1041 std::vector Doubles(Args.size(),
1042 Type::getDoubleTy(getGlobalContext()));
1043 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1044 Doubles, false);
1042 std::vector Doubles(Args.size(),
1043 Type::getDoubleTy(C.getLLVMContext()));
1044 FunctionType *FT =
1045 FunctionType::get(Type::getDoubleTy(C.getLLVMContext()), Doubles, false);
10451046 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName,
10461047 &C.getM());
10471048
11021103 BinopPrecedence[Proto->getOperatorName()] = Proto->Precedence;
11031104
11041105 // Create a new basic block to start insertion into.
1105 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1106 BasicBlock *BB = BasicBlock::Create(C.getLLVMContext(), "entry", TheFunction);
11061107 C.getBuilder().SetInsertPoint(BB);
11071108
11081109 // Add all arguments to the symbol table and create their allocas.
12951296
12961297 /// top ::= definition | external | expression | ';'
12971298 static void MainLoop() {
1298 SessionContext S(getGlobalContext());
1299 LLVMContext TheContext;
1300 SessionContext S(TheContext);
12991301 KaleidoscopeJIT J(S);
13001302
13011303 while (1) {
4242 // to know that getenv() never returns -1, this will do the job.
4343 if (std::getenv("bar") != (char*) -1)
4444 return;
45 (void)new llvm::Module("", llvm::getGlobalContext());
46 (void)new llvm::UnreachableInst(llvm::getGlobalContext());
45 llvm::LLVMContext Context;
46 (void)new llvm::Module("", Context);
47 (void)new llvm::UnreachableInst(Context);
4748 (void) llvm::createVerifierPass();
4849 }
4950 } ForceVMCoreLinking;
2424 Optionally returns a human-readable error message via OutMessage. */
2525 LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
2626 char **OutMessage) {
27 return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
27 return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
2828 OutMessage);
2929 }
3030
3131 LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
3232 LLVMModuleRef *OutModule) {
33 return LLVMParseBitcodeInContext2(wrap(&getGlobalContext()), MemBuf,
34 OutModule);
33 return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
3534 }
3635
3736 static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
19461946 Out << "#include \n";
19471947 Out << "#include \n";
19481948 Out << "using namespace llvm;\n\n";
1949 Out << "static LLVMContext TheContext;\n\n";
19491950 Out << "Module* " << fname << "();\n\n";
19501951 Out << "int main(int argc, char**argv) {\n";
19511952 Out << " Module* Mod = " << fname << "();\n";
19641965 nl(Out,1) << "// Module Construction";
19651966 nl(Out) << "Module* mod = new Module(\"";
19661967 printEscapedString(mName);
1967 Out << "\", getGlobalContext());";
1968 Out << "\", TheContext);";
19681969 if (!TheModule->getTargetTriple().empty()) {
19691970 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayoutStr()
19701971 << "\");";
2222 #include
2323
2424 using namespace llvm;
25
26 // Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead.
27 extern "C" LLVMContextRef LLVMGetGlobalContext(void);
2528
2629 inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) {
2730 return reinterpret_cast(P);
8083 }
8184
8285 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
83 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
86 return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext())));
8487 }
8588
8689 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
87 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), AS));
90 return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext()), AS));
8891 }
8992
9093 LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {
142142 sys::SetInterruptFunction(BugpointInterruptFunction);
143143 #endif
144144
145 LLVMContext& Context = getGlobalContext();
145 LLVMContext Context;
146146 // If we have an override, set it and then track the triple we want Modules
147147 // to use.
148148 if (!OverrideTriple.empty()) {
186186 // Enable debug stream buffering.
187187 EnableDebugBuffering = true;
188188
189 LLVMContext &Context = getGlobalContext();
189 LLVMContext Context;
190190 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
191191
192192 // Initialize targets first, so that --version shows registered targets.
384384 sys::PrintStackTraceOnErrorSignal();
385385 PrettyStackTraceProgram X(argc, argv);
386386
387 LLVMContext &Context = getGlobalContext();
387 LLVMContext Context;
388388 atexit(do_shutdown); // Call llvm_shutdown() on exit.
389389
390390 // If we have a native target, initialize it to ensure it is linked in and
9090 // Print a stack trace if we signal out.
9191 sys::PrintStackTraceOnErrorSignal();
9292 PrettyStackTraceProgram X(argc, argv);
93 LLVMContext &Context = getGlobalContext();
93 LLVMContext Context;
9494 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
9595 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
9696
5252 // parsed is always null terminated.
5353 std::unique_ptr MemBuf = MemoryBuffer::getMemBufferCopy(Input);
5454 SMDiagnostic Err;
55 LLVMContext &Context = getGlobalContext();
55 LLVMContext Context;
5656 std::unique_ptr M;
5757
5858 if (setjmp(JmpBuf))
136136 sys::PrintStackTraceOnErrorSignal();
137137 PrettyStackTraceProgram X(argc, argv);
138138
139 LLVMContext &Context = getGlobalContext();
139 LLVMContext Context;
140140 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
141141
142142 Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
104104 sys::PrintStackTraceOnErrorSignal();
105105 PrettyStackTraceProgram X(argc, argv);
106106
107 LLVMContext &Context = getGlobalContext();
107 LLVMContext Context;
108108 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
109109 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
110110
104104 class JitEventListenerTest {
105105 protected:
106106 void InitEE(const std::string &IRFile) {
107 LLVMContext &Context = getGlobalContext();
108
109107 // If we have a native target, initialize it to ensure it is linked in and
110108 // usable by the JIT.
111109 InitializeNativeTarget();
330330 sys::PrintStackTraceOnErrorSignal();
331331 PrettyStackTraceProgram X(argc, argv);
332332
333 LLVMContext &Context = getGlobalContext();
333 LLVMContext Context;
334334 Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
335335
336336 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
10191019 if (error(BufferOrErr.getError(), Filename))
10201020 return;
10211021
1022 LLVMContext &Context = getGlobalContext();
1022 LLVMContext Context;
10231023 Expected> BinaryOrErr = createBinary(
10241024 BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context);
10251025 if (!BinaryOrErr) {
163163 auto Writer = std::move(WriterOrErr.get());
164164 StringMap ProfileMap;
165165 SmallVector, 5> Readers;
166 LLVMContext Context;
166167 for (const auto &Input : Inputs) {
167 auto ReaderOrErr =
168 SampleProfileReader::create(Input.Filename, getGlobalContext());
168 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
169169 if (std::error_code EC = ReaderOrErr.getError())
170170 exitWithErrorCode(EC, Input.Filename);
171171
364364 bool ShowAllFunctions, std::string ShowFunction,
365365 raw_fd_ostream &OS) {
366366 using namespace sampleprof;
367 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
367 LLVMContext Context;
368 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
368369 if (std::error_code EC = ReaderOrErr.getError())
369370 exitWithErrorCode(EC, Filename);
370371
4040 cl::desc("Split without externalizing locals"));
4141
4242 int main(int argc, char **argv) {
43 LLVMContext &Context = getGlobalContext();
43 LLVMContext Context;
4444 SMDiagnostic Err;
4545 cl::ParseCommandLineOptions(argc, argv, "LLVM module splitter\n");
4646
4242 OutputFilename("o", cl::desc("Override output filename"),
4343 cl::value_desc("filename"));
4444
45 static LLVMContext Context;
46
4547 namespace cl {
4648 template <> class parser final : public basic_parser {
4749 public:
4951
5052 // Parse options as IR types. Return true on error.
5153 bool parse(Option &O, StringRef, StringRef Arg, Type *&Value) {
52 auto &Context = getGlobalContext();
5354 if (Arg == "half") Value = Type::getHalfTy(Context);
5455 else if (Arg == "fp128") Value = Type::getFP128Ty(Context);
5556 else if (Arg == "x86_fp80") Value = Type::getX86_FP80Ty(Context);
686687 cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n");
687688 llvm_shutdown_obj Y;
688689
689 auto M = make_unique("/tmp/autogen.bc", getGlobalContext());
690 auto M = make_unique("/tmp/autogen.bc", Context);
690691 Function *F = GenEmptyFunction(M.get());
691692
692693 // Pick an initial seed value
100100 InitializeAllAsmPrinters();
101101 InitializeAllDisassemblers();
102102
103 LTOContext = &getGlobalContext();
103 static LLVMContext Context;
104 LTOContext = &Context;
104105 LTOContext->setDiagnosticHandler(diagnosticHandler, nullptr, true);
105106 initialized = true;
106107 }
315315 EnableDebugBuffering = true;
316316
317317 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
318 LLVMContext &Context = getGlobalContext();
318 LLVMContext Context;
319319
320320 InitializeAllTargets();
321321 InitializeAllTargetMCs();
524524 EnableDebugBuffering = true;
525525
526526 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
527 LLVMContext &Context = getGlobalContext();
527 LLVMContext Context;
528528
529529 cl::ParseCommandLineOptions(argc, argv,
530530 "llvm tool to verify use-list order\n");
206206
207207 class AAPassInfraTest : public testing::Test {
208208 protected:
209 LLVMContext &C;
209 LLVMContext C;
210210 SMDiagnostic Err;
211211 std::unique_ptr M;
212212
213213 public:
214214 AAPassInfraTest()
215 : C(getGlobalContext()),
216 M(parseAssemblyString("define i32 @f(i32* %x, i32* %y) {\n"
215 : M(parseAssemblyString("define i32 @f(i32* %x, i32* %y) {\n"
217216 "entry:\n"
218217 " %lx = load i32, i32* %x\n"
219218 " %ly = load i32, i32* %y\n"
2929 std::unique_ptr BPI;
3030 std::unique_ptr DT;
3131 std::unique_ptr LI;
32 LLVMContext C;
3233
3334 BlockFrequencyInfo buildBFI(Function &F) {
3435 DT.reset(new DominatorTree(F));
4950 " %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
5051 " ret i32 %y2\n"
5152 "}\n";
52 LLVMContext &C = getGlobalContext();
5353 SMDiagnostic Err;
5454 return parseAssemblyString(ModuleStrig, Err, C);
5555 }
3030 protected:
3131 void ParseAssembly(const char *Assembly) {
3232 SMDiagnostic Error;
33 M = parseAssemblyString(Assembly, Error, getGlobalContext());
33 M = parseAssemblyString(Assembly, Error, Context);
3434
3535 std::string errMsg;
3636 raw_string_ostream os(errMsg);
111111 PM.run(*M);
112112 }
113113
114 LLVMContext Context;
114115 std::unique_ptr M;
115116 Instruction *A, *B;
116117 };
208208 int &RunCount;
209209 };
210210
211 std::unique_ptr parseIR(const char *IR) {
212 LLVMContext &C = getGlobalContext();
211 std::unique_ptr parseIR(LLVMContext &C, const char *IR) {
213212 SMDiagnostic Err;
214213 return parseAssemblyString(IR, Err, C);
215214 }
216215
217216 class CGSCCPassManagerTest : public ::testing::Test {
218217 protected:
218 LLVMContext Context;
219219 std::unique_ptr M;
220220
221221 public:
222222 CGSCCPassManagerTest()
223 : M(parseIR("define void @f() {\n"
224 "entry:\n"
225 " call void @g()\n"
226 " call void @h1()\n"
227 " ret void\n"
228 "}\n"
229 "define void @g() {\n"
230 "entry:\n"
231 " call void @g()\n"
232 " call void @x()\n"
233 " ret void\n"
234 "}\n"
235 "define void @h1() {\n"
236 "entry:\n"
237 " call void @h2()\n"
238 " ret void\n"
239 "}\n"
240 "define void @h2() {\n"
241 "entry:\n"
242 " call void @h3()\n"
243 " call void @x()\n"
244 " ret void\n"
245 "}\n"
246 "define void @h3() {\n"
247 "entry:\n"
248 " call void @h1()\n"
249 " ret void\n"
250 "}\n"
251 "define void @x() {\n"
252 "entry:\n"
253 " ret void\n"
254 "}\n"
255 )) {}
223 : M(parseIR(Context, "define void @f() {\n"
224 "entry:\n"
225 " call void @g()\n"
226 " call void @h1()\n"
227 " ret void\n"
228 "}\n"
229 "define void @g() {\n"
230 "entry:\n"
231 " call void @g()\n"
232 " call void @x()\n"
233 " ret void\n"
234 "}\n"
235 "define void @h1() {\n"
236 "entry:\n"
237 " call void @h2()\n"
238 " ret void\n"
239 "}\n"
240 "define void @h2() {\n"
241 "entry:\n"
242 " call void @h3()\n"
243 " call void @x()\n"
244 " ret void\n"
245 "}\n"
246 "define void @h3() {\n"
247 "entry:\n"
248 " call void @h1()\n"
249 " ret void\n"
250 "}\n"
251 "define void @x() {\n"
252 "entry:\n"
253 " ret void\n"
254 "}\n")) {}
256255 };
257256
258257 TEST_F(CGSCCPassManagerTest, Basic) {
4343 }
4444
4545 TEST(CallGraphTest, GraphTraitsSpecialization) {
46 Module M("", getGlobalContext());
46 LLVMContext Context;
47 Module M("", Context);
4748 CallGraph CG(M);
4849
4950 canSpecializeGraphTraitsIterators(&CG);
5051 }
5152
5253 TEST(CallGraphTest, GraphTraitsConstSpecialization) {
53 Module M("", getGlobalContext());
54 LLVMContext Context;
55 Module M("", Context);
5456 CallGraph CG(M);
5557
5658 canSpecializeGraphTraitsIterators(const_cast(&CG));
2020
2121 namespace {
2222
23 std::unique_ptr parseAssembly(const char *Assembly) {
23 std::unique_ptr parseAssembly(LLVMContext &Context,
24 const char *Assembly) {
2425 SMDiagnostic Error;
25 std::unique_ptr M =
26 parseAssemblyString(Assembly, Error, getGlobalContext());
26 std::unique_ptr M = parseAssemblyString(Assembly, Error, Context);
2727
2828 std::string ErrMsg;
2929 raw_string_ostream OS(ErrMsg);
120120 "}\n";
121121
122122 TEST(LazyCallGraphTest, BasicGraphFormation) {
123 std::unique_ptr M = parseAssembly(DiamondOfTriangles);
123 LLVMContext Context;
124 std::unique_ptr M = parseAssembly(Context, DiamondOfTriangles);
124125 LazyCallGraph CG(*M);
125126
126127 // The order of the entry nodes should be stable w.r.t. the source order of
279280 }
280281
281282 TEST(LazyCallGraphTest, BasicGraphMutation) {
282 std::unique_ptr M = parseAssembly(
283 "define void @a() {\n"
284 "entry:\n"
285 " call void @b()\n"
286 " call void @c()\n"
287 " ret void\n"
288 "}\n"
289 "define void @b() {\n"
290 "entry:\n"
291 " ret void\n"
292 "}\n"
293 "define void @c() {\n"
294 "entry:\n"
295 " ret void\n"
296 "}\n");
283 LLVMContext Context;
284 std::unique_ptr M = parseAssembly(Context, "define void @a() {\n"
285 "entry:\n"
286 " call void @b()\n"
287 " call void @c()\n"
288 " ret void\n"
289 "}\n"
290 "define void @b() {\n"
291 "entry:\n"
292 " ret void\n"
293 "}\n"
294 "define void @c() {\n"
295 "entry:\n"
296 " ret void\n"
297 "}\n");
297298 LazyCallGraph CG(*M);
298299
299300 LazyCallGraph::Node &A = CG.get(lookupFunction(*M, "a"));
327328 }
328329
329330 TEST(LazyCallGraphTest, InnerSCCFormation) {
330 std::unique_ptr M = parseAssembly(DiamondOfTriangles);
331 LLVMContext Context;
332 std::unique_ptr M = parseAssembly(Context, DiamondOfTriangles);
331333 LazyCallGraph CG(*M);
332334
333335 // Now mutate the graph to connect every node into a single RefSCC to ensure
390392 }
391393
392394 TEST(LazyCallGraphTest, MultiArmSCC) {
395 LLVMContext Context;
393396 // Two interlocking cycles. The really useful thing about this SCC is that it
394397 // will require Tarjan's DFS to backtrack and finish processing all of the
395398 // children of each node in the SCC. Since this involves call edges, both
396399 // Tarjan implementations will have to successfully navigate the structure.
397 std::unique_ptr M = parseAssembly(
398 "define void @f1() {\n"
399 "entry:\n"
400 " call void @f2()\n"
401 " call void @f4()\n"
402 " ret void\n"
403 "}\n"
404 "define void @f2() {\n"
405 "entry:\n"
406 " call void @f3()\n"
407 " ret void\n"
408 "}\n"
409 "define void @f3() {\n"
410 "entry:\n"
411 " call void @f1()\n"
412 " ret void\n"
413 "}\n"
414 "define void @f4() {\n"
415 "entry:\n"
416 " call void @f5()\n"
417 " ret void\n"
418 "}\n"
419 "define void @f5() {\n"
420 "entry:\n"
421 " call void @f1()\n"
422 " ret void\n"
423 "}\n");
400 std::unique_ptr M = parseAssembly(Context, "define void @f1() {\n"
401 "entry:\n"
402 " call void @f2()\n"
403 " call void @f4()\n"
404 " ret void\n"
405 "}\n"
406 "define void @f2() {\n"
407 "entry:\n"
408 " call void @f3()\n"
409 " ret void\n"
410 "}\n"
411 "define void @f3() {\n"
412 "entry:\n"
413 " call void @f1()\n"
414 " ret void\n"
415 "}\n"
416 "define void @f4() {\n"
417 "entry:\n"
418 " call void @f5()\n"
419 " ret void\n"
420 "}\n"
421 "define void @f5() {\n"
422 "entry:\n"
423 " call void @f1()\n"
424 " ret void\n"
425 "}\n");
424426 LazyCallGraph CG(*M);
425427
426428 // Force the graph to be fully expanded.
450452 }
451453
452454 TEST(LazyCallGraphTest, OutgoingEdgeMutation) {
453 std::unique_ptr M = parseAssembly(
454 "define void @a() {\n"
455 "entry:\n"
456 " call void @b()\n"
457 " call void @c()\n"
458 " ret void\n"
459 "}\n"
460 "define void @b() {\n"
461 "entry:\n"
462 " call void @d()\n"
463 " ret void\n"
464 "}\n"
465 "define void @c() {\n"
466 "entry:\n"
467 " call void @d()\n"
468 " ret void\n"
469 "}\n"
470 "define void @d() {\n"
471 "entry:\n"
472 " ret void\n"
473 "}\n");
455 LLVMContext Context;
456 std::unique_ptr M = parseAssembly(Context, "define void @a() {\n"
457 "entry:\n"
458 " call void @b()\n"
459 " call void @c()\n"
460 " ret void\n"
461 "}\n"
462 "define void @b() {\n"
463 "entry:\n"
464 " call void @d()\n"
465 " ret void\n"
466 "}\n"
467 "define void @c() {\n"
468 "entry:\n"
469 " call void @d()\n"
470 " ret void\n"
471 "}\n"
472 "define void @d() {\n"
473 "entry:\n"
474 " ret void\n"
475 "}\n");
474476 LazyCallGraph CG(*M);
475477
476478 // Force the graph to be fully expanded.
574576 }
575577
576578 TEST(LazyCallGraphTest, IncomingEdgeInsertion) {
579 LLVMContext Context;
577580 // We want to ensure we can add edges even across complex diamond graphs, so
578581 // we use the diamond of triangles graph defined above. The ascii diagram is
579582 // repeated here for easy reference.
590593 // / \ |
591594 // a3--a2 |
592595 //
593 std::unique_ptr M = parseAssembly(DiamondOfTriangles);
596 std::unique_ptr M = parseAssembly(Context, DiamondOfTriangles);
594597 LazyCallGraph CG(*M);
595598
596599 // Force the graph to be fully expanded.
667670 }
668671
669672 TEST(LazyCallGraphTest, IncomingEdgeInsertionMidTraversal) {
673 LLVMContext Context;
670674 // This is the same fundamental test as the previous, but we perform it
671675 // having only partially walked the RefSCCs of the graph.
672 std::unique_ptr M = parseAssembly(DiamondOfTriangles);
676 std::unique_ptr M = parseAssembly(Context, DiamondOfTriangles);
673677 LazyCallGraph CG(*M);
674678
675679 // Walk the RefSCCs until we find the one containing 'c1'.
743747 }
744748
745749 TEST(LazyCallGraphTest, InternalEdgeMutation) {
746 std::unique_ptr M = parseAssembly(
747 "define void @a() {\n"
748 "entry:\n"
749 " call void @b()\n"
750 " ret void\n"
751 "}\n"
752 "define void @b() {\n"
753 "entry:\n"
754 " call void @c()\n"
755 " ret void\n"
756 "}\n"
757 "define void @c() {\n"
758 "entry:\n"
759 " call void @a()\n"
760 " ret void\n"
761 "}\n");
750 LLVMContext Context;
751 std::unique_ptr M = parseAssembly(Context, "define void @a() {\n"
752 "entry:\n"
753 " call void @b()\n"
754 " ret void\n"
755 "}\n"
756 "define void @b() {\n"
757 "entry:\n"
758 " call void @c()\n"
759 " ret void\n"
760 "}\n"
761 "define void @c() {\n"
762 "entry:\n"
763 " call void @a()\n"
764 " ret void\n"
765 "}\n");
762766 LazyCallGraph CG(*M);
763767
764768 // Force the graph to be fully expanded.
823827 }
824828
825829 TEST(LazyCallGraphTest, InternalEdgeRemoval) {
830 LLVMContext Context;
826831 // A nice fully connected (including self-edges) RefSCC.
827832 std::unique_ptr M = parseAssembly(
828 "define void @a(i8** %ptr) {\n"
829 "entry:\n"
830 " store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
831 " store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
832 " store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
833 " ret void\n"
834 "}\n"
835 "define void @b(i8** %ptr) {\n"
836 "entry:\n"
837 " store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
838 " store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
839 " store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
840 " ret void\n"
841 "}\n"
842 "define void @c(i8** %ptr) {\n"
843 "entry:\n"
844 " store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
845 " store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
846 " store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
847 " ret void\n"
848 "}\n");
833 Context, "define void @a(i8** %ptr) {\n"
834 "entry:\n"
835 " store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
836 " store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
837 " store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
838 " ret void\n"
839 "}\n"
840 "define void @b(i8** %ptr) {\n"
841 "entry:\n"
842 " store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
843 " store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
844 " store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
845 " ret void\n"
846 "}\n"
847 "define void @c(i8** %ptr) {\n"
848 "entry:\n"
849 " store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
850 " store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
851 " store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
852 " ret void\n"
853 "}\n");
849854 LazyCallGraph CG(*M);
850855
851856 // Force the graph to be fully expanded.
881886 }
882887
883888 TEST(LazyCallGraphTest, InternalCallEdgeToRef) {
889 LLVMContext Context;
884890 // A nice fully connected (including self-edges) SCC (and RefSCC)
885 std::unique_ptr M = parseAssembly(
886 "define void @a() {\n"
887 "entry:\n"
888 " call void @a()\n"
889 " call void @b()\n"
890 " call void @c()\n"
891 " ret void\n"
892 "}\n"
893 "define void @b() {\n"
894 "entry:\n"
895 " call void @a()\n"
896 " call void @b()\n"
897 " call void @c()\n"
898 " ret void\n"
899 "}\n"
900 "define void @c() {\n"
901 "entry:\n"
902 " call void @a()\n"
903 " call void @b()\n"
904 " call void @c()\n"
905 " ret void\n"
906 "}\n");
891 std::unique_ptr M = parseAssembly(Context, "define void @a() {\n"
892 "entry:\n"
893 " call void @a()\n"
894 " call void @b()\n"
895 " call void @c()\n"
896 " ret void\n"
897 "}\n"
898 "define void @b() {\n"
899 "entry:\n"
900 " call void @a()\n"
901 " call void @b()\n"
902 " call void @c()\n"
903 " ret void\n"
904 "}\n"
905 "define void @c() {\n"
906 "entry:\n"
907 " call void @a()\n"
908 " call void @b()\n"
909 " call void @c()\n"
910 " ret void\n"
911 "}\n");
907912 LazyCallGraph CG(*M);
908913
909914 // Force the graph to be fully expanded.
963968 }
964969
965970 TEST(LazyCallGraphTest, InternalRefEdgeToCall) {
971 LLVMContext Context;
966972 // Basic tests for making a ref edge a call. This hits the basics of the
967973 // process only.
968 std::unique_ptr M = parseAssembly(
969 "define void @a() {\n"
970 "entry:\n"
971 " call void @b()\n"
972 " call void @c()\n"
973 " store void()* @d, void()** undef\n"
974 " ret void\n"
975 "}\n"
976 "define void @b() {\n"
977 "entry:\n"
978 " store void()* @c, void()** undef\n"
979 " call void @d()\n"
980 " ret void\n"
981 "}\n"
982 "define void @c() {\n"
983 "entry:\n"
984 " store void()* @b, void()** undef\n"
985 " call void @d()\n"
986 " ret void\n"
987 "}\n"
988 "define void @d() {\n"
989 "entry:\n"
990 " store void()* @a, void()** undef\n"
991 " ret void\n"
992 "}\n");
974 std::unique_ptr M =
975 parseAssembly(Context, "define void @a() {\n"
976 "entry:\n"
977 " call void @b()\n"
978 " call void @c()\n"
979 " store void()* @d, void()** undef\n"
980 " ret void\n"
981 "}\n"
982 "define void @b() {\n"
983 "entry:\n"
984 " store void()* @c, void()** undef\n"
985 " call void @d()\n"
986 " ret void\n"
987 "}\n"
988 "define void @c() {\n"
989 "entry:\n"
990 " store void()* @b, void()** undef\n"
991 " call void @d()\n"
992 " ret void\n"
993 "}\n"
994 "define void @d() {\n"
995 "entry:\n"
996 " store void()* @a, void()** undef\n"
997 " ret void\n"
998 "}\n");
993999 LazyCallGraph CG(*M);
9941000
9951001 // Force the graph to be fully expanded.
10481054 }
10491055
10501056 TEST(LazyCallGraphTest, InternalRefEdgeToCallNoCycleInterleaved) {
1057 LLVMContext Context;
10511058 // Test for having a post-order prior to changing a ref edge to a call edge
10521059 // with SCCs connecting to the source and connecting to the target, but not
10531060 // connecting to both, interleaved between the source and target. This
10541061 // ensures we correctly partition the range rather than simply moving one or
10551062 // the other.
1056 std::unique_ptr M = parseAssembly(
1057 "define void @a() {\n"
1058 "entry:\n"
1059 " call void @b1()\n"
1060 " call void @c1()\n"
1061 " ret void\n"
1062 "}\n"
1063 "define void @b1() {\n"
1064 "entry:\n"
1065 " call void @c1()\n"
1066 " call void @b2()\n"
1067 " ret void\n"
1068 "}\n"
1069 "define void @c1() {\n"
1070 "entry:\n"
1071 " call void @b2()\n"
1072 " call void @c2()\n"
1073 " ret void\n"
1074 "}\n"
1075 "define void @b2() {\n"
1076 "entry:\n"
1077 " call void @c2()\n"
1078