-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractBoolCond.cpp
More file actions
426 lines (318 loc) · 12.6 KB
/
ExtractBoolCond.cpp
File metadata and controls
426 lines (318 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "Comm.h"
using namespace llvm;
using namespace clang;
Condition CommManager::extractCondFromBoolExpr(Expr *expr){
string exprStr=stmt2str(&ci->getSourceManager(),ci->getLangOpts(),expr);
cout<<"The expr "<<exprStr<<" is obtained by Comm.cpp"<<endl;
cout<<"The stmt class type is "<<expr->getStmtClassName()<<endl;
//**************************If the condition is a single item***********************************************
//if it is a single number
bool boolResult;
bool canBeEval=expr->EvaluateAsBooleanCondition(boolResult,this->ci->getASTContext());
if(canBeEval){
cout <<"The expr can be evaluated!"<<endl;
cout<<"The boolResult is "<<boolResult<<endl;
if(boolResult){
Condition trueCond(true);
trueCond.setAsTrivial();
return trueCond;
}
else{
Condition falseCond(false);
falseCond.setAsTrivial();
return falseCond;
}
}
else{cout <<"The expr can NOT be evaluated!"<<endl;}
//if it is a single var
if(this->isAVar(exprStr)){
if(rankVarOffsetMapping.count(exprStr))
{
//if the var is rank var, then: if N==1 then fasle, else [1..N-1]
if(N==1)
return Condition(false);
else{
return Condition(Range(1,N-1));
}
}
//a var can contain any value, so assume true.
Condition theCond(true);
if(unboundVarList.count(exprStr))
theCond.isVolatile=true;
theCond.setNonRankVarName(exprStr);
return theCond;
}
if(isa<ParenExpr>(expr)){
ParenExpr *bracketExpr=cast<ParenExpr>(expr);
Expr *withoutParen=bracketExpr->getSubExpr();
cout<<"The expr without brackets is "<<stmt2str(&ci->getSourceManager(),ci->getLangOpts(),withoutParen)<<endl;
return extractCondFromBoolExpr(withoutParen);
}
//The bool expr is a negation
if(isa<UnaryOperator>(expr)){
UnaryOperator *uOP=cast<UnaryOperator>(expr);
string uopstr=UnaryOperator::getOpcodeStr(uOP->getOpcode());
Expr *subExpr=uOP->getSubExpr();
if (uopstr=="!")
{
Condition theCond=this->extractCondFromBoolExpr(subExpr);
string nonRankVarName=theCond.getNonRankVarName();
if (nonRankVarName!="")
{
if(this->tmpNonRankVarCondMap.count(nonRankVarName)){
Condition tmp=this->tmpNonRankVarCondMap[nonRankVarName].top();
this->tmpNonRankVarCondMap[nonRankVarName].pop();
Condition newC=Condition::negateCondition(tmp);
newC.setNonRankVarName(nonRankVarName);
this->insertTmpNonRankVarCond(nonRankVarName,newC);
}
Condition myCond(true);
if(theCond.isVolatile)
myCond.isVolatile=true;
myCond.setNonRankVarName(nonRankVarName);
return myCond;
}
return Condition::negateCondition(theCond);
}
}
/////////////////////////////////////////////////////////////
//the expr is a bin op.
////////////////////////////////////////////////////////////
if(isa<BinaryOperator>(expr)){
BinaryOperator *binOP=cast<BinaryOperator>(expr);
Expr *lhs=binOP->getLHS();
Expr *rhs=binOP->getRHS();
string lhsStr=stmt2str(&ci->getSourceManager(),ci->getLangOpts(),lhs);
string rhsStr=stmt2str(&ci->getSourceManager(),ci->getLangOpts(),rhs);
string op=binOP->getOpcodeStr();
cout<<"The bin op is "<<stmt2str(&ci->getSourceManager(),ci->getLangOpts(),binOP)<<"\n";
cout<<"The lhs is "<<lhsStr<<"\n";
cout<<"The rhs is "<<rhsStr<<"\n";
cout<<"The operator is : "<<op<<endl;
///////////////////////////////////////////////////////////////////////////////////////////////////
if(op=="&&"){
Condition lCond=extractCondFromBoolExpr(lhs);
Condition rCond=extractCondFromBoolExpr(rhs);
bool mixed=false;
if(!lCond.hasSameRankNature(rCond)){
//if warning level is high, then forbid the mixture of rank
//and non-rank condition...
if (STRICT)
{
string lCondStr=stmt2str(&ci->getSourceManager(),ci->getLangOpts(),lhs);
string rCondStr=stmt2str(&ci->getSourceManager(),ci->getLangOpts(),rhs);
string errInfo="\nCondition "+lCondStr+" and Condition "+rCondStr;
errInfo+=" are not compatible. They need to be both rank related or non-rank related.\n";
throw new MPI_TypeChecking_Error(errInfo);
}
mixed=true;
}
string nonRankVarName="";
string lhsNonRankVarName=lCond.getNonRankVarName();
string rhsNonRankVarName=rCond.getNonRankVarName();
if (lhsNonRankVarName!="" && rhsNonRankVarName!="")
nonRankVarName=lhsNonRankVarName+"_"+rhsNonRankVarName;
if(lhsNonRankVarName==rhsNonRankVarName && lhsNonRankVarName!=""){
nonRankVarName=lhsNonRankVarName;
Condition tmpCond1=this->tmpNonRankVarCondMap[lhsNonRankVarName].top();
this->tmpNonRankVarCondMap[lhsNonRankVarName].pop();
Condition tmpCond2=this->tmpNonRankVarCondMap[lhsNonRankVarName].top();
this->tmpNonRankVarCondMap[lhsNonRankVarName].pop();
Condition newTmpCond=tmpCond1.AND(tmpCond2);
newTmpCond.setNonRankVarName(lhsNonRankVarName);
this->insertTmpNonRankVarCond(lhsNonRankVarName,newTmpCond);
}
if(!lCond.hasSameGroupComparedTo(rCond))
{
string errInfo="ERROR: mixture of conditions in Group "+
lCond.getGroupName()+" AND Group "+rCond.getGroupName();
throw new MPI_TypeChecking_Error(errInfo);
}
Condition andCond=lCond.AND(rCond);
if(mixed || lCond.isMixtureCond() || rCond.isMixtureCond())
andCond.setAsMixedCond();
andCond.setNonRankVarName(nonRankVarName);
cout <<"\n\n\n\n\n"<< andCond.printConditionInfo()<<"\n\n\n\n\n" <<endl;
return andCond;
}
else if(op=="||"){
Condition lCond=extractCondFromBoolExpr(lhs);
Condition rCond=extractCondFromBoolExpr(rhs);
bool mixed=false;
if(!lCond.hasSameRankNature(rCond)){
//if warning level is high, then forbid the mixture of rank
//and non-rank condition...
if (STRICT)
{
string lCondStr=stmt2str(&ci->getSourceManager(),ci->getLangOpts(),lhs);
string rCondStr=stmt2str(&ci->getSourceManager(),ci->getLangOpts(),rhs);
string errInfo="\nCondition "+lCondStr+" and Condition "+rCondStr;
errInfo+=" are not compatible. They need to be both rank related or non-rank related.\n";
throw new MPI_TypeChecking_Error(errInfo);
}
mixed=true;
}
string nonRankVarName="";
string lhsNonRankVarName=lCond.getNonRankVarName();
string rhsNonRankVarName=rCond.getNonRankVarName();
if (lhsNonRankVarName!="" || rhsNonRankVarName!="")
nonRankVarName=lhsNonRankVarName+"_"+rhsNonRankVarName;
if(lhsNonRankVarName==rhsNonRankVarName && lhsNonRankVarName!=""){
nonRankVarName=lhsNonRankVarName;
Condition tmpCond1=this->tmpNonRankVarCondMap[lhsNonRankVarName].top();
this->tmpNonRankVarCondMap[lhsNonRankVarName].pop();
Condition tmpCond2=this->tmpNonRankVarCondMap[lhsNonRankVarName].top();
this->tmpNonRankVarCondMap[lhsNonRankVarName].pop();
Condition newTmpCond=tmpCond1.OR(tmpCond2);
newTmpCond.setNonRankVarName(lhsNonRankVarName);
this->insertTmpNonRankVarCond(lhsNonRankVarName,newTmpCond);
}
if(!lCond.hasSameGroupComparedTo(rCond))
{
string errInfo="ERROR: mixture of conditions in Group "+
lCond.getGroupName()+" AND Group "+rCond.getGroupName();
throw new MPI_TypeChecking_Error(errInfo);
}
Condition orCond=lCond.OR(rCond);
if(mixed || lCond.isMixtureCond() || rCond.isMixtureCond())
orCond.setAsMixedCond();
orCond.setNonRankVarName(nonRankVarName);
cout <<"\n\n\n\n\n"<< orCond.printConditionInfo()<<"\n\n\n\n\n" <<endl;
return orCond;
}
////it is a basic op
else {
//if the cur op is not a comparison op, then return true;
if(!binOP->isComparisonOp())
return Condition(true);
bool leftIsVar=false;
bool rightIsVar=false;
string lVarName=lhsStr;
string rVarName=rhsStr;
//check if the lhs or rhs are var decl.
////////////////////////////////////////////////////////////////////////////////
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(lhs)) {
// It's a reference to a declaration...
cout<<"//It's a reference to a declaration..."<<endl;
if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
// It's a reference to a variable (a local, function parameter, global, or static data member).
cout<<"// It's a reference to a variable (a local, function parameter, global, or static data member)."<<endl;
lVarName=VD->getQualifiedNameAsString();
std::cout << "LHS is var: " << lVarName << std::endl;
leftIsVar=true;
}
}
//check if the lhs is a var ref
else{
if(this->isAVar(lVarName)){leftIsVar=true;}
}
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(rhs)) {
// It's a reference to a declaration...
cout<<"//It's a reference to a declaration..."<<endl;
if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
// It's a reference to a variable (a local, function parameter, global, or static data member).
cout<<"// It's a reference to a variable (a local, function parameter, global, or static data member)."<<endl;
rVarName=VD->getQualifiedNameAsString();
std::cout << "RHS is var: " << rVarName << std::endl;
rightIsVar=true;
}
}
else{
if(this->isAVar(rVarName)){rightIsVar=true;}
}
///////////////////////////////////////////////////////////////////////////////
//if i has range [0..2], and the expr is rank==i,
//then the proc with range [0..2] will do the tasks.
if(op=="=="){
if (lVarName==RANKVAR)
{
Condition theCond=this->extractCondFromTargetExpr(rhs);
if(this->containsRankStr(rhsStr) && !theCond.isSameAsCond(VarCondMap[RANKVAR]))
return Condition(false);
if(theCond.size()>1)
theCond.execStr=rhsStr;
return theCond;
}
if (rVarName==RANKVAR)
{
Condition theCond=this->extractCondFromTargetExpr(lhs);
if(this->containsRankStr(lhsStr) && !theCond.isSameAsCond(VarCondMap[RANKVAR]))
return Condition(false);
if(theCond.size()>1)
theCond.execStr=lhsStr;
return theCond;
}
}
//if either lhs or rhs are bin op, then return true;
if(isa<BinaryOperator>(lhs) || isa<BinaryOperator>(rhs))
return Condition(true);
////////////////////////////////////////////////////////////////////////////////
bool isVolatileCond=false;
if(unboundVarList.count(lVarName) || unboundVarList.count(rVarName))
isVolatileCond=true;
if(leftIsVar && rightIsVar){
cout<<"Both lhs and rhs are var, so, all Range Condition!"<<endl;
return Condition(true).setVolatile(isVolatileCond);
}
else if(!this->isVarRelatedToRank(lVarName)
&& !this->isVarRelatedToRank(rVarName)){
APSInt Result;
string nonRankVarName="";
if(leftIsVar){
nonRankVarName=lVarName;
if (rhs->EvaluateAsInt(Result, this->ci->getASTContext())) {
int num=atoi(Result.toString(10).c_str());
std::cout << "The non-rank cond is created by (" <<op<<","<< num <<")"<< std::endl;
Condition cond=Condition::createCondByOp(op,num);
cond.setNonRankVarName(lVarName);
this->insertTmpNonRankVarCond(lVarName,cond);
}
}
if(rightIsVar){
nonRankVarName=rVarName;
if (lhs->EvaluateAsInt(Result, this->ci->getASTContext())) {
int num=atoi(Result.toString(10).c_str());
std::cout << "The non-rank cond is created by (" <<op<<","<< num <<")"<< std::endl;
Condition cond=Condition::createCondByOp(op,num);
cond.setNonRankVarName(rVarName);
this->insertTmpNonRankVarCond(rVarName,cond);
}
}
//if one of the vars is non-rank var, then it might be true
//so return the "all" range.
cout<<"Either the lvar or rvar is a non-rank var. So All Range Condition!"<<endl;
//also set the name of the non-rank var
Condition nonRankCond(true);
nonRankCond.setVolatile(isVolatileCond);
nonRankCond.setNonRankVarName(nonRankVarName);
return nonRankCond;
}
else{
APSInt Result;
if(leftIsVar){
string commGroupName=this->getCommGroup4RankVar(lVarName);
if (rhs->EvaluateAsInt(Result, this->ci->getASTContext())) {
int num=atoi(Result.toString(10).c_str());
std::cout << "The cond is created by (" <<op<<","<< num <<")"<< std::endl;
Condition cond=Condition::createCondByOp(op,num);
if(commGroupName!=WORLD)
cond.setCommGroup(commGroupName);
return cond.setVolatile(isVolatileCond);
}
}
if(rightIsVar){
string commGroupName=this->getCommGroup4RankVar(rVarName);
if (lhs->EvaluateAsInt(Result, this->ci->getASTContext())) {
int num=atoi(Result.toString(10).c_str());
std::cout << "The cond is created by (" <<op<<","<< num <<")"<< std::endl;
Condition cond=Condition::createCondByOp(op,num);
if(commGroupName!=WORLD)
cond.setCommGroup(commGroupName);
return cond.setVolatile(isVolatileCond);
}
}
}
}
}
return Condition(false);
}