OpenADFortTk (including Open64 and OpenAnalysis references)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DepDFSet.cpp
Go to the documentation of this file.
1 
15 #include "DepDFSet.hpp"
16 #include <Utils/Util.hpp>
17 
18 namespace OA {
19  namespace Activity {
20 
21 static bool debug = false;
22 
24 DepDFSet::DepDFSet() //: mMakeImplicitExplicitMemoized(false)
25 {
26  OA_DEBUG_CTRL_MACRO("DEBUG_DepDFSet:ALL", debug);
30 }
31 
33 DepDFSet::DepDFSet(const DepDFSet &other) //: mMakeImplicitExplicitMemoized(false)
34 {
38  mDefs = other.mDefs;
39  mUses = other.mUses;
40 }
41 
44 {
45  //mMakeImplicitExplicitMemoized = other.mMakeImplicitExplicitMemoized;
49  mDefs = other.mDefs;
50  mUses = other.mUses;
51  return *this;
52 }
53 
54 //*****************************************************************
55 // DataFlowSet Interface Implementation
56 //*****************************************************************
57 
59 {
60  OA_ptr<DepDFSet> retval;
61  retval = new DepDFSet(*this);
62  return retval;
63 }
64 
66 {
67  DepDFSet& recastOther = dynamic_cast<DepDFSet&>(other);
68  bool retval = true;
69 
70  // make sure that each have their implicits made explicit
71  // will have to copy our own because makeImplicitExplicit
72  // changes the data-structure
73  //DepDFSet copyThis = *this;
74  //copyThis.makeImplicitExplicit();
75  //recastOther.makeImplicitExplicit();
76 
77  if (mImplicitRemoves == recastOther.mImplicitRemoves
78  && mUseToDefsMap == recastOther.mUseToDefsMap
79  && mDefToUsesMap == recastOther.mDefToUsesMap)
80  {
81  retval = true;
82  } else {
83  retval = false;
84  }
85 
86  /*
87  // make sure each dep pair in this is in other
88  OA_ptr<DepIterator> depIter = getDepIterator();
89  for ( ; depIter->isValid(); (*depIter)++ ) {
90  OA_ptr<Location> use = depIter->use();
91  OA_ptr<Location> def = depIter->def();
92 
93  bool found = false;
94  OA_ptr<DepIterator> otherDepIter = recastOther.getDepIterator();
95  for ( ; otherDepIter->isValid(); (*otherDepIter)++ ) {
96  OA_ptr<Location> otherUse = otherDepIter->use();
97  OA_ptr<Location> otherDef = otherDepIter->def();
98 
99  if (use==otherUse && def==otherDef) {
100  found = true;
101  }
102  }
103 
104  if (!found) {
105  retval = false;
106  }
107  }
108 
109  // make sure each dep pair in this is in other
110  OA_ptr<DepIterator> otherDepIter = recastOther.getDepIterator();
111  for ( ; otherDepIter->isValid(); (*otherDepIter)++ ) {
112  OA_ptr<Location> otherUse = otherDepIter->use();
113  OA_ptr<Location> otherDef = otherDepIter->def();
114 
115  bool found = false;
116  OA_ptr<DepIterator> depIter = getDepIterator();
117  for ( ; depIter->isValid(); (*depIter)++ ) {
118  OA_ptr<Location> use = depIter->use();
119  OA_ptr<Location> def = depIter->def();
120 
121  if (use==otherUse && def==otherDef) {
122  found = true;
123  }
124  }
125 
126  if (!found) {
127  retval = false;
128  }
129  }
130 
131  */
132  if (debug) {
133  std::cout << "operator= returning " << retval << std::endl;
134  }
135  return retval;
136 
137 }
138 
140 {
141  if (*this == other) {
142  if (debug) { std::cout << "operator!= will return false" << std::endl; }
143  return false;
144  } else {
145  if (debug) { std::cout << "operator!= will return true" << std::endl; }
146  return true;
147  }
148 }
149 
150 //*****************************************************************
151 // Methods specific to DepDFSet
152 //*****************************************************************
153 
154 /*
155 void DepDFSet::makeImplicitExplicit()
156 {
157  if (mMakeImplicitExplicitMemoized == true) {
158  return;
159  } else {
160  mMakeImplicitExplicitMemoized = true;
161  }
162 
163  // make all implicit deps that haven't been explicitly
164  // removed explicit in the data-structure as well
165  LocToLocDFSetMap::const_iterator mapIter;
166  for (mapIter=mUseToDefsMap.begin(); mapIter!=mUseToDefsMap.end();
167  mapIter++)
168  {
169  if (! isImplicitRemoved(mapIter->first) ) {
170  insertDep(mapIter->first,mapIter->first);
171  }
172  }
173  for (mapIter=mDefToUsesMap.begin(); mapIter!=mDefToUsesMap.end();
174  mapIter++)
175  {
176  if (! isImplicitRemoved(mapIter->first) ) {
177  insertDep(mapIter->first,mapIter->first);
178  }
179  }
180 }
181 */
182 
188 {
189 
190  // copy self and then create iterator for it
191  OA_ptr<DepDFSet> setCopy; setCopy = new DepDFSet(*this);
192  //setCopy->makeImplicitExplicit();
193  OA_ptr<DepIterator> retval;
194  retval = new DepIterator(setCopy);
195  return retval;
196 }
197 
198 
200 {
202  retSet = new DataFlow::LocDFSet();
203 
204  // get iterator over uses the given location overlaps with
205  OA_ptr<LocIterator> useOverlapIter
207  for ( ; useOverlapIter->isValid(); (*useOverlapIter)++ ) {
208  OA_ptr<Location> loc = useOverlapIter->current();
209 
210  // iterator over associated defs
211  LocToLocDFSetMap::const_iterator pos = mUseToDefsMap.find(loc);
212  if (pos!=mUseToDefsMap.end()) {
213  OA_ptr<LocIterator> locIter = pos->second->getLocIterator();
214  for ( ; locIter->isValid(); (*locIter)++ ) {
215  retSet->insert(locIter->current());
216  }
217  }
218  }
219 
220  // loop over sets that the use
221  /*
222  LocToLocDFSetMap::const_iterator mapIter;
223  for (mapIter=mUseToDefsMap.begin();
224  mapIter!=mUseToDefsMap.end(); mapIter++ )
225  {
226  OA_ptr<Location> depuse = mapIter->first;
227  if (depuse->mayOverlap(*use)) {
228  OA_ptr<LocIterator> locIter = mapIter->second->getLocIterator();
229  for ( ; locIter->isValid(); (*locIter)++ ) {
230  retSet->insert(locIter->current());
231  }
232  }
233  }
234  */
235 
236  // see if reflexive dep for use loc should be included,
237  // only reason why it wouldn't is if use
238  // is in the ImplicitRemoves
239  // FIXME: ok could put hasMustOverlapLoc, but for now going with
240  // conservative assumption which means equivalenced symbols will get
241  // short shrifted. insertDep if use==def makes same assumption.
242  if (!mImplicitRemoves->hasLoc(use)) {
243  retSet->insert(use);
244  }
245 
246  // construct the iterator
248  retval = new DataFlow::LocDFSetIterator(*retSet);
249  return retval;
250 }
251 
253 {
255  retSet = new DataFlow::LocDFSet();
256 
257  // get iterator over uses the given location overlaps with
258  OA_ptr<LocIterator> defOverlapIter
260  for ( ; defOverlapIter->isValid(); (*defOverlapIter)++ ) {
261  OA_ptr<Location> loc = defOverlapIter->current();
262  if (debug) {
263  std::cout << "getUsesIterator: loc = ";
264  loc->dump(std::cout);
265  }
266 
267  // iterator over associated deps
268  LocToLocDFSetMap::const_iterator pos = mDefToUsesMap.find(loc);
269  if (pos!=mDefToUsesMap.end()) {
270  OA_ptr<LocIterator> locIter = pos->second->getLocIterator();
271  for ( ; locIter->isValid(); (*locIter)++ ) {
272  retSet->insert(locIter->current());
273  }
274  }
275  }
276 
277  /*
278  LocToLocDFSetMap::const_iterator mapIter;
279  for (mapIter=mDefToUsesMap.begin();
280  mapIter!=mDefToUsesMap.end(); mapIter++ )
281  {
282  OA_ptr<Location> depdef = mapIter->first;
283  if (depdef->mayOverlap(*def)) {
284  OA_ptr<LocIterator> locIter = mapIter->second->getLocIterator();
285  for ( ; locIter->isValid(); (*locIter)++ ) {
286  retSet->insert(locIter->current());
287  }
288  }
289  }
290  */
291 
292  // see if reflexive dep for def loc should be included,
293  // only reason why it wouldn't is if def
294  // is in the ImplicitRemoves
295  // FIXME: ok could put hasMustOverlapLoc, but for now going with
296  // conservative assumption which means equivalenced symbols will get
297  // short shrifted
298  if (!mImplicitRemoves->hasLoc(def)) {
299  retSet->insert(def);
300  }
301 
303  retval = new DataFlow::LocDFSetIterator(*retSet);
304  return retval;
305 }
306 
311 {
312  DepDFSet& recastOther = dynamic_cast<DepDFSet&>(other);
313  if (debug) {
314  std::cout << "DepDFSet::setUnion" << std::endl;
315  }
316 
317  // for each use in other
318  LocToLocDFSetMap::const_iterator mapIter;
319  for (mapIter=recastOther.mUseToDefsMap.begin();
320  mapIter!=recastOther.mUseToDefsMap.end(); mapIter++ )
321  {
322  OA_ptr<Location> use = mapIter->first;
323 
324  // if this use is already in our map then just union it into
325  // the associated defs location set
326  // if not then will have to make a new LocDFSet and
327  // then essentially do the same thing
328  if (mUseToDefsMap[use].ptrEqual(0)) {
330  }
331  *(mUseToDefsMap[use])
332  = mUseToDefsMap[use]->setUnion(*(mapIter->second));
333  }
334 
335  // for each def in other
336  for (mapIter=recastOther.mDefToUsesMap.begin();
337  mapIter!=recastOther.mDefToUsesMap.end(); mapIter++ )
338  {
339  OA_ptr<Location> def = mapIter->first;
340 
341  if (mDefToUsesMap[def].ptrEqual(0)) {
343  }
344  mDefToUsesMap[def]->setUnion(*(mapIter->second));
345  }
346 
347  // take on all of other's implicitRemoves that we already have
349 
350  // union the uses and defs sets
351  mUses->setUnion(*(recastOther.mUses));
352  mDefs->setUnion(*(recastOther.mDefs));
353 
354  return *this;
355 }
356 
357 
361 {
362  return mImplicitRemoves->hasLoc(loc);
363 }
364 
384 {
385  DepDFSet& recastOther = dynamic_cast<DepDFSet&>(other);
386  if (debug) {
387  std::cout << "DepDFSet::compose" << std::endl;
388  std::cout << "\t*this = ";
389  dump(std::cout);
390  std::cout << "\tother = ";
391  recastOther.dump(std::cout);
392  }
393 
394  OA_ptr<DepDFSet> tempDFSet;
395  tempDFSet = new DepDFSet;
396 
397  // have to do these before do insertDeps because could be inserting
398  // a dep that will take things out of the mImplicitRemoves set
399  tempDFSet->mImplicitRemoves->setUnion(*(mImplicitRemoves));
400  tempDFSet->mImplicitRemoves->setUnion(*(recastOther.mImplicitRemoves));
401 
402  // iterate over deps in this keying off the def
403  // see if the def overlaps with anything in recastOther
404  LocToLocDFSetMap::const_iterator mapIter;
405  for (mapIter=mDefToUsesMap.begin();
406  mapIter!=mDefToUsesMap.end(); mapIter++ )
407  {
408  OA_ptr<Location> def1 = mapIter->first;
409  if (debug) {
410  std::cout << "def1 = "; def1->dump(std::cout);
411  std::cout << std::endl;
412  }
413  OA_ptr<DataFlow::LocDFSet> use1Set = mapIter->second;
414  if (debug) {
415  std::cout << "use1Set = "; use1Set->dump(std::cout);
416  }
417 
418  // get all the defs in use2,def2 where def1 overlaps with use2
419  OA_ptr<LocIterator> def2Iter = recastOther.getDefsIterator(def1);
420 
421  // loop over all use1,def1 pairs
422  OA_ptr<LocIterator> use1Iter = use1Set->getLocIterator();
423  for ( ; use1Iter->isValid(); (*use1Iter)++ ) {
424  OA_ptr<Location> use1 = use1Iter->current();
425 
426  // get all the defs in use2, def2 where def1 overlaps with use2
427  for (def2Iter->reset(); def2Iter->isValid(); (*def2Iter)++ ) {
428  tempDFSet->insertDep(use1, def2Iter->current());
429  if (debug) {
430  std::cout << "inserting <use1,def2Iter->current> = ";
431  use1->dump(std::cout);
432  std::cout << " , ";
433  def2Iter->current()->dump(std::cout);
434  std::cout << std::endl;
435  }
436  }
437 
438  /*
439  // as long as d,d has not been removed from recastOther,
440  // where d overlaps def1, we should add use1,def1 to result
441  // if it was removed but is explicitly in recastOther then
442  // use1,def1 was correctly inserted into the result in above code
443  // eg. x = x + y, x,x will be explictly in recastOther and
444  // be an implicit that is removed due to the mustdef of x
445  if (! recastOther.isImplicitRemoved(def1) ) {
446  tempDFSet->insertDep(use1,def1);
447  if (debug) {
448  std::cout << "inserting <use1,def1> = ";
449  use1->dump(std::cout);
450  std::cout << " , ";
451  def1->dump(std::cout);
452  std::cout << std::endl;
453  }
454  }
455  */
456  }
457  }
458 
459  // iterate over deps in recastOther keying off the use
460  // see if the use overlaps with anything in this
461  // even if it doesn't insert dep pairs for use,def
462  // as long as use,use hasn't been removed from this
463  for (mapIter=recastOther.mUseToDefsMap.begin();
464  mapIter!=recastOther.mUseToDefsMap.end(); mapIter++ )
465  {
466  OA_ptr<Location> use2 = mapIter->first;
467  if (debug) {
468  std::cout << "use2 = "; use2->dump(std::cout);
469  std::cout << std::endl;
470  }
471  OA_ptr<DataFlow::LocDFSet> def2Set = mapIter->second;
472  assert(!def2Set.ptrEqual(0));
473  if (debug) {
474  std::cout << "def2Set = ";
475  def2Set->dump(std::cout);
476  }
477 
478  OA_ptr<LocIterator> use1Iter = getUsesIterator(use2);
479 
480  // loop over all use2,def2 pairs
481  OA_ptr<LocIterator> def2Iter = def2Set->getLocIterator();
482  for ( ; def2Iter->isValid(); (*def2Iter)++ ) {
483  OA_ptr<Location> def2 = def2Iter->current();
484 
485  // get all the uses in use1,def1 where def1 overlaps with use2
486  for (use1Iter->reset(); use1Iter->isValid(); (*use1Iter)++ ) {
487  tempDFSet->insertDep(use1Iter->current(), def2);
488  if (debug) {
489  std::cout << "inserting <use1Iter->current,def2> = ";
490  use1Iter->current()->dump(std::cout);
491  std::cout << " , ";
492  def2->dump(std::cout);
493  std::cout << std::endl;
494  }
495  }
496 
497  /*
498  // as long as u,u has not been removed from this,
499  // where u overlaps use2, we should add use2,def2 to result
500  if (! isImplicitRemoved(use2) ) {
501  tempDFSet->insertDep(use2,def2);
502  if (debug) {
503  std::cout << "inserting <use2,def2> = ";
504  use2->dump(std::cout);
505  std::cout << " , ";
506  def2->dump(std::cout);
507  std::cout << std::endl;
508  }
509  }
510  */
511  }
512  }
513 
514  (*this) = *tempDFSet;
515  if (debug) {
516  std::cout << "\t*this = ";
517  dump(std::cout);
518  }
519 
520 
521  return (*this);
522 }
523 
530 /*
531 DepDFSet& DepDFSet::setIntersect(DataFlowSet &other)
532 {
533  assert(0);
534 }
535 */
536 
537 //*****************************************************************
538 // Output
539 //*****************************************************************
540 void DepDFSet::dump(std::ostream &os)
541 {
542  /*
543  os << "\tmUseToDefsMap.size() = " << mUseToDefsMap.size() << std::endl;
544  os << "\tmDefToUsesMap.size() = " << mDefToUsesMap.size() << std::endl;
545  */
546 
547  os << "\tuses" << std::endl;
548  // iterate over all uses
549  std::map<OA_ptr<Location>,OA_ptr<DataFlow::LocDFSet> >::const_iterator
550  mapIter;
551  for (mapIter=mUseToDefsMap.begin();
552  mapIter!=mUseToDefsMap.end(); mapIter++ )
553  {
554  OA_ptr<Location> use = mapIter->first;
555  OA_ptr<DataFlow::LocDFSet> locset = mapIter->second;
556 
557  os << "\t\t";
558  use->dump(os);
559  os << " ==> ";
560  locset->dump(os);
561  os << std::endl;
562  }
563 
564  os << "\tdefs" << std::endl;
565  // iterate over all defs
566  for (mapIter=mDefToUsesMap.begin();
567  mapIter!=mDefToUsesMap.end(); mapIter++ )
568  {
569  OA_ptr<Location> def = mapIter->first;
570  OA_ptr<DataFlow::LocDFSet> locset = mapIter->second;
571 
572  os << "\t\t";
573  def->dump(os);
574  os << " ==> ";
575  locset->dump(os);
576  os << std::endl;
577  }
578 
579  os << "\tmImplicitRemoves = ";
580  mImplicitRemoves->dump(os);
581 
582  os << "\tmUses = ";
583  mUses->dump(os);
584 
585  os << "\tmDefs = ";
586  mDefs->dump(os);
587 
588 }
589 
590 void DepDFSet::dump(std::ostream &os) const
591 {
592 
593 /*
594  os << "\tmUseToDefsMap.size() = " << mUseToDefsMap.size() << std::endl;
595  os << "\tmDefToUsesMap.size() = " << mDefToUsesMap.size() << std::endl;
596 */
597 
598  os << "\tuses" << std::endl;
599  // iterate over all uses
600  std::map<OA_ptr<Location>,OA_ptr<DataFlow::LocDFSet> >::const_iterator
601  mapIter;
602  for (mapIter=mUseToDefsMap.begin();
603  mapIter!=mUseToDefsMap.end(); mapIter++ )
604  {
605  OA_ptr<Location> use = mapIter->first;
606  OA_ptr<DataFlow::LocDFSet> locset = mapIter->second;
607 
608  os << "\t\t";
609  use->dump(os);
610  os << " ==> ";
611  locset->dump(os);
612  os << std::endl;
613  }
614 
615  os << "\tdefs" << std::endl;
616  // iterate over all defs
617  for (mapIter=mDefToUsesMap.begin();
618  mapIter!=mDefToUsesMap.end(); mapIter++ )
619  {
620  OA_ptr<Location> def = mapIter->first;
621  OA_ptr<DataFlow::LocDFSet> locset = mapIter->second;
622 
623  os << "\t\t";
624  def->dump(os);
625  os << " ==> ";
626  locset->dump(os);
627  os << std::endl;
628  }
629 
630  os << "\tmImplicitRemoves = ";
631  mImplicitRemoves->dump(os);
632 
633  os << "\tmUses = ";
634  mUses->dump(os);
635 
636  os << "\tmDefs = ";
637  mDefs->dump(os);
638 
639  }
640 
641 
642 void DepDFSet::dump(std::ostream &os, OA_ptr<IRHandlesIRInterface> ir)
643 {
644 /*
645  os << "\tmUseToDefsMap.size() = " << mUseToDefsMap.size() << std::endl;
646  os << "\tmDefToUsesMap.size() = " << mDefToUsesMap.size() << std::endl;
647 */
648 
649 
650  os << "\tuses" << std::endl;
651  // iterate over all uses
652  std::map<OA_ptr<Location>,OA_ptr<DataFlow::LocDFSet> >::const_iterator mapIter;
653  for (mapIter=mUseToDefsMap.begin();
654  mapIter!=mUseToDefsMap.end(); mapIter++ )
655  {
656  OA_ptr<Location> use = mapIter->first;
657  OA_ptr<DataFlow::LocDFSet> locset = mapIter->second;
658 
659  os << "\t\t";
660  use->dump(os,ir);
661  os << " ==> ";
662  locset->dump(os,ir);
663  os << std::endl;
664  }
665 
666  os << "\tdefs" << std::endl;
667  // iterate over all defs
668  for (mapIter=mDefToUsesMap.begin();
669  mapIter!=mDefToUsesMap.end(); mapIter++ )
670  {
671  OA_ptr<Location> def = mapIter->first;
672  OA_ptr<DataFlow::LocDFSet> locset = mapIter->second;
673 
674  os << "\t\t";
675  def->dump(os,ir);
676  os << " ==> ";
677  locset->dump(os,ir);
678  os << std::endl;
679  }
680 
681  // dump implicitRemoves
682  os << "\tmImplicitRemoves = ";
683  mImplicitRemoves->dump(os,ir);
684 
685  os << "\tmUses = ";
686  mUses->dump(os,ir);
687 
688  os << "\tmDefs = ";
689  mDefs->dump(os,ir);
690 
691 }
692 
694 
695  // iterate over all uses
696  sOutBuild->mapStart("mUseToDefsMap","UseLoc","DefLocSet");
697  std::map<OA_ptr<Location>,OA_ptr<DataFlow::LocDFSet> >::const_iterator mapIter;
698  for (mapIter=mUseToDefsMap.begin();
699  mapIter!=mUseToDefsMap.end(); mapIter++ )
700  {
701  OA_ptr<Location> use = mapIter->first;
702  OA_ptr<DataFlow::LocDFSet> locset = mapIter->second;
703  sOutBuild->mapEntryStart();
704  sOutBuild->mapKeyStart();
705  use->output(ir);
706  sOutBuild->mapKeyEnd();
707  sOutBuild->mapValueStart();
708  locset->output(ir);
709  sOutBuild->mapValueEnd();
710  sOutBuild->mapEntryEnd();
711  }
712  sOutBuild->mapEnd("mUseToDefsMap");
713 
714  // iterate over all defs
715  sOutBuild->mapStart("mDefToUsesMap","DefLoc","UseLocSet");
716  for (mapIter=mDefToUsesMap.begin();
717  mapIter!=mDefToUsesMap.end(); mapIter++ )
718  {
719  OA_ptr<Location> def = mapIter->first;
720  OA_ptr<DataFlow::LocDFSet> locset = mapIter->second;
721  sOutBuild->mapEntryStart();
722  sOutBuild->mapKeyStart();
723  def->output(ir);
724  sOutBuild->mapKeyEnd();
725  sOutBuild->mapValueStart();
726  locset->output(ir);
727  sOutBuild->mapValueEnd();
728  sOutBuild->mapEntryEnd();
729  }
730  sOutBuild->mapEnd("mDefToUsesMap");
731 
732 
733  // dump implicitRemoves
734  std::ostringstream label1;
735  label1 << indt << "ImplicitRemoves:";
736  sOutBuild->outputString( label1.str() );
737  mImplicitRemoves->output(ir);
738 
739  std::ostringstream label2;
740  label2 << indt << "Uses:";
741  sOutBuild->outputString( label2.str() );
742  mUses->output(ir);
743 
744  std::ostringstream label3;
745  label3 << indt << "Defs:";
746  sOutBuild->outputString( label3.str() );
747  mDefs->output(ir);
748 
749 }
750 
751 
752 //*****************************************************************
753 // Construction
754 //*****************************************************************
755 
769 {
770  if (use==def) {
771  mImplicitRemoves->remove(use);
772 
773  } else {
774  if (mUseToDefsMap[use].ptrEqual(0)) {
776  }
777  if (mDefToUsesMap[def].ptrEqual(0)) {
779  }
780 
781  mUseToDefsMap[use]->insert(def);
782  mDefToUsesMap[def]->insert(use);
783 
784  mUses->insert(use);
785  mDefs->insert(def);
786  }
787 }
788 
795 {
796 // mMakeImplicitExplicitMemoized = false;
797  // to be conservative only insert locations when the use and def must
798  // overlap each other, sublocs that only have partial accuracy will
799  // only may loc
800  if (use->mustOverlap(*def)) {
801  mImplicitRemoves->insert(use);
802  }
803 }
804 
805  } // end of Activity namespace
806 } // end of OA namespace