Skip to content

Commit a41226b

Browse files
authored
[ValueTracking] Fix KnownBits conflict for calls (range vs returned) (#84353)
If a function only exits for certain input values we can still derive that an argument is "returned". We can also derive range metadata that describe the possible value range returned by the function. However, it turns out that those two analyses can result in conflicting information. Example: declare i16 @foo(i16 returned) ... %A = call i16 @foo(i16 4095), !range !{i16 32, i16 33} To avoid "Bits known to be one AND zero?" assertion failures we know make sure to discard the known bits for this kind of scenario.
1 parent d0b7022 commit a41226b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,12 @@ static void computeKnownBitsFromOperator(const Operator *I,
14761476
if (RV->getType() == I->getType()) {
14771477
computeKnownBits(RV, Known2, Depth + 1, Q);
14781478
Known = Known.unionWith(Known2);
1479+
// If the function doesn't return properly for all input values
1480+
// (e.g. unreachable exits) then there might be conflicts between the
1481+
// argument value and the range metadata. Simply discard the known bits
1482+
// in case of conflicts.
1483+
if (Known.hasConflict())
1484+
Known.resetAll();
14791485
}
14801486
}
14811487
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {

llvm/unittests/Analysis/ValueTrackingTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,20 @@ TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) {
23592359
EXPECT_EQ(Known.One.getZExtValue(), 0u);
23602360
}
23612361

2362+
TEST_F(ComputeKnownBitsTest, ComputeKnownBitsReturnedRangeConflict) {
2363+
parseAssembly(
2364+
"declare i16 @foo(i16 returned)\n"
2365+
"\n"
2366+
"define i16 @test() {\n"
2367+
" %A = call i16 @foo(i16 4095), !range !{i16 32, i16 33}\n"
2368+
" ret i16 %A\n"
2369+
"}\n");
2370+
// The call returns 32 according to range metadata, but 4095 according to the
2371+
// returned arg operand. Given the conflicting information we expect that the
2372+
// known bits information simply is cleared.
2373+
expectKnownBits(/*zero*/ 0u, /*one*/ 0u);
2374+
}
2375+
23622376
TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) {
23632377
parseAssembly("define void @test(ptr %p) {\n"
23642378
" %A = load i64, ptr %p, !range !{i64 64, i64 65536}\n"

0 commit comments

Comments
 (0)