Added phi elimination code - not final
authorRuchira Sasanka <sasanka@students.uiuc.edu>
Mon, 12 Nov 2001 14:44:50 +0000 (14:44 +0000)
committerRuchira Sasanka <sasanka@students.uiuc.edu>
Mon, 12 Nov 2001 14:44:50 +0000 (14:44 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1264 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/InstrSelection/InstrSelection.cpp
lib/Target/SparcV9/InstrSelection/InstrSelection.cpp

index fafe0234e34d62b9b2f922db69a37ca325c04247..b749873c4c50720e8ef2e0236699a7eb94d7c21a 100644 (file)
@@ -21,6 +21,8 @@
 #include "llvm/Instruction.h"
 #include "llvm/BasicBlock.h"
 #include "llvm/Method.h"
+#include "llvm/iOther.h"
+#include "llvm/Target/MachineRegInfo.h"
 
 
 //******************** Internal Data Declarations ************************/
@@ -57,6 +59,9 @@ static void PostprocessMachineCodeForTree(InstructionNode* instrNode,
                                           short* nts,
                                           TargetMachine &target);
 
+static void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target);
+
+
 
 //******************* Externally Visible Functions *************************/
 
@@ -125,6 +130,10 @@ SelectInstructionsForMethod(Method* method, TargetMachine &target)
            bbMvec.push_back(mvec[i]);
        }
     }
+
+  // Insert phi elimination code -- added by Ruchira
+  InsertCode4AllPhisInMeth(method, target);
+
   
   if (SelectDebugLevel >= Select_PrintMachineCode)
     {
@@ -140,6 +149,97 @@ SelectInstructionsForMethod(Method* method, TargetMachine &target)
 //*********************** Private Functions *****************************/
 
 
+//-------------------------------------------------------------------------
+// Thid method inserts a copy instruction to a predecessor BB as a result
+// of phi elimination.
+//-------------------------------------------------------------------------
+
+void InsertPhiElimInst(BasicBlock *BB, vector<MachineInstr*>& CopyInstVec) { // bak
+
+  TerminatorInst *TermInst = BB->getTerminator();
+  MachineCodeForVMInstr &MC4Term = TermInst->getMachineInstrVec();
+  MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
+
+  assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
+
+  // get an iterator to machine instructions in the BB
+  MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
+  MachineCodeForBasicBlock::iterator MCIt =  bbMvec.begin();
+
+  // find the position of first machine instruction generated by the
+  // terminator of this BB
+  for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt ) ;
+  
+  assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
+  assert( (CopyInstVec.size()==1) && "Must be only one copy instr");
+
+  // insert the copy instruction just before the first machine instruction
+  // generated for the terminator
+  bbMvec.insert( MCIt , CopyInstVec[0] );
+
+  cerr << "\nPhiElimination copy inst: " <<   *CopyInstVec[0];
+
+}
+
+
+//-------------------------------------------------------------------------
+// This method inserts phi elimination code for all BBs in a method
+//-------------------------------------------------------------------------
+void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target) {
+
+
+  // for all basic blocks in method
+  //
+  for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) {
+
+    BasicBlock *BB = *BI;
+    const BasicBlock::InstListType &InstList = BB->getInstList();
+    BasicBlock::InstListType::const_iterator  IIt = InstList.begin();
+
+    // for all instructions in the basic block
+    //
+    for( ; IIt != InstList.end(); ++IIt ) {
+
+      if( (*IIt)->getOpcode() == Instruction::PHINode ) {
+
+       PHINode *PN = (PHINode *) (*IIt);
+
+       // for each incoming value of the phi, insert phi elimination
+       //
+        for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
+
+         // insert the copy instruction to the predecessor BB
+
+         vector<MachineInstr*> CopyInstVec;
+
+         // target.getInstrInfo().CreateCopyInstructionsByType(
+         //    target, PN->getIncomingValue(i), PN, CopyInstVec );
+
+         MachineInstr *MI =
+           target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PN);
+
+         CopyInstVec.push_back( MI );
+
+         InsertPhiElimInst( PN->getIncomingBlock(i), CopyInstVec);
+
+         // Map the generated copy instruction in pred BB to this phi
+         // (PN->getMachineInstrVec()).push_back( CopyInstVec[0] );
+
+       }
+      }
+      else break;   // since PHI nodes can only be at the top
+      
+    }  // for each Phi Instr in BB
+
+  } // for all BBs in method
+
+}
+
+
+
+
+
+
 //---------------------------------------------------------------------------
 // Function AppendMachineCodeForVMInstr
 // 
