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

Icky code generated for std::min/std::max #734

Closed
lattner opened this issue Jun 8, 2004 · 3 comments
Closed

Icky code generated for std::min/std::max #734

lattner opened this issue Jun 8, 2004 · 3 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla code-quality

Comments

@lattner
Copy link
Collaborator

lattner commented Jun 8, 2004

Bugzilla Link 362
Resolution FIXED
Resolved on Feb 22, 2010 12:49
Version 1.0
OS All

Extended Description

Consider this C++ testcase:


#include
void test1(int &x, int &y) {
x = std::min(x,y);
}

void test2(int &x, int &y) {
int tmp = x;
y = std::max(tmp,y);
}

We're currently compiling this into:

void %Z5test1RiS(int* %x, int* %y) {
entry:
%tmp.1.i = load int* %y
%tmp.3.i = load int* %x
%tmp.4.i = setlt int %tmp.1.i, %tmp.3.i
%retval.i = select bool %tmp.4.i, int* %y, int* %x
%tmp.4 = load int* %retval.i
store int %tmp.4, int* %x
ret void
}

Note that for this function the %tmp.4 load is redundant with the loads of X or
Y. It would be better to select one of those load's values.

void %Z5test2RiS(int* %x, int* %y) {
entry:
%tmp.0 = alloca int
%tmp.2 = load int* %x
store int %tmp.2, int* %tmp.0
%tmp.3.i = load int* %y
%tmp.4.i = setlt int %tmp.2, %tmp.3.i
%retval.i = select bool %tmp.4.i, int* %y, int* %tmp.0
%tmp.6 = load int* %retval.i
store int %tmp.6, int* %y
ret void
}

This is even worse. Here we are doing the same thing, selecting the address of
Y or the tmp. Because we need it's address, we are not able to register promote
the tmp, leaving it as an alloca. :(

We should transform these selects into selects of the value not of the pointer
when safe.

-Chris

@lattner
Copy link
Collaborator Author

lattner commented Jun 8, 2004

assigned to @lattner

@lattner
Copy link
Collaborator Author

lattner commented Jun 20, 2004

Note that the trick to this bug is the whole "when safe" bit. In particular, we
can only turn "*(cond ? P1 : P2)" into "(cond ? *P1 : *P2)" when we know it's
safe to dereference P1 and P2.

-Chris

@lattner
Copy link
Collaborator Author

lattner commented Sep 19, 2004

Implemented.

Patch here:
http://mail.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20040913/018402.html

Testcase here:
test/Regression/Transforms/InstCombine/CPP_min_max.llx

-Chris

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
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 code-quality
Projects
None yet
Development

No branches or pull requests

1 participant