Repository navigation
Open
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone
Description
https://godbolt.org/z/x3e349nEW
uint ConstSubVar(uint x)
{
return 32 - x;
}Program:ConstSubVar(uint):uint:this (FullOpts):
mov eax, esi
neg eax
add eax, 32
ret Should be:
Program:ConstSubVar(uint):uint:this (FullOpts):
mov eax, 32
sub eax, esi
ret We canonicalize into (cns1 + (-op2)) in morph:
runtime/src/coreclr/jit/morph.cpp
Lines 7890 to 7920 in 209c66f
| /* Check for "cns1 - op2" , we change it to "(cns1 + (-op2))" */ | |
| noway_assert(op1); | |
| if (op1->IsCnsIntOrI()) | |
| { | |
| noway_assert(varTypeIsIntegralOrI(tree)); | |
| // The type of the new GT_NEG node cannot just be op2->TypeGet(). | |
| // Otherwise we may sign-extend incorrectly in cases where the GT_NEG | |
| // node ends up feeding directly into a cast, for example in | |
| // GT_CAST<ubyte>(GT_SUB(0, s_1.ubyte)) | |
| if (op1->IsIntegralConst(0)) | |
| { | |
| tree->ChangeOper(GT_NEG); | |
| tree->gtType = genActualType(op2->TypeGet()); | |
| tree->AsOp()->gtOp1 = op2; | |
| tree->AsOp()->gtOp2 = nullptr; | |
| DEBUG_DESTROY_NODE(op1); | |
| return tree; | |
| } | |
| tree->AsOp()->gtOp2 = op2 = gtNewOperNode(GT_NEG, genActualType(op2->TypeGet()), op2); | |
| fgMorphTreeDone(op2); | |
| oper = GT_ADD; | |
| tree->ChangeOper(oper); | |
| goto CM_ADD_OP; | |
| } |
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Activity
dotnet-policy-service commentedon Apr 2, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
BoyBaykiller commentedon Apr 2, 2026
I could work on this. Where would be a good place to turn the canonicalized form back into just the subtract (assuming thats the correct solution). Maybe in
Lowering::LowerAdd?EgorBo commentedon Apr 2, 2026
I think so
BoyBaykiller commentedon Apr 3, 2026
I added this transformation in
LowerAdd:Unfortunately, the diffs are a net size increase: https://gist.github.com/BoyBaykiller/eae58ed0fd4221d42d120472d28bf045.
The main issue is that we tend to need an extra mov to place the subtract result into the destination register. Meanwhile the neg+add chain can do it in place:
On the other hand in cases where previously a mov was done we can now do that with the subtract:
I need register allocation info in order to make optimal decission and avoid regressions. So I need to move this to some later phase, do you know where?
tannergooding commentedon Apr 3, 2026
There isn't really a later phase. We have:
Lower- sets up basic preferencing info for register allocationLSRA- does the actual register allocationCodegen- generates the instructions using the selected registersThis is likely one of those cases that simply don't matter and where trying to micro-optimize out a
movinstruction or aneg; addin favor of asubisn't worth the extra complexity for the case where the non-constant is already in the register you want the result to be in. There may be a small subset of cases where you can peephole the optimization, but even that is probably questionable overall and will go away with APX and the non-RMW forms of instructionsBoyBaykiller commentedon Apr 3, 2026
Hm ok. I was looking at how clang handles this. It doesn't seem to canonicalize into add+neg form (https://godbolt.org/z/Kbr54r3zj). But how does it avoid the extra mov then? - Through better register alloc.
For example here we have a regression:
mov eax, ecx shr eax, 1 and eax, 0x55555555 or eax, ecx - neg eax - add eax, 31 + mov ecx, 31 + sub ecx, eax + mov eax, ecx - ;; size=16 bbWeight=1 PerfScore 1.75 + ;; size=20 bbWeight=1 PerfScore 2.00Because we place
yintoeax, at the time when the sub comes we have to put the constant 31 into a different register and then mov the result intoeaxagain. Meanwhile clang storesyintoecxand then copies intoeaxas part ofsub:JIT: Transform 'CONST - x' to 'xor x, CONST' when possible (#126529)
JIT: Transform 'CONST - x' to 'xor x, CONST' when possible (#126529)