| OLD | NEW |
| 1 //===- subzero/src/IceCfgNode.cpp - Basic block (node) implementation -----===// | 1 //===- subzero/src/IceCfgNode.cpp - Basic block (node) 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 // This file implements the CfgNode class, including the complexities | 10 // This file implements the CfgNode class, including the complexities |
| (...skipping 738 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 } | 749 } |
| 750 | 750 |
| 751 // If this node contains only deleted instructions, and ends in an | 751 // If this node contains only deleted instructions, and ends in an |
| 752 // unconditional branch, contract the node by repointing all its | 752 // unconditional branch, contract the node by repointing all its |
| 753 // in-edges to its successor. | 753 // in-edges to its successor. |
| 754 void CfgNode::contractIfEmpty() { | 754 void CfgNode::contractIfEmpty() { |
| 755 if (InEdges.size() == 0) | 755 if (InEdges.size() == 0) |
| 756 return; | 756 return; |
| 757 Inst *Branch = NULL; | 757 Inst *Branch = NULL; |
| 758 for (Inst *I : Insts) { | 758 for (Inst *I : Insts) { |
| 759 if (!I->isDeleted() && !I->isUnconditionalBranch()) | 759 if (I->isDeleted()) |
| 760 continue; |
| 761 if (I->isUnconditionalBranch()) |
| 762 Branch = I; |
| 763 else if (!I->isRedundantAssign()) |
| 760 return; | 764 return; |
| 761 Branch = I; | |
| 762 } | 765 } |
| 763 Branch->setDeleted(); | 766 Branch->setDeleted(); |
| 764 assert(OutEdges.size() == 1); | 767 assert(OutEdges.size() == 1); |
| 765 // Repoint all this node's in-edges to this node's successor. | 768 // Repoint all this node's in-edges to this node's successor. |
| 766 for (CfgNode *Pred : InEdges) { | 769 for (CfgNode *Pred : InEdges) { |
| 767 for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); I != E; | 770 for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); I != E; |
| 768 ++I) { | 771 ++I) { |
| 769 if (*I == this) { | 772 if (*I == this) { |
| 770 *I = OutEdges[0]; | 773 *I = OutEdges[0]; |
| 771 OutEdges[0]->InEdges.push_back(Pred); | 774 OutEdges[0]->InEdges.push_back(Pred); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 791 // (currently not the case for x86 lowering). | 794 // (currently not the case for x86 lowering). |
| 792 for (Inst *I : Insts) { | 795 for (Inst *I : Insts) { |
| 793 if (!I->isDeleted()) { | 796 if (!I->isDeleted()) { |
| 794 Target->doBranchOpt(I, NextNode); | 797 Target->doBranchOpt(I, NextNode); |
| 795 } | 798 } |
| 796 } | 799 } |
| 797 } | 800 } |
| 798 | 801 |
| 799 // ======================== Dump routines ======================== // | 802 // ======================== Dump routines ======================== // |
| 800 | 803 |
| 804 namespace { |
| 805 |
| 806 // Helper functions for emit(). |
| 807 |
| 808 void emitRegisterUsage(Ostream &Str, const Cfg *Func, const CfgNode *Node, |
| 809 bool IsLiveIn, std::vector<SizeT> &LiveRegCount) { |
| 810 Liveness *Liveness = Func->getLiveness(); |
| 811 const LivenessBV *Live; |
| 812 if (IsLiveIn) { |
| 813 Live = &Liveness->getLiveIn(Node); |
| 814 Str << "\t\t\t\t# LiveIn="; |
| 815 } else { |
| 816 Live = &Liveness->getLiveOut(Node); |
| 817 Str << "\t\t\t\t# LiveOut="; |
| 818 } |
| 819 if (!Live->empty()) { |
| 820 bool First = true; |
| 821 for (SizeT i = 0; i < Live->size(); ++i) { |
| 822 if ((*Live)[i]) { |
| 823 Variable *Var = Liveness->getVariable(i, Node); |
| 824 if (Var->hasReg()) { |
| 825 if (IsLiveIn) |
| 826 ++LiveRegCount[Var->getRegNum()]; |
| 827 if (!First) |
| 828 Str << ","; |
| 829 First = false; |
| 830 Var->emit(Func); |
| 831 } |
| 832 } |
| 833 } |
| 834 } |
| 835 Str << "\n"; |
| 836 } |
| 837 |
| 838 void emitLiveRangesEnded(Ostream &Str, const Cfg *Func, const Inst *Instr, |
| 839 std::vector<SizeT> &LiveRegCount) { |
| 840 bool First = true; |
| 841 Variable *Dest = Instr->getDest(); |
| 842 if (Dest && Dest->hasReg()) |
| 843 ++LiveRegCount[Dest->getRegNum()]; |
| 844 for (SizeT I = 0; I < Instr->getSrcSize(); ++I) { |
| 845 Operand *Src = Instr->getSrc(I); |
| 846 SizeT NumVars = Src->getNumVars(); |
| 847 for (SizeT J = 0; J < NumVars; ++J) { |
| 848 const Variable *Var = Src->getVar(J); |
| 849 if (Var->hasReg()) { |
| 850 if (Instr->isLastUse(Var) && |
| 851 --LiveRegCount[Var->getRegNum()] == 0) { |
| 852 if (First) |
| 853 Str << " \t# END="; |
| 854 else |
| 855 Str << ","; |
| 856 Var->emit(Func); |
| 857 First = false; |
| 858 } |
| 859 } |
| 860 } |
| 861 } |
| 862 } |
| 863 |
| 864 } // end of anonymous namespace |
| 865 |
| 801 void CfgNode::emit(Cfg *Func) const { | 866 void CfgNode::emit(Cfg *Func) const { |
| 802 Func->setCurrentNode(this); | 867 Func->setCurrentNode(this); |
| 803 Ostream &Str = Func->getContext()->getStrEmit(); | 868 Ostream &Str = Func->getContext()->getStrEmit(); |
| 869 Liveness *Liveness = Func->getLiveness(); |
| 870 bool DecorateAsm = Liveness && Func->getContext()->getFlags().DecorateAsm; |
| 804 if (Func->getEntryNode() == this) { | 871 if (Func->getEntryNode() == this) { |
| 805 Str << Func->getContext()->mangleName(Func->getFunctionName()) << ":\n"; | 872 Str << Func->getContext()->mangleName(Func->getFunctionName()) << ":\n"; |
| 806 } | 873 } |
| 807 Str << getAsmName() << ":\n"; | 874 Str << getAsmName() << ":\n"; |
| 808 if (Func->useIntegratedAssembler()) { | 875 if (Func->useIntegratedAssembler()) { |
| 809 Assembler *Asm = Func->getAssembler<Assembler>(); | 876 Assembler *Asm = Func->getAssembler<Assembler>(); |
| 810 Asm->BindCfgNodeLabel(getIndex()); | 877 Asm->BindCfgNodeLabel(getIndex()); |
| 811 } | 878 } |
| 879 std::vector<SizeT> LiveRegCount(Func->getTarget()->getNumRegisters()); |
| 880 if (DecorateAsm) |
| 881 emitRegisterUsage(Str, Func, this, true, LiveRegCount); |
| 882 |
| 812 for (InstPhi *Phi : Phis) { | 883 for (InstPhi *Phi : Phis) { |
| 813 if (Phi->isDeleted()) | 884 if (Phi->isDeleted()) |
| 814 continue; | 885 continue; |
| 815 // Emitting a Phi instruction should cause an error. | 886 // Emitting a Phi instruction should cause an error. |
| 816 Inst *Instr = Phi; | 887 Inst *Instr = Phi; |
| 817 Instr->emit(Func); | 888 Instr->emit(Func); |
| 818 } | 889 } |
| 819 for (Inst *I : Insts) { | 890 for (Inst *I : Insts) { |
| 820 if (I->isDeleted()) | 891 if (I->isDeleted()) |
| 821 continue; | 892 continue; |
| 893 if (I->isRedundantAssign()) { |
| 894 Variable *Dest = I->getDest(); |
| 895 if (DecorateAsm && Dest->hasReg() && !I->isLastUse(I->getSrc(0))) |
| 896 ++LiveRegCount[Dest->getRegNum()]; |
| 897 continue; |
| 898 } |
| 822 if (Func->useIntegratedAssembler()) { | 899 if (Func->useIntegratedAssembler()) { |
| 823 I->emitIAS(Func); | 900 I->emitIAS(Func); |
| 824 } else { | 901 } else { |
| 825 I->emit(Func); | 902 I->emit(Func); |
| 903 if (DecorateAsm) |
| 904 emitLiveRangesEnded(Str, Func, I, LiveRegCount); |
| 826 Str << "\n"; | 905 Str << "\n"; |
| 827 } | 906 } |
| 828 // Update emitted instruction count, plus fill/spill count for | 907 // Update emitted instruction count, plus fill/spill count for |
| 829 // Variable operands without a physical register. | 908 // Variable operands without a physical register. |
| 830 if (uint32_t Count = I->getEmitInstCount()) { | 909 if (uint32_t Count = I->getEmitInstCount()) { |
| 831 Func->getContext()->statsUpdateEmitted(Count); | 910 Func->getContext()->statsUpdateEmitted(Count); |
| 832 if (Variable *Dest = I->getDest()) { | 911 if (Variable *Dest = I->getDest()) { |
| 833 if (!Dest->hasReg()) | 912 if (!Dest->hasReg()) |
| 834 Func->getContext()->statsUpdateFills(); | 913 Func->getContext()->statsUpdateFills(); |
| 835 } | 914 } |
| 836 for (SizeT S = 0; S < I->getSrcSize(); ++S) { | 915 for (SizeT S = 0; S < I->getSrcSize(); ++S) { |
| 837 if (Variable *Src = llvm::dyn_cast<Variable>(I->getSrc(S))) { | 916 if (Variable *Src = llvm::dyn_cast<Variable>(I->getSrc(S))) { |
| 838 if (!Src->hasReg()) | 917 if (!Src->hasReg()) |
| 839 Func->getContext()->statsUpdateSpills(); | 918 Func->getContext()->statsUpdateSpills(); |
| 840 } | 919 } |
| 841 } | 920 } |
| 842 } | 921 } |
| 843 } | 922 } |
| 923 if (DecorateAsm) |
| 924 emitRegisterUsage(Str, Func, this, false, LiveRegCount); |
| 844 } | 925 } |
| 845 | 926 |
| 846 void CfgNode::dump(Cfg *Func) const { | 927 void CfgNode::dump(Cfg *Func) const { |
| 847 Func->setCurrentNode(this); | 928 Func->setCurrentNode(this); |
| 848 Ostream &Str = Func->getContext()->getStrDump(); | 929 Ostream &Str = Func->getContext()->getStrDump(); |
| 849 Liveness *Liveness = Func->getLiveness(); | 930 Liveness *Liveness = Func->getLiveness(); |
| 850 if (Func->getContext()->isVerbose(IceV_Instructions)) { | 931 if (Func->getContext()->isVerbose(IceV_Instructions)) { |
| 851 Str << getName() << ":\n"; | 932 Str << getName() << ":\n"; |
| 852 } | 933 } |
| 853 // Dump list of predecessor nodes. | 934 // Dump list of predecessor nodes. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 if (!First) | 994 if (!First) |
| 914 Str << ", "; | 995 Str << ", "; |
| 915 First = false; | 996 First = false; |
| 916 Str << "%" << I->getName(); | 997 Str << "%" << I->getName(); |
| 917 } | 998 } |
| 918 Str << "\n"; | 999 Str << "\n"; |
| 919 } | 1000 } |
| 920 } | 1001 } |
| 921 | 1002 |
| 922 } // end of namespace Ice | 1003 } // end of namespace Ice |
| OLD | NEW |