FAUST compiler  0.9.9.6b8
loop.cpp
Go to the documentation of this file.
00001 #include "loop.hh"
00002 extern bool gVectorSwitch;
00003 extern bool gOpenMPSwitch;
00004 extern bool gOpenMPLoop;
00005 
00006 using namespace std;
00007 
00013 static void tab (int n, ostream& fout)
00014 {
00015     fout << '\n';
00016     while (n--) fout << '\t';
00017 }
00018 
00019 
00026 static void printlines (int n, list<string>& lines, ostream& fout)
00027 {
00028     list<string>::iterator s;
00029     for (s = lines.begin(); s != lines.end(); s++) {
00030         tab(n, fout); fout << *s;
00031     }
00032 }
00033 
00034 
00041 Loop::Loop(Tree recsymbol, Loop* encl, const string& size)
00042         : fIsRecursive(true), fRecSymbolSet(singleton(recsymbol)), fEnclosingLoop(encl), fSize(size), fOrder(-1), fIndex(-1), fUseCount(0), fPrinted(0)
00043 {}
00044 
00045 
00051 Loop::Loop(Loop* encl, const string& size) 
00052         : fIsRecursive(false), fRecSymbolSet(nil), fEnclosingLoop(encl), fSize(size), fOrder(-1), fIndex(-1), fUseCount(0), fPrinted(0)
00053 {}
00054 
00055 
00063 bool Loop::hasRecDependencyIn(Tree S)
00064 {
00065     Loop* l = this;
00066     while ( l && isNil(setIntersection(l->fRecSymbolSet,S)) ) l=l->fEnclosingLoop;
00067     return l != 0;
00068 }
00069 
00074 bool Loop::isEmpty()                  
00075 { 
00076     return fPreCode.empty() && fExecCode.empty() && fPostCode.empty() && (fExtraLoops.begin()==fExtraLoops.end()); 
00077 }
00078 
00082 void Loop::addPreCode (const string& str)    
00083 { 
00084    // cerr << this << "->addExecCode " << str << endl;
00085     fPreCode.push_back(str); 
00086 }
00087 
00091 void Loop::addExecCode (const string& str)    
00092 { 
00093    // cerr << this << "->addExecCode " << str << endl;
00094     fExecCode.push_back(str); 
00095 }
00096 
00097 
00101 void Loop::addPostCode (const string& str)    
00102 { 
00103    // cerr << this << "->addPostCode " << str << endl;
00104     fPostCode.push_front(str); 
00105 }
00106 
00107 
00113 void Loop::absorb (Loop* l)    
00114 { 
00115     // the loops must have the same number of iterations
00116     assert(fSize == l->fSize); 
00117     fRecSymbolSet = setUnion(fRecSymbolSet, l->fRecSymbolSet);
00118 
00119     // update loop dependencies by adding those from the absorbed loop
00120     fBackwardLoopDependencies.insert(l->fBackwardLoopDependencies.begin(), l->fBackwardLoopDependencies.end());  
00121 
00122     // add the line of code of the absorbed loop
00123     fPreCode.insert(fPreCode.end(), l->fPreCode.begin(), l->fPreCode.end());
00124     fExecCode.insert(fExecCode.end(), l->fExecCode.begin(), l->fExecCode.end());
00125     fPostCode.insert(fPostCode.begin(), l->fPostCode.begin(), l->fPostCode.end());
00126 }
00127 
00128 
00134 void Loop::println(int n, ostream& fout)
00135 {
00136     for (list<Loop*>::const_iterator s = fExtraLoops.begin(); s != fExtraLoops.end(); s++) {
00137         (*s)->println(n, fout);
00138     }
00139 
00140     if (fPreCode.size()+fExecCode.size()+fPostCode.size() > 0) {
00141 /*        if (gVectorSwitch) {
00142             tab(n,fout);
00143             fout << ((fIsRecursive) ? "// recursive loop" : "// vectorizable loop");
00144         }*/
00145 
00146         tab(n,fout); fout << "// LOOP " << this ;
00147         if (fPreCode.size()>0) {
00148             tab(n,fout); fout << "// pre processing";
00149             printlines(n, fPreCode, fout);
00150         }
00151 
00152         tab(n,fout); fout << "// exec code";
00153         tab(n,fout); fout << "for (int i=0; i<" << fSize << "; i++) {";
00154         printlines(n+1, fExecCode, fout);
00155         tab(n,fout); fout << "}";
00156 
00157         if (fPostCode.size()>0) {
00158             tab(n,fout); fout << "// post processing";
00159             printlines(n, fPostCode, fout);
00160         }
00161         tab(n,fout);
00162     }
00163 }
00164 
00165 
00172 void Loop::printParLoopln(int n, ostream& fout)
00173 {
00174     for (list<Loop*>::const_iterator s = fExtraLoops.begin(); s != fExtraLoops.end(); s++) {
00175         tab(n,fout); fout << "#pragma omp single";
00176         tab(n,fout); fout << "{";
00177         (*s)->println(n+1, fout);
00178         tab(n,fout); fout << "}";
00179     }
00180 
00181     if (fPreCode.size()+fExecCode.size()+fPostCode.size() > 0) {
00182 
00183         tab(n,fout); fout << "// LOOP " << this ;
00184         if (fPreCode.size()>0) {
00185             tab(n,fout); fout << "#pragma omp single";
00186             tab(n,fout); fout << "{";
00187             tab(n+1,fout); fout << "// pre processing";
00188             printlines(n+1, fPreCode, fout);
00189             tab(n,fout); fout << "}";
00190         }
00191 
00192         tab(n,fout); fout << "// exec code";
00193         tab(n,fout); fout << "#pragma omp for";
00194         tab(n,fout); fout << "for (int i=0; i<" << fSize << "; i++) {";
00195         printlines(n+1, fExecCode, fout);
00196         tab(n,fout); fout << "}";
00197 
00198         if (fPostCode.size()>0) {
00199             tab(n,fout); fout << "#pragma omp single";
00200             tab(n,fout); fout << "{";
00201             tab(n+1,fout); fout << "// post processing";
00202             printlines(n+1, fPostCode, fout);
00203             tab(n,fout); fout << "}";
00204         }
00205         tab(n,fout);
00206     }
00207 }
00208 
00214 void Loop::printoneln(int n, ostream& fout)
00215 {
00216     if (fPreCode.size()+fExecCode.size()+fPostCode.size() > 0) {
00217 /*        if (gVectorSwitch) {
00218             tab(n,fout); 
00219             fout << ((fIsRecursive) ? "// recursive loop" : "// vectorizable loop");
00220         }*/
00221             
00222         tab(n,fout); fout << "for (int i=0; i<" << fSize << "; i++) {";
00223         if (fPreCode.size()>0) {
00224             tab(n+1,fout); fout << "// pre processing";
00225             printlines(n+1, fPreCode, fout);
00226         }
00227         printlines(n+1, fExecCode, fout);
00228         if (fPostCode.size()>0) {
00229             tab(n+1,fout); fout << "// post processing";
00230             printlines(n+1, fPostCode, fout);
00231         }
00232         tab(n,fout); fout << "}";
00233     }
00234 }
00235 
00236 //-------------------------------------------------------
00237 void Loop::concat(Loop* l)
00238 {
00239     assert(l->fUseCount == 1);
00240     assert(fBackwardLoopDependencies.size() == 1);
00241     assert((*fBackwardLoopDependencies.begin()) == l);
00242     
00243     fExtraLoops.push_front(l);
00244     fBackwardLoopDependencies = l->fBackwardLoopDependencies;   
00245 }