{ "bug_id": "100298", "issue_url": "https://github.com/llvm/llvm-project/issues/100298", "bug_type": "miscompilation", "base_commit": "75642a00e15b722bdfb90726be31f1c8adaeb0c5", "knowledge_cutoff": "2024-07-24T04:19:53Z", "lit_test_dir": [ "llvm/test/Transforms/InstCombine" ], "hints": { "fix_commit": "59eae919c938f890e9b9b4be8a3fa3cb1b11ed89", "components": [ "ValueTracking" ], "bug_location_lineno": { "llvm/include/llvm/Analysis/SimplifyQuery.h": [ [ 130, 135 ] ], "llvm/lib/Analysis/ValueTracking.cpp": [ [ 1435, 1441 ], [ 1468, 1474 ], [ 1546, 1552 ], [ 2329, 2335 ], [ 2943, 2949 ], [ 3509, 3515 ], [ 4001, 4007 ], [ 5909, 5918 ] ] }, "bug_location_funcname": { "llvm/include/llvm/Analysis/SimplifyQuery.h": [ "getWithCondContext" ], "llvm/lib/Analysis/ValueTracking.cpp": [ "ComputeNumSignBitsImpl", "computeKnownBitsFromOperator", "computeKnownFPClass", "isKnownNonZeroFromOperator", "isKnownToBeAPowerOfTwo", "isNonEqualPHIs" ] } }, "patch": "commit a87fbeb3a77a53ded341277c5b326f7696d47594\nAuthor: Yingwei Zheng \nDate: Wed Jul 24 20:06:36 2024 +0800\n\n [ValueTracking] Don't use CondContext in dataflow analysis of phi nodes (#100316)\n \n See the following case:\n ```\n define i16 @pr100298() {\n entry:\n br label %for.inc\n \n for.inc:\n %indvar = phi i32 [ -15, %entry ], [ %mask, %for.inc ]\n %add = add nsw i32 %indvar, 9\n %mask = and i32 %add, 65535\n %cmp1 = icmp ugt i32 %mask, 5\n br i1 %cmp1, label %for.inc, label %for.end\n \n for.end:\n %conv = trunc i32 %add to i16\n %cmp2 = icmp ugt i32 %mask, 3\n %shl = shl nuw i16 %conv, 14\n %res = select i1 %cmp2, i16 %conv, i16 %shl\n ret i16 %res\n }\n ```\n \n When computing knownbits of `%shl` with `%cmp2=false`, we cannot use\n this condition in the analysis of `%mask (%for.inc -> %for.inc)`.\n \n Fixes https://github.com/llvm/llvm-project/issues/100298.\n \n (cherry picked from commit 59eae919c938f890e9b9b4be8a3fa3cb1b11ed89)\n\ndiff --git a/llvm/include/llvm/Analysis/SimplifyQuery.h b/llvm/include/llvm/Analysis/SimplifyQuery.h\nindex a560744f0122..e8f43c8c2e91 100644\n--- a/llvm/include/llvm/Analysis/SimplifyQuery.h\n+++ b/llvm/include/llvm/Analysis/SimplifyQuery.h\n@@ -130,6 +130,12 @@ struct SimplifyQuery {\n Copy.CC = &CC;\n return Copy;\n }\n+\n+ SimplifyQuery getWithoutCondContext() const {\n+ SimplifyQuery Copy(*this);\n+ Copy.CC = nullptr;\n+ return Copy;\n+ }\n };\n \n } // end namespace llvm\ndiff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp\nindex 40fe1ffe13f1..4b77c0046cc7 100644\n--- a/llvm/lib/Analysis/ValueTracking.cpp\n+++ b/llvm/lib/Analysis/ValueTracking.cpp\n@@ -1435,7 +1435,7 @@ static void computeKnownBitsFromOperator(const Operator *I,\n // inferred hold at original context instruction. TODO: It may be\n // correct to use the original context. IF warranted, explore and\n // add sufficient tests to cover.\n- SimplifyQuery RecQ = Q;\n+ SimplifyQuery RecQ = Q.getWithoutCondContext();\n RecQ.CxtI = P;\n computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);\n switch (Opcode) {\n@@ -1468,7 +1468,7 @@ static void computeKnownBitsFromOperator(const Operator *I,\n // phi. This is important because that is where the value is actually\n // \"evaluated\" even though it is used later somewhere else. (see also\n // D69571).\n- SimplifyQuery RecQ = Q;\n+ SimplifyQuery RecQ = Q.getWithoutCondContext();\n \n unsigned OpNum = P->getOperand(0) == R ? 0 : 1;\n Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();\n@@ -1546,7 +1546,7 @@ static void computeKnownBitsFromOperator(const Operator *I,\n // phi. This is important because that is where the value is actually\n // \"evaluated\" even though it is used later somewhere else. (see also\n // D69571).\n- SimplifyQuery RecQ = Q;\n+ SimplifyQuery RecQ = Q.getWithoutCondContext();\n RecQ.CxtI = P->getIncomingBlock(u)->getTerminator();\n \n Known2 = KnownBits(BitWidth);\n@@ -2329,7 +2329,7 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,\n // it is an induction variable where in each step its value is a power of\n // two.\n auto *PN = cast(I);\n- SimplifyQuery RecQ = Q;\n+ SimplifyQuery RecQ = Q.getWithoutCondContext();\n \n // Check if it is an induction variable and always power of two.\n if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ))\n@@ -2943,7 +2943,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,\n return true;\n \n // Check if all incoming values are non-zero using recursion.\n- SimplifyQuery RecQ = Q;\n+ SimplifyQuery RecQ = Q.getWithoutCondContext();\n unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);\n return llvm::all_of(PN->operands(), [&](const Use &U) {\n if (U.get() == PN)\n@@ -3509,7 +3509,7 @@ static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,\n if (UsedFullRecursion)\n return false;\n \n- SimplifyQuery RecQ = Q;\n+ SimplifyQuery RecQ = Q.getWithoutCondContext();\n RecQ.CxtI = IncomBB->getTerminator();\n if (!isKnownNonEqual(IV1, IV2, DemandedElts, Depth + 1, RecQ))\n return false;\n@@ -4001,7 +4001,7 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,\n \n // Take the minimum of all incoming values. This can't infinitely loop\n // because of our depth threshold.\n- SimplifyQuery RecQ = Q;\n+ SimplifyQuery RecQ = Q.getWithoutCondContext();\n Tmp = TyBits;\n for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {\n if (Tmp == 1) return Tmp;\n@@ -5909,10 +5909,10 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,\n // Recurse, but cap the recursion to two levels, because we don't want\n // to waste time spinning around in loops. We need at least depth 2 to\n // detect known sign bits.\n- computeKnownFPClass(\n- IncValue, DemandedElts, InterestedClasses, KnownSrc,\n- PhiRecursionLimit,\n- Q.getWithInstruction(P->getIncomingBlock(U)->getTerminator()));\n+ computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,\n+ PhiRecursionLimit,\n+ Q.getWithoutCondContext().getWithInstruction(\n+ P->getIncomingBlock(U)->getTerminator()));\n \n if (First) {\n Known = KnownSrc;\n", "tests": [ { "file": "llvm/test/Transforms/InstCombine/pr100298.ll", "commands": [ "opt -S -passes=instcombine < %s" ], "tests": [ { "test_name": "pr100298", "test_body": "define i16 @pr100298() {\nentry:\n br label %for.inc\n\nfor.inc: ; preds = %for.inc, %entry\n %indvar = phi i32 [ -15, %entry ], [ %mask, %for.inc ]\n %add = add nsw i32 %indvar, 9\n %mask = and i32 %add, 65535\n %cmp1 = icmp ugt i32 %mask, 5\n br i1 %cmp1, label %for.inc, label %for.end\n\nfor.end: ; preds = %for.inc\n %conv = trunc i32 %add to i16\n %cmp2 = icmp ugt i32 %mask, 3\n %shl = shl nuw i16 %conv, 14\n %res = select i1 %cmp2, i16 %conv, i16 %shl\n ret i16 %res\n}\n" } ] } ], "issue": { "title": "Miscompilation at O3", "body": "```\r\n#include \"csmith.h\"\r\nuint32_t a;\r\nint16_t c;\r\nvoid d() {\r\n for (a = -15; a >= 6; a = safe_add_func_uint16_t_u_u(a, 9))\r\n ;\r\n c = safe_lshift_func_int16_t_s_u(safe_lshift_func_uint16_t_u_s(a,14), 0);\r\n}\r\nint main() {\r\n d();\r\n printf(\"%d\\n\", c);\r\n return 0;\r\n}\r\n```\r\n\r\n```\r\ndtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ gcc -O0 test.c -fsanitize=undefined -I/usr/include/csmith -w\r\ndtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out \r\n-16384\r\ndtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ bin/clang -O3 test.c -I/usr/include/csmith -w\r\ndtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out \r\n0\r\n```\r\n\r\nllvm version: 2d6ff0c533aa1b0fe6d91764846f636c4a4e911c", "author": "dtcxzyw", "labels": [ "miscompilation", "llvm:analysis" ], "comments": [ { "author": "dtcxzyw", "body": "Reduced testcase: https://alive2.llvm.org/ce/z/9fExrA\r\n```\r\n; bin/opt -passes=instcombine test.ll -S\r\ndefine i16 @src() {\r\nentry:\r\n br label %for.inc\r\n\r\nfor.inc:\r\n %storemerge6 = phi i32 [ -15, %entry ], [ %conv1, %for.inc ]\r\n %add.i = add nsw i32 %storemerge6, 9\r\n %conv1 = and i32 %add.i, 65535\r\n %cmp = icmp ugt i32 %conv1, 5\r\n br i1 %cmp, label %for.inc, label %for.end\r\n\r\nfor.end:\r\n %conv2 = trunc nuw nsw i32 %conv1 to i16\r\n %cmp3.i = icmp ugt i32 %conv1, 3\r\n %0 = shl nuw i16 %conv2, 14\r\n %cond.in.i = select i1 %cmp3.i, i16 %conv2, i16 %0\r\n ret i16 %cond.in.i\r\n}\r\n```" } ] }, "verified": true }