| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2017, 2020 The University of Utah |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See the file COPYING for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #if HAVE_CONFIG_H |
| 12 | # include <config.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "ReplaceArrayIndexVar.h" |
| 16 | |
| 17 | #include <sstream> |
| 18 | #include "clang/AST/RecursiveASTVisitor.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | |
| 22 | #include "TransformationManager.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | static const char *DescriptionMsg = |
| 27 | "Looking for the pattern like below: \n\ |
| 28 | for (...;...; i++) { \n\ |
| 29 | a[i]; \n\ |
| 30 | } \n\ |
| 31 | if is less than 15, then replace a[i] with a[0] ... a[num-1]"; |
| 32 | |
| 33 | static RegisterTransformation<ReplaceArrayIndexVar> |
| 34 | Trans("replace-array-index-var", DescriptionMsg); |
| 35 | |
| 36 | typedef llvm::SmallPtrSet<const clang::ArraySubscriptExpr *, 10> |
| 37 | ArraySubscriptExprSet; |
| 38 | |
| 39 | typedef llvm::DenseMap<const clang::VarDecl *, ArraySubscriptExprSet *> |
| 40 | VarDeclToASESetMap; |
| 41 | |
| 42 | typedef llvm::DenseMap<const clang::VarDecl *, unsigned> |
| 43 | ArrayVarsToSizeMap; |
| 44 | |
| 45 | class ReplaceArrayIndexVarCollectionVisitor : public |
| 46 | RecursiveASTVisitor<ReplaceArrayIndexVarCollectionVisitor> { |
| 47 | |
| 48 | public: |
| 49 | |
| 50 | explicit ReplaceArrayIndexVarCollectionVisitor(ReplaceArrayIndexVar *Instance) |
| 51 | : ConsumerInstance(Instance) |
| 52 | { } |
| 53 | |
| 54 | bool VisitVarDecl(VarDecl *VD); |
| 55 | |
| 56 | bool VisitForStmt(ForStmt *FS); |
| 57 | |
| 58 | private: |
| 59 | |
| 60 | ReplaceArrayIndexVar *ConsumerInstance; |
| 61 | }; |
| 62 | |
| 63 | class ArraySubscriptExprCollectionVisitor : public |
| 64 | RecursiveASTVisitor<ArraySubscriptExprCollectionVisitor> { |
| 65 | |
| 66 | public: |
| 67 | |
| 68 | ArraySubscriptExprCollectionVisitor(const VarDecl *VD, |
| 69 | ArrayVarsToSizeMap &Vars, |
| 70 | VarDeclToASESetMap &Map) |
| 71 | : IdxVD(VD), |
| 72 | CstArrayVars(Vars), |
| 73 | ASEMap(Map) |
| 74 | { } |
| 75 | |
| 76 | ~ArraySubscriptExprCollectionVisitor(); |
| 77 | |
| 78 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *ASE); |
| 79 | |
| 80 | private: |
| 81 | |
| 82 | const VarDecl *getVarDeclFromExpr( const Expr *E); |
| 83 | |
| 84 | const VarDecl *IdxVD; |
| 85 | |
| 86 | ArrayVarsToSizeMap &CstArrayVars; |
| 87 | |
| 88 | VarDeclToASESetMap &ASEMap; |
| 89 | }; |
| 90 | |
| 91 | bool ReplaceArrayIndexVarCollectionVisitor::VisitVarDecl(VarDecl *VD) |
| 92 | { |
| 93 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 94 | if (ConsumerInstance->CstArrayVars[CanonicalVD]) |
| 95 | return true; |
| 96 | |
| 97 | const Type *Ty = CanonicalVD->getType().getTypePtr(); |
| 98 | const ConstantArrayType *CstArrayTy = dyn_cast<ConstantArrayType>(Ty); |
| 99 | if (!CstArrayTy) |
| 100 | return true; |
| 101 | |
| 102 | // skip multi-dimension arrays |
| 103 | const Type *ElemTy = CstArrayTy->getElementType().getTypePtr(); |
| 104 | if (dyn_cast<ArrayType>(ElemTy)) |
| 105 | return true; |
| 106 | |
| 107 | llvm::APInt APSz = CstArrayTy->getSize(); |
| 108 | unsigned Sz = (unsigned int)(*APSz.getRawData()); |
| 109 | if (Sz <= ConsumerInstance->MaxSize) |
| 110 | ConsumerInstance->CstArrayVars[CanonicalVD] = Sz; |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | bool ReplaceArrayIndexVarCollectionVisitor::VisitForStmt(ForStmt *FS) |
| 116 | { |
| 117 | if (ConsumerInstance->isInIncludedFile(FS)) |
| 118 | return true; |
| 119 | |
| 120 | const Expr *Inc = FS->getInc(); |
| 121 | if (!Inc) |
| 122 | return true; |
| 123 | |
| 124 | Stmt *Body = FS->getBody(); |
| 125 | if (!Body || dyn_cast<NullStmt>(Body)) |
| 126 | return true; |
| 127 | |
| 128 | const DeclRefExpr *DRE; |
| 129 | if (const BinaryOperator *BO = |
| 130 | dyn_cast<BinaryOperator>(Inc->IgnoreParenCasts())) { |
| 131 | if (!BO || !BO->isAssignmentOp()) |
| 132 | return true; |
| 133 | |
| 134 | const Expr *Lhs = BO->getLHS()->IgnoreParenCasts(); |
| 135 | DRE = dyn_cast<DeclRefExpr>(Lhs); |
| 136 | } |
| 137 | else if (const UnaryOperator *UO = |
| 138 | dyn_cast<UnaryOperator>(Inc->IgnoreParenCasts())) { |
| 139 | if (!UO || !UO->isIncrementDecrementOp()) |
| 140 | return true; |
| 141 | |
| 142 | const Expr *SubE = UO->getSubExpr()->IgnoreParenCasts(); |
| 143 | DRE = dyn_cast<DeclRefExpr>(SubE); |
| 144 | } |
| 145 | else { |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | if (!DRE) |
| 150 | return true; |
| 151 | |
| 152 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 153 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 154 | if (!VD) |
| 155 | return true; |
| 156 | |
| 157 | VarDeclToASESetMap ASEMap; |
| 158 | ArraySubscriptExprCollectionVisitor |
| 159 | ASEVisitor(VD->getCanonicalDecl(), ConsumerInstance->CstArrayVars, ASEMap); |
| 160 | |
| 161 | ASEVisitor.TraverseStmt(Body); |
| 162 | |
| 163 | for (VarDeclToASESetMap::iterator I = ASEMap.begin(), |
| 164 | E = ASEMap.end(); I != E; ++I) { |
| 165 | const VarDecl *CurrVD = (*I).first; |
| 166 | ArraySubscriptExprSet *ESet = (*I).second; |
| 167 | if (!ESet) |
| 168 | continue; |
| 169 | |
| 170 | TransAssert(ConsumerInstance->CstArrayVars.count(CurrVD) && |
| 171 | "Cannot find CurrVD!"); |
| 172 | unsigned Sz = ConsumerInstance->CstArrayVars[CurrVD]; |
| 173 | for (unsigned Idx = 0; Idx < Sz; Idx++) { |
| 174 | ConsumerInstance->ValidInstanceNum++; |
| 175 | if (!ConsumerInstance->TheASESet && (ConsumerInstance->ValidInstanceNum == |
| 176 | ConsumerInstance->TransformationCounter)) { |
| 177 | ConsumerInstance->TheASESet = new ArraySubscriptExprSet(*ESet); |
| 178 | ConsumerInstance->CurrIdx = Idx; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | const VarDecl *ArraySubscriptExprCollectionVisitor::getVarDeclFromExpr( |
| 186 | const Expr *E) |
| 187 | { |
| 188 | TransAssert(E && "NULL Expr!"); |
| 189 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 190 | if (!DRE) |
| 191 | return NULL; |
| 192 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 193 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 194 | if (!VD) |
| 195 | return NULL; |
| 196 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 197 | return CanonicalVD; |
| 198 | } |
| 199 | |
| 200 | bool ArraySubscriptExprCollectionVisitor::VisitArraySubscriptExpr( |
| 201 | ArraySubscriptExpr *ASE) |
| 202 | { |
| 203 | const VarDecl *BaseVD = getVarDeclFromExpr(ASE->getBase()); |
| 204 | if (!BaseVD || !CstArrayVars[BaseVD]) |
| 205 | return true; |
| 206 | |
| 207 | const VarDecl *VD = getVarDeclFromExpr(ASE->getIdx()); |
| 208 | if (!VD || (VD != IdxVD)) |
| 209 | return true; |
| 210 | |
| 211 | ArraySubscriptExprSet *ESet = ASEMap[BaseVD]; |
| 212 | if (!ESet) { |
| 213 | ESet = new ArraySubscriptExprSet(); |
| 214 | ASEMap[BaseVD] = ESet; |
| 215 | } |
| 216 | ESet->insert(ASE); |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | ArraySubscriptExprCollectionVisitor::~ArraySubscriptExprCollectionVisitor(void) |
| 221 | { |
| 222 | for (VarDeclToASESetMap::iterator I = ASEMap.begin(), |
| 223 | E = ASEMap.end(); I != E; ++I) { |
| 224 | ArraySubscriptExprSet *Set = (*I).second; |
| 225 | delete Set; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void ReplaceArrayIndexVar::Initialize(ASTContext &context) |
| 230 | { |
| 231 | Transformation::Initialize(context); |
| 232 | CollectionVisitor = new ReplaceArrayIndexVarCollectionVisitor(this); |
| 233 | } |
| 234 | |
| 235 | void ReplaceArrayIndexVar::HandleTranslationUnit(ASTContext &Ctx) |
| 236 | { |
| 237 | TransAssert(CollectionVisitor && "NULL CollectionVisitor!"); |
| 238 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 239 | |
| 240 | if (QueryInstanceOnly) |
| 241 | return; |
| 242 | |
| 243 | if (TransformationCounter > ValidInstanceNum) { |
| 244 | TransError = TransMaxInstanceError; |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 249 | doRewrite(); |
| 250 | |
| 251 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 252 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 253 | TransError = TransInternalError; |
| 254 | } |
| 255 | |
| 256 | void ReplaceArrayIndexVar::doRewrite(void) |
| 257 | { |
| 258 | for (ArraySubscriptExprSet::iterator I = TheASESet->begin(), |
| 259 | E = TheASESet->end(); I != E; ++I) { |
| 260 | const ArraySubscriptExpr *ASE = (*I); |
| 261 | const Expr *Idx = ASE->getIdx(); |
| 262 | TransAssert(Idx && "Bad Idx!"); |
| 263 | |
| 264 | std::stringstream TmpSS; |
| 265 | TmpSS << CurrIdx; |
| 266 | RewriteHelper->replaceExpr(Idx, TmpSS.str()); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | ReplaceArrayIndexVar::~ReplaceArrayIndexVar(void) |
| 271 | { |
| 272 | delete CollectionVisitor; |
| 273 | delete TheASESet; |
| 274 | } |
| 275 |