This is an archive of the discontinued LLVM Phabricator instance.

[RISCV]Enable isIntDivCheap when attribute is minsize
ClosedPublic

Authored by liaolucy on Jul 25 2022, 9:11 PM.

Details

Summary

Don't expand divisions by constants when attribute is minsize.

Diff Detail

Event Timeline

liaolucy created this revision.Jul 25 2022, 9:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 25 2022, 9:11 PM
liaolucy requested review of this revision.Jul 25 2022, 9:11 PM
craig.topper added inline comments.Jul 25 2022, 9:48 PM
llvm/test/CodeGen/RISCV/div_minsize.ll
9

RV32 and RV64 aren't in your RUN lines

34

This is overly indented

liaolucy updated this revision to Diff 447578.Jul 25 2022, 11:32 PM

address comments. update testcase

This code in ReplaceNodeResults needs to call isIntDivCheap so that we promote div by constant when isIntDivCheap returns false. That will fix testsize2 and testsize4 to use divw and divuw for RV64IM.

case ISD::SDIV:                                                                
case ISD::UDIV:                                                                
case ISD::UREM: {                                                              
  MVT VT = N->getSimpleValueType(0);                                           
  assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&                
         Subtarget.is64Bit() && Subtarget.hasStdExtM() &&                      
         "Unexpected custom legalisation");                                    
  // Don't promote division/remainder by constant since we should expand those 
  // to multiply by magic constant.                                            
  // FIXME: What if the expansion is disabled for minsize.                     
  if (N->getOperand(1).getOpcode() == ISD::Constant)                           
    return;
liaolucy updated this revision to Diff 447917.Jul 26 2022, 8:25 PM

address comments. fix testcases to use divw and divuw for RV64IM.

barannikov88 added inline comments.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
12541

There is MF->getFunction().hasOptSize() which also checks for Attribute::OptimizeForSize.
Just a suggestion.

craig.topper added inline comments.Jul 26 2022, 11:13 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
12541

Every other target that does this only does it for MinSize. Are you suggesting we do this for -Os as well?

barannikov88 added inline comments.Jul 26 2022, 11:23 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
12541

That's right.
According to the comment near hasOptSize, it checks for Os and Oz, one of which is "optimize for size" and the other is "optimize for minimum size".
Has the other attribute been introduced after other targets implemented the hook?..

barannikov88 added inline comments.Jul 27 2022, 12:23 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
12541

Please ignore my comment, I mixed up OptimizeForSize and MinSize. Returning false only for MinSize sounds reasonable.

This revision is now accepted and ready to land.Jul 27 2022, 12:29 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
12540

The reason why other targets make vector an exception is because there is no vector integer division as the comments said, so I think it is OK for RVV since there are vector integer divide instructions. While for P extension, it could be an exception.

This revision was landed with ongoing or failed builds.Jul 27 2022, 3:23 AM
This revision was automatically updated to reflect the committed changes.
liaolucy added inline comments.Jul 27 2022, 3:26 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
12540

Yes, that is why I added a TODO.