00001 #include "loop.hh"
00002 extern bool gVectorSwitch;
00003
00004 using namespace std;
00005
00011 static void tab (int n, ostream& fout)
00012 {
00013 fout << '\n';
00014 while (n--) fout << '\t';
00015 }
00016
00017
00024 static void printlines (int n, list<string>& lines, ostream& fout)
00025 {
00026 list<string>::iterator s;
00027 for (s = lines.begin(); s != lines.end(); s++) {
00028 tab(n, fout); fout << *s;
00029 }
00030 }
00031
00032
00039 Loop::Loop( Tree recsymbol, Loop* encl, const string& size)
00040 : fIsRecursive(true), fRecSymbol(recsymbol), fEnclosingLoop(encl), fSize(size), fOrder(-1), fIndex(-1), fUseCount(0)
00041 {}
00042
00043
00049 Loop::Loop(Loop* encl, const string& size)
00050 : fIsRecursive(false), fRecSymbol(), fEnclosingLoop(encl), fSize(size), fOrder(-1), fIndex(-1), fUseCount(0)
00051 {}
00052
00053
00060 bool Loop::hasRecDependencies()
00061 {
00062 return !fRecDependencies.empty();
00063 }
00064
00065
00070 bool Loop::isEmpty()
00071 {
00072 return fPreCode.empty() && fExecCode.empty() && fPostCode.empty() && (fExtraLoops.begin()==fExtraLoops.end());
00073 }
00074
00075
00079 void Loop::addRecDependency(Tree t)
00080 {
00081 if (t != fRecSymbol) {
00082 fRecDependencies.insert(t);
00083 }
00084 }
00085
00086
00091 bool Loop::findRecDefinition(Tree t)
00092 {
00093 Loop* l = this;
00094 while (l && l->fRecSymbol != t) l=l->fEnclosingLoop;
00095 return l != 0;
00096 }
00097
00101 void Loop::addPreCode (const string& str)
00102 {
00103
00104 fPreCode.push_back(str);
00105 }
00106
00110 void Loop::addExecCode (const string& str)
00111 {
00112
00113 fExecCode.push_back(str);
00114 }
00115
00116
00120 void Loop::addPostCode (const string& str)
00121 {
00122
00123 fPostCode.push_front(str);
00124 }
00125
00126
00132 void Loop::absorb (Loop* l)
00133 {
00134
00135 assert(fSize == l->fSize);
00136
00137
00138 fRecDependencies.insert(l->fRecDependencies.begin(), l->fRecDependencies.end());
00139 if (fIsRecursive) fRecDependencies.erase(fRecSymbol);
00140
00141
00142 fBackwardLoopDependencies.insert(l->fBackwardLoopDependencies.begin(), l->fBackwardLoopDependencies.end());
00143
00144
00145 fPreCode.insert(fPreCode.end(), l->fPreCode.begin(), l->fPreCode.end());
00146 fExecCode.insert(fExecCode.end(), l->fExecCode.begin(), l->fExecCode.end());
00147 fPostCode.insert(fPostCode.begin(), l->fPostCode.begin(), l->fPostCode.end());
00148 }
00149
00150
00156 void Loop::println(int n, ostream& fout)
00157 {
00158 for (list<Loop*>::const_iterator s = fExtraLoops.begin(); s != fExtraLoops.end(); s++) {
00159 (*s)->println(n, fout);
00160 }
00161
00162 if (fPreCode.size()+fExecCode.size()+fPostCode.size() > 0) {
00163
00164
00165
00166
00167
00168 tab(n,fout); fout << "// LOOP " << this ;
00169 if (fPreCode.size()>0) {
00170 tab(n,fout); fout << "// pre processing";
00171 printlines(n, fPreCode, fout);
00172 }
00173
00174 tab(n,fout); fout << "// exec code";
00175 tab(n,fout); fout << "for (int i=0; i<" << fSize << "; i++) {";
00176 printlines(n+1, fExecCode, fout);
00177 tab(n,fout); fout << "}";
00178
00179 if (fPostCode.size()>0) {
00180 tab(n,fout); fout << "// post processing";
00181 printlines(n, fPostCode, fout);
00182 }
00183 tab(n,fout);
00184 }
00185 }
00186
00192 void Loop::printoneln(int n, ostream& fout)
00193 {
00194 if (fPreCode.size()+fExecCode.size()+fPostCode.size() > 0) {
00195
00196
00197
00198
00199
00200 tab(n,fout); fout << "for (int i=0; i<" << fSize << "; i++) {";
00201 if (fPreCode.size()>0) {
00202 tab(n+1,fout); fout << "// pre processing";
00203 printlines(n+1, fPreCode, fout);
00204 }
00205 printlines(n+1, fExecCode, fout);
00206 if (fPostCode.size()>0) {
00207 tab(n+1,fout); fout << "// post processing";
00208 printlines(n+1, fPostCode, fout);
00209 }
00210 tab(n,fout); fout << "}";
00211 }
00212 }
00213
00214
00215 void Loop::concat(Loop* l)
00216 {
00217 assert(l->fUseCount == 1);
00218 assert(fBackwardLoopDependencies.size() == 1);
00219 assert((*fBackwardLoopDependencies.begin()) == l);
00220
00221 fExtraLoops.push_front(l);
00222 fBackwardLoopDependencies = l->fBackwardLoopDependencies;
00223 }