00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include "lego.H"
00050 #include "search.H"
00051 #include "addremove.H"
00052 #include "modify.H"
00053
00054
00055 #ifdef ADDREMOVE_DEBUG
00056 #define dprintf(s) fprintf s
00057 #define OUTF stderr
00058 #else
00059 #define dprintf(s)
00060 #endif // ADDREMOVE_DEBUG
00061
00062
00063 #define ADDREMOVE_REGION (0)
00064 #define ADDREMOVE_OP (1)
00065 #define ADDREMOVE_EDGE (2)
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 opList *
00087 AddToList (opList *list, legoOp *object, int valid = 1, double weight = 0.0)
00088 {
00089 opList *lprev, *l, *add;
00090
00091
00092 for (lprev = NULL, l = list; l != NULL;
00093 lprev = l, l = l->GetNextListPtr())
00094 {
00095 if (l->GetOpPtr() == object) return list;
00096 }
00097
00098 add = new opList;
00099 if (lprev != NULL)
00100 lprev->SetNextListPtr (add);
00101 else
00102 list = add;
00103
00104 add->SetOpPtr (object);
00105 add->SetOpId (object->GetOpId());
00106 add->SetValid (valid);
00107 add->SetWeight (weight);
00108
00109 return list;
00110 }
00111
00112 edgeList *
00113 AddToList (edgeList *list, opEdges *object, int valid = 1)
00114 {
00115 edgeList *lprev, *l, *add;
00116
00117
00118 for (lprev = NULL, l = list; l != NULL;
00119 lprev = l, l = l->GetNextListPtr())
00120 {
00121 if (l->GetEdgePtr() == object) return list;
00122 }
00123
00124 add = new edgeList;
00125 if (lprev != NULL)
00126 lprev->SetNextListPtr (add);
00127 else
00128 list = add;
00129
00130 add->SetEdgePtr (object);
00131 add->SetEdgeId (object->GetEdgeId());
00132 add->SetValid (valid);
00133
00134 return list;
00135 }
00136
00137 regionList *
00138 AddToList (regionList *list, legoRegion *object, int valid = 1)
00139 {
00140 regionList *lprev, *l, *add;
00141
00142
00143 for (lprev = NULL, l = list; l != NULL;
00144 lprev = l, l = l->GetNextListPtr())
00145 {
00146 if (l->GetRegionPtr() == object) return list;
00147 }
00148
00149 add = new regionList;
00150 if (lprev != NULL)
00151 lprev->SetNextListPtr (add);
00152 else
00153 list = add;
00154
00155 add->SetRegionPtr (object);
00156 add->SetValid (valid);
00157
00158 return list;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 opList *
00182 RemoveFromList (opList *list, legoOp *object, double weight = -1.0)
00183 {
00184 opList *l, *lprev, *remove;
00185
00186 if (list == NULL) return NULL;
00187
00188 for (lprev = NULL, l = list; l != NULL;
00189 lprev = l, l = l->GetNextListPtr())
00190 {
00191 if (weight != -1.0 && l->GetWeight() != weight) continue;
00192 if (l->GetOpPtr() == object) break;
00193 }
00194
00195 if (l == NULL) return list;
00196
00197 if (lprev != NULL)
00198 {
00199 lprev->SetNextListPtr (l->GetNextListPtr());
00200 remove = list;
00201 }
00202 else
00203 remove = l->GetNextListPtr();
00204
00205 l->SetNextListPtr (NULL);
00206 delete l;
00207
00208 return remove;
00209 }
00210
00211 edgeList *
00212 RemoveFromList (edgeList *list, opEdges *object)
00213 {
00214 edgeList *l, *lprev, *remove;
00215
00216 if (list == NULL) return NULL;
00217
00218 for (lprev = NULL, l = list; l->GetEdgePtr() != object && l != NULL;
00219 lprev = l, l = l->GetNextListPtr()) ;
00220
00221 if (l == NULL) return list;
00222
00223 if (lprev != NULL)
00224 {
00225 lprev->SetNextListPtr (l->GetNextListPtr());
00226 remove = list;
00227 }
00228 else
00229 remove = l->GetNextListPtr();
00230
00231 l->SetNextListPtr (NULL);
00232 delete l;
00233
00234 return remove;
00235 }
00236
00237 regionList *
00238 RemoveFromList (regionList *list, legoRegion *object)
00239 {
00240 regionList *l, *lprev, *remove;
00241
00242 if (list == NULL) return NULL;
00243
00244 for (lprev = NULL, l = list; l->GetRegionPtr() != object && l != NULL;
00245 lprev = l, l = l->GetNextListPtr()) ;
00246
00247 if (l == NULL) return list;
00248
00249 if (lprev != NULL)
00250 {
00251 lprev->SetNextListPtr (l->GetNextListPtr());
00252 remove = list;
00253 }
00254 else
00255 remove = l->GetNextListPtr();
00256
00257 l->SetNextListPtr (NULL);
00258 delete l;
00259
00260 return remove;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 opEdges *
00278 AddEdge (legoOp *fromop, legoOp *toop, enum edgeTypes et,
00279 int srcPort = 0, int destPort = 0)
00280 {
00281 legoProc *proc;
00282 opEdges *edgeprev, *edge;
00283 attrs *attr;
00284 int maxedgeid = -1;
00285
00286 proc = (legoProc *) FindParentRegionType (RT_PROC, fromop);
00287 if (proc == NULL)
00288 {
00289 LegoNonFatal ("AddEdge", "Couldn't find proc of op %d.",
00290 fromop->GetOpId());
00291 return NULL;
00292 }
00293
00294
00295 for (edgeprev = NULL, edge = proc->GetEdgeDictionary();
00296 edge != NULL;
00297 edgeprev = edge, edge = edge->GetNextOpEdgePtr())
00298 if (edge->GetFromOpPtr() == fromop &&
00299 edge->GetToOpPtr() == toop &&
00300 edge->GetEdgeType() == et &&
00301 edge->GetSrcPort() == srcPort &&
00302 edge->GetDestPort() == destPort)
00303 return NULL;
00304
00305 maxedgeid = FindMaxEdgeAttrId (proc);
00306
00307 edge = new opEdges();
00308 edge->SetEdgeId (maxedgeid + 1);
00309 edge->SetFromOpId (fromop->GetOpId());
00310 edge->SetFromOpPtr (fromop);
00311 edge->SetSrcPort (srcPort);
00312 edge->SetToOpId (toop->GetOpId());
00313 edge->SetToOpPtr (toop);
00314 edge->SetDestPort (destPort);
00315 edge->SetEdgeStatus (STATUS_CERTAIN);
00316 edge->SetEdgeLat (1);
00317 edge->SetEdgeType (et);
00318 edge->SetNextOpEdgePtr(NULL);
00319 switch (et)
00320 {
00321 case ET_CNTL:
00322 edge->SetSrcPortType (PT_CTL);
00323 edge->SetDestPortType (PT_CTL);
00324 break;
00325 case ET_REGFLOW:
00326 edge->SetSrcPortType (PT_DEST);
00327 edge->SetDestPortType (PT_SRC);
00328 break;
00329 case ET_REGANTI:
00330 edge->SetSrcPortType (PT_SRC);
00331 edge->SetDestPortType (PT_DEST);
00332 break;
00333 case ET_REGOUT:
00334 edge->SetSrcPortType (PT_DEST);
00335 edge->SetDestPortType (PT_DEST);
00336 break;
00337 case ET_MEM:
00338 edge->SetSrcPortType (PT_MEM);
00339 edge->SetDestPortType (PT_MEM);
00340 break;
00341 default:
00342 break;
00343 }
00344
00345 if (edgeprev != NULL)
00346 edgeprev->SetNextOpEdgePtr (edge);
00347 else
00348 proc->SetEdgeDictionary (edge);
00349
00350 return edge;
00351 }
00352
00353 opEdges *
00354 AddEdge (legoOp *fromop, legoOp *toop, legoProc *proc, enum edgeTypes et,
00355 int srcPort = 0, int destPort = 0)
00356 {
00357 LegoNonFatal ("AddEdge", "This is a deprecated version of this function.\n"
00358 "Remove the legoProc * parameter from the call.");
00359
00360 return AddEdge (fromop, toop, et, srcPort, destPort);
00361 }
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 void
00374 AddMidOp (legoOp *midop, legoOp *beforeop)
00375 {
00376 legoRegion *region;
00377 legoProc *proc;
00378 opEdges *edge;
00379 legoOp *afterop;
00380 opList *oplscan;
00381
00382 if (midop == NULL || beforeop == NULL)
00383 {
00384 LegoNonFatal ("AddMidOp", "Passed NULL argument.");
00385 return;
00386 }
00387
00388 proc = (legoProc *) FindParentRegionType (RT_PROC, beforeop);
00389 if (proc == NULL)
00390 {
00391 LegoNonFatal ("AddMidOp", "Couldn't find proc of op %d.",
00392 beforeop->GetOpId());
00393 return;
00394 }
00395
00396 afterop = beforeop->GetNextLink();
00397 if (afterop == NULL)
00398 {
00399 LegoNonFatal ("AddMidOp", "Couldn't find op after %d.",
00400 beforeop->GetOpId());
00401 return;
00402 }
00403
00404
00405 edge = FindControlEdge (beforeop, afterop);
00406 if (edge == NULL)
00407 {
00408 LegoNonFatal ("AddMidOp", "Couldn't find edge from beforeop to "
00409 "afterop.");
00410 return;
00411 }
00412
00413
00414 edge->SetToOpPtr (midop);
00415 edge->SetToOpId (midop->GetOpId());
00416 beforeop->SetNextLink (midop);
00417 midop->SetPrevLink (beforeop);
00418
00419
00420 region = (legoRegion *) beforeop->GetParentBlockPtr();
00421 region->AddItem (midop);
00422 midop->SetParentBlockPtr (region);
00423 midop->SetListOpPtr (proc->GetListOpPtr());
00424 proc->SetListOpPtr (midop);
00425
00426
00427 AddEdge (midop, afterop, ET_CNTL);
00428 midop->SetNextLink (afterop);
00429 afterop->SetPrevLink (midop);
00430
00431
00432 for (oplscan = beforeop->GetOutListPtr();
00433 oplscan != NULL;
00434 oplscan = oplscan->GetNextListPtr())
00435 {
00436 if (oplscan->GetOpId() == afterop->GetOpId())
00437 {
00438 oplscan->SetOpPtr (midop);
00439 oplscan->SetOpId (midop->GetOpId());
00440 break;
00441 }
00442 }
00443
00444 return;
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 int
00463 RemoveEdge (int fromopid, int toopid, legoProc *proc, enum edgeTypes et,
00464 int srcPort = EDGEPORT_DONTCARE, int destPort = EDGEPORT_DONTCARE)
00465 {
00466 opEdges *edgeprev, *edge;
00467 int retval = 0;
00468
00469 for (edgeprev = NULL, edge = proc->GetEdgeDictionary();
00470 edge != NULL;
00471 edgeprev = edge, edge = edge->GetNextOpEdgePtr())
00472 {
00473 if (edge->GetFromOpId() != fromopid || edge->GetToOpId() != toopid)
00474 continue;
00475 if (srcPort > EDGEPORT_DONTCARE && edge->GetSrcPort() != srcPort)
00476 continue;
00477 if (destPort > EDGEPORT_DONTCARE && edge->GetDestPort() != destPort)
00478 continue;
00479
00480 if (edgeprev != NULL)
00481 edgeprev->SetNextOpEdgePtr (edge->GetNextOpEdgePtr());
00482 else
00483 proc->SetEdgeDictionary (edge->GetNextOpEdgePtr());
00484 edge->SetNextOpEdgePtr (NULL);
00485 delete edge;
00486 retval = 1; break;
00487 }
00488
00489 return retval;
00490 }
00491
00492 int
00493 RemoveEdge (legoOp *fromop, legoOp *toop, enum edgeTypes et,
00494 int srcPort = EDGEPORT_DONTCARE, int destPort = EDGEPORT_DONTCARE)
00495 {
00496 legoProc *proc = (legoProc *) FindParentRegionType (RT_PROC, fromop);
00497 if (proc == NULL)
00498 {
00499 LegoNonFatal ("RemoveEdge", "Couldn't find proc of op %d.",
00500 fromop->GetOpId());
00501 return 0;
00502 }
00503
00504 return RemoveEdge (fromop->GetOpId(), toop->GetOpId(), proc, et, srcPort,
00505 destPort);
00506 }
00507
00508 int
00509 RemoveEdge (legoOp *fromop, legoOp *toop, legoProc *proc, enum edgeTypes et,
00510 int srcPort = EDGEPORT_DONTCARE, int destPort = EDGEPORT_DONTCARE)
00511 {
00512
00513
00514
00515 return RemoveEdge (fromop, toop, et, srcPort, destPort);
00516 }
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528 void
00529 RemoveMidOp (legoOp *midop, int destroy = 0)
00530 {
00531 legoProc *proc;
00532 legoOp *listop, *listopprev;
00533 opEdges *edgeprev, *edge;
00534 opList *oplscan;
00535 legoRegion *block;
00536 unsigned loc;
00537
00538 proc = (legoProc *) FindParentRegionType (RT_PROC, midop);
00539 if (proc == NULL)
00540 {
00541 LegoNonFatal ("RemoveMidOp", "Couldn't find proc of op %d.",
00542 midop->GetOpId());
00543 return;
00544 }
00545
00546 if (midop->GetPrevLink() == NULL || midop->GetNextLink() == NULL)
00547 {
00548 LegoNonFatal ("RemoveMidOp", "Op %d not in middle.", midop->GetOpId());
00549 return;
00550 }
00551
00552
00553 RemoveEdge (midop->GetOpId(), midop->GetNextLink()->GetOpId(), proc,
00554 ET_CNTL);
00555
00556 for (edgeprev = NULL, edge = proc->GetEdgeDictionary();
00557 edge != NULL &&
00558 (edge->GetFromOpId() != midop->GetPrevLink()->GetOpId() ||
00559 edge->GetToOpId() != midop->GetOpId());
00560 edgeprev = edge, edge = edge->GetNextOpEdgePtr())
00561 ;
00562 if (edge != NULL)
00563 {
00564 edge->SetToOpPtr (midop->GetNextLink());
00565 edge->SetToOpId (midop->GetNextLink()->GetOpId());
00566 }
00567
00568
00569
00570 for (oplscan = midop->GetPrevLink()->GetOutListPtr();
00571 oplscan != NULL;
00572 oplscan = oplscan->GetNextListPtr())
00573 {
00574 if (oplscan->GetOpId() == midop->GetOpId())
00575 {
00576 oplscan->SetOpPtr (midop->GetNextLink());
00577 oplscan->SetOpId (midop->GetNextLink()->GetOpId());
00578 break;
00579 }
00580 }
00581
00582
00583 midop->GetPrevLink()->SetNextLink (midop->GetNextLink());
00584 midop->GetNextLink()->SetPrevLink (midop->GetPrevLink());
00585 midop->SetPrevLink (NULL);
00586 midop->SetNextLink (NULL);
00587
00588
00589
00590 if ((listop = proc->GetListOpPtr()) != midop) {
00591 for (;
00592 listop != NULL && listop->GetListOpPtr() != midop;
00593 listop = listop->GetListOpPtr())
00594 ;
00595 }
00596 if (listop != NULL)
00597 {
00598 if (proc->GetListOpPtr() != midop)
00599 listop->SetListOpPtr (midop->GetListOpPtr());
00600 else
00601 proc->SetListOpPtr (midop->GetListOpPtr());
00602 }
00603 midop->SetListOpPtr (NULL);
00604
00605
00606 block = (legoRegion *) midop->GetParentBlockPtr();
00607 loc = block->Search (midop);
00608 if (loc != MAX_UNSIGNED)
00609 block->Detach (loc);
00610
00611 if (destroy)
00612 {
00613 attrList *atl;
00614 attrs *a;
00615 enum attrTypes atype;
00616
00617 RemoveLiveAttribute (midop, proc);
00618
00619 atl = midop->GetOpAttrListPtr();
00620 while (atl != NULL)
00621 {
00622 atype = atl->GetAttrType();
00623 if (atype != ATTR_LC)
00624 {
00625 atl = atl->GetNextListPtr(); continue;
00626 }
00627
00628
00629 a = atl->GetAttrPtr();
00630 RemoveLcAttribute (a->GetAttrString(), midop, proc);
00631
00632 atl = midop->GetOpAttrListPtr();
00633 }
00634 delete midop;
00635 }
00636 return;
00637 }
00638
00639 void
00640 RemoveMidOp (legoOp *midop, legoProc *proc, int destroy = 0)
00641 {
00642
00643
00644
00645 RemoveMidOp (midop, destroy);
00646 }
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 void
00659 RemoveFinalOp (legoOp *finalop, int destroy = 0)
00660 {
00661 legoProc *proc;
00662 legoOp *listop, *listopprev;
00663 opEdges *edgeprev, *edge;
00664 opList *oplscan;
00665 legoRegion *block;
00666 unsigned loc;
00667
00668 proc = (legoProc *) FindParentRegionType (RT_PROC, finalop);
00669 if (proc == NULL)
00670 {
00671 LegoNonFatal ("RemoveFinalOp", "Couldn't find proc of op %d.",
00672 finalop->GetOpId());
00673 return;
00674 }
00675
00676 if (finalop->GetPrevLink() == NULL || finalop->GetNextLink() != NULL)
00677 {
00678 LegoNonFatal ("RemoveFinalOp", "Op %d not the final op in block.", finalop->GetOpId());
00679 return;
00680 }
00681
00682
00683 RemoveEdge (finalop->GetPrevLink()->GetOpId(), finalop->GetOpId(), proc,
00684 ET_CNTL);
00685
00686
00687 finalop->GetPrevLink()->SetNextLink (NULL);
00688 finalop->SetPrevLink (NULL);
00689 finalop->SetNextLink (NULL);
00690
00691
00692
00693 if ((listop = proc->GetListOpPtr()) != finalop) {
00694 for (;
00695 listop != NULL && listop->GetListOpPtr() != finalop;
00696 listop = listop->GetListOpPtr())
00697 ;
00698 }
00699 if (listop != NULL)
00700 {
00701 if (proc->GetListOpPtr() != finalop)
00702 listop->SetListOpPtr (finalop->GetListOpPtr());
00703 else
00704 proc->SetListOpPtr (finalop->GetListOpPtr());
00705 }
00706 finalop->SetListOpPtr (NULL);
00707
00708
00709 block = (legoRegion *) finalop->GetParentBlockPtr();
00710 loc = block->Search (finalop);
00711 if (loc != MAX_UNSIGNED)
00712 block->Detach (loc);
00713
00714 if (destroy)
00715 {
00716 attrList *atl;
00717 attrs *a;
00718 enum attrTypes atype;
00719
00720 RemoveLiveAttribute (finalop, proc);
00721
00722 atl = finalop->GetOpAttrListPtr();
00723 while (atl != NULL)
00724 {
00725 atype = atl->GetAttrType();
00726 if (atype != ATTR_LC)
00727 {
00728 atl = atl->GetNextListPtr(); continue;
00729 }
00730
00731
00732 a = atl->GetAttrPtr();
00733 RemoveLcAttribute (a->GetAttrString(), finalop, proc);
00734
00735 atl = finalop->GetOpAttrListPtr();
00736 }
00737 delete finalop;
00738 }
00739 return;
00740 }
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750 void
00751 RemoveFirstOp (legoOp *firstop, int destroy = 0)
00752 {
00753 legoProc *proc;
00754 legoOp *listop, *listopprev;
00755 opEdges *edgeprev, *edge;
00756 opList *oplscan;
00757 legoRegion *block;
00758 unsigned loc;
00759
00760 proc = (legoProc *) FindParentRegionType (RT_PROC, firstop);
00761 if (proc == NULL)
00762 {
00763 LegoNonFatal ("RemoveFirstOp", "Couldn't find proc of op %d.",
00764 firstop->GetOpId());
00765 return;
00766 }
00767
00768 if (firstop->GetPrevLink() != NULL || firstop->GetNextLink() == NULL)
00769 {
00770 LegoNonFatal ("RemoveFirstOp", "Op %d not the first op in block.", firstop->GetOpId());
00771 return;
00772 }
00773
00774
00775 RemoveEdge (firstop->GetOpId(), firstop->GetNextLink()->GetOpId(), proc,
00776 ET_CNTL);
00777
00778
00779 firstop->GetNextLink()->SetPrevLink (NULL);
00780 firstop->SetPrevLink (NULL);
00781 firstop->SetNextLink (NULL);
00782
00783
00784
00785 if ((listop = proc->GetListOpPtr()) != firstop) {
00786 for (;
00787 listop != NULL && listop->GetListOpPtr() != firstop;
00788 listop = listop->GetListOpPtr())
00789 ;
00790 }
00791 if (listop != NULL)
00792 {
00793 if (proc->GetListOpPtr() != firstop)
00794 listop->SetListOpPtr (firstop->GetListOpPtr());
00795 else
00796 proc->SetListOpPtr (firstop->GetListOpPtr());
00797 }
00798 firstop->SetListOpPtr (NULL);
00799
00800
00801 block = (legoRegion *) firstop->GetParentBlockPtr();
00802 loc = block->Search (firstop);
00803 if (loc != MAX_UNSIGNED)
00804 block->Detach (loc);
00805
00806 if (destroy)
00807 {
00808 attrList *atl;
00809 attrs *a;
00810 enum attrTypes atype;
00811
00812 RemoveLiveAttribute (firstop, proc);
00813
00814 atl = firstop->GetOpAttrListPtr();
00815 while (atl != NULL)
00816 {
00817 atype = atl->GetAttrType();
00818 if (atype != ATTR_LC)
00819 {
00820 atl = atl->GetNextListPtr(); continue;
00821 }
00822
00823
00824 a = atl->GetAttrPtr();
00825 RemoveLcAttribute (a->GetAttrString(), firstop, proc);
00826
00827 atl = firstop->GetOpAttrListPtr();
00828 }
00829 delete firstop;
00830 }
00831 return;
00832 }
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842 void
00843 RemoveVeryLastOp (legoOp *verylastop, int destroy = 0)
00844 {
00845 legoProc *proc;
00846 legoOp *listop, *listopprev;
00847 opEdges *edgeprev, *edge;
00848 opList *oplscan;
00849 legoRegion *block;
00850 unsigned loc;
00851
00852 proc = (legoProc *) FindParentRegionType (RT_PROC, verylastop);
00853 if (proc == NULL)
00854 {
00855 LegoNonFatal ("RemoveVeryLastOp", "Couldn't find proc of op %d.",
00856 verylastop->GetOpId());
00857 return;
00858 }
00859
00860 if (verylastop->GetPrevLink() != NULL || verylastop->GetNextLink() != NULL)
00861 {
00862 LegoNonFatal ("RemoveVeryLastOp", "Op %d not the very last op in block.", verylastop->GetOpId());
00863 return;
00864 }
00865
00866
00867 verylastop->SetPrevLink (NULL);
00868 verylastop->SetNextLink (NULL);
00869
00870
00871
00872 if ((listop = proc->GetListOpPtr()) != verylastop) {
00873 for (;
00874 listop != NULL && listop->GetListOpPtr() != verylastop;
00875 listop = listop->GetListOpPtr())
00876 ;
00877 }
00878 if (listop != NULL)
00879 {
00880 if (proc->GetListOpPtr() != verylastop)
00881 listop->SetListOpPtr (verylastop->GetListOpPtr());
00882 else
00883 proc->SetListOpPtr (verylastop->GetListOpPtr());
00884 }
00885 verylastop->SetListOpPtr (NULL);
00886
00887
00888 block = (legoRegion *) verylastop->GetParentBlockPtr();
00889 loc = block->Search (verylastop);
00890 if (loc != MAX_UNSIGNED)
00891 block->Detach (loc);
00892
00893 if (destroy)
00894 {
00895 attrList *atl;
00896 attrs *a;
00897 enum attrTypes atype;
00898
00899 RemoveLiveAttribute (verylastop, proc);
00900
00901 atl = verylastop->GetOpAttrListPtr();
00902 while (atl != NULL)
00903 {
00904 atype = atl->GetAttrType();
00905 if (atype != ATTR_LC)
00906 {
00907 atl = atl->GetNextListPtr(); continue;
00908 }
00909
00910
00911 a = atl->GetAttrPtr();
00912 RemoveLcAttribute (a->GetAttrString(), verylastop, proc);
00913
00914 atl = verylastop->GetOpAttrListPtr();
00915 }
00916 delete verylastop;
00917 }
00918 return;
00919 }
00920
00921
00922
00923
00924
00925
00926
00927
00928 void RemoveLastOp(legoOp *last_op,int destroy = 0)
00929 {
00930 legoOp *prev_op = NULL;
00931 legoOp *list_op = NULL;
00932 legoProc *proc_ptr = NULL;
00933 opEdges *op_edge = NULL;
00934
00935
00936 if(!last_op){
00937 LegoNonFatal ("RemoveLastOp", "Empty argument passed");
00938 return;
00939 }
00940 if(!(prev_op = last_op->GetPrevLink())){
00941 LegoNonFatal ("RemoveLastOp", "Empty GetPrevLink");
00942 return;
00943 }
00944 if(!(proc_ptr = (legoProc *) FindParentRegionType (RT_PROC, last_op))){
00945 LegoNonFatal ("RemoveLastOp", "Couldn't find proc of op %d.",last_op->GetOpId());
00946 return;
00947 }
00948
00949
00950
00951 int prev_op_id = prev_op->GetOpId();
00952 int last_op_id = last_op->GetOpId();
00953 opEdges *edge_dict = proc_ptr->GetEdgeDictionary();
00954 opEdges *prev_op_edge = NULL;
00955
00956 for(op_edge=edge_dict;op_edge;op_edge=op_edge->GetNextOpEdgePtr()){
00957 if(op_edge->GetFromOpId()==prev_op_id && op_edge->GetToOpId()==last_op_id) break;
00958 prev_op_edge = op_edge;
00959 }
00960
00961
00962
00963 assert(op_edge!=NULL);
00964 prev_op_edge->SetNextOpEdgePtr(op_edge->GetNextOpEdgePtr());
00965
00966
00967
00968 prev_op->SetNextLink(NULL);
00969
00970 for(list_op=proc_ptr->GetListOpPtr();list_op!=NULL && list_op->GetListOpPtr()!=last_op;list_op=list_op->GetListOpPtr());
00971 if(list_op){
00972 if(list_op!=proc_ptr->GetListOpPtr()) list_op->SetListOpPtr(last_op->GetListOpPtr());
00973 else proc_ptr->SetListOpPtr(last_op->GetListOpPtr());
00974 }
00975 last_op->SetListOpPtr(NULL);
00976
00977
00978
00979
00980 legoRegion *bb = (legoRegion *)last_op->GetParentBlockPtr();
00981 unsigned loc = bb->Search(last_op);
00982 if(loc != MAX_UNSIGNED) bb->Detach(loc);
00983
00984
00985
00986 if (destroy){
00987 attrList *atl;
00988 attrs *a;
00989 enum attrTypes atype;
00990
00991 RemoveLiveAttribute (last_op, proc_ptr);
00992 atl = last_op->GetOpAttrListPtr();
00993 while(atl){
00994 atype = atl->GetAttrType();
00995 if (atype != ATTR_LC){
00996 atl = atl->GetNextListPtr();
00997 continue;
00998 }
00999
01000 a = atl->GetAttrPtr();
01001 RemoveLcAttribute (a->GetAttrString(), last_op, proc_ptr);
01002 atl = last_op->GetOpAttrListPtr();
01003 }
01004 delete last_op;
01005 }
01006 }
01007
01008 void RemoveLastOp(legoOp *last_op, legoProc *proc, int destroy = 0)
01009 {
01010 RemoveLastOp(last_op,destroy);
01011 }
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024 legoBB *
01025 AddBBAlongEdge (opEdges *edge, legoProc *proc)
01026 {
01027 opEdges *newedge;
01028 legoOp *fromop, *toop, *alllist, *top, *dummy_br;
01029 legoRegion *fromregion, *toregion, *parentregion, *r;
01030 legoBB *newblock;
01031 legoOprd *oprd;
01032 opList *opl;
01033 int id;
01034 unsigned loc;
01035 double fromweight;
01036
01037 if (edge == NULL || proc == NULL)
01038 {
01039 LegoNonFatal ("AddBBAlongEdge", "Passed NULL parameter.");
01040 return NULL;
01041 }
01042
01043 fromop = edge->GetFromOpPtr();
01044 toop = edge->GetToOpPtr();
01045 if (fromop == NULL || toop == NULL)
01046 {
01047 LegoFatal ("AddBBAlongEdge", "Edge source is %p, destination %p - "
01048 "one of them is NULL.", fromop, toop);
01049 }
01050
01051 fromregion = (legoRegion *) fromop->GetParentBlockPtr();
01052 toregion = (legoRegion *) toop->GetParentBlockPtr();
01053 if (fromregion == NULL || toregion == NULL)
01054 {
01055 LegoFatal ("AddBBAlongEdge", "Parent of op %p is %p; parent of %p "
01056 "is %p - one of the parents is NULL.", fromop, fromregion,
01057 toop, toregion);
01058 }
01059
01060 parentregion = toregion->GetParentPtr();
01061 if (parentregion == NULL)
01062 {
01063 LegoFatal ("AddBBAlongEdge", "Parent of %p region is NULL.",
01064 toregion);
01065 }
01066
01067 dprintf ((OUTF, "AddBBAlongEdge: insering block along edge %d into region "
01068 "%d.\n", edge->GetEdgeId(), parentregion->GetRegionId()));
01069
01070
01071 id = proc->FindMaxRegionId()->GetRegionId();
01072 newblock = new legoBB (id + 1);
01073 loc = parentregion->Search (toregion);
01074 if (loc != MAX_UNSIGNED)
01075 parentregion->Insert (newblock, loc);
01076 else
01077 parentregion->AddItem (newblock);
01078 newblock->SetParentPtr (parentregion);
01079
01080
01081 for (alllist = proc->GetListOpPtr(); alllist->GetListOpPtr() != NULL;
01082 alllist = alllist->GetListOpPtr()) ;
01083 id = (FindMaxOpId (proc))->GetOpId() + 1;
01084
01085
01086 top = new legoOp (id++);
01087 top->SetOpcode (C_MERGE);
01088 top->SetParentBlockPtr (newblock);
01089 alllist->SetListOpPtr (top);
01090 alllist = top;
01091
01092 oprd = new legoOprd;
01093 oprd->SetOprdType (OT_UNDEFINED);
01094 oprd->SetParentOpPtr (top);
01095 top->SetSrcOprdPtr (oprd);
01096 oprd = new legoOprd;
01097 oprd->SetOprdType (OT_UNDEFINED);
01098 oprd->SetParentOpPtr (top);
01099 top->SetDestOprdPtr (oprd);
01100
01101 newblock->AddItem(top);
01102 dprintf ((OUTF, "Added C_MERGE %d to new basic block.\n", top->GetOpId()));
01103
01104
01105 dummy_br = new legoOp (id++);
01106 dummy_br->SetOpcode (DUMMY_BR);
01107 dummy_br->SetParentBlockPtr (newblock);
01108 alllist->SetListOpPtr (dummy_br);
01109 alllist = dummy_br;
01110
01111 oprd = new legoOprd;
01112 oprd->SetOprdType (OT_UNDEFINED);
01113 oprd->SetParentOpPtr (dummy_br);
01114 dummy_br->SetSrcOprdPtr (oprd);
01115 oprd = new legoOprd;
01116 oprd->SetOprdType (OT_UNDEFINED);
01117 oprd->SetParentOpPtr (dummy_br);
01118 dummy_br->SetDestOprdPtr (oprd);
01119
01120 top->SetNextLink (dummy_br);
01121 dummy_br->SetPrevLink (top);
01122 newblock->AddItem (dummy_br);
01123 dprintf ((OUTF, "Added DUMMY_BR %d to new basic block.\n",
01124 dummy_br->GetOpId()));
01125
01126
01127 newedge = AddEdge (top, dummy_br, ET_CNTL);
01128 dprintf ((OUTF, "Added edge %d between new ops.\n", newedge->GetEdgeId()));
01129
01130
01131
01132 RedirectEdge (edge, EDGE_TO, top);
01133 dprintf ((OUTF, "Redirected edge %d to point to op %d.\n",
01134 edge->GetEdgeId(), top->GetOpId()));
01135
01136
01137 fromweight = (double) FindEdgeFrequency (edge);
01138 newblock->SetWeight (fromweight);
01139
01140
01141
01142 newedge = AddEdge (dummy_br, toop, ET_CNTL);
01143 AddFreqAttribute ((int) fromweight, 0, newedge);
01144 dprintf ((OUTF, "Added edge %d from new basic block to following block.\n",
01145 newedge->GetEdgeId()));
01146
01147 opl = new opList;
01148 dummy_br->SetOutListPtr (opl);
01149 opl->SetOpPtr (toop);
01150 opl->SetOpId (toop->GetOpId());
01151 opl->SetValid (1);
01152 opl->SetWeight (fromweight);
01153 dprintf ((OUTF, "Added ref to op %d to dummy_br %d.\n", toop->GetOpId(),
01154 dummy_br->GetOpId()));
01155
01156
01157 opl = AddToList (toop->GetInListPtr(), dummy_br, 1, fromweight);
01158 toop->SetInListPtr (opl);
01159 dprintf ((OUTF, "Added ref the other way.\n"));
01160
01161
01162
01163
01164
01165
01166 ClearMarks (RM_GENERAL, proc);
01167
01168 dprintf ((OUTF, "Refreshing new block...\n"));
01169 newblock->RefreshOpsAndEdges();
01170 newblock->Mark (RM_GENERAL);
01171
01172 dprintf ((OUTF, "Refreshing toregion and parents...\n"));
01173 toregion->RefreshEdges();
01174 for (r = toregion->GetParentPtr(); r->GetRegionType() != RT_MODULE;
01175 r = r->GetParentPtr())
01176 {
01177 r->RefreshOpsAndEdges();
01178 r->Mark (RM_GENERAL);
01179 }
01180
01181 dprintf ((OUTF, "Refreshing fromregion and parents...\n"));
01182 fromregion->RefreshEdges();
01183 for (r = fromregion->GetParentPtr(); r->GetRegionType() != RT_MODULE;
01184 r = r->GetParentPtr())
01185 {
01186 if (r->IsMarked (RM_GENERAL)) break;
01187 r->RefreshEdges();
01188 r->Mark (RM_GENERAL);
01189 }
01190
01191 dprintf ((OUTF, "Done creating new block.\n"));
01192 return newblock;
01193 }
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204 legoRegion *SplitParentBlockBeforeOp(legoOp *op_ptr)
01205 {
01206 legoProc *proc_ptr = NULL;
01207 legoOp *branch_from_region = NULL;
01208 legoRegion *from_region = NULL;
01209 legoRegion *new_region = NULL;
01210 legoRegion *parent_region = NULL;
01211 int index = 0;
01212 int this_edge_weight = 0;
01213 int max_op_id = 0;
01214
01215 if(!op_ptr){
01216 LegoNonFatal ("SplitParentBlockBeforeOp", "Empty argument passed");
01217 return NULL;
01218 }
01219 if(!(proc_ptr = (legoProc *) FindParentRegionType (RT_PROC, op_ptr))){
01220 LegoNonFatal ("SplitParentBlockBeforeOp", "Couldn't find proc of op %d.",op_ptr->GetOpId());
01221 return NULL;
01222 }
01223 if(!(from_region = (legoRegion *)op_ptr->GetParentBlockPtr())){
01224 LegoNonFatal ("SplitParentBlockBeforeOp", "Couldn't find Parent block of op %d.",op_ptr->GetOpId());
01225 return NULL;
01226 }
01227 if(!(branch_from_region = op_ptr->GetPrevLink())){
01228 LegoNonFatal ("SplitParentBlockBeforeOp", "Empty GetPrevLink");
01229 return NULL;
01230 }
01231
01232
01233
01234 new_region = new legoBB((proc_ptr->FindMaxRegionId()->GetRegionId())+1);
01235 parent_region = from_region->GetParentPtr();
01236 this_edge_weight = (int)from_region->GetWeight();
01237 max_op_id = (FindMaxOpId(proc_ptr))->GetOpId()+1;
01238
01239 for(index=0;index<parent_region->GetCount();index++)
01240 if(from_region==(legoRegion *)parent_region->GetItem(index)) break;
01241
01242 if(index < (parent_region->GetCount())-1) parent_region->Insert(new_region,index+1);
01243 else parent_region->AddItem(new_region);
01244
01245 new_region->SetParentPtr(parent_region);
01246
01247
01248
01249 legoOp *alllist,*top,*dummy_br,*pbrr;
01250
01251 for(alllist=proc_ptr->GetListOpPtr();alllist->GetListOpPtr()!=NULL;alllist=alllist->GetListOpPtr()) ;
01252 top = new legoOp (max_op_id++);
01253 top->SetOpcode (C_MERGE);
01254 top->SetParentBlockPtr (new_region);
01255 alllist->SetListOpPtr (top);
01256 alllist = top;
01257
01258 legoOprd *oprd = new legoOprd;
01259 oprd->SetOprdType (OT_UNDEFINED);
01260 oprd->SetParentOpPtr (top);
01261 top->SetSrcOprdPtr (oprd);
01262 oprd = new legoOprd;
01263 oprd->SetOprdType (OT_UNDEFINED);
01264 oprd->SetParentOpPtr (top);
01265 top->SetDestOprdPtr (oprd);
01266 new_region->AddItem(top);
01267
01268
01269
01270 dummy_br = new legoOp (max_op_id++);
01271 dummy_br->SetOpcode (DUMMY_BR);
01272 dummy_br->SetParentBlockPtr (new_region);
01273 alllist->SetListOpPtr (dummy_br);
01274 alllist = dummy_br;
01275
01276 oprd = new legoOprd;
01277 oprd->SetOprdType (OT_UNDEFINED);
01278 oprd->SetParentOpPtr (dummy_br);
01279 dummy_br->SetSrcOprdPtr (oprd);
01280 oprd = new legoOprd;
01281 oprd->SetOprdType (OT_UNDEFINED);
01282 oprd->SetParentOpPtr (dummy_br);
01283 dummy_br->SetDestOprdPtr (oprd);
01284
01285 top->SetNextLink (dummy_br);
01286 dummy_br->SetPrevLink (top);
01287 new_region->AddItem (dummy_br);
01288
01289
01290
01291
01292 opEdges *new_edge = AddEdge (top, dummy_br, ET_CNTL);
01293
01294
01295
01296 legoOp *current_op = top;
01297 legoOp *copy_op_ptr = NULL;
01298 attrs *attrdictionary;
01299 attrs *attrtail, *atscan;
01300 attrList *atlscan;
01301 int eaid = FindMaxEdgeAttrId(proc_ptr);
01302
01303 if(!(attrdictionary = proc_ptr->GetAttrDictionary())){
01304 LegoNonFatal ("SplitParentBlockBeforeOp", "Attribute dictionary is missing.");
01305 return NULL;
01306 }
01307 for (attrtail = attrdictionary; attrtail->GetNextAttrPtr(); attrtail = attrtail->GetNextAttrPtr()) ;
01308
01309 for(legoOp *my_op=op_ptr;my_op!=NULL;my_op=my_op->GetNextLink()){
01310 copy_op_ptr = new legoOp(*my_op);
01311 copy_op_ptr->SetParentBlockPtr(new_region);
01312 AddMidOp(copy_op_ptr,current_op);
01313
01314
01315 for (atlscan = copy_op_ptr->GetOpAttrListPtr();atlscan;atlscan = atlscan->GetNextListPtr()){
01316 if (atlscan->GetAttrPtr() == NULL) continue;
01317 atscan = new attrs (*(atlscan->GetAttrPtr()),copy_op_ptr,++eaid);
01318 atlscan->SetAttrPtr (atscan);
01319 atscan->SetAttrId (eaid);
01320 atlscan->SetAttrId (eaid);
01321 attrtail->SetNextAttrPtr (atscan);
01322 attrtail = attrtail->GetNextAttrPtr();
01323 }
01324 current_op = copy_op_ptr;
01325 }
01326
01327
01328
01329 legoOp *next_op;
01330 for(legoOp *my_op = op_ptr;my_op;my_op=next_op){
01331 if(next_op = my_op->GetNextLink()) RemoveMidOp(my_op,1);
01332 else RemoveLastOp(my_op,1);
01333 }
01334
01335
01336 legoOp *last_branch_new_region = dummy_br->GetPrevLink();
01337
01338
01339
01340 RemoveLastOp(dummy_br,1);
01341
01342
01343
01344 new_region->SetWeight(from_region->GetWeight());
01345
01346
01347
01348 opList *top_op_list=new opList();
01349 top->SetInListPtr(top_op_list);
01350 top_op_list->SetOpPtr(branch_from_region);
01351 top_op_list->SetOpId(branch_from_region->GetOpId());
01352 top_op_list->SetValid(1);
01353 top_op_list->SetWeight(this_edge_weight);
01354
01355
01356
01357 opList *entry_op_list=new opList();
01358 entry_op_list->SetOpPtr(top);
01359 entry_op_list->SetOpId(top->GetOpId());
01360 entry_op_list->SetValid(1);
01361 new_region->SetEntryOpsPtr(entry_op_list);
01362
01363
01364
01365 opList *exit_op_list=new opList();
01366 exit_op_list->SetOpPtr(last_branch_new_region);
01367 exit_op_list->SetOpId(last_branch_new_region->GetOpId());
01368 exit_op_list->SetValid(1);
01369 new_region->SetExitOpsPtr(exit_op_list);
01370
01371
01372
01373 opEdges *last_op_edge;
01374 opEdges *edge_dict=proc_ptr->GetEdgeDictionary();
01375 for(last_op_edge=edge_dict;last_op_edge->GetNextOpEdgePtr()!=NULL;last_op_edge=last_op_edge->GetNextOpEdgePtr());
01376
01377
01378
01379 opEdges *new_op_edge = new opEdges();
01380 new_op_edge->SetEdgeType(ET_CNTL);
01381 new_op_edge->SetEdgeId(last_op_edge->GetEdgeId()+1);
01382 new_op_edge->SetFromOpId(branch_from_region->GetOpId());
01383 new_op_edge->SetToOpId(top->GetOpId());
01384 new_op_edge->SetFromOpPtr(branch_from_region);
01385 new_op_edge->SetToOpPtr(top);
01386 new_op_edge->SetNextOpEdgePtr(NULL);
01387 new_op_edge->SetSrcPortType(PT_CTL);
01388 new_op_edge->SetDestPortType(PT_CTL);
01389 new_op_edge->SetEdgeLat(1);
01390
01391 last_op_edge->SetNextOpEdgePtr(new_op_edge);
01392 AddFreqAttribute(this_edge_weight,0,new_op_edge);
01393
01394
01395
01396
01397 edgeList *new_edge_list=new edgeList();
01398 new_edge_list->SetValid(1);
01399 new_edge_list->SetEdgePtr(new_op_edge);
01400 new_edge_list->SetEdgeId(new_op_edge->GetEdgeId());
01401 new_region->SetInEdgesPtr(new_edge_list);
01402
01403
01404
01405
01406 new_region->SetOutEdgesPtr(from_region->GetOutEdgesPtr());
01407
01408 edgeList *out_edges_of_original_block = new_region->GetOutEdgesPtr();
01409 opEdges *current_out_edge = NULL;
01410
01411 while(out_edges_of_original_block){
01412 if(current_out_edge = out_edges_of_original_block->GetEdgePtr())
01413 current_out_edge->SetFromOpPtr(last_branch_new_region);
01414 out_edges_of_original_block = out_edges_of_original_block->GetNextListPtr();
01415 }
01416
01417
01418
01419
01420 new_edge_list=new edgeList();
01421 new_edge_list->SetValid(1);
01422 new_edge_list->SetEdgePtr(new_op_edge);
01423 new_edge_list->SetEdgeId(new_op_edge->GetEdgeId());
01424 from_region->SetOutEdgesPtr(new_edge_list);
01425
01426
01427
01428 opList *bfr_op_list=new opList();
01429 branch_from_region->SetOutListPtr(bfr_op_list);
01430 bfr_op_list->SetOpPtr(top);
01431 bfr_op_list->SetOpId(top->GetOpId());
01432 bfr_op_list->SetValid(1);
01433 bfr_op_list->SetWeight(this_edge_weight);
01434
01435
01436
01437 exit_op_list=new opList();
01438 exit_op_list->SetOpPtr(branch_from_region);
01439 exit_op_list->SetOpId(branch_from_region->GetOpId());
01440 exit_op_list->SetValid(1);
01441 from_region->SetExitOpsPtr(exit_op_list);
01442
01443
01444
01445 return new_region;
01446 }
01447
01448
01449
01450 static flags *
01451 AddFlag (int flagname, void *object, int objtype)
01452 {
01453 flags *f;
01454
01455 if (objtype == ADDREMOVE_REGION)
01456 f = ((legoRegion *) object)->GetFlagPtr();
01457 else
01458 f = ((legoOp *) object)->GetOpFlagPtr();
01459
01460 for ( ; f != NULL && f->GetNextFlagPtr() != NULL;
01461 f = f->GetNextFlagPtr())
01462 if (f->GetFlagName() == flagname) return f;
01463 if (f != NULL && f->GetFlagName() == flagname) return f;
01464
01465
01466 if (f == NULL)
01467 {
01468 f = new flags;
01469 if (objtype == ADDREMOVE_REGION)
01470 ((legoRegion *) object)->SetFlagPtr (f);
01471 else
01472 ((legoOp *) object)->SetFlagPtr (f);
01473 }
01474 else
01475 {
01476 f->SetNextFlagPtr (new flags);
01477 f = f->GetNextFlagPtr();
01478 }
01479 f->SetFlagName (flagname);
01480
01481 return f;
01482 }
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492 flags *
01493 AddFlag (int flagname, legoRegion *region)
01494 {
01495 return AddFlag (flagname, (void *) region, ADDREMOVE_REGION);
01496 }
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506 flags *
01507 AddFlag (int flagname, legoOp *op)
01508 {
01509 return AddFlag (flagname, (void *) op, ADDREMOVE_OP);
01510 }
01511
01512
01513
01514 static void
01515 RemoveFlag (int flagname, void *object, int objtype)
01516 {
01517 flags *fprev, *f;
01518
01519 fprev = NULL;
01520 if (objtype == ADDREMOVE_REGION)
01521 f = ((legoRegion *) object)->GetFlagPtr();
01522 else
01523 f = ((legoOp *) object)->GetOpFlagPtr();
01524 if (f == NULL) return;
01525
01526
01527 for ( ; f->GetFlagName() != flagname;
01528 fprev = f, f = f->GetNextFlagPtr()) ;
01529 if (f == NULL) return;
01530
01531
01532 if (fprev == NULL)
01533 {
01534 if (objtype == ADDREMOVE_REGION)
01535 ((legoRegion *) object)->SetFlagPtr (f->GetNextFlagPtr());
01536 else
01537 ((legoOp *) object)->SetFlagPtr (f->GetNextFlagPtr());
01538 }
01539 else
01540 fprev->SetNextFlagPtr (f->GetNextFlagPtr());
01541 f->SetNextFlagPtr (NULL);
01542 delete f;
01543
01544 return;
01545 }
01546
01547
01548
01549
01550
01551
01552
01553
01554 void
01555 RemoveFlag (int flagname, legoRegion *region)
01556 {
01557 RemoveFlag (flagname, (void *) region, ADDREMOVE_REGION);
01558 }
01559
01560
01561
01562
01563
01564
01565
01566
01567 void
01568 RemoveFlag (int flagname, legoOp *op)
01569 {
01570 RemoveFlag (flagname, (void *) op, ADDREMOVE_OP);
01571 }
01572
01573
01574
01575 attrs *
01576 AddPointerAttribute (enum attrTypes atype, char *lcname, legoOprd *oprd,
01577 void *object, int objtype, legoProc *proc)
01578 {
01579 opEdges *edgedictionary;
01580 attrs *newattr, *attrdictionary;
01581 attrList *atl;
01582 int foundit, maxeaid;
01583
01584
01585 edgedictionary = proc->GetEdgeDictionary();
01586 if (edgedictionary == NULL)
01587 {
01588 LegoNonFatal ("AddPointerAttribute", "Edge dictionary is missing.");
01589 return NULL;
01590 }
01591 attrdictionary = proc->GetAttrDictionary();
01592 if (attrdictionary == NULL)
01593 {
01594 LegoNonFatal ("AddPointerAttribute", "Attribute dictionary is missing.");
01595 return NULL;
01596 }
01597 maxeaid = -1;
01598
01599 for ( ; edgedictionary != NULL;
01600 edgedictionary = edgedictionary->GetNextOpEdgePtr())
01601 if (edgedictionary->GetEdgeId() > maxeaid)
01602 maxeaid = edgedictionary->GetEdgeId();
01603 for ( ; attrdictionary->GetNextAttrPtr() != NULL;
01604 attrdictionary = attrdictionary->GetNextAttrPtr())
01605 if (attrdictionary->GetAttrId() > maxeaid)
01606 maxeaid = attrdictionary->GetAttrId();
01607 if (attrdictionary->GetAttrId() > maxeaid)
01608 maxeaid = attrdictionary->GetAttrId();
01609
01610
01611 if (objtype == ADDREMOVE_REGION)
01612 atl = ((legoRegion *) object)->GetRegionAttrListPtr();
01613 else if (objtype == ADDREMOVE_OP)
01614 atl = ((legoOp *) object)->GetOpAttrListPtr();
01615 else
01616 atl = ((opEdges *) object)->GetEdgeAttrListPtr();
01617 for ( ; atl != NULL && atl->GetNextListPtr() != NULL;
01618 atl = atl->GetNextListPtr())
01619 if (atl->GetAttrType() == atype) break;
01620 if (atl == NULL)
01621 {
01622 atl = new attrList;
01623 if (objtype == ADDREMOVE_REGION)
01624 ((legoRegion *) object)->SetRegionAttrListPtr (atl);
01625 else if (objtype == ADDREMOVE_OP)
01626 ((legoOp *) object)->SetOpAttrListPtr (atl);
01627 else
01628 ((opEdges *) object)->SetEdgeAttrListPtr (atl);
01629 atl->SetAttrType (atype);
01630 atl->SetValid (1);
01631 }
01632 else if (atl->GetAttrType() != atype)
01633 {
01634 atl->SetNextListPtr (new attrList);
01635 atl = atl->GetNextListPtr();
01636 atl->SetAttrType (atype);
01637 atl->SetValid (1);
01638 }
01639
01640
01641 if (atype == ATTR_LC)
01642 {
01643 for (foundit = 0, newattr = atl->GetAttrPtr();
01644 newattr != NULL && newattr->GetNextLcEntryPtr() != NULL;
01645 newattr = newattr->GetNextLcEntryPtr())
01646 if (strcmp (newattr->GetAttrString(), lcname) == 0)
01647 {
01648 foundit = 1; break;
01649 }
01650 if (newattr != NULL &&
01651 strcmp (newattr->GetAttrString(), lcname) == 0)
01652 foundit = 1;
01653 }
01654 else
01655 newattr = atl->GetAttrPtr();
01656 if (newattr == NULL)
01657 {
01658 newattr = new attrs;
01659 atl->SetAttrPtr (newattr);
01660 newattr->SetAttrId (++maxeaid);
01661 atl->SetAttrId (maxeaid);
01662 attrdictionary->SetNextAttrPtr (newattr);
01663 attrdictionary = attrdictionary->GetNextAttrPtr();
01664 }
01665 else if (!foundit && atype == ATTR_LC)
01666 {
01667 newattr->SetNextLcEntryPtr (new attrs);
01668 newattr = newattr->GetNextLcEntryPtr();
01669
01670
01671
01672 }
01673
01674
01675 newattr->SetAttrType (atype);
01676 newattr->SetAttrString (lcname);
01677 delete newattr->GetAttrOprdPtr();
01678 newattr->SetAttrOprdPtr (oprd);
01679 if (oprd != NULL)
01680 {
01681 oprd->SetParentAttrPtr (newattr);
01682 if (objtype == ADDREMOVE_OP)
01683 oprd->SetParentOpPtr ((legoOp *) object);
01684 }
01685 return newattr;
01686 }
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698
01699
01700 attrs *
01701 AddLcAttribute (char *lcname, legoOprd *oprd, legoRegion *region,
01702 legoProc *proc)
01703 {
01704 return AddPointerAttribute (ATTR_LC, lcname, oprd, (void *) region,
01705 ADDREMOVE_REGION, proc);
01706 }
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718 attrs *
01719 AddLcAttribute (char *lcname, legoOprd *oprd, legoOp *op, legoProc *proc)
01720 {
01721 return AddPointerAttribute (ATTR_LC, lcname, oprd, (void *) op,
01722 ADDREMOVE_OP, proc);
01723 }
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735 attrs *
01736 AddLcAttribute (char *lcname, legoOprd *oprd, opEdges *edge, legoProc *proc)
01737 {
01738 return AddPointerAttribute (ATTR_LC, lcname, oprd, (void *) edge,
01739 ADDREMOVE_EDGE, proc);
01740 }
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752 attrs *
01753 AddLiveAttribute (legoOprd *oprd, legoRegion *region, legoProc *proc)
01754 {
01755 return AddPointerAttribute (ATTR_LIVE, NULL, oprd, (void *) region,
01756 ADDREMOVE_REGION, proc);
01757 }
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767
01768 attrs *
01769 AddLiveAttribute (legoOprd *oprd, legoOp *op, legoProc *proc)
01770 {
01771 return AddPointerAttribute (ATTR_LIVE, NULL, oprd, (void *) op,
01772 ADDREMOVE_OP, proc);
01773 }
01774
01775
01776
01777
01778
01779
01780
01781
01782
01783
01784 attrs *
01785 AddLiveAttribute (legoOprd *oprd, opEdges *edge, legoProc *proc)
01786 {
01787 return AddPointerAttribute (ATTR_LIVE, NULL, oprd, (void *) edge,
01788 ADDREMOVE_EDGE, proc);
01789 }
01790
01791
01792
01793 static void
01794 RemovePointerAttribute (int atype, char *lcname, void *object, int objtype,
01795 legoProc *proc)
01796 {
01797 attrList *atl, *atlprev;
01798 attrs *aprev, *a, *adictprev, *adict;
01799 int i;
01800
01801
01802 if (objtype == ADDREMOVE_REGION)
01803 atl = ((legoRegion *) object)->GetRegionAttrListPtr();
01804 else if (objtype == ADDREMOVE_OP)
01805 atl = ((legoOp *) object)->GetOpAttrListPtr();
01806 else
01807 atl = ((opEdges *) object)->GetEdgeAttrListPtr();
01808
01809 for (atlprev = NULL;
01810 atl != NULL;
01811 atlprev = atl, atl = atl->GetNextListPtr())
01812 {
01813 if (atl->GetAttrType() != atype) continue;
01814
01815 aprev = NULL;
01816 if (atype == ATTR_LC)
01817 {
01818 for (a = atl->GetAttrPtr();
01819 a != NULL;
01820 aprev = a, a = a->GetNextLcEntryPtr())
01821 if (strcmp (a->GetAttrString(), lcname) == 0) break;
01822 }
01823 else
01824 a = atl->GetAttrPtr();
01825 if (a == NULL) continue;
01826
01827
01828
01829 if (aprev == NULL)
01830 {
01831 adict = proc->GetAttrDictionary();
01832 if (adict != NULL)
01833 {
01834
01835 for (adictprev = NULL; adict != NULL;
01836 adictprev = adict, adict = adict->GetNextAttrPtr())
01837 if (adict == a) break;
01838
01839
01840
01841
01842
01843
01844 if (atype == ATTR_LC && a->GetNextLcEntryPtr() != NULL)
01845 {
01846 if (adictprev != NULL)
01847 adictprev->SetNextAttrPtr (a->GetNextLcEntryPtr());
01848 else
01849 proc->SetAttrDictionary (a->GetNextLcEntryPtr());
01850 a->GetNextLcEntryPtr()->SetNextAttrPtr
01851 (a->GetNextAttrPtr());
01852 a->GetNextLcEntryPtr()->SetAttrType (ATTR_LC);
01853 a->GetNextLcEntryPtr()->SetAttrId (a->GetAttrId());
01854
01855 atl->SetAttrPtr (a->GetNextLcEntryPtr());
01856 }
01857 else
01858 {
01859 if (adictprev != NULL)
01860 adictprev->SetNextAttrPtr (a->GetNextAttrPtr());
01861 else
01862 proc->SetAttrDictionary (a->GetNextAttrPtr());
01863
01864 }
01865 }
01866
01867
01868
01869 if (atype != ATTR_LC || a->GetNextLcEntryPtr() == NULL)
01870 {
01871 if (atlprev != NULL)
01872 atlprev->SetNextListPtr (atl->GetNextListPtr());
01873 else
01874 {
01875 if (objtype == ADDREMOVE_REGION)
01876 ((legoRegion *) object)->SetRegionAttrListPtr
01877 (atl->GetNextListPtr());
01878 else if (objtype == ADDREMOVE_OP)
01879 ((legoOp *) object)->SetOpAttrListPtr
01880 (atl->GetNextListPtr());
01881 else
01882 ((opEdges *) object)->SetEdgeAttrListPtr
01883 (atl->GetNextListPtr());
01884 }
01885 atl->SetNextListPtr (NULL);
01886 delete atl;
01887 atl = atlprev;
01888 }
01889
01890 a->SetNextLcEntryPtr (NULL);
01891 a->SetNextAttrPtr (NULL);
01892 delete a;
01893
01894 if (atl == NULL) break;
01895 }
01896 else
01897 {
01898 aprev->SetNextLcEntryPtr (a->GetNextLcEntryPtr());
01899 a->SetNextLcEntryPtr (NULL);
01900 a->SetNextAttrPtr (NULL);
01901 delete a;
01902 }
01903 }
01904
01905 return;
01906 }
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918 void
01919 RemoveLcAttribute (char *lcname, legoRegion *region, legoProc *proc)
01920 {
01921 RemovePointerAttribute (ATTR_LC, lcname, (void *) region,
01922 ADDREMOVE_REGION, proc);
01923 return;
01924 }
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934 void
01935 RemoveLcAttribute (char *lcname, legoOp *op, legoProc *proc)
01936 {
01937 RemovePointerAttribute (ATTR_LC, lcname, (void *) op,
01938 ADDREMOVE_OP, proc);
01939 return;
01940 }
01941
01942
01943
01944
01945
01946
01947
01948
01949
01950 void
01951 RemoveLcAttribute (char *lcname, opEdges *edge, legoProc *proc)
01952 {
01953 RemovePointerAttribute (ATTR_LC, lcname, (void *) edge,
01954 ADDREMOVE_EDGE, proc);
01955 return;
01956 }
01957
01958
01959
01960
01961
01962
01963
01964
01965 void
01966 RemoveLiveAttribute (legoRegion *region, legoProc *proc)
01967 {
01968 RemovePointerAttribute (ATTR_LIVE, NULL, (void *) region,
01969 ADDREMOVE_REGION, proc);
01970 return;
01971 }
01972
01973
01974
01975
01976
01977
01978
01979
01980 void
01981 RemoveLiveAttribute (legoOp *op, legoProc *proc)
01982 {
01983 RemovePointerAttribute (ATTR_LIVE, NULL, (void *) op,
01984 ADDREMOVE_OP, proc);
01985 return;
01986 }
01987
01988
01989
01990
01991
01992
01993
01994
01995 void
01996 RemoveLiveAttribute (opEdges *edge, legoProc *proc)
01997 {
01998 RemovePointerAttribute (ATTR_LIVE, NULL, (void *) edge,
01999 ADDREMOVE_EDGE, proc);
02000 return;
02001 }
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014 attrList *
02015 AddFreqAttribute (int weight, int value, opEdges *edge)
02016 {
02017 attrList *atl;
02018 intList *il;
02019
02020 for (atl = edge->GetEdgeAttrListPtr(); atl != NULL &&
02021 atl->GetNextListPtr() != NULL; atl = atl->GetNextListPtr())
02022 if (atl->GetAttrType() == ATTR_FREQ) break;
02023
02024 if (atl == NULL)
02025 {
02026 atl = new attrList;
02027 atl->SetAttrType (ATTR_FREQ);
02028 atl->SetValid (1);
02029 edge->SetEdgeAttrListPtr (atl);
02030 }
02031 else if (atl->GetAttrType() != ATTR_FREQ)
02032 {
02033 atl->SetNextListPtr (new attrList);
02034 atl = atl->GetNextListPtr();
02035 atl->SetAttrType (ATTR_FREQ);
02036 atl->SetValid (1);
02037 }
02038 il = atl->GetAttrValPtr();
02039 if (il == NULL)
02040 {
02041 il = new intList;
02042 atl->SetAttrValPtr (il);
02043 }
02044 il->SetAttrValue (weight);
02045 if (il->GetNextIntListPtr() == NULL)
02046 il->SetNextIntListPtr (new intList);
02047 il->GetNextIntListPtr()->SetAttrValue (value);
02048
02049 return atl;
02050 }
02051
02052
02053
02054
02055
02056
02057
02058 void
02059 RemoveFreqAttribute (opEdges *edge)
02060 {
02061 attrList *atl, *atlprev;
02062
02063 for (atlprev = NULL, atl = edge->GetEdgeAttrListPtr();
02064 atl != NULL;
02065 atlprev = atl, atl = atl->GetNextListPtr())
02066 if (atl->GetAttrType() == ATTR_FREQ)
02067 {
02068 if (atlprev != NULL)
02069 atlprev->SetNextListPtr (atl->GetNextListPtr());
02070 else
02071 edge->SetEdgeAttrListPtr (atl->GetNextListPtr());
02072 atl->SetNextListPtr (NULL);
02073 delete atl;
02074 break;
02075 }
02076 return;
02077 }
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088 void
02089 ClearMarks (long long m, legoRegion *region)
02090 {
02091 legoRegion *component;
02092
02093 region->Unmark (m);
02094 if (!(IS_BLOCK (region->GetRegionType())))
02095 for (unsigned i = 0; i < region->GetCount(); i++)
02096 {
02097 component = (legoRegion *) region->GetItem(i);
02098 if (IS_BLOCK (component->GetRegionType()))
02099 component->Unmark (m);
02100 else
02101 ClearMarks (m, component);
02102 }
02103
02104 return;
02105 }
02106
02107
02108
02109
02110
02111
02112
02113
02114 void
02115 ClearMarks (long long m, opEdges *edge)
02116 {
02117 opEdges *e;
02118
02119 for (e = edge; e != NULL; e = e->GetNextOpEdgePtr())
02120 e->Unmark (m);
02121 return;
02122 }
02123
02124