diff options
author | Mehdi Amini <mehdi.amini@apple.com> | 2016-04-14 21:59:01 +0000 |
---|---|---|
committer | Mehdi Amini <mehdi.amini@apple.com> | 2016-04-14 21:59:01 +0000 |
commit | 03b42e41bf4bdd9ab05ea4c6905bae5d19971960 (patch) | |
tree | d4249845a564b11af673299653e7c4e876d8c4f2 /llvm/docs/tutorial | |
parent | 3d1c1deb04efe0022f11ebc18bb43d7341ba0c75 (diff) | |
download | llvm-03b42e41bf4bdd9ab05ea4c6905bae5d19971960.zip llvm-03b42e41bf4bdd9ab05ea4c6905bae5d19971960.tar.gz llvm-03b42e41bf4bdd9ab05ea4c6905bae5d19971960.tar.bz2 |
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>
llvm-svn: 266379
Diffstat (limited to 'llvm/docs/tutorial')
-rw-r--r-- | llvm/docs/tutorial/LangImpl3.rst | 12 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl4.rst | 3 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl5.rst | 22 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl6.rst | 4 | ||||
-rw-r--r-- | llvm/docs/tutorial/LangImpl7.rst | 4 |
5 files changed, 23 insertions, 22 deletions
diff --git a/llvm/docs/tutorial/LangImpl3.rst b/llvm/docs/tutorial/LangImpl3.rst index 89ecee9..a74f874 100644 --- a/llvm/docs/tutorial/LangImpl3.rst +++ b/llvm/docs/tutorial/LangImpl3.rst @@ -74,7 +74,7 @@ parser, which will be used to report errors found during code generation .. code-block:: c++ static std::unique_ptr<Module> *TheModule; - static IRBuilder<> Builder(getGlobalContext()); + static IRBuilder<> Builder(LLVMContext); static std::map<std::string, Value*> NamedValues; Value *LogErrorV(const char *Str) { @@ -116,7 +116,7 @@ First we'll do numeric literals: .. code-block:: c++ Value *NumberExprAST::codegen() { - return ConstantFP::get(getGlobalContext(), APFloat(Val)); + return ConstantFP::get(LLVMContext, APFloat(Val)); } In the LLVM IR, numeric constants are represented with the @@ -165,7 +165,7 @@ variables <LangImpl7.html#user-defined-local-variables>`_. case '<': L = Builder.CreateFCmpULT(L, R, "cmptmp"); // Convert bool 0/1 to double 0.0 or 1.0 - return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), + return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext), "booltmp"); default: return LogErrorV("invalid binary operator"); @@ -264,9 +264,9 @@ with: Function *PrototypeAST::codegen() { // Make the function type: double(double,double) etc. std::vector<Type*> Doubles(Args.size(), - Type::getDoubleTy(getGlobalContext())); + Type::getDoubleTy(LLVMContext)); FunctionType *FT = - FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); + FunctionType::get(Type::getDoubleTy(LLVMContext), Doubles, false); Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); @@ -340,7 +340,7 @@ assert that the function is empty (i.e. has no body yet) before we start. .. code-block:: c++ // Create a new basic block to start insertion into. - BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction); + BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction); Builder.SetInsertPoint(BB); // Record the function arguments in the NamedValues map. diff --git a/llvm/docs/tutorial/LangImpl4.rst b/llvm/docs/tutorial/LangImpl4.rst index a671d0c3..7668a29 100644 --- a/llvm/docs/tutorial/LangImpl4.rst +++ b/llvm/docs/tutorial/LangImpl4.rst @@ -131,7 +131,8 @@ for us: void InitializeModuleAndPassManager(void) { // Open a new module. - TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext()); + Context LLVMContext; + TheModule = llvm::make_unique<Module>("my cool jit", LLVMContext); TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout()); // Create a new pass manager attached to it. diff --git a/llvm/docs/tutorial/LangImpl5.rst b/llvm/docs/tutorial/LangImpl5.rst index e4dc8ab..eb76e66 100644 --- a/llvm/docs/tutorial/LangImpl5.rst +++ b/llvm/docs/tutorial/LangImpl5.rst @@ -292,7 +292,7 @@ for ``IfExprAST``: // Convert condition to a bool by comparing equal to 0.0. CondV = Builder.CreateFCmpONE( - CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond"); + CondV, ConstantFP::get(LLVMContext, APFloat(0.0)), "ifcond"); This code is straightforward and similar to what we saw before. We emit the expression for the condition, then compare that value to zero to get @@ -305,9 +305,9 @@ a truth value as a 1-bit (bool) value. // Create blocks for the then and else cases. Insert the 'then' block at the // end of the function. BasicBlock *ThenBB = - BasicBlock::Create(getGlobalContext(), "then", TheFunction); - BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else"); - BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont"); + BasicBlock::Create(LLVMContext, "then", TheFunction); + BasicBlock *ElseBB = BasicBlock::Create(LLVMContext, "else"); + BasicBlock *MergeBB = BasicBlock::Create(LLVMContext, "ifcont"); Builder.CreateCondBr(CondV, ThenBB, ElseBB); @@ -400,7 +400,7 @@ code: TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); PHINode *PN = - Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp"); + Builder.CreatePHI(Type::getDoubleTy(LLVMContext), 2, "iftmp"); PN->addIncoming(ThenV, ThenBB); PN->addIncoming(ElseV, ElseBB); @@ -625,7 +625,7 @@ expression). Function *TheFunction = Builder.GetInsertBlock()->getParent(); BasicBlock *PreheaderBB = Builder.GetInsertBlock(); BasicBlock *LoopBB = - BasicBlock::Create(getGlobalContext(), "loop", TheFunction); + BasicBlock::Create(LLVMContext, "loop", TheFunction); // Insert an explicit fall through from the current block to the LoopBB. Builder.CreateBr(LoopBB); @@ -642,7 +642,7 @@ the two blocks. Builder.SetInsertPoint(LoopBB); // Start the PHI node with an entry for Start. - PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), + PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(LLVMContext), 2, VarName.c_str()); Variable->addIncoming(StartVal, PreheaderBB); @@ -693,7 +693,7 @@ table. return nullptr; } else { // If not specified, use 1.0. - StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0)); + StepVal = ConstantFP::get(LLVMContext, APFloat(1.0)); } Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar"); @@ -712,7 +712,7 @@ iteration of the loop. // Convert condition to a bool by comparing equal to 0.0. EndCond = Builder.CreateFCmpONE( - EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond"); + EndCond, ConstantFP::get(LLVMContext, APFloat(0.0)), "loopcond"); Finally, we evaluate the exit value of the loop, to determine whether the loop should exit. This mirrors the condition evaluation for the @@ -723,7 +723,7 @@ if/then/else statement. // Create the "after loop" block and insert it. BasicBlock *LoopEndBB = Builder.GetInsertBlock(); BasicBlock *AfterBB = - BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction); + BasicBlock::Create(LLVMContext, "afterloop", TheFunction); // Insert the conditional branch into the end of LoopEndBB. Builder.CreateCondBr(EndCond, LoopBB, AfterBB); @@ -751,7 +751,7 @@ insertion position to it. NamedValues.erase(VarName); // for expr always returns 0.0. - return Constant::getNullValue(Type::getDoubleTy(getGlobalContext())); + return Constant::getNullValue(Type::getDoubleTy(LLVMContext)); } The final code handles various cleanups: now that we have the "NextVar" diff --git a/llvm/docs/tutorial/LangImpl6.rst b/llvm/docs/tutorial/LangImpl6.rst index 5a77d6d..2b6c2b1 100644 --- a/llvm/docs/tutorial/LangImpl6.rst +++ b/llvm/docs/tutorial/LangImpl6.rst @@ -251,7 +251,7 @@ default case for our existing binary operator node: case '<': L = Builder.CreateFCmpULT(L, R, "cmptmp"); // Convert bool 0/1 to double 0.0 or 1.0 - return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), + return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext), "booltmp"); default: break; @@ -288,7 +288,7 @@ The final piece of code we are missing, is a bit of top-level magic: BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence(); // Create a new basic block to start insertion into. - BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction); + BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction); Builder.SetInsertPoint(BB); if (Value *RetVal = Body->codegen()) { diff --git a/llvm/docs/tutorial/LangImpl7.rst b/llvm/docs/tutorial/LangImpl7.rst index 5d536bf8..efc38f62 100644 --- a/llvm/docs/tutorial/LangImpl7.rst +++ b/llvm/docs/tutorial/LangImpl7.rst @@ -339,7 +339,7 @@ the function: const std::string &VarName) { IRBuilder<> TmpB(&TheFunction->getEntryBlock(), TheFunction->getEntryBlock().begin()); - return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0, + return TmpB.CreateAlloca(Type::getDoubleTy(LLVMContext), 0, VarName.c_str()); } @@ -812,7 +812,7 @@ previous value that we replace in OldBindings. if (!InitVal) return nullptr; } else { // If not specified, use 0.0. - InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0)); + InitVal = ConstantFP::get(LLVMContext, APFloat(0.0)); } AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName); |