llvm.org GIT mirror llvm / df7d2b4
merge of 49966 from branches/ggreif/use-diet to trunk. these are already active API changes git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49968 91177308-0d34-0410-b5e6-96231b3b80d8 Gabor Greif 11 years ago
8 changed file(s) with 48 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
218218
219219

The foregoing is such an important principal, its worth making an idiom:

220220

                  
                
221 BasicBlock* bb = new BasicBlock();
222 bb->getInstList().push_back( new Branch( ... ) );
221 BasicBlock* bb = BasicBlock::Create();
222 bb->getInstList().push_back( BranchInst::Create( ... ) );
223223 new Instruction(..., bb->getTerminator() );
224224
225225

To make this clear, consider the typical if-then-else statement

231231 MyCompiler::handle_if( BasicBlock* bb, ICmpInst* condition )
232232 {
233233 // Create the blocks to contain code in the structure of if/then/else
234 BasicBlock* then_bb = new BasicBlock();
235 BasicBlock* else_bb = new BasicBlock();
236 BasicBlock* exit_bb = new BasicBlock();
234 BasicBlock* then_bb = BasicBlock::Create();
235 BasicBlock* else_bb = BasicBlock::Create();
236 BasicBlock* exit_bb = BasicBlock::Create();
237237
238238 // Insert the branch instruction for the "if"
239 bb->getInstList().push_back( new BranchInst( then_bb, else_bb, condition ) );
239 bb->getInstList().push_back( BranchInst::Create( then_bb, else_bb, condition ) );
240240
241241 // Set up the terminating instructions
242 then->getInstList().push_back( new BranchInst( exit_bb ) );
243 else->getInstList().push_back( new BranchInst( exit_bb ) );
242 then->getInstList().push_back( BranchInst::Create( exit_bb ) );
243 else->getInstList().push_back( BranchInst::Create( exit_bb ) );
244244
245245 // Fill in the then part .. details excised for brevity
246246 this->fill_in( then_bb );
309309 std::vector<Value*> index_vector;
310310 index_vector.push_back( ConstantInt::get( Type::LongTy, 0 );
311311 // ... push other indices ...
312 GetElementPtrInst* gep = new GetElementPtrInst( ptr, index_vector );
312 GetElementPtrInst* gep = GetElementPtrInst::Create( ptr, index_vector );
313313
314314

For example, suppose we have a global variable whose type is [24 x int]. The

315315 variable itself represents a pointer to that array. To subscript the
141141
142142
143143

                  
                
144 BasicBlock* block = new BasicBlock("entry", mul_add);
144 BasicBlock* block = BasicBlock::Create("entry", mul_add);
145145 IRBuilder builder(block);
146146
147147
9797
9898
9999

                  
                
100 BasicBlock* entry = new BasicBlock("entry", gcd);
101 BasicBlock* ret = new BasicBlock("return", gcd);
102 BasicBlock* cond_false = new BasicBlock("cond_false", gcd);
103 BasicBlock* cond_true = new BasicBlock("cond_true", gcd);
104 BasicBlock* cond_false_2 = new BasicBlock("cond_false", gcd);
100 BasicBlock* entry = BasicBlock::Create("entry", gcd);
101 BasicBlock* ret = BasicBlock::Create("return", gcd);
102 BasicBlock* cond_false = BasicBlock::Create("cond_false", gcd);
103 BasicBlock* cond_true = BasicBlock::Create("cond_true", gcd);
104 BasicBlock* cond_false_2 = BasicBlock::Create("cond_false", gcd);
105105
106106
107107
305305 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
306306 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
307307
308 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
308 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
309309
310310
311311
434434
435435

                  
                
436436 // Create a new basic block to start insertion into.
437 BasicBlock *BB = new BasicBlock("entry", TheFunction);
437 BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
438438 Builder.SetInsertPoint(BB);
439439
440440 if (Value *RetVal = Body->Codegen()) {
10781078 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
10791079 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
10801080
1081 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
1081 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
10821082
10831083 // If F conflicted, there was already something named 'Name'. If it has a
10841084 // body, don't allow redefinition or reextern.
11211121 return 0;
11221122
11231123 // Create a new basic block to start insertion into.
1124 BasicBlock *BB = new BasicBlock("entry", TheFunction);
1124 BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
11251125 Builder.SetInsertPoint(BB);
11261126
11271127 if (Value *RetVal = Body->Codegen()) {
912912 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
913913 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
914914
915 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
915 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
916916
917917 // If F conflicted, there was already something named 'Name'. If it has a
918918 // body, don't allow redefinition or reextern.
955955 return 0;
956956
957957 // Create a new basic block to start insertion into.
958 BasicBlock *BB = new BasicBlock("entry", TheFunction);
958 BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
959959 Builder.SetInsertPoint(BB);
960960
961961 if (Value *RetVal = Body->Codegen()) {
378378
379379 // Create blocks for the then and else cases. Insert the 'then' block at the
380380 // end of the function.
381 BasicBlock *ThenBB = new BasicBlock("then", TheFunction);
382 BasicBlock *ElseBB = new BasicBlock("else");
383 BasicBlock *MergeBB = new BasicBlock("ifcont");
381 BasicBlock *ThenBB = BasicBlock::Create("then", TheFunction);
382 BasicBlock *ElseBB = BasicBlock::Create("else");
383 BasicBlock *MergeBB = BasicBlock::Create("ifcont");
384384
385385 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
386386
726726 // block.
727727 Function *TheFunction = Builder.GetInsertBlock()->getParent();
728728 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
729 BasicBlock *LoopBB = new BasicBlock("loop", TheFunction);
729 BasicBlock *LoopBB = BasicBlock::Create("loop", TheFunction);
730730
731731 // Insert an explicit fall through from the current block to the LoopBB.
732732 Builder.CreateBr(LoopBB);
827827

                  
                
828828 // Create the "after loop" block and insert it.
829829 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
830 BasicBlock *AfterBB = new BasicBlock("afterloop", TheFunction);
830 BasicBlock *AfterBB = BasicBlock::Create("afterloop", TheFunction);
831831
832832 // Insert the conditional branch into the end of LoopEndBB.
833833 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
14161416
14171417 // Create blocks for the then and else cases. Insert the 'then' block at the
14181418 // end of the function.
1419 BasicBlock *ThenBB = new BasicBlock("then", TheFunction);
1420 BasicBlock *ElseBB = new BasicBlock("else");
1421 BasicBlock *MergeBB = new BasicBlock("ifcont");
1419 BasicBlock *ThenBB = BasicBlock::Create("then", TheFunction);
1420 BasicBlock *ElseBB = BasicBlock::Create("else");
1421 BasicBlock *MergeBB = BasicBlock::Create("ifcont");
14221422
14231423 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
14241424
14781478 // block.
14791479 Function *TheFunction = Builder.GetInsertBlock()->getParent();
14801480 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
1481 BasicBlock *LoopBB = new BasicBlock("loop", TheFunction);
1481 BasicBlock *LoopBB = BasicBlock::Create("loop", TheFunction);
14821482
14831483 // Insert an explicit fall through from the current block to the LoopBB.
14841484 Builder.CreateBr(LoopBB);
15241524
15251525 // Create the "after loop" block and insert it.
15261526 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
1527 BasicBlock *AfterBB = new BasicBlock("afterloop", TheFunction);
1527 BasicBlock *AfterBB = BasicBlock::Create("afterloop", TheFunction);
15281528
15291529 // Insert the conditional branch into the end of LoopEndBB.
15301530 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
15511551 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
15521552 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
15531553
1554 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
1554 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
15551555
15561556 // If F conflicted, there was already something named 'Name'. If it has a
15571557 // body, don't allow redefinition or reextern.
15941594 return 0;
15951595
15961596 // Create a new basic block to start insertion into.
1597 BasicBlock *BB = new BasicBlock("entry", TheFunction);
1597 BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
15981598 Builder.SetInsertPoint(BB);
15991599
16001600 if (Value *RetVal = Body->Codegen()) {
320320 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
321321
322322 // Create a new basic block to start insertion into.
323 BasicBlock *BB = new BasicBlock("entry", TheFunction);
323 BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
324324 Builder.SetInsertPoint(BB);
325325
326326 if (Value *RetVal = Body->Codegen()) {
14411441
14421442 // Create blocks for the then and else cases. Insert the 'then' block at the
14431443 // end of the function.
1444 BasicBlock *ThenBB = new BasicBlock("then", TheFunction);
1445 BasicBlock *ElseBB = new BasicBlock("else");
1446 BasicBlock *MergeBB = new BasicBlock("ifcont");
1444 BasicBlock *ThenBB = BasicBlock::Create("then", TheFunction);
1445 BasicBlock *ElseBB = BasicBlock::Create("else");
1446 BasicBlock *MergeBB = BasicBlock::Create("ifcont");
14471447
14481448 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
14491449
15031503 // block.
15041504 Function *TheFunction = Builder.GetInsertBlock()->getParent();
15051505 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
1506 BasicBlock *LoopBB = new BasicBlock("loop", TheFunction);
1506 BasicBlock *LoopBB = BasicBlock::Create("loop", TheFunction);
15071507
15081508 // Insert an explicit fall through from the current block to the LoopBB.
15091509 Builder.CreateBr(LoopBB);
15491549
15501550 // Create the "after loop" block and insert it.
15511551 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
1552 BasicBlock *AfterBB = new BasicBlock("afterloop", TheFunction);
1552 BasicBlock *AfterBB = BasicBlock::Create("afterloop", TheFunction);
15531553
15541554 // Insert the conditional branch into the end of LoopEndBB.
15551555 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
15761576 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
15771577 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
15781578
1579 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
1579 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
15801580
15811581 // If F conflicted, there was already something named 'Name'. If it has a
15821582 // body, don't allow redefinition or reextern.
16231623 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
16241624
16251625 // Create a new basic block to start insertion into.
1626 BasicBlock *BB = new BasicBlock("entry", TheFunction);
1626 BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
16271627 Builder.SetInsertPoint(BB);
16281628
16291629 if (Value *RetVal = Body->Codegen()) {
17211721
17221722 // Create blocks for the then and else cases. Insert the 'then' block at the
17231723 // end of the function.
1724 BasicBlock *ThenBB = new BasicBlock("then", TheFunction);
1725 BasicBlock *ElseBB = new BasicBlock("else");
1726 BasicBlock *MergeBB = new BasicBlock("ifcont");
1724 BasicBlock *ThenBB = BasicBlock::Create("then", TheFunction);
1725 BasicBlock *ElseBB = BasicBlock::Create("else");
1726 BasicBlock *MergeBB = BasicBlock::Create("ifcont");
17271727
17281728 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
17291729
17941794 // Make the new basic block for the loop header, inserting after current
17951795 // block.
17961796 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
1797 BasicBlock *LoopBB = new BasicBlock("loop", TheFunction);
1797 BasicBlock *LoopBB = BasicBlock::Create("loop", TheFunction);
17981798
17991799 // Insert an explicit fall through from the current block to the LoopBB.
18001800 Builder.CreateBr(LoopBB);
18401840
18411841 // Create the "after loop" block and insert it.
18421842 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
1843 BasicBlock *AfterBB = new BasicBlock("afterloop", TheFunction);
1843 BasicBlock *AfterBB = BasicBlock::Create("afterloop", TheFunction);
18441844
18451845 // Insert the conditional branch into the end of LoopEndBB.
18461846 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
19111911 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
19121912 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
19131913
1914 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
1914 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
19151915
19161916 // If F conflicted, there was already something named 'Name'. If it has a
19171917 // body, don't allow redefinition or reextern.
19711971 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
19721972
19731973 // Create a new basic block to start insertion into.
1974 BasicBlock *BB = new BasicBlock("entry", TheFunction);
1974 BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
19751975 Builder.SetInsertPoint(BB);
19761976
19771977 // Add all arguments to the symbol table and create their allocas.