index fafe0234e34d62b9b2f922db69a37ca325c04247..b749873c4c50720e8ef2e0236699a7eb94d7c21a 100644 (file)
@@ -21,6 +21,8 @@
 #include "llvm/Instruction.h"
 #include "llvm/BasicBlock.h"
 #include "llvm/Method.h"
+#include "llvm/iOther.h"
+#include "llvm/Target/MachineRegInfo.h"
 
 
 //******************** Internal Data Declarations ************************/
@@ -57,6 +59,9 @@ static void PostprocessMachineCodeForTree(InstructionNode* instrNode,
                                           short* nts,
                                           TargetMachine &target);
 
+static void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target);
+
+
 
 //******************* Externally Visible Functions *************************/
 
@@ -125,6 +130,10 @@ SelectInstructionsForMethod(Method* method, TargetMachine &target)
            bbMvec.push_back(mvec[i]);
        }
     }
+
+  // Insert phi elimination code -- added by Ruchira
+  InsertCode4AllPhisInMeth(method, target);
+
   
   if (SelectDebugLevel >= Select_PrintMachineCode)
     {
@@ -140,6 +149,97 @@ SelectInstructionsForMethod(Method* method, TargetMachine &target)
 //*********************** Private Functions *****************************/
 
 
+//-------------------------------------------------------------------------
+// Thid method inserts a copy instruction to a predecessor BB as a result
+// of phi elimination.
+//-------------------------------------------------------------------------
+
+void InsertPhiElimInst(BasicBlock *BB, vector<MachineInstr*>& CopyInstVec) { // bak
+
+  TerminatorInst *TermInst = BB->getTerminator();
+  MachineCodeForVMInstr &MC4Term = TermInst->getMachineInstrVec();
+  MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
+
+  assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
+
+  // get an iterator to machine instructions in the BB
+  MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
+  MachineCodeForBasicBlock::iterator MCIt =  bbMvec.begin();
+
+  // find the position of first machine instruction generated by the
+  // terminator of this BB
+  for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt ) ;
+  
+  assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
+  assert( (CopyInstVec.size()==1) && "Must be only one copy instr");
+
+  // insert the copy instruction just before the first machine instruction
+  // generated for the terminator
+  bbMvec.insert( MCIt , CopyInstVec[0] );
+
+  cerr << "\nPhiElimination copy inst: " <<   *CopyInstVec[0];
+
+}
+
+
+//-------------------------------------------------------------------------
+// This method inserts phi elimination code for all BBs in a method
+//-------------------------------------------------------------------------
+void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target) {
+
+
+  // for all basic blocks in method
+  //
+  for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) {
+
+    BasicBlock *BB = *BI;
+    const BasicBlock::InstListType &InstList = BB->getInstList();
+    BasicBlock::InstListType::const_iterator  IIt = InstList.begin();
+
+    // for all instructions in the basic block
+    //
+    for( ; IIt != InstList.end(); ++IIt ) {
+
+      if( (*IIt)->getOpcode() == Instruction::PHINode ) {
+
+       PHINode *PN = (PHINode *) (*IIt);
+
+       // for each incoming value of the phi, insert phi elimination
+       //
+        for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
+
+         // insert the copy instruction to the predecessor BB
+
+         vector<MachineInstr*> CopyInstVec;
+
+         // target.getInstrInfo().CreateCopyInstructionsByType(
+         //    target, PN->getIncomingValue(i), PN, CopyInstVec );
+
+         MachineInstr *MI =
+           target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PN);
+
+         CopyInstVec.push_back( MI );
+
+         InsertPhiElimInst( PN->getIncomingBlock(i), CopyInstVec);
+
+         // Map the generated copy instruction in pred BB to this phi
+         // (PN->getMachineInstrVec()).push_back( CopyInstVec[0] );
+
+       }
+      }
+      else break;   // since PHI nodes can only be at the top
+      
+    }  // for each Phi Instr in BB
+
+  } // for all BBs in method
+
+}
+
+
+
+
+
+
 //---------------------------------------------------------------------------
 // Function AppendMachineCodeForVMInstr
 //