Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instcombine misoptimizes shr+and to setgt #1285

Closed
nlewycky opened this issue Sep 16, 2006 · 6 comments
Closed

instcombine misoptimizes shr+and to setgt #1285

nlewycky opened this issue Sep 16, 2006 · 6 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla miscompilation

Comments

@nlewycky
Copy link
Contributor

Bugzilla Link 913
Resolution FIXED
Resolved on Nov 07, 2018 00:17
Version 1.7
OS All

Extended Description

The instruction combiner is making the following transformation:

    %tmp = load int* %tmp1          ; <int> [#uses=1]
    %tmp = cast int %tmp to uint            ; <uint> [#uses=1]
  •   %tmp2 = shr uint %tmp, ubyte 5          ; <uint> [#uses=1]
    
  •   %tmp2 = cast uint %tmp2 to int          ; <int> [#uses=1]
    
  •   %tmp3 = and int %tmp2, 1                ; <int> [#uses=1]
    
  •   %tmp3 = cast int %tmp3 to bool          ; <bool> [#uses=1]
    
  •   %tmp34 = cast bool %tmp3 to int         ; <int> [#uses=1]
    
  •   %tmp3 = setgt uint %tmp, 31             ; <bool> [#uses=1]
    
  •   %tmp34 = cast bool %tmp3 to int         ; <int> [#uses=2]
    

simplifying "(x >> 5) & 1 != 0" into "x > 31". They are not the same; consider
64: 64 >> 5 = 2, 2 & 1 = 0. But 64 > 31.

@nlewycky
Copy link
Contributor Author

assigned to @lattner

@isanbard
Copy link
Contributor

How about translating the (x >> 5) & 1 != 0 into (x & 0x0020) instead?

@lattner
Copy link
Collaborator

lattner commented Sep 16, 2006

compilable testcase:

int %test(int* %tmp1) {
%tmp = load int* %tmp1 ; [#uses=1]
%tmp = cast int %tmp to uint ; [#uses=1]
%tmp2 = shr uint %tmp, ubyte 5 ; [#uses=1]
%tmp2 = cast uint %tmp2 to int ; [#uses=1]
%tmp3 = and int %tmp2, 1 ; [#uses=1]
%tmp3 = cast int %tmp3 to bool ; [#uses=1]
%tmp34 = cast bool %tmp3 to int ; [#uses=1]
ret int %tmp34
}

@lattner
Copy link
Collaborator

lattner commented Sep 16, 2006

Fixed. Testcase here: Transforms/InstCombine/2006-09-15-CastToBool.ll

Patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20060911/037892.html

BTW, instcombine now reduces the function in the testcase to:

int %test(int* %tmp1) {
%tmp = load int* %tmp1 ; [#uses=1]
%tmp = cast int %tmp to uint ; [#uses=1]
%tmp2 = shr uint %tmp, ubyte 5 ; [#uses=1]
%tmp2 = cast uint %tmp2 to int ; [#uses=1]
%tmp34 = and int %tmp2, 1 ; [#uses=1]
ret int %tmp34
}

The shift is required because the function is required to deliver int 1/0, not a bool.

Thanks!

-Chris

@nlewycky
Copy link
Contributor Author

Fantastic. Thanks Chris!

@lattner
Copy link
Collaborator

lattner commented Oct 18, 2006

*** Bug llvm/llvm-bugzilla-archive#953 has been marked as a duplicate of this bug. ***

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
jeanPerier pushed a commit to jeanPerier/llvm-project that referenced this issue Jan 4, 2022
* Use undef when extent of array is non-constant or simply undefined (rather than -1).

Add code to handle copy-in/copy-out of assumed-size arrays.

Add test. Check before and after array value copy.

* Address Pete's review comments.
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla miscompilation
Projects
None yet
Development

No branches or pull requests

3 participants