CTSRD-CHERI / llvm-project

Fork of LLVM adding CHERI support
46 stars 39 forks source link

Missing DAGCombine folds for PTRADD #369

Open arichardson opened 4 years ago

arichardson commented 4 years ago

We are currently missing various folds that are performed for ADD/SUB.

This causes __builtin_align_down to generate the following:

daddiu $1, $zero, 1
dsubu $1, $1, $4
cjr $c17
candaddr $c3, $c3, $1

But it should instead use dnegu:

dnegu   $1, $4
cjr $c17
candaddr    $c3, $c3, $1

Similarly align_up is also inefficient:

cgetaddr $1, $c3
daddu $1, $4, $1
daddiu $1, $1, -1
dnegu $2, $4
and $1, $1, $2
cjr $c17
csetaddr $c3, $c3, $1
jrtc27 commented 2 years ago

Test cases: https://cheri-compiler-explorer.cl.cam.ac.uk/z/zMW3r4

jrtc27 commented 2 years ago

We are currently missing various folds that are performed for ADD/SUB.

This causes __builtin_align_down to generate the following:

daddiu $1, $zero, 1
dsubu $1, $1, $4
cjr $c17
candaddr $c3, $c3, $1

But it should instead use dnegu:

dnegu $1, $4
cjr   $c17
candaddr  $c3, $c3, $1

Note that these are not equivalent; the former is incorrect and was fixed in https://github.com/CTSRD-CHERI/llvm-project/commit/3bcce7d22ed378994612b7bbe1a48ceefe6145db, which gives the latter. We still don't have nice folds for p + (q - p)-like constructs on non-MIPS to be set address operations, though, which is the pattern generated by __builtin_align_up.