Skip to content

Commit 2d9af1c

Browse files
committed
[CVP] Implement type narrowing for LShr
Implements type narrowing for LShr. The treatment is analogous to the type narrowing of UDiv. Since LShr is a relatively cheap instruction, the narrowing occurs only if the following conditions hold: i) all the users of the LShr instruction are already TruncInst; ii) the narrowing is carried out to the largest TruncInst following the LShr instruction. Additionally, the function optimizes the cases where the result of the LShr instruction is guaranteed to vanish or be equal to poison.
1 parent e2cfa08 commit 2d9af1c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,15 @@ static Domain getDomain(const ConstantRange &CR) {
758758
return Domain::Unknown;
759759
}
760760

761+
/// Returns the optimal bit width for the narrowed type.
762+
/// If desiredBitWidth is greater that 32 returns max int and thus effectively
763+
/// turns of the narrowing. For desiredBitWidth <= 32 returns i8, i16, i32.
764+
static unsigned getNarrowedWidth(unsigned desiredBitWidth) {
765+
return desiredBitWidth <= 32
766+
? std::max<unsigned>(PowerOf2Ceil(desiredBitWidth), 8)
767+
: std::numeric_limits<unsigned>::max();
768+
}
769+
761770
/// Try to shrink a sdiv/srem's width down to the smallest power of two that's
762771
/// sufficient to contain its operands.
763772
static bool narrowSDivOrSRem(BinaryOperator *Instr, const ConstantRange &LCR,
@@ -781,7 +790,7 @@ static bool narrowSDivOrSRem(BinaryOperator *Instr, const ConstantRange &LCR,
781790
++MinSignedBits;
782791

783792
// Don't shrink below 8 bits wide.
784-
unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MinSignedBits), 8);
793+
unsigned NewWidth = getNarrowedWidth(MinSignedBits);
785794

786795
// NewWidth might be greater than OrigWidth if OrigWidth is not a power of
787796
// two.
@@ -899,7 +908,7 @@ static bool narrowUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
899908
// of both of the operands?
900909
unsigned MaxActiveBits = std::max(XCR.getActiveBits(), YCR.getActiveBits());
901910
// Don't shrink below 8 bits wide.
902-
unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MaxActiveBits), 8);
911+
unsigned NewWidth = getNarrowedWidth(MaxActiveBits);
903912

904913
// NewWidth might be greater than OrigWidth if OrigWidth is not a power of
905914
// two.
@@ -1156,7 +1165,7 @@ static bool narrowLShr(BinaryOperator *LShr, LazyValueInfo *LVI) {
11561165

11571166
// What is the smallest bit width that can accommodate the entire value ranges
11581167
// of both of the operands? Don't shrink below 8 bits wide.
1159-
unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MaxActiveBits), 8);
1168+
unsigned NewWidth = getNarrowedWidth(MaxActiveBits);
11601169

11611170
// NewWidth might be greater than OrigWidth if OrigWidth is not a power of
11621171
// two.

0 commit comments

Comments
 (0)