Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MIPS]Remove unnecessary SLL instructions on MIPS64el #109386

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

Merged
merged 1 commit into from
May 7, 2025
Merged

Conversation

anbbna
Copy link
Contributor

@anbbna anbbna commented Sep 20, 2024

Optimize ((signext (xor (trunc X), imm)) to (xor (X, imm)).

Fix #99783

Copy link

github-actions bot commented Sep 20, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@anbbna anbbna force-pushed the mips-xor branch 3 times, most recently from 7e8a482 to a3aa217 Compare September 20, 2024 09:32
@brad0
Copy link
Contributor

brad0 commented Sep 22, 2024

cc @topperc @wzssyqa @yingopq

@brad0
Copy link
Contributor

brad0 commented Oct 3, 2024

@nikic

EVT VT = N->getValueType(0);

// Pattern match XOR.
// $dst = sign_extend (xor (trunc $src), imm)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trunc needs an argument. I guess the argument will have some effects on the result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, if $src=0x12345678; imm=0xffff.

lets truncate it to i8, then it will be 0x78, the result will be ~0x78.
lets truncate it to i16, then it will be 0x5678, the result will be ~0x5678.

Copy link
Contributor

@wzssyqa wzssyqa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is not correct.

@anbbna
Copy link
Contributor Author

anbbna commented Oct 12, 2024

Maybe it is not correct.

Thanks for your review, I would find another solution.

@anbbna anbbna force-pushed the mips-xor branch 4 times, most recently from 591d423 to 1fdb6f2 Compare October 29, 2024 10:47
@anbbna
Copy link
Contributor Author

anbbna commented Nov 26, 2024

@wzssyqa Could you help review again, thanks!

@wzssyqa
Copy link
Contributor

wzssyqa commented Nov 26, 2024

Please read: https://llvm.org/docs/LangRef.html#id2158
The IR instruction trunc has a IMM argument. It will be quilte different if the IMM is different.

SDValue Xor = DAG.getNode(ISD::XOR, SDLoc(N), VT, TruncateOperand,
DAG.getTargetConstant(Mask, SDLoc(N), VT));
SDValue Truncate =
DAG.getNode(ISD::TRUNCATE, SDLoc(N), N0->getValueType(0), Xor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wzssyqa The third parameter N0->getValueType(0) is the truncate's destination type, that you mentioned the code lacked. Through this Node we can truncate the fourth parameter Xor to type N0->getValueType(0).
So I think this did not lack a imm. May I understand right?

@brad0
Copy link
Contributor

brad0 commented Jan 26, 2025

Ping.

@anbbna anbbna force-pushed the mips-xor branch 2 times, most recently from 525bdfb to 013a7b7 Compare January 26, 2025 11:32
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this transform is correct: https://alive2.llvm.org/ce/z/-kNt7y

SDValue TruncateOperand = N0.getOperand(0).getOperand(0);
if (VT == MVT::i64 && VT == TruncateOperand->getValueType(0)) {
APInt MinusOne(32, -1, true);
if (N0.getConstantOperandAPInt(1) == MinusOne) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N0.getOperandAPInt(1).isAllOnes()?

APInt MinusOne(32, -1, true);
if (N0.getConstantOperandAPInt(1) == MinusOne) {
return DAG.getNode(ISD::XOR, SDLoc(N0), VT, TruncateOperand,
DAG.getTargetConstant(-1, SDLoc(N0), VT));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be a target constant. TargetConstant should only nodes that always take an immediate for that operand.

SDValue N0 = N->getOperand(0);
EVT VT = N->getValueType(0);

if (N0->getNumOperands() != 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is unnecessary. The N0.getOpcode() == ISD::XOR check below covers it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will remove it.

N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
N0.getOperand(1).getOpcode() == ISD::Constant) {
SDValue TruncateOperand = N0.getOperand(0).getOperand(0);
if (VT == MVT::i64 && VT == TruncateOperand->getValueType(0)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be TruncateOperand.getValueType(). You don't that result 0 of the TruncateOperand node is being used. The result number is stored in the SDValue so you should use it.

@topperc
Copy link
Collaborator

topperc commented Jan 26, 2025

I don't think this transform is correct: https://alive2.llvm.org/ce/z/-kNt7y

I agree. You either need to call ComputeNumSignBits on the truncate input or do this transform instead:

((signext (xor (trunc X), imm)) to (xor (signext_inreg X, i32), imm).

@anbbna
Copy link
Contributor Author

anbbna commented Jan 27, 2025

I don't think this transform is correct: https://alive2.llvm.org/ce/z/-kNt7y

I agree. You either need to call ComputeNumSignBits on the truncate input or do this transform instead:

((signext (xor (trunc X), imm)) to (xor (signext_inreg X, i32), imm).

Thanks for your review. I will modify it to " (xor (signext_inreg X, i32), imm)" based on your comments.

@anbbna anbbna force-pushed the mips-xor branch 2 times, most recently from 046f4d4 to 87852ae Compare February 18, 2025 11:02
@anbbna
Copy link
Contributor Author

anbbna commented Apr 1, 2025

Ping.

@brad0 brad0 requested a review from wzssyqa April 6, 2025 10:09
@brad0 brad0 requested a review from topperc April 6, 2025 10:09
@brad0
Copy link
Contributor

brad0 commented Apr 12, 2025

cc @nikic

@brad0 brad0 requested review from nikic and yingopq April 20, 2025 04:48
; RUN: -check-prefixes=ALL,MM,MM32
; RUN: llc < %s -mtriple=mips -mcpu=mips32r6 -mattr=+micromips | FileCheck %s \
; RUN: -check-prefixes=ALL,MM,MM32
; RUN: llc < %s -march=mips -mcpu=mips2 | FileCheck %s -check-prefixes=GP32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change -mtriple to -march?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my oversight, I will restore it to -mtriple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I checked the code again and changing -mtriple to -march is the result after running test_checks.py.Because the original code only specifies the CPU architecture .

@yingopq
Copy link
Contributor

yingopq commented Apr 28, 2025

This new patch LGTM.

…inreg X, i32), imm)

The constant operand of the XOR is an i32, which makes the two operands
being truncated to i32, and sign extended to i64 afterwards. Since both
of the i32 operands are sign extended to i64 before performing the
TRUNCATE operation, we just remove the unnecessary TRUNCATE and
SIGN_EXTEND.

Fix llvm#99783
@yingopq
Copy link
Contributor

yingopq commented May 7, 2025

LGTM.

@brad0 brad0 merged commit 69f3552 into llvm:main May 7, 2025
8 of 11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 7, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/21788

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Python/Crashlog/interactive_crashlog_json.test (2881 of 2890)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/watchpoint_callback.test (2882 of 2890)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/partial_statements.test (2883 of 2890)
PASS: lldb-api :: tools/lldb-server/TestLldbGdbServer.py (2884 of 2890)
PASS: lldb-api :: api/multithreaded/TestMultithreaded.py (2885 of 2890)
PASS: lldb-api :: terminal/TestEditlineCompletions.py (2886 of 2890)
PASS: lldb-api :: commands/process/attach/TestProcessAttach.py (2887 of 2890)
PASS: lldb-api :: repl/clang/TestClangREPL.py (2888 of 2890)
PASS: lldb-api :: tools/lldb-dap/attach/TestDAP_attachByPortNum.py (2889 of 2890)
TIMEOUT: lldb-api :: tools/lldb-dap/breakpoint/TestDAP_setExceptionBreakpoints.py (2890 of 2890)
******************** TEST 'lldb-api :: tools/lldb-dap/breakpoint/TestDAP_setExceptionBreakpoints.py' FAILED ********************
Script:
--
/usr/bin/python3 /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./lib --env LLVM_INCLUDE_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/include --env LLVM_TOOLS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./bin --arch x86_64 --build-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex --lldb-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/lldb --compiler /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/clang --dsymutil /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./bin --lldb-obj-root /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb --lldb-libs-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./lib -t /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/breakpoint -p TestDAP_setExceptionBreakpoints.py
--
Exit Code: -9
Timeout: Reached timeout of 600 seconds

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision 69f3552d5ee27b4a2fb160600564d3d4d3c3868b)
  clang revision 69f3552d5ee27b4a2fb160600564d3d4d3c3868b
  llvm revision 69f3552d5ee27b4a2fb160600564d3d4d3c3868b

--
Command Output (stderr):
--
Change dir to: /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/breakpoint
runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 

runCmd: settings set target.auto-apply-fixits false

GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Optimize ((signext (xor (trunc X), imm)) to (xor (X, imm)).

Fix llvm#99783
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MIPS64] Failure to remove random NOPs in the middle of instructions
7 participants