More register optimizations
[fw/sdcc] / src / pic / pcode.c
index 2acf43f53ee03805b66d5a4acc2a6f8822216b1d..3e5aa27791e1be167fd575250408742b92cbf79f 100644 (file)
@@ -83,7 +83,7 @@ int GpcFlowSeq = 1;
 extern void RemoveUnusedRegisters(void);
 extern void RegsUnMapLiveRanges(void);
 extern void BuildFlowTree(pBlock *pb);
-extern void pCodeRegOptimizeRegUsage(void);
+extern void pCodeRegOptimizeRegUsage(int level);
 
 /****************************************************************/
 /*                      Forward declarations                    */
@@ -2224,16 +2224,22 @@ void pCodeReadCodeTable(void)
 /*-----------------------------------------------------------------*/
 void addpCode2pBlock(pBlock *pb, pCode *pc)
 {
+
+  if(!pc)
+    return;
+
   if(!pb->pcHead) {
     /* If this is the first pcode to be added to a block that
      * was initialized with a NULL pcode, then go ahead and
      * make this pcode the head and tail */
     pb->pcHead  = pb->pcTail = pc;
   } else {
+    //    if(pb->pcTail)
     pb->pcTail->next = pc;
+
     pc->prev = pb->pcTail;
-    //pc->next = NULL;
     pc->pb = pb;
+
     pb->pcTail = pc;
   }
 }
@@ -4315,7 +4321,11 @@ void pBlockMergeLabels(pBlock *pb)
 
   /* Now loop through the pBlock and merge the labels with the opcodes */
 
-  for(pc = pb->pcHead; pc; pc = pc->next) {
+  pc = pb->pcHead;
+  //  for(pc = pb->pcHead; pc; pc = pc->next) {
+
+  while(pc) {
+    pCode *pcn = pc->next;
 
     if(pc->type == PC_LABEL) {
 
@@ -4323,8 +4333,6 @@ void pBlockMergeLabels(pBlock *pb)
       //fprintf(stderr,"Checking label key = %d\n",PCL(pc)->key);
       if((pcnext = findNextInstruction(pc) )) {
 
-       pCode *pcn = pc->next;
-
        // Unlink the pCode label from it's pCode chain
        unlinkpCode(pc);
        
@@ -4340,10 +4348,7 @@ void pBlockMergeLabels(pBlock *pb)
        pbr->pc = pc;
        pbr->next = NULL;
 
-
        PCI(pcnext)->label = pBranchAppend(PCI(pcnext)->label,pbr);
-      
-       pc = pcn;
 
       } else {
        fprintf(stderr, "WARNING: couldn't associate label %s with an instruction\n",PCL(pc)->label);
@@ -4353,18 +4358,15 @@ void pBlockMergeLabels(pBlock *pb)
       /* merge the source line symbolic info into the next instruction */
       if((pcnext = findNextInstruction(pc) )) {
 
-       pCode *pcn = pc->next;
-
        // Unlink the pCode label from it's pCode chain
        unlinkpCode(pc);
        PCI(pcnext)->cline = PCCS(pc);
        //fprintf(stderr, "merging CSRC\n");
        //genericPrint(stderr,pcnext);
-       pc = pcn;
       }
 
     }
-
+    pc = pcn;
   }
   pBlockRemoveUnusedLabels(pb);
 
@@ -4579,9 +4581,14 @@ void mergepBlocks(char dbName)
 /*-----------------------------------------------------------------*/
 /* AnalyzeFlow - Examine the flow of the code and optimize         */
 /*                                                                 */
+/* level 0 == minimal optimization                                 */
+/*   optimize registers that are used only by two instructions     */
+/* level 1 == maximal optimization                                 */
+/*   optimize by looking at pairs of instructions that use the     */
+/*   register.                                                     */
 /*-----------------------------------------------------------------*/
 
-void AnalyzeFlow(void)
+void AnalyzeFlow(int level)
 {
   static int times_called=0;
 
@@ -4659,7 +4666,7 @@ void AnalyzeFlow(void)
   RemoveUnusedRegisters();
 
   //  for(pb = the_pFile->pbHead; pb; pb = pb->next)
-  pCodeRegOptimizeRegUsage();
+  pCodeRegOptimizeRegUsage(level);
 
   OptimizepCode('*');
 
@@ -4708,8 +4715,8 @@ void AnalyzeBanking(void)
    * to determine the Register Banks they use
    */
 
-  AnalyzeFlow();
-  AnalyzeFlow();
+  AnalyzeFlow(0);
+  AnalyzeFlow(1);
 
   for(pb = the_pFile->pbHead; pb; pb = pb->next)
     BanksUsedFlow(pb);