OpenADFortTk (including Open64 and OpenAnalysis references)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CalleeToCallerVisitor.cpp
Go to the documentation of this file.
1 
16 #include <Utils/Util.hpp>
17 
18 
19 namespace OA {
20  namespace DataFlow {
21 
22 static bool debug = false;
23 
24 
25 
27  ProcHandle callee, CallHandle call, ProcHandle caller,
31  : mIR(_ir),mCall(call), mCaller(caller),
32  mInterAlias(interAlias), mParamBind(paramBind), mCallee(callee)
33 {
34  OA_DEBUG_CTRL_MACRO("DEBUG_CalleeToCallerVisitor:ALL", debug);
35 
37 }
38 
40 {
41 
43  retval = new LocSetIterator(mLocSet);
44  return retval;
45 }
46 
47 
49 {
50  // should start newly empty each time
51  mLocSet = new LocSet;
52 
53  // if the location is not local then just return it
54  if (loc.isLocal()==false) {
55  OA_ptr<Location> locCopy; locCopy = new NamedLoc(loc);
56  mLocSet->insert(locCopy);
57  if (debug) {
58  std::cout << "CalleeToCallerVisitor: loc is not local, loc = ";
59  loc.dump(std::cout);
60  std::cout << std::endl;
61  }
62 
63  }
64  /*
65  } else {
66  // assume it is a parameter and try to get corresponding memref
67  SymHandle formal = loc.getSymHandle();
68  if (debug) {
69  std::cout << "CalleeToCallerVisitor: formal = " << formal.hval();
70  std::cout << std::endl;
71  }
72 
73  // get the memory reference in the caller
74  MemRefHandle memref = mParamBind->getCallerMemRef(mCall,formal);
75 
76  // only do the conversion if actual parameter is a memory reference
77  if (memref!=MemRefHandle(0)) {
78  // get all the may locs for the mem ref in caller
79  // and insert them into the caller locs for this reference
80  OA_ptr<LocIterator> mayLocIter = mCallerAlias->getMayLocs(memref);
81  for ( ; mayLocIter->isValid(); (*mayLocIter)++ ) {
82  if (debug) {
83  std::cout << "\tmemref = " << memref.hval()
84  << " has mayLoc ";
85  mayLocIter->current()->dump(std::cout);
86  std::cout << std::endl;
87  }
88  mLocSet->insert(mayLocIter->current());
89  }
90  }
91  }
92  */
93 }
94 
96 {
97  mLocSet = new LocSet;
98  OA_ptr<Location> locptr;
99  locptr = new UnnamedLoc(loc);
100  mLocSet->insert(locptr);
101 }
102 
103 
112 class ConstructMREVisitor : public virtual MemRefExprVisitor{
113 
114 public:
115 
117  return mNewMRE;
118  }
119 
122  : mActualMRE(actualMRE), mIR(_ir)
123  {
124  }
125 
126  void visitNamedRef(NamedRef& ref) {
127  // should never get here because will stop at RefOp that
128  // points at the base NamedRef
129  }
130 
132  // should never get here because the base ref in an InvisibleLoc
133  // is always a NamedRef
134  }
135 
137  // should never get here because the base ref in an InvisibleLoc is
138  // always a NamedRef
139  }
140 
142 
143 
144  OA_ptr<AddressOf> refOp;
145 
146  refOp = new AddressOf(ref);
147 
148  mRefOpStack.push(refOp);
149 
150  // see if this is the last RefOp before the NamedRef to the formal parameter
151  OA_ptr<MemRefExpr> mre = ref.getMemRefExpr();
152  if (mre->isaNamed()) {
153 
154  // if it is then call the method that will create the newMRE
155  createNewMRE();
156 
157  if (debug) {
158  std::cout << "\t Immediately after mActualMRE = ";
159  mActualMRE->output(*mIR);
160  }
161  if (debug) {
162  std::cout << "\t Immediately after mNewMRE = ";
163  mNewMRE->output(*mIR);
164  }
165 
166  } else {
167  mre->acceptVisitor(*this);
168  }
169 
170  if (debug) {
171  std::cout << "\t Inside (end) of visitAddressOf mNewMRE = ";
172  mNewMRE->output(*mIR);
173  }
174 
175  }
176 
177 
178  void visitDeref(Deref& ref) {
179  // push empty version of this kind of RefOp onto stack
180  OA_ptr<MemRefExpr> nullMRE;
181  // will need to write getMRType()
182 
183  /* PLM 1/23/07 deprecated accuracy field
184  OA_ptr<Deref> refOp;
185 
186  refOp =
187  new Deref(ref.hasFullAccuracy(), ref.getMRType(),
188  nullMRE, ref.getNumDerefs()) ;
189  */
190 
191 
192 
193  OA_ptr<Deref> refOp;
194 
195  refOp =
196  new Deref(ref.getMRType(), nullMRE, ref.getNumDerefs()) ;
197 
198 
199  mRefOpStack.push(refOp);
200 
201  // see if this is the last RefOp before the NamedRef to the formal parameter
202  OA_ptr<MemRefExpr> mre = ref.getMemRefExpr();
203  if (mre->isaNamed()) {
204 
205  // if it is then call the method that will create the newMRE
206  createNewMRE();
207 
208  if (debug) {
209  std::cout << "\t Immediately after mActualMRE = ";
210  mActualMRE->output(*mIR);
211  }
212  if (debug) {
213  std::cout << "\t Immediately after mNewMRE = ";
214  mNewMRE->output(*mIR);
215  }
216 
217  } else {
218  mre->acceptVisitor(*this);
219  }
220 
221  if (debug) {
222  std::cout << "\t Inside visiDeref mNewMRE = ";
223  mNewMRE->output(*mIR);
224  }
225  }
226 
228  //assert(0); // not implemented yet, MMS
229 
230  OA::OA_ptr<OA::SubSetRef> subset_mre;
232 
233  subset_mre = new OA::SubSetRef(
235  nullMRE
236  );
237 
238  mRefOpStack.push(subset_mre);
239 
240  // see if this is the last RefOp before the NamedRef to the formal parameter
241  OA_ptr<MemRefExpr> mre = ref.getMemRefExpr();
242  if (mre->isaNamed()) {
243  // if it is then call the method that will create the newMRE
244  createNewMRE();
245  } else {
246  mre->acceptVisitor(*this);
247  }
248  }
249 
250 private:
251 
253  {
255 
256 
257  // pop things on the list one at a time,
258  // for each RefOp, create one of the same kind but have
259  // it point to mNewMRE and then assign newly constructed MRE to mNewMRE
260  while(!mRefOpStack.empty())
261  {
262  OA_ptr<RefOp> refop = mRefOpStack.top();
263  mRefOpStack.pop();
264 
265 
266  mNewMRE = refop->composeWith(mNewMRE);
267 
268 
269  if (debug) {
270  std::cout << "\t Inside createNewMRE mNewMRE = ";
271  mNewMRE->output(*mIR);
272  }
273  }
274  }
275 
276 private:
277  std::stack<OA_ptr<RefOp> > mRefOpStack;
281 
282 
283 };
284 
285 
287 {
288 
289  if (debug) {
290  std::cout << "In CalleeToCallerVisitor::visitInvisibleLoc" << std::endl;
291  }
292  mLocSet = new LocSet;
293 
294  // if base symbols is not local
295  // InvisibleLoc
296 
297 
298  SymHandle baseSym = loc.getBaseSym();
299 
300 
301  OA_ptr<Location> baseSymLoc = mIR->getLocation(mCallee, baseSym);
302  if (!baseSymLoc->isLocal())
303  {
304  // pull out the MRE
305  OA_ptr<MemRefExpr> memref = loc.getMemRefExpr();
306  if (debug) {
307  std::cout << "\tBaseSymLoc is NOT local = ";
308  memref->output(*mIR);
309  }
310 
311  // ask caller alias analysis to getMayLocs
312  // insert each of the may locs into mLocSet
313  OA_ptr<LocIterator> mayLocIter = mCallerAlias->getMayLocs(*memref, mCaller);
314 
315  for ( ; mayLocIter->isValid(); (*mayLocIter)++ ) {
316  mLocSet->insert(mayLocIter->current());
317  }
318 
319  // base symbol is local
320  // the base symbol should be a parameter
321  } else {
322  OA_ptr<MemRefExpr> mre = loc.getMemRefExpr();
323  if (debug) {
324  std::cout << "\tBaseSymLoc IS local = ";
325  mre->output(*mIR);
326  }
327 
328  // get the base symbol for the mre within the invisible location
329  // it should be a formal
330  OA_ptr<RefOp> refop;
331  if (mre->isaRefOp()) {
332  refop = mre.convert<RefOp>();
333  } else {
334  assert(0); // should not have an MRE in InvLoc that isn't a RefOp
335  }
336  SymHandle formal = refop->getBaseSym();
337 
338  // get the memory reference in the caller
339  MemRefHandle memref = mParamBind->getCallerMemRef(mCall,formal);
340 
341  // iterate over all mres for the actual memory reference in caller
342  OA_ptr<MemRefExprIterator> mreIterPtr
343  = mIR->getMemRefExprIterator(memref);
344  for (; mreIterPtr->isValid(); (*mreIterPtr)++) {
345  OA_ptr<OA::MemRefExpr> actual_mre = mreIterPtr->current();
346 
347  // create a visitor parameterized with mre in caller
348  ConstructMREVisitor visitor(actual_mre, mIR);
349 
350  if (debug) {
351  std::cout << "\tActual mre = ";
352  actual_mre->output(*mIR);
353  }
354 
355  // visit the mre in the callee
356  mre->acceptVisitor(visitor);
357  OA_ptr<MemRefExpr> composed_mre = visitor.getMemRefExpr();
358 
359  if (debug) {
360  std::cout << "\tComposed mre = ";
361  composed_mre->output(*mIR);
362  }
363 
364 
365  // visit the may locs for resulting mre
366  OA_ptr<LocIterator> mayLocIter
367  = mCallerAlias->getMayLocs(*composed_mre, mCaller);
368  if (debug) {
369  std::cout << "\tConstructing mayLocs from Caller Proc " << mIR->toString(mCaller)
370  << " for Composed mre:\n";
371  }
372  for ( ; mayLocIter->isValid(); (*mayLocIter)++ ) {
373  mLocSet->insert(mayLocIter->current());
374  if (debug) {
375  std::cout << "\t\tinserted==> ";
376  (mayLocIter->current())->output(*mIR);
377  }
378  }
379  }
380  }
381 }
382 
384 {
385  mLocSet = new LocSet;
386  OA_ptr<Location> locptr;
387  locptr = new UnknownLoc();
388  mLocSet->insert(locptr);
389 }
390 
392 {
393  // not handling any of the more precise LocSubSet classes
394  // just going to make a generic LocSubSet to wrap around
395  // all the NamedLoc's the underlying location maps to
396  OA_ptr<Location> baseLoc = loc.getLoc();
397  if (debug) {
398  std::cout << "CalleeToCallerVisitor::visitLocSubSet" << std::endl;
399  std::cout << "\tbaseLoc = ";
400  baseLoc->dump(std::cout);
401  }
402  baseLoc->acceptVisitor(*this);
403  OA_ptr<LocSet> temp = mLocSet;
404  mLocSet = new LocSet;
405  OA_ptr<LocIterator> locIter; locIter = new LocSetIterator(temp);
406  for ( ; locIter->isValid(); (*locIter)++ ) {
407  OA_ptr<Location> mayLoc = locIter->current();
408  if (debug) {
409  std::cout << "\t\tmayLoc = ";
410  mayLoc->dump(std::cout);
411  }
412 
413  // the constructor for LocSubSet handles various mayLoc
414  // subclasses
415  OA_ptr<Location> subLoc;
416  subLoc = new LocSubSet(mayLoc);
417  mLocSet->insert(subLoc);
418  }
419 
420 }
421 
422 
423  } // end of DataFlow namespace
424 } // end of OA namespace
425 
426 
427 
428 
429 
430 
431 
432 
433 /*
434 class ConstructMREVisitor {
435 
436 public:
437 
438  ConstructMREVisitor(OA_ptr<MemRefExpr> actualMRE) : mActualMRE(actualMRE)
439  {
440  mMREStack = new std::stack<OA_ptr<MemRefExpr> >;
441  }
442 
443  void visitNamedRef(NamedRef& ref) {
444  // should never get here because will stop at RefOp that points at the base NamedRef
445  }
446 
447  void visitUnnamedRef(UnnamedRef& ref) {
448  // should never get here because the base ref in an InvisibleLoc is always a NamedRef
449  }
450 
451  void visitUnknownRef(UnknownRef& ref) {
452  // should never get here because the base ref in an InvisibleLoc is always a NamedRef
453  }
454 
455  void visitDeref(Deref& ref)
456  {
457  // push empty version of this kind of RefOp onto stack
458  OA_ptr<MemRefExpr> nullMRE;
459  // will need to write getMRType()
460  OA_ptr<MemRefExpr> refOp = new RefOp(ref.hasAddressTaken(), ref.hasFullAccuracy(), ref.getMRType(), nullMRE );
461  mRefOpStack.push(refOp);
462 
463  // see if this is the last RefOp before the NamedRef to the formal parameter
464  OA_ptr<MemRefExpr> mre = ref.getMemRefExpr();
465  if (mre->isaNamed()) {
466 
467  // if it is then call the method that will create the newMRE
468  // createNewMRE();
469  }else {
470  mre->acceptVisitor(*this);
471  }
472  }
473 
474  void visitSubSetRef(SubSetRef& ref) {
475 
476  }
477 
478 private:
479 
480 
481  void createNewMRE()
482  {
483  OA_ptr<MemRefExpr>mNewMRE = mActualMRE;
484  // pop things on the list one at a time, for each RefOp, create one of the same kind but have it point to mNewMRE and then assign newly constructed MRE to mNewMRE
485 
486  OA_ptr<MemRefExprIterator> mreIterPtr
487  = mIR->getMemRefExprIterator(mRefOpStack);
488 
489  OA_ptr<OA::MemRefExpr> mre;
490  for (; mreIterPtr->isValid(); (*mreIterPtr)++) {
491  mre = mreIterPtr->current();
492  mre.push(mNewMRE);
493  mNewMRE = mre;
494  }
495 
496 
497 
498  if (mre->isaDeref()) {
499 
500  MRE = new Deref(mre->hasAddressTaken, ..., mNewMRE);
501 
502  } else if isaSubsetRef {
503 
504  MRE = new Deref(mre->hasAddressTaken, ..., mNewMRE);
505  }
506 
507  }
508 private:
509 
510  std::stack<OA_ptr<MemRefExpr> > mRefOpStack;
511  OA_ptr<MemRefExpr>mActualMRE;
512  OA_ptr<MemRefExpr> mNewMRE;
513 
514 } */
515