From: John Criswell Date: Wed, 20 Oct 2004 14:38:39 +0000 (+0000) Subject: Small performance improvement in generated C code: X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=57bbfcec9172face0ffca2c1922d57d3d140bd43;p=oota-llvm.git Small performance improvement in generated C code: Instead of unconditionally copying all phi node values into temporaries for all successor blocks, generate code that will determine what successor block will be called and then copy only those phi node values needed by the successor block. This seems to cut down namd execution time from being 8% higher than GCC to 4% higher than GCC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17144 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 9e835f6093b..7af9bfe4fa6 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -204,6 +204,8 @@ namespace { } bool isGotoCodeNecessary(BasicBlock *From, BasicBlock *To); + void printPHICopiesForSuccessor(BasicBlock *CurBlock, + BasicBlock *Successor, unsigned Indent); void printPHICopiesForSuccessors(BasicBlock *CurBlock, unsigned Indent); void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock, @@ -1230,6 +1232,23 @@ bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) { return false; } +void CWriter::printPHICopiesForSuccessor (BasicBlock *CurBlock, + BasicBlock *Successor, + unsigned Indent) { + for (BasicBlock::iterator I = Successor->begin(); isa(I); ++I) { + PHINode *PN = cast(I); + // Now we have to do the printing. + Value *IV = PN->getIncomingValueForBlock(CurBlock); + if (!isa(IV)) { + Out << std::string(Indent, ' '); + Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = "; + writeOperand(IV); + Out << "; /* for PHI node */\n"; + } + } +} + + void CWriter::printPHICopiesForSuccessors(BasicBlock *CurBlock, unsigned Indent) { for (succ_iterator SI = succ_begin(CurBlock), E = succ_end(CurBlock); @@ -1261,7 +1280,6 @@ void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ, // that immediately succeeds the current one. // void CWriter::visitBranchInst(BranchInst &I) { - printPHICopiesForSuccessors(I.getParent(), 0); if (I.isConditional()) { if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) { @@ -1269,10 +1287,12 @@ void CWriter::visitBranchInst(BranchInst &I) { writeOperand(I.getCondition()); Out << ") {\n"; + printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 2); printBranchToBlock(I.getParent(), I.getSuccessor(0), 2); if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) { Out << " } else {\n"; + printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2); printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); } } else { @@ -1281,11 +1301,13 @@ void CWriter::visitBranchInst(BranchInst &I) { writeOperand(I.getCondition()); Out << ") {\n"; + printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2); printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); } Out << " }\n"; } else { + printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 0); printBranchToBlock(I.getParent(), I.getSuccessor(0), 0); } Out << "\n"; diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index 9e835f6093b..7af9bfe4fa6 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -204,6 +204,8 @@ namespace { } bool isGotoCodeNecessary(BasicBlock *From, BasicBlock *To); + void printPHICopiesForSuccessor(BasicBlock *CurBlock, + BasicBlock *Successor, unsigned Indent); void printPHICopiesForSuccessors(BasicBlock *CurBlock, unsigned Indent); void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock, @@ -1230,6 +1232,23 @@ bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) { return false; } +void CWriter::printPHICopiesForSuccessor (BasicBlock *CurBlock, + BasicBlock *Successor, + unsigned Indent) { + for (BasicBlock::iterator I = Successor->begin(); isa(I); ++I) { + PHINode *PN = cast(I); + // Now we have to do the printing. + Value *IV = PN->getIncomingValueForBlock(CurBlock); + if (!isa(IV)) { + Out << std::string(Indent, ' '); + Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = "; + writeOperand(IV); + Out << "; /* for PHI node */\n"; + } + } +} + + void CWriter::printPHICopiesForSuccessors(BasicBlock *CurBlock, unsigned Indent) { for (succ_iterator SI = succ_begin(CurBlock), E = succ_end(CurBlock); @@ -1261,7 +1280,6 @@ void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ, // that immediately succeeds the current one. // void CWriter::visitBranchInst(BranchInst &I) { - printPHICopiesForSuccessors(I.getParent(), 0); if (I.isConditional()) { if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) { @@ -1269,10 +1287,12 @@ void CWriter::visitBranchInst(BranchInst &I) { writeOperand(I.getCondition()); Out << ") {\n"; + printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 2); printBranchToBlock(I.getParent(), I.getSuccessor(0), 2); if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) { Out << " } else {\n"; + printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2); printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); } } else { @@ -1281,11 +1301,13 @@ void CWriter::visitBranchInst(BranchInst &I) { writeOperand(I.getCondition()); Out << ") {\n"; + printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2); printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); } Out << " }\n"; } else { + printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 0); printBranchToBlock(I.getParent(), I.getSuccessor(0), 0); } Out << "\n";