Index: docs/tutorial/LangImpl5.rst =================================================================== --- docs/tutorial/LangImpl5.rst +++ docs/tutorial/LangImpl5.rst @@ -306,8 +306,8 @@ // end of the function. BasicBlock *ThenBB = BasicBlock::Create(LLVMContext, "then", TheFunction); - BasicBlock *ElseBB = BasicBlock::Create(LLVMContext, "else"); - BasicBlock *MergeBB = BasicBlock::Create(LLVMContext, "ifcont"); + BasicBlock *ElseBB = BasicBlock::Create(LLVMContext, "else", TheFunction); + BasicBlock *MergeBB = BasicBlock::Create(LLVMContext, "ifcont", TheFunction); Builder.CreateCondBr(CondV, ThenBB, ElseBB); @@ -319,18 +319,14 @@ into). Once it has that, it creates three blocks. Note that it passes -"TheFunction" into the constructor for the "then" block. This causes the +"TheFunction" into the constructor of all the blocks. This causes the constructor to automatically insert the new block into the end of the -specified function. The other two blocks are created, but aren't yet -inserted into the function. +specified function. Once the blocks are created, we can emit the conditional branch that chooses between them. Note that creating new blocks does not implicitly affect the IRBuilder, so it is still inserting into the block that the -condition went into. Also note that it is creating a branch to the -"then" block and the "else" block, even though the "else" block isn't -inserted into the function yet. This is all ok: it is the standard way -that LLVM supports forward references. +condition went into. .. code-block:: c++ @@ -376,7 +372,6 @@ .. code-block:: c++ // Emit else block. - TheFunction->getBasicBlockList().push_back(ElseBB); Builder.SetInsertPoint(ElseBB); Value *ElseV = Else->codegen(); @@ -388,16 +383,12 @@ ElseBB = Builder.GetInsertBlock(); Code generation for the 'else' block is basically identical to codegen -for the 'then' block. The only significant difference is the first line, -which adds the 'else' block to the function. Recall previously that the -'else' block was created, but not added to the function. Now that the -'then' and 'else' blocks are emitted, we can finish up with the merge -code: +for the 'then' block. Now that the 'then' and 'else' blocks are emitted, +we can finish up with the merge code: .. code-block:: c++ // Emit merge block. - TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(LLVMContext), 2, "iftmp"); @@ -407,10 +398,8 @@ return PN; } -The first two lines here are now familiar: the first adds the "merge" -block to the Function object (it was previously floating, like the else -block above). The second changes the insertion point so that newly -created code will go into the "merge" block. Once that is done, we need +The first line here is now familiar: it changes the insertion point so that +newly created code will go into the "merge" block. Once that is done, we need to create the PHI node and set up the block/value pairs for the PHI. Finally, the CodeGen function returns the phi node as the value computed Index: examples/Kaleidoscope/Chapter5/toy.cpp =================================================================== --- examples/Kaleidoscope/Chapter5/toy.cpp +++ examples/Kaleidoscope/Chapter5/toy.cpp @@ -604,11 +604,11 @@ Function *TheFunction = Builder.GetInsertBlock()->getParent(); - // Create blocks for the then and else cases. Insert the 'then' block at the + // Create blocks for the then and else cases. Insert the blocks at the // end of the function. BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); - BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); - BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); + BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else", TheFunction); + BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont", TheFunction); Builder.CreateCondBr(CondV, ThenBB, ElseBB); @@ -624,7 +624,6 @@ ThenBB = Builder.GetInsertBlock(); // Emit else block. - TheFunction->getBasicBlockList().push_back(ElseBB); Builder.SetInsertPoint(ElseBB); Value *ElseV = Else->codegen(); @@ -636,7 +635,6 @@ ElseBB = Builder.GetInsertBlock(); // Emit merge block. - TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp"); Index: examples/Kaleidoscope/Chapter6/toy.cpp =================================================================== --- examples/Kaleidoscope/Chapter6/toy.cpp +++ examples/Kaleidoscope/Chapter6/toy.cpp @@ -715,11 +715,11 @@ Function *TheFunction = Builder.GetInsertBlock()->getParent(); - // Create blocks for the then and else cases. Insert the 'then' block at the + // Create blocks for the then and else cases. Insert the blocks at the // end of the function. BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); - BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); - BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); + BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else", TheFunction); + BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont", TheFunction); Builder.CreateCondBr(CondV, ThenBB, ElseBB); @@ -735,7 +735,6 @@ ThenBB = Builder.GetInsertBlock(); // Emit else block. - TheFunction->getBasicBlockList().push_back(ElseBB); Builder.SetInsertPoint(ElseBB); Value *ElseV = Else->codegen(); @@ -747,7 +746,6 @@ ElseBB = Builder.GetInsertBlock(); // Emit merge block. - TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp"); Index: examples/Kaleidoscope/Chapter7/toy.cpp =================================================================== --- examples/Kaleidoscope/Chapter7/toy.cpp +++ examples/Kaleidoscope/Chapter7/toy.cpp @@ -820,11 +820,11 @@ Function *TheFunction = Builder.GetInsertBlock()->getParent(); - // Create blocks for the then and else cases. Insert the 'then' block at the + // Create blocks for the then and else cases. Insert the blocks at the // end of the function. BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); - BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); - BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); + BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else", TheFunction); + BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont", TheFunction); Builder.CreateCondBr(CondV, ThenBB, ElseBB); @@ -840,7 +840,6 @@ ThenBB = Builder.GetInsertBlock(); // Emit else block. - TheFunction->getBasicBlockList().push_back(ElseBB); Builder.SetInsertPoint(ElseBB); Value *ElseV = Else->codegen(); @@ -852,7 +851,6 @@ ElseBB = Builder.GetInsertBlock(); // Emit merge block. - TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp"); Index: examples/Kaleidoscope/Chapter8/toy.cpp =================================================================== --- examples/Kaleidoscope/Chapter8/toy.cpp +++ examples/Kaleidoscope/Chapter8/toy.cpp @@ -1010,11 +1010,11 @@ Function *TheFunction = Builder.GetInsertBlock()->getParent(); - // Create blocks for the then and else cases. Insert the 'then' block at the + // Create blocks for the then and else cases. Insert the blocks at the // end of the function. BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); - BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); - BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); + BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else", TheFunction); + BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont", TheFunction); Builder.CreateCondBr(CondV, ThenBB, ElseBB); @@ -1030,7 +1030,6 @@ ThenBB = Builder.GetInsertBlock(); // Emit else block. - TheFunction->getBasicBlockList().push_back(ElseBB); Builder.SetInsertPoint(ElseBB); Value *ElseV = Else->codegen(); @@ -1042,7 +1041,6 @@ ElseBB = Builder.GetInsertBlock(); // Emit merge block. - TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");