llvm-project/llvm/lib/CodeGen/SelectionDAG
Craig Topper 38ffa2bb96 [LegalizeTypes] Improve splitting for urem/udiv by constant for some constants.
For remainder:
If (1 << (Bitwidth / 2)) % Divisor == 1, we can add the high and low halves
together and use a (Bitwidth / 2) urem. If (BitWidth /2) is a legal integer
type, this urem will be expand by DAGCombiner using multiply by magic
constant. We do have to take into account that adding high and low
together can produce a carry, making it a (BitWidth / 2)+1 bit number.
So we need to also add back in the carry from the first addition.

For division:
We can use the above trick to compute the remainder, subtract that
remainder from the dividend, then multiply by the multiplicative
inverse of the Divisor modulo (1 << BitWidth).

This is based on the section "Remainder by Summing Digits" in
Hacker's delight.

The remainder trick is similar to a trick you may have learned for
determining if a decimal number is divisible by 3. You can add all the
digits together and see if the sum is divisible by 3. If you're not sure
if the sum is divisible by 3, you can add its digits together. This
can be repeated until you have a single decimal digit. If that digit
is 3, 6, or 9, then the original number is divisible by 3. This works
because 10 % 3 == 1.

gcc already does this same trick. There are additional tricks gcc
does urem as well as srem, udiv, and sdiv that I plan to add in
future patches.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D130862
2022-09-12 10:34:52 -07:00
..
CMakeLists.txt
DAGCombiner.cpp [DAGCombiner] Use HandleSDNode to keep node alive across call to getNegatedExpression. 2022-09-09 22:02:41 -07:00
FastISel.cpp [FastISel] Propagate PCSections metadata to MachineInstr 2022-09-07 11:36:01 +02:00
FunctionLoweringInfo.cpp Untangle the mess which is MachineBasicBlock::hasAddressTaken(). 2022-08-16 16:15:44 -07:00
InstrEmitter.cpp KCFI sanitizer 2022-08-24 22:41:38 +00:00
InstrEmitter.h [DebugInfo][InstrRef] Avoid a crash from mixed variable location modes 2022-04-06 11:55:38 +01:00
LegalizeDAG.cpp [SelectionDAG] Rewrite bfloat16 softening to use the "half promotion" path 2022-09-06 11:54:34 +02:00
LegalizeFloatTypes.cpp [SelectionDAG] Rewrite bfloat16 softening to use the "half promotion" path 2022-09-06 11:54:34 +02:00
LegalizeIntegerTypes.cpp [LegalizeTypes] Improve splitting for urem/udiv by constant for some constants. 2022-09-12 10:34:52 -07:00
LegalizeTypes.cpp [LegalizeTypes] Fix bug in expensive checks verification 2022-05-26 13:13:32 -07:00
LegalizeTypes.h [LegalizeTypes] Support widen result for VECTOR_REVERSE. 2022-08-30 10:01:26 +08:00
LegalizeTypesGeneric.cpp [LegalizeTypes][NFC] Use getConstantOperandVal instead of cast constant getvalue 2022-08-12 14:35:10 +08:00
LegalizeVectorOps.cpp [llvm] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 11:24:15 -07:00
LegalizeVectorTypes.cpp [llvm] Use std::size instead of llvm::array_lengthof 2022-09-08 09:01:53 -06:00
ResourcePriorityQueue.cpp Remove unneeded cl::ZeroOrMore for cl::opt/cl::list options 2022-06-05 00:31:44 -07:00
SDNodeDbgValue.h Cleanup includes: DebugInfo & CodeGen 2022-03-12 17:26:40 +01:00
ScheduleDAGFast.cpp [CodeGen] Qualify auto variables in for loops (NFC) 2022-07-17 01:33:28 -07:00
ScheduleDAGRRList.cpp [NFC][ScheduleDAG] Use a reference to iterate over NodeSuccs/ChainSuccs 2022-09-12 15:54:48 +03:00
ScheduleDAGSDNodes.cpp [SelectionDAG] Propagate PCSections through SDNodes 2022-09-07 11:22:50 +02:00
ScheduleDAGSDNodes.h
ScheduleDAGVLIW.cpp Cleanup includes: DebugInfo & CodeGen 2022-03-12 17:26:40 +01:00
SelectionDAG.cpp [SelectionDAG] Propagate PCSections through SDNodes 2022-09-07 11:22:50 +02:00
SelectionDAGAddressAnalysis.cpp [llvm] Don't use Optional::hasValue (NFC) 2022-06-20 10:38:12 -07:00
SelectionDAGBuilder.cpp DAG: Sink some getter code closer to uses 2022-09-12 08:38:35 -04:00
SelectionDAGBuilder.h Remove redundaunt virtual specifiers (NFC) 2022-07-25 23:00:59 -07:00
SelectionDAGDumper.cpp [stackmaps] Legalise patchpoint arguments. 2022-07-15 12:01:59 +01:00
SelectionDAGISel.cpp [SelectionDAG] Propagate PCSections through SDNodes 2022-09-07 11:22:50 +02:00
SelectionDAGPrinter.cpp Revert "[CodeGen] Place SDNode debug ID declaration under appropriate #if" 2022-04-06 20:32:53 +03:00
SelectionDAGTargetInfo.cpp
StatepointLowering.cpp Remove redundant initialization of Optional (NFC) 2022-08-20 21:18:28 -07:00
StatepointLowering.h
TargetLowering.cpp [LegalizeTypes] Improve splitting for urem/udiv by constant for some constants. 2022-09-12 10:34:52 -07:00