forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplaceArrayIndexVar.cpp
274 lines (216 loc) · 7.09 KB
/
ReplaceArrayIndexVar.cpp
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
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2012, 2013 The University of Utah
// All rights reserved.
//
// This file is distributed under the University of Illinois Open Source
// License. See the file COPYING for details.
//
//===----------------------------------------------------------------------===//
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "ReplaceArrayIndexVar.h"
#include <sstream>
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceManager.h"
#include "TransformationManager.h"
using namespace clang;
using namespace llvm;
static const char *DescriptionMsg =
"Looking for the pattern like below: \n\
for (...;...; i++) { \n\
a[i]; \n\
} \n\
if is less than 15, then replace a[i] with a[0] ... a[num-1]";
static RegisterTransformation<ReplaceArrayIndexVar>
Trans("replace-array-index-var", DescriptionMsg);
typedef SmallPtrSet<const clang::ArraySubscriptExpr *, 10>
ArraySubscriptExprSet;
typedef DenseMap<const clang::VarDecl *, ArraySubscriptExprSet *>
VarDeclToASESetMap;
typedef llvm::DenseMap<const clang::VarDecl *, unsigned>
ArrayVarsToSizeMap;
class ReplaceArrayIndexVarCollectionVisitor : public
RecursiveASTVisitor<ReplaceArrayIndexVarCollectionVisitor> {
public:
explicit ReplaceArrayIndexVarCollectionVisitor(ReplaceArrayIndexVar *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitVarDecl(VarDecl *VD);
bool VisitForStmt(ForStmt *FS);
private:
ReplaceArrayIndexVar *ConsumerInstance;
};
class ArraySubscriptExprCollectionVisitor : public
RecursiveASTVisitor<ArraySubscriptExprCollectionVisitor> {
public:
ArraySubscriptExprCollectionVisitor(const VarDecl *VD,
ArrayVarsToSizeMap &Vars,
VarDeclToASESetMap &Map)
: IdxVD(VD),
CstArrayVars(Vars),
ASEMap(Map)
{ }
~ArraySubscriptExprCollectionVisitor();
bool VisitArraySubscriptExpr(ArraySubscriptExpr *ASE);
private:
const VarDecl *getVarDeclFromExpr( const Expr *E);
const VarDecl *IdxVD;
ArrayVarsToSizeMap &CstArrayVars;
VarDeclToASESetMap &ASEMap;
};
bool ReplaceArrayIndexVarCollectionVisitor::VisitVarDecl(VarDecl *VD)
{
const VarDecl *CanonicalVD = VD->getCanonicalDecl();
if (ConsumerInstance->CstArrayVars[CanonicalVD])
return true;
const Type *Ty = CanonicalVD->getType().getTypePtr();
const ConstantArrayType *CstArrayTy = dyn_cast<ConstantArrayType>(Ty);
if (!CstArrayTy)
return true;
// skip multi-dimension arrays
const Type *ElemTy = CstArrayTy->getElementType().getTypePtr();
if (dyn_cast<ArrayType>(ElemTy))
return true;
APInt APSz = CstArrayTy->getSize();
unsigned Sz = (unsigned int)(*APSz.getRawData());
if (Sz <= ConsumerInstance->MaxSize)
ConsumerInstance->CstArrayVars[CanonicalVD] = Sz;
return true;
}
bool ReplaceArrayIndexVarCollectionVisitor::VisitForStmt(ForStmt *FS)
{
const Expr *Inc = FS->getInc();
if (!Inc)
return true;
Stmt *Body = FS->getBody();
if (!Body || dyn_cast<NullStmt>(Body))
return true;
const DeclRefExpr *DRE;
if (const BinaryOperator *BO =
dyn_cast<BinaryOperator>(Inc->IgnoreParenCasts())) {
if (!BO || !BO->isAssignmentOp())
return true;
const Expr *Lhs = BO->getLHS()->IgnoreParenCasts();
DRE = dyn_cast<DeclRefExpr>(Lhs);
}
else if (const UnaryOperator *UO =
dyn_cast<UnaryOperator>(Inc->IgnoreParenCasts())) {
if (!UO || !UO->isIncrementDecrementOp())
return true;
const Expr *SubE = UO->getSubExpr()->IgnoreParenCasts();
DRE = dyn_cast<DeclRefExpr>(SubE);
}
else {
return true;
}
if (!DRE)
return true;
const ValueDecl *OrigDecl = DRE->getDecl();
const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl);
if (!VD)
return true;
VarDeclToASESetMap ASEMap;
ArraySubscriptExprCollectionVisitor
ASEVisitor(VD->getCanonicalDecl(), ConsumerInstance->CstArrayVars, ASEMap);
ASEVisitor.TraverseStmt(Body);
for (VarDeclToASESetMap::iterator I = ASEMap.begin(),
E = ASEMap.end(); I != E; ++I) {
const VarDecl *CurrVD = (*I).first;
ArraySubscriptExprSet *ESet = (*I).second;
if (!ESet)
continue;
TransAssert(ConsumerInstance->CstArrayVars.count(CurrVD) &&
"Cannot find CurrVD!");
unsigned Sz = ConsumerInstance->CstArrayVars[CurrVD];
for (unsigned Idx = 0; Idx < Sz; Idx++) {
ConsumerInstance->ValidInstanceNum++;
if (!ConsumerInstance->TheASESet && (ConsumerInstance->ValidInstanceNum ==
ConsumerInstance->TransformationCounter)) {
ConsumerInstance->TheASESet = new ArraySubscriptExprSet(*ESet);
ConsumerInstance->CurrIdx = Idx;
}
}
}
return true;
}
const VarDecl *ArraySubscriptExprCollectionVisitor::getVarDeclFromExpr(
const Expr *E)
{
TransAssert(E && "NULL Expr!");
const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
if (!DRE)
return NULL;
const ValueDecl *OrigDecl = DRE->getDecl();
const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl);
if (!VD)
return NULL;
const VarDecl *CanonicalVD = VD->getCanonicalDecl();
return CanonicalVD;
}
bool ArraySubscriptExprCollectionVisitor::VisitArraySubscriptExpr(
ArraySubscriptExpr *ASE)
{
const VarDecl *BaseVD = getVarDeclFromExpr(ASE->getBase());
if (!BaseVD || !CstArrayVars[BaseVD])
return true;
const VarDecl *VD = getVarDeclFromExpr(ASE->getIdx());
if (!VD || (VD != IdxVD))
return true;
ArraySubscriptExprSet *ESet = ASEMap[BaseVD];
if (!ESet) {
ESet = new ArraySubscriptExprSet();
ASEMap[BaseVD] = ESet;
}
ESet->insert(ASE);
return true;
}
ArraySubscriptExprCollectionVisitor::~ArraySubscriptExprCollectionVisitor(void)
{
for (VarDeclToASESetMap::iterator I = ASEMap.begin(),
E = ASEMap.end(); I != E; ++I) {
ArraySubscriptExprSet *Set = (*I).second;
if (Set)
delete Set;
}
}
void ReplaceArrayIndexVar::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
CollectionVisitor = new ReplaceArrayIndexVarCollectionVisitor(this);
}
void ReplaceArrayIndexVar::HandleTranslationUnit(ASTContext &Ctx)
{
TransAssert(CollectionVisitor && "NULL CollectionVisitor!");
CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
if (QueryInstanceOnly)
return;
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
doRewrite();
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
void ReplaceArrayIndexVar::doRewrite(void)
{
for (ArraySubscriptExprSet::iterator I = TheASESet->begin(),
E = TheASESet->end(); I != E; ++I) {
const ArraySubscriptExpr *ASE = (*I);
const Expr *Idx = ASE->getIdx();
TransAssert(Idx && "Bad Idx!");
std::stringstream TmpSS;
TmpSS << CurrIdx;
RewriteHelper->replaceExpr(Idx, TmpSS.str());
}
}
ReplaceArrayIndexVar::~ReplaceArrayIndexVar(void)
{
delete CollectionVisitor;
delete TheASESet;
}