r177183 - [AST] Add a fast path to ConstantArrayType::getNumAddressingBits().
Daniel Dunbar
daniel at zuster.org
Fri Mar 15 13:55:28 PDT 2013
Author: ddunbar
Date: Fri Mar 15 15:55:27 2013
New Revision: 177183
URL: http://llvm.org/viewvc/llvm-project?rev=177183&view=rev
Log:
[AST] Add a fast path to ConstantArrayType::getNumAddressingBits().
- This fast path is almost 100% effective on real code, and lets us avoid
multiple allocations of 128-bit APSInt objects in the common case.
- As with any overflow-check-skipping-code, I'd appreciate someone double
checking my logic.
Modified:
cfe/trunk/lib/AST/Type.cpp
Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=177183&r1=177182&r2=177183&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Fri Mar 15 15:55:27 2013
@@ -76,16 +76,35 @@ bool QualType::isConstant(QualType T, AS
unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
QualType ElementType,
const llvm::APInt &NumElements) {
+ uint64_t ElementSize = Context.getTypeSizeInChars(ElementType).getQuantity();
+
+ // Fast path the common cases so we can avoid the conservative computation
+ // below, which in common cases allocates "large" APSInt values, which are
+ // slow.
+
+ // If the element size is a power of 2, we can directly compute the additional
+ // number of addressing bits beyond those required for the element count.
+ if (llvm::isPowerOf2_64(ElementSize)) {
+ return NumElements.getActiveBits() + llvm::Log2_64(ElementSize);
+ }
+
+ // If both the element count and element size fit in 32-bits, we can do the
+ // computation directly in 64-bits.
+ if ((ElementSize >> 32) == 0 && NumElements.getBitWidth() <= 64 &&
+ (NumElements.getZExtValue() >> 32) == 0) {
+ uint64_t TotalSize = NumElements.getZExtValue() * ElementSize;
+ return 64 - llvm::CountLeadingZeros_64(TotalSize);
+ }
+
+ // Otherwise, use APSInt to handle arbitrary sized values.
llvm::APSInt SizeExtended(NumElements, true);
unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
SizeExtended = SizeExtended.extend(std::max(SizeTypeBits,
SizeExtended.getBitWidth()) * 2);
- uint64_t ElementSize
- = Context.getTypeSizeInChars(ElementType).getQuantity();
llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
TotalSize *= SizeExtended;
-
+
return TotalSize.getActiveBits();
}
More information about the cfe-commits
mailing list