Skip to content

Commit 21a77e8

Browse files
committed
[IR] Reorder Value fields to put the SubclassID first (#53520)
Placing the class id at offset 0 should make `isa` and `dyn_cast` faster by eliminating the field offset (previously 0x10) from the memory operand, saving encoding space on x86, and, in theory, an add micro-op. You can see the load encodes one byte smaller here: https://godbolt.org/z/Whvz4can9 The compile time tracker shows some modestly positive results in the on the `cycle` metric and in the final clang binary size metric: https://llvm-compile-time-tracker.com/compare.php?from=33b54f01fe32030ff60d661a7a951e33360f82ee&to=2530347a57401744293c54f92f9781fbdae3d8c2&stat=cycles Clicking through to the per-library size breakdown shows that instcombine size reduces by 0.68%, which is meaningful, and I believe instcombine is known to be a hotspot. It is, however, potentially noise. I still think we should do this, because notionally, the class id really acts as the vptr of the Value, and conventionally the vptr is always at offset 0.
1 parent dc1e279 commit 21a77e8

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

llvm/include/llvm/IR/Value.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ using ValueName = StringMapEntry<Value *>;
7272
/// objects that watch it and listen to RAUW and Destroy events. See
7373
/// llvm/IR/ValueHandle.h for details.
7474
class Value {
75-
Type *VTy;
76-
Use *UseList;
77-
78-
friend class ValueAsMetadata; // Allow access to IsUsedByMD.
79-
friend class ValueHandleBase;
80-
8175
const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
8276
unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
8377

@@ -121,6 +115,12 @@ class Value {
121115
unsigned HasDescriptor : 1;
122116

123117
private:
118+
Type *VTy;
119+
Use *UseList;
120+
121+
friend class ValueAsMetadata; // Allow access to IsUsedByMD.
122+
friend class ValueHandleBase; // Allow access to HasValueHandle.
123+
124124
template <typename UseT> // UseT == 'Use' or 'const Use'
125125
class use_iterator_impl {
126126
friend class Value;

llvm/lib/IR/Value.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ static inline Type *checkType(Type *Ty) {
5151
}
5252

5353
Value::Value(Type *ty, unsigned scid)
54-
: VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0),
55-
SubclassOptionalData(0), SubclassData(0), NumUserOperands(0),
56-
IsUsedByMD(false), HasName(false), HasMetadata(false) {
54+
: SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
55+
SubclassData(0), NumUserOperands(0), IsUsedByMD(false), HasName(false),
56+
HasMetadata(false), VTy(checkType(ty)), UseList(nullptr) {
5757
static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)");
5858
// FIXME: Why isn't this in the subclass gunk??
5959
// Note, we cannot call isa<CallInst> before the CallInst has been

0 commit comments

Comments
 (0)