| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2016, 2017, 2019 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 "AggregateToScalar.h" |
| 16 | |
| 17 | #include <sstream> |
| 18 | |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | |
| 23 | #include "TransformationManager.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | static const char *DescriptionMsg = |
| 28 | "Replace accesses to an aggregate member with \ |
| 29 | a corresponding scalar variable. \ |
| 30 | In more detail, the transformation creates \ |
| 31 | a scalar variable for an aggregate access, \ |
| 32 | assigns the initial value of the aggregate member to \ |
| 33 | the scalar, and substitutes all accesses to the same aggregate \ |
| 34 | member with the accesses to the corresponding scalar variable. \ |
| 35 | (Note that this transformation is unsound).\n"; |
| 36 | |
| 37 | static RegisterTransformation<AggregateToScalar> |
| 38 | Trans("aggregate-to-scalar", DescriptionMsg); |
| 39 | |
| 40 | class ATSCollectionVisitor : public RecursiveASTVisitor<ATSCollectionVisitor> { |
| 41 | |
| 42 | public: |
| 43 | explicit ATSCollectionVisitor(AggregateToScalar *Instance) |
| 44 | : ConsumerInstance(Instance) |
| 45 | { } |
| 46 | |
| 47 | bool VisitMemberExpr(MemberExpr *ME); |
| 48 | |
| 49 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *ASE); |
| 50 | |
| 51 | bool VisitDeclStmt(DeclStmt *DS); |
| 52 | |
| 53 | private: |
| 54 | |
| 55 | AggregateToScalar *ConsumerInstance; |
| 56 | |
| 57 | }; |
| 58 | |
| 59 | bool ATSCollectionVisitor::VisitMemberExpr(MemberExpr *ME) |
| 60 | { |
| 61 | if (ConsumerInstance->isInIncludedFile(ME)) |
| 62 | return true; |
| 63 | |
| 64 | ValueDecl *OrigDecl = ME->getMemberDecl(); |
| 65 | FieldDecl *FD = dyn_cast<FieldDecl>(OrigDecl); |
| 66 | |
| 67 | if (!FD) { |
| 68 | // in C++, getMemberDecl returns a CXXMethodDecl. |
| 69 | if (TransformationManager::isCXXLangOpt()) |
| 70 | return true; |
| 71 | TransAssert(0 && "Bad FD!\n"); |
| 72 | } |
| 73 | |
| 74 | const Type *T = FD->getType().getTypePtr(); |
| 75 | if (!T->isScalarType()) |
| 76 | return true; |
| 77 | |
| 78 | RecordDecl *RD = FD->getParent(); |
| 79 | TransAssert(RD && "NULL RecordDecl!"); |
| 80 | if (!RD->isStruct() && !RD->isUnion()) |
| 81 | return true; |
| 82 | |
| 83 | ConsumerInstance->addOneExpr(ME); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool ATSCollectionVisitor::VisitArraySubscriptExpr(ArraySubscriptExpr *ASE) |
| 88 | { |
| 89 | if (ConsumerInstance->isInIncludedFile(ASE)) |
| 90 | return true; |
| 91 | |
| 92 | const Type *T = ASE->getType().getTypePtr(); |
| 93 | if (!T->isScalarType()) |
| 94 | return true; |
| 95 | |
| 96 | ConsumerInstance->addOneExpr(ASE); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool ATSCollectionVisitor::VisitDeclStmt(DeclStmt *DS) |
| 101 | { |
| 102 | for (DeclStmt::decl_iterator I = DS->decl_begin(), |
| 103 | E = DS->decl_end(); I != E; ++I) { |
| 104 | VarDecl *CurrDecl = dyn_cast<VarDecl>(*I); |
| 105 | if (CurrDecl) |
| 106 | ConsumerInstance->VarDeclToDeclStmtMap[CurrDecl] = DS; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | void AggregateToScalar::Initialize(ASTContext &context) |
| 112 | { |
| 113 | Transformation::Initialize(context); |
| 114 | AggregateAccessVisitor = new ATSCollectionVisitor(this); |
| 115 | } |
| 116 | |
| 117 | bool AggregateToScalar::HandleTopLevelDecl(DeclGroupRef D) |
| 118 | { |
| 119 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 120 | AggregateAccessVisitor->TraverseDecl(*I); |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | void AggregateToScalar::HandleTranslationUnit(ASTContext &Ctx) |
| 126 | { |
| 127 | if (QueryInstanceOnly) |
| 128 | return; |
| 129 | |
| 130 | if (TransformationCounter > ValidInstanceNum) { |
| 131 | TransError = TransMaxInstanceError; |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 136 | |
| 137 | TransAssert(TheVarDecl && "NULL TheVarDecl!"); |
| 138 | TransAssert(TheIdx && "NULL TheIdx!"); |
| 139 | |
| 140 | doRewrite(); |
| 141 | |
| 142 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 143 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 144 | TransError = TransInternalError; |
| 145 | } |
| 146 | |
| 147 | bool AggregateToScalar::addTmpVar(const Expr *RefE, |
| 148 | const std::string &VarName, |
| 149 | const std::string *InitStr) |
| 150 | { |
| 151 | std::string VarStr(VarName); |
| 152 | QualType QT = RefE->getType(); |
| 153 | QT.getAsStringInternal(VarStr, Context->getPrintingPolicy()); |
| 154 | |
| 155 | if (InitStr) { |
| 156 | VarStr += " = "; |
| 157 | VarStr += (*InitStr); |
| 158 | } |
| 159 | VarStr += ";"; |
| 160 | |
| 161 | if (TheVarDecl->getStorageClass() == SC_Static) |
| 162 | VarStr = "static " + VarStr; |
| 163 | |
| 164 | TransAssert(!dyn_cast<ParmVarDecl>(TheVarDecl) && |
| 165 | "We don't handle ParmVarDecl!"); |
| 166 | if (TheVarDecl->isLocalVarDecl()) { |
| 167 | DeclStmt *TheDeclStmt = VarDeclToDeclStmtMap[TheVarDecl]; |
| 168 | TransAssert(TheDeclStmt && "NULL TheDeclStmt"); |
| 169 | return RewriteHelper->addStringAfterStmt(TheDeclStmt, VarStr); |
| 170 | } |
| 171 | else { |
| 172 | return RewriteHelper->addStringAfterVarDecl(TheVarDecl, VarStr); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void AggregateToScalar::createNewVarName(std::string &VarName) |
| 177 | { |
| 178 | std::stringstream SS; |
| 179 | SS << TheVarDecl->getNameAsString(); |
| 180 | for (IndexVector::const_reverse_iterator I = TheIdx->rbegin(), |
| 181 | E = TheIdx->rend(); I != E; ++I) { |
| 182 | SS << "_" << (*I); |
| 183 | } |
| 184 | |
| 185 | VarName = SS.str(); |
| 186 | } |
| 187 | |
| 188 | bool AggregateToScalar::createNewVar(const Expr *RefE, std::string &VarName) |
| 189 | { |
| 190 | const Type *VarT = TheVarDecl->getType().getTypePtr(); |
| 191 | TransAssert((VarT->isStructureType() || VarT->isUnionType() |
| 192 | || VarT->isArrayType() || VarT->isPointerType()) |
| 193 | && "Non-valid var type!"); |
| 194 | |
| 195 | createNewVarName(VarName); |
| 196 | |
| 197 | if (VarT->isPointerType()) |
| 198 | return addTmpVar(RefE, VarName, NULL); |
| 199 | |
| 200 | const Expr *IE = TheVarDecl->getAnyInitializer(); |
| 201 | if (!IE) |
| 202 | return addTmpVar(RefE, VarName, NULL); |
| 203 | |
| 204 | const InitListExpr *ILE = dyn_cast<InitListExpr>(IE); |
| 205 | if (!ILE) { |
| 206 | return addTmpVar(RefE, VarName, NULL); |
| 207 | } |
| 208 | |
| 209 | const Expr *InitE = getInitExprByIndex(*TheIdx, ILE); |
| 210 | // We might have incomplete initialization list |
| 211 | if (!InitE) |
| 212 | return addTmpVar(RefE, VarName, NULL); |
| 213 | |
| 214 | std::string InitStr; |
| 215 | if (InitE->getBeginLoc().isInvalid()) { |
| 216 | const Type *ET = InitE->getType().getTypePtr(); |
| 217 | if (ET->isIntegerType() || ET->isPointerType()) |
| 218 | InitStr = "0"; |
| 219 | else |
| 220 | return addTmpVar(RefE, VarName, NULL); |
| 221 | } |
| 222 | else { |
| 223 | RewriteHelper->getExprString(InitE, InitStr); |
| 224 | } |
| 225 | return addTmpVar(RefE, VarName, &InitStr); |
| 226 | } |
| 227 | |
| 228 | void AggregateToScalar::doRewrite(void) |
| 229 | { |
| 230 | ExprSet *TheExprSet = ValidExprs[TheIdx]; |
| 231 | TransAssert(!TheExprSet->empty() && "TheExprSet cannot be empty!"); |
| 232 | |
| 233 | ExprSet::iterator I = TheExprSet->begin(), E = TheExprSet->end(); |
| 234 | std::string VarName(""); |
| 235 | createNewVar(*I, VarName); |
| 236 | |
| 237 | for (; I != E; ++I) { |
| 238 | RewriteHelper->replaceExpr((*I), VarName); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void AggregateToScalar::addOneIdx(const Expr *E, |
| 243 | const VarDecl *VD, |
| 244 | IdxVectorSet *IdxSet, |
| 245 | IndexVector *Idx) |
| 246 | { |
| 247 | IdxSet->insert(Idx); |
| 248 | ExprSet *ESet = new ExprSet(); |
| 249 | ValidExprs[Idx] = ESet; |
| 250 | ESet->insert(E); |
| 251 | ValidInstanceNum++; |
| 252 | if (ValidInstanceNum == TransformationCounter) { |
| 253 | TheVarDecl = VD; |
| 254 | TheIdx = Idx; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | bool AggregateToScalar::isStructuralEqualVectors(IndexVector *IV1, |
| 259 | IndexVector *IV2) |
| 260 | { |
| 261 | unsigned int Sz = IV1->size(); |
| 262 | if (Sz != IV2->size()) |
| 263 | return false; |
| 264 | |
| 265 | for (unsigned int I = 0; I < Sz; ++I) { |
| 266 | unsigned int Idx1 = (*IV1)[I]; |
| 267 | unsigned int Idx2 = (*IV2)[I]; |
| 268 | if (Idx1 != Idx2) |
| 269 | return false; |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | void AggregateToScalar::addOneExpr(const Expr *Exp) |
| 275 | { |
| 276 | IndexVector *Idx = new IndexVector(); |
| 277 | const Expr *BaseE = getBaseExprAndIdxs(Exp, *Idx); |
| 278 | TransAssert(BaseE && "Invalid Base Expr for ArraySubscriptExpr!"); |
| 279 | |
| 280 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseE); |
| 281 | if (!DRE) { |
| 282 | delete Idx; |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 287 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 288 | if (!VD) { |
| 289 | delete Idx; |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | if (dyn_cast<ParmVarDecl>(VD)) { |
| 294 | delete Idx; |
| 295 | return; |
| 296 | } |
| 297 | const Type *VarT = VD->getType().getTypePtr(); |
| 298 | if (!VarT->isStructureType() && !VarT->isUnionType() && |
| 299 | !VarT->isArrayType() && !VarT->isPointerType()) { |
| 300 | delete Idx; |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | const VarDecl *CanonicalDecl = VD->getCanonicalDecl(); |
| 305 | IdxVectorSet *IdxSet = ValidVars[CanonicalDecl]; |
| 306 | if (!IdxSet) { |
| 307 | IdxSet = new IdxVectorSet(); |
| 308 | ValidVars[VD] = IdxSet; |
| 309 | addOneIdx(Exp, VD, IdxSet, Idx); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | IndexVector *CachedIdx = NULL; |
| 314 | for (IdxVectorSet::iterator I = IdxSet->begin(), E = IdxSet->end(); |
| 315 | I != E; ++I) { |
| 316 | if (isStructuralEqualVectors(*I, Idx)) { |
| 317 | CachedIdx = (*I); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (!CachedIdx) { |
| 323 | addOneIdx(Exp, VD, IdxSet, Idx); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | ExprSet *CachedESet = ValidExprs[CachedIdx]; |
| 328 | TransAssert(CachedESet && "NULL CachedESet!"); |
| 329 | CachedESet->insert(Exp); |
| 330 | delete Idx; |
| 331 | } |
| 332 | |
| 333 | AggregateToScalar::~AggregateToScalar(void) |
| 334 | { |
| 335 | delete AggregateAccessVisitor; |
| 336 | |
| 337 | for (VarToIdx::iterator I = ValidVars.begin(), |
| 338 | E = ValidVars.end(); I != E; ++I) { |
| 339 | delete (*I).second; |
| 340 | } |
| 341 | |
| 342 | for (IdxToExpr::iterator I = ValidExprs.begin(), |
| 343 | E = ValidExprs.end(); I != E; ++I) { |
| 344 | delete (*I).first; |
| 345 | delete (*I).second; |
| 346 | } |
| 347 | } |
| 348 |