|
| 1 | +// |
| 2 | +// This file is distributed under the MIT License. See LICENSE for details. |
| 3 | +// |
| 4 | + |
| 5 | +#define DEBUG_TYPE "smack-loop-info" |
| 6 | +#include "smack/LoopInfo.h" |
| 7 | +#include "smack/Debug.h" |
| 8 | +#include "smack/SmackWarnings.h" |
| 9 | +#include "llvm/Analysis/LoopInfo.h" |
| 10 | +#include "llvm/Analysis/ScalarEvolution.h" |
| 11 | +#include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 12 | +#include "llvm/Support/raw_ostream.h" |
| 13 | + |
| 14 | +namespace smack { |
| 15 | + |
| 16 | +using namespace llvm; |
| 17 | + |
| 18 | +void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 19 | + AU.setPreservesAll(); |
| 20 | + AU.addRequired<LoopInfoWrapperPass>(); |
| 21 | + AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 22 | +} |
| 23 | + |
| 24 | +bool LoopInfo::runOnFunction(Function &F) { |
| 25 | + auto &loopInfo = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 26 | + auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 27 | + for (auto LI = loopInfo.begin(), LIEnd = loopInfo.end(); LI != LIEnd; ++LI) { |
| 28 | + auto L = *LI; |
| 29 | + auto lr = L->getLocRange(); |
| 30 | + auto sl = lr.getStart(); |
| 31 | + auto el = lr.getEnd(); |
| 32 | + std::string description; |
| 33 | + raw_string_ostream o(description); |
| 34 | + o << "found loop from line " << sl.getLine() << " to line " << el.getLine() |
| 35 | + << " in function " << F.getName() << " with "; |
| 36 | + // TODO: figure out why induction variable won't work |
| 37 | + // auto bs = L->getInductionVariable(SE); |
| 38 | + if (auto C = |
| 39 | + dyn_cast<SCEVConstant>(SE.getConstantMaxBackedgeTakenCount(L))) { |
| 40 | + auto CI = C->getValue()->getValue().getZExtValue(); |
| 41 | + o << "known loop bound " << CI; |
| 42 | + } else |
| 43 | + o << "unknown loop bound"; |
| 44 | + SmackWarnings::warnLoop(o.str()); |
| 45 | + } |
| 46 | + return false; |
| 47 | +} |
| 48 | + |
| 49 | +char LoopInfo::ID = 0; |
| 50 | + |
| 51 | +StringRef LoopInfo::getPassName() const { |
| 52 | + return "Get Loop Information for the purpose of sound analysis"; |
| 53 | +} |
| 54 | +} // namespace smack |
0 commit comments