comparison gen/statements.cpp @ 1508:e1e93343fc11

Move function codegen data from IrFunction to new FuncGen. This change reduces memory consumption significantly by releasing the memory held by the STL containers that are now inside FuncGen.
author Christian Kamm <kamm incasoftware de>
date Sat, 20 Jun 2009 19:11:44 +0200
parents 303e6422f785
children ad7f2f1862d6
comparison
equal deleted inserted replaced
1503:cc5fee7836dc 1508:e1e93343fc11
294 294
295 // rewrite scope 295 // rewrite scope
296 gIR->scope() = IRScope(whilebodybb,endbb); 296 gIR->scope() = IRScope(whilebodybb,endbb);
297 297
298 // while body code 298 // while body code
299 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,whilebb,endbb)); 299 p->func()->gen->targetScopes.push_back(IRTargetScope(this,NULL,whilebb,endbb));
300 body->toIR(p); 300 body->toIR(p);
301 p->func()->targetScopes.pop_back(); 301 p->func()->gen->targetScopes.pop_back();
302 302
303 // loop 303 // loop
304 if (!gIR->scopereturned()) 304 if (!gIR->scopereturned())
305 llvm::BranchInst::Create(whilebb, gIR->scopebb()); 305 llvm::BranchInst::Create(whilebb, gIR->scopebb());
306 306
330 330
331 // replace current scope 331 // replace current scope
332 gIR->scope() = IRScope(dowhilebb,condbb); 332 gIR->scope() = IRScope(dowhilebb,condbb);
333 333
334 // do-while body code 334 // do-while body code
335 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,condbb,endbb)); 335 p->func()->gen->targetScopes.push_back(IRTargetScope(this,NULL,condbb,endbb));
336 body->toIR(p); 336 body->toIR(p);
337 p->func()->targetScopes.pop_back(); 337 p->func()->gen->targetScopes.pop_back();
338 338
339 // branch to condition block 339 // branch to condition block
340 llvm::BranchInst::Create(condbb, gIR->scopebb()); 340 llvm::BranchInst::Create(condbb, gIR->scopebb());
341 gIR->scope() = IRScope(condbb,endbb); 341 gIR->scope() = IRScope(condbb,endbb);
342 342
375 375
376 // move into the for condition block, ie. start the loop 376 // move into the for condition block, ie. start the loop
377 assert(!gIR->scopereturned()); 377 assert(!gIR->scopereturned());
378 llvm::BranchInst::Create(forbb, gIR->scopebb()); 378 llvm::BranchInst::Create(forbb, gIR->scopebb());
379 379
380 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,forincbb,endbb)); 380 p->func()->gen->targetScopes.push_back(IRTargetScope(this,NULL,forincbb,endbb));
381 381
382 // replace current scope 382 // replace current scope
383 gIR->scope() = IRScope(forbb,forbodybb); 383 gIR->scope() = IRScope(forbb,forbodybb);
384 384
385 // create the condition 385 // create the condition
418 418
419 // loop 419 // loop
420 if (!gIR->scopereturned()) 420 if (!gIR->scopereturned())
421 llvm::BranchInst::Create(forbb, gIR->scopebb()); 421 llvm::BranchInst::Create(forbb, gIR->scopebb());
422 422
423 p->func()->targetScopes.pop_back(); 423 p->func()->gen->targetScopes.pop_back();
424 424
425 // rewrite the scope 425 // rewrite the scope
426 gIR->scope() = IRScope(endbb,oldend); 426 gIR->scope() = IRScope(endbb,oldend);
427 } 427 }
428 428
452 while(tmp = targetLoopStatement->isScopeStatement()) 452 while(tmp = targetLoopStatement->isScopeStatement())
453 targetLoopStatement = tmp->statement; 453 targetLoopStatement = tmp->statement;
454 454
455 // find the right break block and jump there 455 // find the right break block and jump there
456 bool found = false; 456 bool found = false;
457 IrFunction::TargetScopeVec::reverse_iterator it = p->func()->targetScopes.rbegin(); 457 FuncGen::TargetScopeVec::reverse_iterator it = p->func()->gen->targetScopes.rbegin();
458 IrFunction::TargetScopeVec::reverse_iterator it_end = p->func()->targetScopes.rend(); 458 FuncGen::TargetScopeVec::reverse_iterator it_end = p->func()->gen->targetScopes.rend();
459 while(it != it_end) { 459 while(it != it_end) {
460 if(it->s == targetLoopStatement) { 460 if(it->s == targetLoopStatement) {
461 llvm::BranchInst::Create(it->breakTarget, p->scopebb()); 461 llvm::BranchInst::Create(it->breakTarget, p->scopebb());
462 found = true; 462 found = true;
463 break; 463 break;
466 } 466 }
467 assert(found); 467 assert(found);
468 } 468 }
469 else { 469 else {
470 // find closest scope with a break target 470 // find closest scope with a break target
471 IrFunction::TargetScopeVec::reverse_iterator it = p->func()->targetScopes.rbegin(); 471 FuncGen::TargetScopeVec::reverse_iterator it = p->func()->gen->targetScopes.rbegin();
472 IrFunction::TargetScopeVec::reverse_iterator it_end = p->func()->targetScopes.rend(); 472 FuncGen::TargetScopeVec::reverse_iterator it_end = p->func()->gen->targetScopes.rend();
473 while(it != it_end) { 473 while(it != it_end) {
474 if(it->breakTarget) { 474 if(it->breakTarget) {
475 break; 475 break;
476 } 476 }
477 ++it; 477 ++it;
507 while(tmp = targetLoopStatement->isScopeStatement()) 507 while(tmp = targetLoopStatement->isScopeStatement())
508 targetLoopStatement = tmp->statement; 508 targetLoopStatement = tmp->statement;
509 509
510 // find the right continue block and jump there 510 // find the right continue block and jump there
511 bool found = false; 511 bool found = false;
512 IrFunction::TargetScopeVec::reverse_iterator it = p->func()->targetScopes.rbegin(); 512 FuncGen::TargetScopeVec::reverse_iterator it = p->func()->gen->targetScopes.rbegin();
513 IrFunction::TargetScopeVec::reverse_iterator it_end = p->func()->targetScopes.rend(); 513 FuncGen::TargetScopeVec::reverse_iterator it_end = p->func()->gen->targetScopes.rend();
514 while(it != it_end) { 514 while(it != it_end) {
515 if(it->s == targetLoopStatement) { 515 if(it->s == targetLoopStatement) {
516 llvm::BranchInst::Create(it->continueTarget, gIR->scopebb()); 516 llvm::BranchInst::Create(it->continueTarget, gIR->scopebb());
517 found = true; 517 found = true;
518 break; 518 break;
521 } 521 }
522 assert(found); 522 assert(found);
523 } 523 }
524 else { 524 else {
525 // find closest scope with a continue target 525 // find closest scope with a continue target
526 IrFunction::TargetScopeVec::reverse_iterator it = p->func()->targetScopes.rbegin(); 526 FuncGen::TargetScopeVec::reverse_iterator it = p->func()->gen->targetScopes.rbegin();
527 IrFunction::TargetScopeVec::reverse_iterator it_end = p->func()->targetScopes.rend(); 527 FuncGen::TargetScopeVec::reverse_iterator it_end = p->func()->gen->targetScopes.rend();
528 while(it != it_end) { 528 while(it != it_end) {
529 if(it->continueTarget) { 529 if(it->continueTarget) {
530 break; 530 break;
531 } 531 }
532 ++it; 532 ++it;
590 // set up the landing pad 590 // set up the landing pad
591 // 591 //
592 p->scope() = IRScope(landingpadbb, endbb); 592 p->scope() = IRScope(landingpadbb, endbb);
593 593
594 assert(finalbody); 594 assert(finalbody);
595 gIR->func()->landingPadInfo.addFinally(finalbody); 595 IRLandingPad& pad = gIR->func()->gen->landingPadInfo;
596 gIR->func()->landingPadInfo.push(landingpadbb); 596 pad.addFinally(finalbody);
597 gIR->func()->targetScopes.push_back(IRTargetScope(this,new EnclosingTryFinally(this,gIR->func()->landingPad),NULL,NULL)); 597 pad.push(landingpadbb);
598 gIR->func()->landingPad = gIR->func()->landingPadInfo.get(); 598 gIR->func()->gen->targetScopes.push_back(IRTargetScope(this,new EnclosingTryFinally(this,gIR->func()->gen->landingPad),NULL,NULL));
599 gIR->func()->gen->landingPad = pad.get();
599 600
600 // 601 //
601 // do the try block 602 // do the try block
602 // 603 //
603 p->scope() = IRScope(trybb,finallybb); 604 p->scope() = IRScope(trybb,finallybb);
607 608
608 // terminate try BB 609 // terminate try BB
609 if (!p->scopereturned()) 610 if (!p->scopereturned())
610 llvm::BranchInst::Create(finallybb, p->scopebb()); 611 llvm::BranchInst::Create(finallybb, p->scopebb());
611 612
612 gIR->func()->landingPadInfo.pop(); 613 pad.pop();
613 gIR->func()->landingPad = gIR->func()->landingPadInfo.get(); 614 gIR->func()->gen->landingPad = pad.get();
614 gIR->func()->targetScopes.pop_back(); 615 gIR->func()->gen->targetScopes.pop_back();
615 616
616 // 617 //
617 // do finally block 618 // do finally block
618 // 619 //
619 p->scope() = IRScope(finallybb,landingpadbb); 620 p->scope() = IRScope(finallybb,landingpadbb);
655 // do catches and the landing pad 656 // do catches and the landing pad
656 // 657 //
657 assert(catches); 658 assert(catches);
658 gIR->scope() = IRScope(landingpadbb, endbb); 659 gIR->scope() = IRScope(landingpadbb, endbb);
659 660
661 IRLandingPad& pad = gIR->func()->gen->landingPadInfo;
660 for (int i = 0; i < catches->dim; i++) 662 for (int i = 0; i < catches->dim; i++)
661 { 663 {
662 Catch *c = (Catch *)catches->data[i]; 664 Catch *c = (Catch *)catches->data[i];
663 gIR->func()->landingPadInfo.addCatch(c, endbb); 665 pad.addCatch(c, endbb);
664 } 666 }
665 667
666 gIR->func()->landingPadInfo.push(landingpadbb); 668 pad.push(landingpadbb);
667 gIR->func()->landingPad = gIR->func()->landingPadInfo.get(); 669 gIR->func()->gen->landingPad = pad.get();
668 670
669 // 671 //
670 // do the try block 672 // do the try block
671 // 673 //
672 p->scope() = IRScope(trybb,landingpadbb); 674 p->scope() = IRScope(trybb,landingpadbb);
675 body->toIR(p); 677 body->toIR(p);
676 678
677 if (!gIR->scopereturned()) 679 if (!gIR->scopereturned())
678 llvm::BranchInst::Create(endbb, p->scopebb()); 680 llvm::BranchInst::Create(endbb, p->scopebb());
679 681
680 gIR->func()->landingPadInfo.pop(); 682 pad.pop();
681 gIR->func()->landingPad = gIR->func()->landingPadInfo.get(); 683 gIR->func()->gen->landingPad = pad.get();
682 684
683 // rewrite the scope 685 // rewrite the scope
684 p->scope() = IRScope(endbb,oldend); 686 p->scope() = IRScope(endbb,oldend);
685 } 687 }
686 688
861 863
862 // do switch body 864 // do switch body
863 assert(body); 865 assert(body);
864 866
865 p->scope() = IRScope(bodybb, endbb); 867 p->scope() = IRScope(bodybb, endbb);
866 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,NULL,endbb)); 868 p->func()->gen->targetScopes.push_back(IRTargetScope(this,NULL,NULL,endbb));
867 body->toIR(p); 869 body->toIR(p);
868 p->func()->targetScopes.pop_back(); 870 p->func()->gen->targetScopes.pop_back();
869 871
870 if (!p->scopereturned()) 872 if (!p->scopereturned())
871 llvm::BranchInst::Create(endbb, p->scopebb()); 873 llvm::BranchInst::Create(endbb, p->scopebb());
872 874
873 // add the cases 875 // add the cases
982 // update scope 984 // update scope
983 p->scope() = IRScope(thisbb,nextbb); 985 p->scope() = IRScope(thisbb,nextbb);
984 986
985 // push loop scope 987 // push loop scope
986 // continue goes to next statement, break goes to end 988 // continue goes to next statement, break goes to end
987 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); 989 p->func()->gen->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb));
988 990
989 // do statement 991 // do statement
990 s->toIR(p); 992 s->toIR(p);
991 993
992 // pop loop scope 994 // pop loop scope
993 p->func()->targetScopes.pop_back(); 995 p->func()->gen->targetScopes.pop_back();
994 996
995 // next stmt 997 // next stmt
996 if (!p->scopereturned()) 998 if (!p->scopereturned())
997 p->ir->CreateBr(nextbb); 999 p->ir->CreateBr(nextbb);
998 } 1000 }
1110 // Use the GEP as the address of the value variable. 1112 // Use the GEP as the address of the value variable.
1111 DtoRawVarDeclaration(value, gep); 1113 DtoRawVarDeclaration(value, gep);
1112 } 1114 }
1113 1115
1114 // emit body 1116 // emit body
1115 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); 1117 p->func()->gen->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb));
1116 if(body) 1118 if(body)
1117 body->toIR(p); 1119 body->toIR(p);
1118 p->func()->targetScopes.pop_back(); 1120 p->func()->gen->targetScopes.pop_back();
1119 1121
1120 if (!p->scopereturned()) 1122 if (!p->scopereturned())
1121 llvm::BranchInst::Create(nextbb, p->scopebb()); 1123 llvm::BranchInst::Create(nextbb, p->scopebb());
1122 1124
1123 // next 1125 // next
1206 v = p->ir->CreateSub(v, one); 1208 v = p->ir->CreateSub(v, one);
1207 DtoStore(v, keyval); 1209 DtoStore(v, keyval);
1208 } 1210 }
1209 1211
1210 // emit body 1212 // emit body
1211 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); 1213 p->func()->gen->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb));
1212 if (body) 1214 if (body)
1213 body->toIR(p); 1215 body->toIR(p);
1214 p->func()->targetScopes.pop_back(); 1216 p->func()->gen->targetScopes.pop_back();
1215 1217
1216 // jump to next iteration 1218 // jump to next iteration
1217 if (!p->scopereturned()) 1219 if (!p->scopereturned())
1218 llvm::BranchInst::Create(nextbb, p->scopebb()); 1220 llvm::BranchInst::Create(nextbb, p->scopebb());
1219 1221
1259 // disable inlining 1261 // disable inlining
1260 gIR->func()->setNeverInline(); 1262 gIR->func()->setNeverInline();
1261 } 1263 }
1262 else 1264 else
1263 { 1265 {
1264 std::string labelname = p->func()->getScopedLabelName(ident->toChars()); 1266 std::string labelname = p->func()->gen->getScopedLabelName(ident->toChars());
1265 llvm::BasicBlock*& labelBB = p->func()->labelToBB[labelname]; 1267 llvm::BasicBlock*& labelBB = p->func()->gen->labelToBB[labelname];
1266 1268
1267 llvm::BasicBlock* oldend = gIR->scopeend(); 1269 llvm::BasicBlock* oldend = gIR->scopeend();
1268 if (labelBB != NULL) { 1270 if (labelBB != NULL) {
1269 labelBB->moveBefore(oldend); 1271 labelBB->moveBefore(oldend);
1270 } else { 1272 } else {
1276 1278
1277 p->scope() = IRScope(labelBB,oldend); 1279 p->scope() = IRScope(labelBB,oldend);
1278 } 1280 }
1279 1281
1280 if (statement) { 1282 if (statement) {
1281 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,NULL,NULL)); 1283 p->func()->gen->targetScopes.push_back(IRTargetScope(this,NULL,NULL,NULL));
1282 statement->toIR(p); 1284 statement->toIR(p);
1283 p->func()->targetScopes.pop_back(); 1285 p->func()->gen->targetScopes.pop_back();
1284 } 1286 }
1285 } 1287 }
1286 1288
1287 ////////////////////////////////////////////////////////////////////////////// 1289 //////////////////////////////////////////////////////////////////////////////
1288 1290
1400 llsync = generate_unique_critical_section(); 1402 llsync = generate_unique_critical_section();
1401 DtoEnterCritical(llsync); 1403 DtoEnterCritical(llsync);
1402 } 1404 }
1403 1405
1404 // emit body 1406 // emit body
1405 p->func()->targetScopes.push_back(IRTargetScope(this,new EnclosingSynchro(this),NULL,NULL)); 1407 p->func()->gen->targetScopes.push_back(IRTargetScope(this,new EnclosingSynchro(this),NULL,NULL));
1406 body->toIR(p); 1408 body->toIR(p);
1407 p->func()->targetScopes.pop_back(); 1409 p->func()->gen->targetScopes.pop_back();
1408 1410
1409 // exit lock 1411 // exit lock
1410 // no point in a unreachable unlock, terminating statements must insert this themselves. 1412 // no point in a unreachable unlock, terminating statements must insert this themselves.
1411 if (p->scopereturned()) 1413 if (p->scopereturned())
1412 return; 1414 return;
1434 { 1436 {
1435 // load-store 1437 // load-store
1436 DtoMemoryBarrier(false, true, false, false); 1438 DtoMemoryBarrier(false, true, false, false);
1437 1439
1438 // do statement 1440 // do statement
1439 p->func()->targetScopes.push_back(IRTargetScope(this,new EnclosingVolatile(this),NULL,NULL)); 1441 p->func()->gen->targetScopes.push_back(IRTargetScope(this,new EnclosingVolatile(this),NULL,NULL));
1440 statement->toIR(p); 1442 statement->toIR(p);
1441 p->func()->targetScopes.pop_back(); 1443 p->func()->gen->targetScopes.pop_back();
1442 1444
1443 // no point in a unreachable barrier, terminating statements must insert this themselves. 1445 // no point in a unreachable barrier, terminating statements must insert this themselves.
1444 if (statement->blockExit() & BEfallthru) 1446 if (statement->blockExit() & BEfallthru)
1445 { 1447 {
1446 // store-load 1448 // store-load