From b7eaa99578037789a337c5061c0ea8ee3150b63c Mon Sep 17 00:00:00 2001 From: "Zancanaro; Carlo" Date: Thu, 1 Nov 2012 18:06:13 +1100 Subject: A bunch of fixes to the solver, and moving it in to clang. Also some contribution writing stuff. Basically: lots of work. --- clang/lib/Analysis/Interval.cpp | 35 +++-------------------------------- 1 file changed, 3 insertions(+), 32 deletions(-) (limited to 'clang/lib/Analysis') diff --git a/clang/lib/Analysis/Interval.cpp b/clang/lib/Analysis/Interval.cpp index ac96107..3a82344 100644 --- a/clang/lib/Analysis/Interval.cpp +++ b/clang/lib/Analysis/Interval.cpp @@ -221,11 +221,6 @@ M merge_maps_with(const F& f, const M& left, const M& right) { return result; } -template<> -Vector minimum(const Vector& l, const Vector& r) { - return (l < r ? l : r); - return merge_maps_with(minimum, l, r); -} template T max(const T& l, const T& r) { return (l < r ? l : r); @@ -702,39 +697,15 @@ void IntervalAnalysis::runOnAllBlocks() { } } - std::vector a; - - a.push_back(&system.constant(-infinity())); - a.push_back(&system.constant(0)); - system[system.variable("x")] = &system.maxExpression(a); - a.clear(); - - system.variable("y"); - - a.push_back(&system.variable("x")); - a.push_back(&system.variable("z")); - EqnExpr* minExpr = &system.expression(new Maximum(), a); - a.clear(); - - a.push_back(&system.constant(-infinity())); - a.push_back(minExpr); - system[system.variable("y")] = &system.maxExpression(a); - a.clear(); - - a.push_back(&system.constant(-infinity())); - a.push_back(&system.variable("y")); - system[system.variable("z")] = &system.maxExpression(a); - llvm::errs() << toString(system) << "\n"; system.indexMaxExpressions(); - DynamicMaxStrategy strategy(system); - DynamicVariableAssignment rho(system, strategy); - strategy.setRho(rho); + + Solver solver(system); for (unsigned int i = 0, size = system.variableCount(); i < size; ++i) { EqnVar& var = system.variable(size - i - 1); - llvm::errs() << toString(var.name()) << " = " << toString(rho[var]) << "\n"; + llvm::errs() << toString(var.name()) << " = " << toString(solver.solve(var)) << "\n"; } } -- cgit v1.2.3 From ff5ec13849a69ef4a53bab009eed880456931a14 Mon Sep 17 00:00:00 2001 From: "Zancanaro; Carlo" Date: Thu, 1 Nov 2012 20:02:14 +1100 Subject: Fixing up some equation system stuff. Adding function arguments to the system, as well as making it slightly easier to read. --- clang/include/clang/Analysis/Analyses/.#Interval.h | 1 - .../clang/Analysis/Analyses/.#Interval_flymake.h | 1 - clang/include/clang/Analysis/Analyses/Interval.h | 2 +- clang/lib/Analysis/Interval.cpp | 34 ++++++++++++++++++---- .../lib/StaticAnalyzer/Checkers/.#IntervalTest.cpp | 1 - clang/lib/StaticAnalyzer/Checkers/IntervalTest.cpp | 2 +- impl/test/14.eqns | 3 ++ impl/test/14.soln | 3 ++ 8 files changed, 36 insertions(+), 11 deletions(-) delete mode 120000 clang/include/clang/Analysis/Analyses/.#Interval.h delete mode 120000 clang/include/clang/Analysis/Analyses/.#Interval_flymake.h delete mode 120000 clang/lib/StaticAnalyzer/Checkers/.#IntervalTest.cpp create mode 100644 impl/test/14.eqns create mode 100644 impl/test/14.soln (limited to 'clang/lib/Analysis') diff --git a/clang/include/clang/Analysis/Analyses/.#Interval.h b/clang/include/clang/Analysis/Analyses/.#Interval.h deleted file mode 120000 index 235903b..0000000 --- a/clang/include/clang/Analysis/Analyses/.#Interval.h +++ /dev/null @@ -1 +0,0 @@ -carlo@pc-4w14-0.cs.usyd.edu.au.1585:1347012043 \ No newline at end of file diff --git a/clang/include/clang/Analysis/Analyses/.#Interval_flymake.h b/clang/include/clang/Analysis/Analyses/.#Interval_flymake.h deleted file mode 120000 index 235903b..0000000 --- a/clang/include/clang/Analysis/Analyses/.#Interval_flymake.h +++ /dev/null @@ -1 +0,0 @@ -carlo@pc-4w14-0.cs.usyd.edu.au.1585:1347012043 \ No newline at end of file diff --git a/clang/include/clang/Analysis/Analyses/Interval.h b/clang/include/clang/Analysis/Analyses/Interval.h index 6c0d5cf..8bb8d0f 100644 --- a/clang/include/clang/Analysis/Analyses/Interval.h +++ b/clang/include/clang/Analysis/Analyses/Interval.h @@ -33,7 +33,7 @@ public: IntervalAnalysis(AnalysisDeclContext &analysisContext); virtual ~IntervalAnalysis(); - void runOnAllBlocks(); + void runOnAllBlocks(const Decl&); static IntervalAnalysis *create(AnalysisDeclContext &analysisContext) { return new IntervalAnalysis(analysisContext); diff --git a/clang/lib/Analysis/Interval.cpp b/clang/lib/Analysis/Interval.cpp index 3a82344..7096d75 100644 --- a/clang/lib/Analysis/Interval.cpp +++ b/clang/lib/Analysis/Interval.cpp @@ -637,10 +637,16 @@ void runOnBlock(const CFGBlock* block, EqnSys& system, BlockVars& block_vars) { std::vector plusArgs; for (int negate = 0; negate < 2; ++negate) { std::string var_name = negate ? negate_string(variables->first) : variables->first; - std::vector minArgs; - minArgs.push_back(vars[var_name]); - minArgs.push_back(&system.constant(cond[var_name])); - plusArgs.push_back(&system.expression(new Minimum(), minArgs)); + if (cond[var_name] == infinity()) { + plusArgs.push_back(vars[var_name]); + } else if (cond[var_name] == -infinity()) { + plusArgs.push_back(&system.constant(-infinity())); + } else { + std::vector minArgs; + minArgs.push_back(vars[var_name]); + minArgs.push_back(&system.constant(cond[var_name])); + plusArgs.push_back(&system.expression(new Minimum(), minArgs)); + } } std::vector guard_args; @@ -667,15 +673,31 @@ IntervalAnalysis :: IntervalAnalysis(AnalysisDeclContext &context) IntervalAnalysis :: ~IntervalAnalysis() { } -void IntervalAnalysis::runOnAllBlocks() { +void IntervalAnalysis::runOnAllBlocks(const Decl& decl) { const CFG *cfg = this->context->getCFG(); cfg->dump(context->getASTContext().getLangOpts(), llvm::sys::Process::StandardErrHasColors()); - EqnSys system; + EqnSys system; BlockVars block_vars; + std::vector infArg; + infArg.push_back(&system.constant(infinity())); + std::set& function_arguments = block_vars[&cfg->getEntry()]; + std::string block_id = toString(cfg->getEntry().getBlockID()); + if (const FunctionDecl* func = dyn_cast(&decl)) { + for (unsigned int i = func->getNumParams(); i > 0; i--) { + std::string name = func->getParamDecl(i-1)->getNameAsString(); + + function_arguments.insert(name); // add it to the first block's vars + function_arguments.insert(negate_string(name)); // add it to the first block's vars + + system[system.variable(name + '-' + block_id + "-pre")] = &system.maxExpression(infArg); // set the vars to infinity in the system here + system[system.variable(negate_string(name) + '-' + block_id + "-pre")] = &system.maxExpression(infArg); // set the vars to infinity in the system here + } + } + std::set seen; std::deque todo; todo.push_back(&cfg->getEntry()); diff --git a/clang/lib/StaticAnalyzer/Checkers/.#IntervalTest.cpp b/clang/lib/StaticAnalyzer/Checkers/.#IntervalTest.cpp deleted file mode 120000 index 235903b..0000000 --- a/clang/lib/StaticAnalyzer/Checkers/.#IntervalTest.cpp +++ /dev/null @@ -1 +0,0 @@ -carlo@pc-4w14-0.cs.usyd.edu.au.1585:1347012043 \ No newline at end of file diff --git a/clang/lib/StaticAnalyzer/Checkers/IntervalTest.cpp b/clang/lib/StaticAnalyzer/Checkers/IntervalTest.cpp index 2f3e155..badb671 100644 --- a/clang/lib/StaticAnalyzer/Checkers/IntervalTest.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/IntervalTest.cpp @@ -14,7 +14,7 @@ public: void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, BugReporter &BR) const { if (IntervalAnalysis *a = mgr.getAnalysis(D)) { - a->runOnAllBlocks(); + a->runOnAllBlocks(*D); } } }; diff --git a/impl/test/14.eqns b/impl/test/14.eqns new file mode 100644 index 0000000..6c28481 --- /dev/null +++ b/impl/test/14.eqns @@ -0,0 +1,3 @@ +x = 0 +y = max(x, z) +z = y + 1 diff --git a/impl/test/14.soln b/impl/test/14.soln new file mode 100644 index 0000000..c0301bb --- /dev/null +++ b/impl/test/14.soln @@ -0,0 +1,3 @@ +x = 0 +y = inf +z = inf -- cgit v1.2.3