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/examples/Kaleidoscope/MCJIT/complete/toy.cpp | |
parent | 3d1c1deb04efe0022f11ebc18bb43d7341ba0c75 (diff) | |
download | bcm5719-llvm-03b42e41bf4bdd9ab05ea4c6905bae5d19971960.tar.gz bcm5719-llvm-03b42e41bf4bdd9ab05ea4c6905bae5d19971960.zip |
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/examples/Kaleidoscope/MCJIT/complete/toy.cpp')
-rw-r--r-- | llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp | 52 |
1 files changed, 24 insertions, 28 deletions
diff --git a/llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp b/llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp index c78ec35fa0b..40a00693e8c 100644 --- a/llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp +++ b/llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp @@ -1066,7 +1066,8 @@ void MCJITHelper::dump() //===----------------------------------------------------------------------===// static BaseHelper *TheHelper; -static IRBuilder<> Builder(getGlobalContext()); +static LLVMContext TheContext; +static IRBuilder<> Builder(TheContext); static std::map<std::string, AllocaInst*> NamedValues; Value *ErrorV(const char *Str) { Error(Str); return 0; } @@ -1077,12 +1078,11 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, const std::string &VarName) { IRBuilder<> TmpB(&TheFunction->getEntryBlock(), TheFunction->getEntryBlock().begin()); - return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0, - VarName.c_str()); + return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str()); } Value *NumberExprAST::Codegen() { - return ConstantFP::get(getGlobalContext(), APFloat(Val)); + return ConstantFP::get(TheContext, APFloat(Val)); } Value *VariableExprAST::Codegen() { @@ -1140,8 +1140,7 @@ Value *BinaryExprAST::Codegen() { 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()), - "booltmp"); + return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp"); default: break; } @@ -1182,17 +1181,16 @@ Value *IfExprAST::Codegen() { if (CondV == 0) return 0; // Convert condition to a bool by comparing equal to 0.0. - CondV = Builder.CreateFCmpONE(CondV, - ConstantFP::get(getGlobalContext(), APFloat(0.0)), - "ifcond"); + CondV = Builder.CreateFCmpONE( + CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); Function *TheFunction = Builder.GetInsertBlock()->getParent(); // 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 *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); + BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); + BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); Builder.CreateCondBr(CondV, ThenBB, ElseBB); @@ -1220,8 +1218,7 @@ Value *IfExprAST::Codegen() { // Emit merge block. TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); - PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, - "iftmp"); + PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp"); PN->addIncoming(ThenV, ThenBB); PN->addIncoming(ElseV, ElseBB); @@ -1263,7 +1260,7 @@ Value *ForExprAST::Codegen() { // Make the new basic block for the loop header, inserting after current // block. - BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction); + BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction); // Insert an explicit fall through from the current block to the LoopBB. Builder.CreateBr(LoopBB); @@ -1289,7 +1286,7 @@ Value *ForExprAST::Codegen() { if (StepVal == 0) return 0; } else { // If not specified, use 1.0. - StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0)); + StepVal = ConstantFP::get(TheContext, APFloat(1.0)); } // Compute the end condition. @@ -1303,12 +1300,12 @@ Value *ForExprAST::Codegen() { Builder.CreateStore(NextVar, Alloca); // Convert condition to a bool by comparing equal to 0.0. - EndCond = Builder.CreateFCmpONE(EndCond, - ConstantFP::get(getGlobalContext(), APFloat(0.0)), - "loopcond"); + EndCond = Builder.CreateFCmpONE( + EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); // Create the "after loop" block and insert it. - BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction); + BasicBlock *AfterBB = + BasicBlock::Create(TheContext, "afterloop", TheFunction); // Insert the conditional branch into the end of LoopEndBB. Builder.CreateCondBr(EndCond, LoopBB, AfterBB); @@ -1324,7 +1321,7 @@ Value *ForExprAST::Codegen() { // for expr always returns 0.0. - return Constant::getNullValue(Type::getDoubleTy(getGlobalContext())); + return Constant::getNullValue(Type::getDoubleTy(TheContext)); } Value *VarExprAST::Codegen() { @@ -1347,7 +1344,7 @@ Value *VarExprAST::Codegen() { InitVal = Init->Codegen(); if (InitVal == 0) return 0; } else { // If not specified, use 0.0. - InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0)); + InitVal = ConstantFP::get(TheContext, APFloat(0.0)); } AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName); @@ -1375,10 +1372,9 @@ Value *VarExprAST::Codegen() { Function *PrototypeAST::Codegen() { // Make the function type: double(double,double) etc. - std::vector<Type*> Doubles(Args.size(), - Type::getDoubleTy(getGlobalContext())); - FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), - Doubles, false); + std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext)); + FunctionType *FT = + FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false); std::string FnName; FnName = MakeLegalFunctionName(Name); @@ -1443,7 +1439,7 @@ Function *FunctionAST::Codegen() { 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(TheContext, "entry", TheFunction); Builder.SetInsertPoint(BB); // Add all arguments to the symbol table and create their allocas. @@ -1565,7 +1561,7 @@ int main(int argc, char **argv) { InitializeNativeTarget(); InitializeNativeTargetAsmPrinter(); InitializeNativeTargetAsmParser(); - LLVMContext &Context = getGlobalContext(); + LLVMContext &Context = TheContext; cl::ParseCommandLineOptions(argc, argv, "Kaleidoscope example program\n"); |