[llvm] [LLVM] Improve the DemandedBits Analysis (PR #148853)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 15 09:44:38 PDT 2025
================
@@ -246,6 +247,59 @@ void DemandedBits::determineLiveOperandBits(
else
AB &= ~(Known.One & ~Known2.One);
break;
+ case Instruction::SRem:
+ case Instruction::URem:
+ case Instruction::UDiv:
+ case Instruction::SDiv: {
+ auto Opc = UserI->getOpcode();
+ auto IsDiv = Opc == Instruction::UDiv || Opc == Instruction::SDiv;
+ bool IsSigned = Opc == Instruction::SDiv || Opc == Instruction::SRem;
+ if (OperandNo == 0) {
+ const APInt *DivAmnt;
+ if (match(UserI->getOperand(1), m_APInt(DivAmnt))) {
+ uint64_t D = DivAmnt->getZExtValue();
+ if (isPowerOf2_64(D)) {
+ unsigned Sh = Log2_64(D);
+ if (IsDiv) {
+ AB = AOut.shl(Sh);
+ } else {
+ AB = AOut & APInt::getLowBitsSet(BitWidth, Sh);
+ }
+ } else if (IsDiv) { // Non power of 2 constant div
+ // x = q * C + r;
+ // q = x / C;
+ // We think of it like grade school division in base 2.
+ //
+ // x = [ unused | window m-bits | ... | needed bits ]
+ // ^ each step emits 1 quotient bit
+ // |
+ // |
+ // C fits in m = ⌈log₂ C⌉ bits
----------------
dtcxzyw wrote:
```suggestion
// C fits in m = ⌈log2 C⌉ bits
```
Avoid non-ASCII characters.
https://github.com/llvm/llvm-project/pull/148853
More information about the llvm-commits
mailing list