| OLD | NEW |
| 1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// | 1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| (...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 } | 561 } |
| 562 | 562 |
| 563 void Cfg::doBranchOpt() { | 563 void Cfg::doBranchOpt() { |
| 564 TimerMarker T(TimerStack::TT_doBranchOpt, this); | 564 TimerMarker T(TimerStack::TT_doBranchOpt, this); |
| 565 for (auto I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | 565 for (auto I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
| 566 auto NextNode = I + 1; | 566 auto NextNode = I + 1; |
| 567 (*I)->doBranchOpt(NextNode == E ? nullptr : *NextNode); | 567 (*I)->doBranchOpt(NextNode == E ? nullptr : *NextNode); |
| 568 } | 568 } |
| 569 } | 569 } |
| 570 | 570 |
| 571 void Cfg::markNodesForSandboxing() { |
| 572 for (const InstJumpTable *JT : JumpTables) |
| 573 for (SizeT I = 0; I < JT->getNumTargets(); ++I) |
| 574 JT->getTarget(I)->setNeedsAlignment(); |
| 575 } |
| 576 |
| 571 // ======================== Dump routines ======================== // | 577 // ======================== Dump routines ======================== // |
| 572 | 578 |
| 573 // emitTextHeader() is not target-specific (apart from what is | 579 // emitTextHeader() is not target-specific (apart from what is |
| 574 // abstracted by the Assembler), so it is defined here rather than in | 580 // abstracted by the Assembler), so it is defined here rather than in |
| 575 // the target lowering class. | 581 // the target lowering class. |
| 576 void Cfg::emitTextHeader(const IceString &MangledName, GlobalContext *Ctx, | 582 void Cfg::emitTextHeader(const IceString &MangledName, GlobalContext *Ctx, |
| 577 const Assembler *Asm) { | 583 const Assembler *Asm) { |
| 578 if (!BuildDefs::dump()) | 584 if (!BuildDefs::dump()) |
| 579 return; | 585 return; |
| 580 Ostream &Str = Ctx->getStrEmit(); | 586 Ostream &Str = Ctx->getStrEmit(); |
| 581 Str << "\t.text\n"; | 587 Str << "\t.text\n"; |
| 582 if (Ctx->getFlags().getFunctionSections()) | 588 if (Ctx->getFlags().getFunctionSections()) |
| 583 Str << "\t.section\t.text." << MangledName << ",\"ax\",@progbits\n"; | 589 Str << "\t.section\t.text." << MangledName << ",\"ax\",@progbits\n"; |
| 584 if (!Asm->getInternal() || Ctx->getFlags().getDisableInternal()) { | 590 if (!Asm->getInternal() || Ctx->getFlags().getDisableInternal()) { |
| 585 Str << "\t.globl\t" << MangledName << "\n"; | 591 Str << "\t.globl\t" << MangledName << "\n"; |
| 586 Str << "\t.type\t" << MangledName << ",%function\n"; | 592 Str << "\t.type\t" << MangledName << ",%function\n"; |
| 587 } | 593 } |
| 588 Str << "\t" << Asm->getNonExecPadDirective() << " " | 594 Str << "\t" << Asm->getAlignDirective() << " " |
| 589 << Asm->getBundleAlignLog2Bytes() << ",0x"; | 595 << Asm->getBundleAlignLog2Bytes() << ",0x"; |
| 590 for (uint8_t I : Asm->getNonExecBundlePadding()) | 596 for (uint8_t I : Asm->getNonExecBundlePadding()) |
| 591 Str.write_hex(I); | 597 Str.write_hex(I); |
| 592 Str << "\n"; | 598 Str << "\n"; |
| 593 Str << MangledName << ":\n"; | 599 Str << MangledName << ":\n"; |
| 594 } | 600 } |
| 595 | 601 |
| 602 void Cfg::deleteJumpTableInsts() { |
| 603 for (InstJumpTable *JumpTable : JumpTables) |
| 604 JumpTable->setDeleted(); |
| 605 } |
| 606 |
| 607 void Cfg::emitJumpTables() { |
| 608 switch (Ctx->getFlags().getOutFileType()) { |
| 609 case FT_Elf: |
| 610 case FT_Iasm: { |
| 611 // The emission needs to be delayed until the after the text section so save |
| 612 // the offsets in the global context. |
| 613 IceString MangledName = Ctx->mangleName(getFunctionName()); |
| 614 for (const InstJumpTable *JumpTable : JumpTables) { |
| 615 SizeT NumTargets = JumpTable->getNumTargets(); |
| 616 JumpTableData &JT = |
| 617 Ctx->addJumpTable(MangledName, JumpTable->getId(), NumTargets); |
| 618 for (SizeT I = 0; I < NumTargets; ++I) { |
| 619 SizeT Index = JumpTable->getTarget(I)->getIndex(); |
| 620 JT.pushTarget( |
| 621 getAssembler()->getOrCreateCfgNodeLabel(Index)->getPosition()); |
| 622 } |
| 623 } |
| 624 } break; |
| 625 case FT_Asm: { |
| 626 // Emit the assembly directly so we don't need to hang on to all the names |
| 627 for (const InstJumpTable *JumpTable : JumpTables) |
| 628 getTarget()->emitJumpTable(this, JumpTable); |
| 629 } break; |
| 630 default: |
| 631 llvm::report_fatal_error("Invalid out file type."); |
| 632 break; |
| 633 } |
| 634 } |
| 635 |
| 596 void Cfg::emit() { | 636 void Cfg::emit() { |
| 597 if (!BuildDefs::dump()) | 637 if (!BuildDefs::dump()) |
| 598 return; | 638 return; |
| 599 TimerMarker T(TimerStack::TT_emit, this); | 639 TimerMarker T(TimerStack::TT_emit, this); |
| 600 if (Ctx->getFlags().getDecorateAsm()) { | 640 if (Ctx->getFlags().getDecorateAsm()) { |
| 601 renumberInstructions(); | 641 renumberInstructions(); |
| 602 getVMetadata()->init(VMK_Uses); | 642 getVMetadata()->init(VMK_Uses); |
| 603 liveness(Liveness_Basic); | 643 liveness(Liveness_Basic); |
| 604 dump("After recomputing liveness for -decorate-asm"); | 644 dump("After recomputing liveness for -decorate-asm"); |
| 605 } | 645 } |
| 606 OstreamLocker L(Ctx); | 646 OstreamLocker L(Ctx); |
| 607 Ostream &Str = Ctx->getStrEmit(); | 647 Ostream &Str = Ctx->getStrEmit(); |
| 608 IceString MangledName = getContext()->mangleName(getFunctionName()); | 648 IceString MangledName = Ctx->mangleName(getFunctionName()); |
| 609 emitTextHeader(MangledName, Ctx, getAssembler<>()); | 649 const Assembler *Asm = getAssembler<>(); |
| 610 for (CfgNode *Node : Nodes) | 650 const bool NeedSandboxing = Ctx->getFlags().getUseSandboxing(); |
| 651 |
| 652 emitTextHeader(MangledName, Ctx, Asm); |
| 653 deleteJumpTableInsts(); |
| 654 for (CfgNode *Node : Nodes) { |
| 655 if (NeedSandboxing && Node->needsAlignment()) { |
| 656 Str << "\t" << Asm->getAlignDirective() << " " |
| 657 << Asm->getBundleAlignLog2Bytes() << "\n"; |
| 658 } |
| 611 Node->emit(this); | 659 Node->emit(this); |
| 660 } |
| 661 emitJumpTables(); |
| 612 Str << "\n"; | 662 Str << "\n"; |
| 613 } | 663 } |
| 614 | 664 |
| 615 void Cfg::emitIAS() { | 665 void Cfg::emitIAS() { |
| 616 TimerMarker T(TimerStack::TT_emit, this); | 666 TimerMarker T(TimerStack::TT_emit, this); |
| 617 // The emitIAS() routines emit into the internal assembler buffer, | 667 // The emitIAS() routines emit into the internal assembler buffer, |
| 618 // so there's no need to lock the streams. | 668 // so there's no need to lock the streams. |
| 619 for (CfgNode *Node : Nodes) | 669 deleteJumpTableInsts(); |
| 670 const bool NeedSandboxing = Ctx->getFlags().getUseSandboxing(); |
| 671 for (CfgNode *Node : Nodes) { |
| 672 if (NeedSandboxing && Node->needsAlignment()) |
| 673 getAssembler()->alignCfgNode(); |
| 620 Node->emitIAS(this); | 674 Node->emitIAS(this); |
| 675 } |
| 676 emitJumpTables(); |
| 621 } | 677 } |
| 622 | 678 |
| 623 // Dumps the IR with an optional introductory message. | 679 // Dumps the IR with an optional introductory message. |
| 624 void Cfg::dump(const IceString &Message) { | 680 void Cfg::dump(const IceString &Message) { |
| 625 if (!BuildDefs::dump()) | 681 if (!BuildDefs::dump()) |
| 626 return; | 682 return; |
| 627 if (!isVerbose()) | 683 if (!isVerbose()) |
| 628 return; | 684 return; |
| 629 OstreamLocker L(Ctx); | 685 OstreamLocker L(Ctx); |
| 630 Ostream &Str = Ctx->getStrDump(); | 686 Ostream &Str = Ctx->getStrDump(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 660 } | 716 } |
| 661 } | 717 } |
| 662 // Print each basic block | 718 // Print each basic block |
| 663 for (CfgNode *Node : Nodes) | 719 for (CfgNode *Node : Nodes) |
| 664 Node->dump(this); | 720 Node->dump(this); |
| 665 if (isVerbose(IceV_Instructions)) | 721 if (isVerbose(IceV_Instructions)) |
| 666 Str << "}\n"; | 722 Str << "}\n"; |
| 667 } | 723 } |
| 668 | 724 |
| 669 } // end of namespace Ice | 725 } // end of namespace Ice |
| OLD | NEW |