This is an archive of the discontinued LLVM Phabricator instance.

[MIPS] Add support to match more patterns for DINS instruction
ClosedPublic

Authored by spetrovic on Mar 29 2017, 8:53 AM.

Details

Summary

This patch adds support for recognizing patterns to match DINS instruction. It replaces set of instructions with DINS instruction and one more instruction (add instruction or shift right depending of case).
Example:

#include <stdint.h>
struct buf_str {

struct
{
    unsigned long long addr :37;
    unsigned long long addr1 :15;
    unsigned int length:14;
    uint64_t total_bytes:16;
    uint64_t segs : 6;
} s;

};

void foo(volatile struct buf_str bufptr) {

bufptr.s.segs = 4;

}

Output (objdump) without patch:

Disassembly of section .text:

0000000000000000 <foo>:

 0:	67bdfff0 	daddiu	sp,sp,-16
 4:	ffa50008 	sd	a1,8(sp)
 8:	64010001 	daddiu	at,zero,1
 c:	0001093c 	dsll32	at,at,0x4
10:	6421ffc1 	daddiu	at,at,-63
14:	00010f38 	dsll	at,at,0x1c
18:	6421ffff 	daddiu	at,at,-1
1c:	dfa20008 	ld	v0,8(sp)
20:	00410824 	and	at,v0,at
24:	3c024000 	lui	v0,0x4000
28:	00220825 	or	at,at,v0
2c:	ffa10008 	sd	at,8(sp)
30:	03e00008 	jr	ra
34:	67bd0010 	daddiu	sp,sp,16

Output (objdump) with patch:

Disassembly of section .text:

0000000000000000 <foo>:

 0:	67bdfff0 	daddiu	sp,sp,-16
 4:	ffa50008 	sd	a1,8(sp)
 8:	64010004 	daddiu	at,zero,4
 c:	dfa20008 	ld	v0,8(sp)
10:	7c220f05 	dins	v0,at,0x1c,0x6
14:	ffa20008 	sd	v0,8(sp)
18:	03e00008 	jr	ra
1c:	67bd0010 	daddiu	sp,sp,16

Diff Detail

Repository
rL LLVM

Event Timeline

spetrovic created this revision.Mar 29 2017, 8:53 AM
sdardis requested changes to this revision.Apr 7 2017, 9:03 AM

This looks mostly ok, I've highlighted the issues inline. The big change are lines 846 & 847. As it stands, they don't guard against MIPS32 code having out of range values.

lib/Target/Mips/MipsISelLowering.cpp
808 ↗(On Diff #93376)

Spurious newline.

813–840 ↗(On Diff #93376)

Correct the indentation for this hunk, as you've introduced a new lexical scope.

845 ↗(On Diff #93376)
// => dins $dst, $src, pos, size
847 ↗(On Diff #93376)

This is incorrect for MIPS32. Your provided test case when compiled for MIPS32R2 produces bad assembly + wrong object code. E.g:

ins $1, $2, 18, 46

I believe the conditional needs to be extended along the lines of:

&& ((SMSize0 + SMPos0 <= 64 && Subtarget.hasMips64r2()) || (SMSize0 + SMPos0 <= 32)) {

Also, these two lines need to be clang-formatted.

865–871 ↗(On Diff #93376)

This hunk needs to be clang-formatted.

test/CodeGen/Mips/dins.ll
1 ↗(On Diff #93376)

Add testing for mips32r2 for the 'ins' instruction. Also test that out of range values for the ins instruction are not produced.

Also, test that ins / dins are not produced for pre mips32r2 / mips64r2.

3–22 ↗(On Diff #93376)

This test case can be reduced to:

; struct s {
;   unsigned long long addr :37;
;   unsigned long long addr1 :15;
;   unsigned int length:14;
;   unsigned long long total_bytes:16;
;   unsigned long long segs : 6;
; };

; struct s foo(volatile struct s s1) {
;   s1.addr = 123;
;   s1.segs = 4;
;   s1.length = 5;
;   s1.total_bytes = s1.length;
;   return s1;
; }

The volatile qualifier can be removed, doing so leaves only 2 dins instructions.

23 ↗(On Diff #93376)

Include a comment stating what you're testing for.

55 ↗(On Diff #93376)

This needs a CHECK-LABEL:

test/CodeGen/Mips/mips64-f128.ll
428–431 ↗(On Diff #93376)

Check for dins here for mips64r2.

This revision now requires changes to proceed.Apr 7 2017, 9:03 AM
spetrovic updated this revision to Diff 94946.Apr 12 2017, 3:21 AM
spetrovic edited edge metadata.
spetrovic marked 7 inline comments as done.Apr 12 2017, 3:22 AM

Comments addressed.

sdardis accepted this revision.Apr 18 2017, 7:40 AM

LGTM with some nits addressed.

lib/Target/Mips/MipsISelLowering.cpp
815–834 ↗(On Diff #94946)

There is some additional whitespace here on the seemingly blank lines.

821–822 ↗(On Diff #94946)

This check is now redundant.

test/CodeGen/Mips/dins.ll
70 ↗(On Diff #94946)

You can drop the ${{[0-9]+}} for the MIPS16-NOT. The MIPS16e ISA doesn't contain the insert instruction so we can save a few cycles of testing by just testing for the lack of 'ins'.

This revision is now accepted and ready to land.Apr 18 2017, 7:40 AM
spetrovic updated this revision to Diff 96718.Apr 26 2017, 8:05 AM
spetrovic marked 3 inline comments as done.
This revision was automatically updated to reflect the committed changes.

Hi Simon,

I'm looking into this problem.

Thanks,
Strahinja

Can you commit a revert while you investigate? I need to investigate some issues with our buildbots.

Thanks,
Simon

spetrovic updated this revision to Diff 99542.May 19 2017, 5:01 AM

Hi Simon,

This patch should resolve problem that you reported, can you try this patch on your buildbot to make sure I will not break it again, or I can commit it and see what happens.

Thanks,
Strahinja

Yes, this seems ok on my end.