LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 1245 - llvm-as mis-reads some negative integers
Summary: llvm-as mis-reads some negative integers
Status: RESOLVED FIXED
Alias: None
Product: tools
Classification: Unclassified
Component: llvm-as (show other bugs)
Version: trunk
Hardware: PC Linux
: P normal
Assignee: Reid Spencer
URL:
Keywords: miscompilation
Depends on:
Blocks: 1246
  Show dependency tree
 
Reported: 2007-03-09 05:31 PST by Anton Korobeynikov
Modified: 2010-02-22 12:46 PST (History)
2 users (show)

See Also:
Fixed By Commit(s):


Attachments
failed .ll (55.25 KB, text/plain)
2007-03-09 05:32 PST, Anton Korobeynikov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Anton Korobeynikov 2007-03-09 05:31:42 PST
Consider the attached .ll file.

Do:
llvm-as foo.ll -o - | ./llvm-dif -o foo-cmp.ll

I have (x86-linux):

....
-       %ov35 = add i32 %ov16, -29              ; <i32> [#uses=1]
+       %ov35 = add i32 %ov16, -93              ; <i32> [#uses=1]
        store i32 %ov35, i32* %state
        %pcc8 = add i32 %pcv7, 1                ; <i32> [#uses=1]
        store i32 %tmp20, i32* %tmp20_c
@@ -321,7 +321,7 @@
        %ov7 = xor i32 %ov18_cc, 251            ; <i32> [#uses=3]
        %ov41 = xor i32 %ov7, 116               ; <i32> [#uses=1]
        %ov63 = sub i32 139, %ov7               ; <i32> [#uses=2]
-       %hvar68 = add i32 %ov63, -1             ; <i32> [#uses=1]
+       %hvar68 = add i32 %ov63, -9             ; <i32> [#uses=1]

and so on. Valgrind is quite. It's definitely an assembler bug, since output
binary code is broken.
Comment 1 Anton Korobeynikov 2007-03-09 05:32:25 PST
Created attachment 699 [details]
failed .ll
Comment 2 Anton Korobeynikov 2007-03-09 06:11:51 PST
Note, that -93 is 0xFFF...FE3 and -29 is 0xFFFF...FA3. APInt issues?
Comment 3 Anton Korobeynikov 2007-03-09 06:12:25 PST
Err. Vice-versa. A3 and E3
Comment 4 Anton Korobeynikov 2007-03-09 13:00:44 PST
Well. The problem is definitely APInt issue. Let's look into {NInteger} rule in
the Lexer.l file. It correctly receives "-93" as yytext. But we have minBits = 6
and numBits = 7. After trunc we have "-29" as a value...
Comment 5 Anton Korobeynikov 2007-03-09 13:06:54 PST
One more addition. In any of such bad cases executing getSExtValue() before
trunc will result *positive* number. E.g.: +35 for "-93", "7 for "-9", +58 for
"-70". Funny, eh? :)
Comment 6 Reid Spencer 2007-03-09 13:15:02 PST
This is definitely a bug in APInt::getMinSignedBits(). For some values it is
off-by-one. I will fix this shortly.
Comment 7 Reid Spencer 2007-03-09 13:30:43 PST
This problem only affected negative integers whose "next to last" significant
bit was set. In such cases the number of bits computed as needed by the value
was off-by-one as it did not account for the sign bit. Consequently, the number
was parsed correctly by the "fromString" constructor but truncated to the wrong
length which changed the value.

This simple patch fixes it:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070305/045759.html