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

Coalescer cannot coalesce A = B .. B = A if 'A' is live out #1237

Closed
lattner opened this issue Aug 3, 2006 · 9 comments
Closed

Coalescer cannot coalesce A = B .. B = A if 'A' is live out #1237

lattner opened this issue Aug 3, 2006 · 9 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla code-quality llvm:codegen

Comments

@lattner
Copy link
Collaborator

lattner commented Aug 3, 2006

Bugzilla Link 865
Resolution FIXED
Resolved on Aug 29, 2016 12:44
Version 1.0
OS All
CC @isanbard

Extended Description

llc -fast -march=x86 on this .ll file:

int %foo(int* %a, int %t, int %C) {
entry:
%a = cast int* %a to uint ; [#uses=1]
br label %cond_true

cond_true: ; preds = %cond_true, %entry
%iv. = phi uint [ %a, %entry ], [ %iv..inc, %cond_true ] ; [#uses=2]
%t_addr.0.0 = phi int [ %t, %entry ], [ %tmp7, %cond_true ] ; [#uses=2]
%iv. = cast uint %iv. to int* ; <int*> [#uses=1]
%tmp3 = load int* %iv. ; [#uses=1]
%tmp7 = add int %t_addr.0.0, %tmp3 ; [#uses=1]
%tmp = setgt int %C, 39 ; [#uses=1]
%iv..inc = add uint %iv., 4 ; [#uses=1]
br bool %tmp, label %bb12, label %cond_true

bb12: ; preds = %cond_true
ret int %t_addr.0.0
}

produces:

_foo:
subl $4, %esp
movl %esi, (%esp)
movl 8(%esp), %ecx
movl 16(%esp), %edx
movl 12(%esp), %esi
LBB1_1: #cond_true
*** movl %esi, %eax
*** movl %eax, %esi
addl (%ecx), %esi
addl $4, %ecx
cmpl $40, %edx
jl LBB1_1 #cond_true
LBB1_2: #bb12
movl (%esp), %esi
addl $4, %esp
ret

It is safe to say that that is not the fastest way to copy esi into esi. Note that eax is dead.

-Chris

@lattner
Copy link
Collaborator Author

lattner commented Aug 3, 2006

assigned to @lattner

@llvmbot
Copy link
Collaborator

llvmbot commented Aug 6, 2006

Reduced testcase:

int %foo(int %t, int %C) {
entry:
br label %cond_true

cond_true: ; preds = %cond_true, %entry
%t_addr.0.0 = phi int [ %t, %entry ], [ %tmp7, %cond_true ] ; [#uses=2]
%tmp7 = add int %t_addr.0.0, 1 ; [#uses=1]
%tmp = setgt int %C, 39 ; [#uses=1]
br bool %tmp, label %bb12, label %cond_true

bb12: ; preds = %cond_true
ret int %t_addr.0.0
}

_foo:
movl 8(%esp), %ecx
movl 4(%esp), %edx
LBB1_1: #cond_true
movl %edx, %eax
movl %eax, %edx
incl %edx
cmpl $40, %ecx
jl LBB1_1 #cond_true
LBB1_2: #bb12
ret

@lattner
Copy link
Collaborator Author

lattner commented Aug 18, 2006

Actually, I was wrong above, eax isn't dead, it's used by the ret outside the loop.

@isanbard
Copy link
Contributor

This fix for this one case is pretty trivial (I'm testing it now). One question, should we modify it so that
the general case is also caught? Something like this:

LBB1_1: #cond_true
movl %edx, %eax
;; Only uses of %eax
movl %eax, %edx

Is transformed into:

LBB1_1: #cond_true
movl %edx, %eax
;; Only uses of %eax

?

-bw

@isanbard
Copy link
Contributor

Fixed. After thinking about it some more, doing a check for the "general" case would be at least O(N^2)
and hairy. If it's something we want to perform in the future, we should create a new report for it.

@lattner
Copy link
Collaborator Author

lattner commented Aug 22, 2006

This is definitely an improvement, but not enough. The generated code now looks like this:

_foo:
movl 8(%esp), %ecx
movl 4(%esp), %edx
LBB1_1: #cond_true
movl %edx, %eax
incl %edx
cmpl $40, %ecx
jl LBB1_1 #cond_true
LBB1_2: #bb12
ret

This is clearly better than having two movs, but it would be better to have:

_foo:
movl 8(%esp), %ecx
movl 4(%esp), %eax
LBB1_1: #cond_true
incl %eax
cmpl $40, %ecx
jl LBB1_1 #cond_true
LBB1_2: #bb12
ret

I think that this is a coallescing problem, the PHI live-range isn't getting coallesced with the copy into
EAX at the end properly or something.

-Chris

@isanbard
Copy link
Contributor

Ah! Yes. I see what you mean. Okay, I'll delve more deeply into the coalescer.

@lattner
Copy link
Collaborator Author

lattner commented Aug 22, 2006

No, I'm confused. My suggestion would produce a different result. However, it does seem better to solve
this at the interval level if possible. I'll take a quick look and if not, I'll close it.

@lattner
Copy link
Collaborator Author

lattner commented Aug 25, 2006

@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
kitano-metro pushed a commit to RIKEN-RCCS/llvm-project that referenced this issue Feb 14, 2023
kitano-metro pushed a commit to RIKEN-RCCS/llvm-project that referenced this issue Feb 14, 2023
kitano-metro pushed a commit to RIKEN-RCCS/llvm-project that referenced this issue Feb 14, 2023
refs llvm#1237 既存のtest/MTをlit対応する(grepで文字列を確認5)

See merge request a64fx-swpl/llvm-project!25
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 llvm:codegen
Projects
None yet
Development

No branches or pull requests

3 participants