diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index 70ea990f55..f811fa3f0f 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -467,6 +467,30 @@ private: CachedBoolEvals[S] = Result; // update or insert return Result; } + else { + switch (Bop->getOpcode()) { + default: break; + // For 'x & 0' and 'x * 0', we can determine that + // the value is always false. + case BO_Mul: + case BO_And: { + // If either operand is zero, we know the value + // must be false. + llvm::APSInt IntVal; + if (Bop->getLHS()->EvaluateAsInt(IntVal, *Context)) { + if (IntVal.getBoolValue() == false) { + return TryResult(false); + } + } + if (Bop->getRHS()->EvaluateAsInt(IntVal, *Context)) { + if (IntVal.getBoolValue() == false) { + return TryResult(false); + } + } + } + break; + } + } } return evaluateAsBooleanConditionNoCache(S); diff --git a/test/Sema/warn-unreachable.c b/test/Sema/warn-unreachable.c index 636513f62c..2fbe1c78eb 100644 --- a/test/Sema/warn-unreachable.c +++ b/test/Sema/warn-unreachable.c @@ -132,3 +132,12 @@ void PR9774(int *s) { s[i] = 0; } +// Test case for . We should treat code guarded +// by 'x & 0' and 'x * 0' as unreachable. +void calledFun(); +void test_mul_and_zero(int x) { + if (x & 0) calledFun(); // expected-warning {{will never be executed}} + if (0 & x) calledFun(); // expected-warning {{will never be executed}} + if (x * 0) calledFun(); // expected-warning {{will never be executed}} + if (0 * x) calledFun(); // expected-warning {{will never be executed}} +}