[llvm] 063258e - [Alignment][NFC] Use 5 bits to store Instructions Alignment
Guillaume Chatelet via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 3 01:54:39 PDT 2020
Author: Guillaume Chatelet
Date: 2020-07-03T08:54:27Z
New Revision: 063258eb6e32d7c185023b81f10b34f9bddbd8b3
URL: https://github.com/llvm/llvm-project/commit/063258eb6e32d7c185023b81f10b34f9bddbd8b3
DIFF: https://github.com/llvm/llvm-project/commit/063258eb6e32d7c185023b81f10b34f9bddbd8b3.diff
LOG: [Alignment][NFC] Use 5 bits to store Instructions Alignment
As per [MaxAlignmentExponent]{https://github.com/llvm/llvm-project/blob/b7338fb1a6a464472850211165391983d2c8fdf3/llvm/include/llvm/IR/Value.h#L688} alignment is not allowed to be more than 2^29.
Encoded as Log2, this means that storing alignment uses 5 bits.
This patch makes sure all instructions store their alignment in a consistent way, encoded as Log2 and using 5 bits.
Differential Revision: https://reviews.llvm.org/D83119
Added:
Modified:
llvm/include/llvm/IR/Instruction.h
llvm/include/llvm/IR/Instructions.h
llvm/lib/IR/Instructions.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h
index ba0f4566c8aa..a85353ff9027 100644
--- a/llvm/include/llvm/IR/Instruction.h
+++ b/llvm/include/llvm/IR/Instruction.h
@@ -54,6 +54,17 @@ class Instruction : public User,
// The 15 first bits of `Value::SubclassData` are available for subclasses of
// `Instruction` to use.
using OpaqueField = Bitfield::Element<uint16_t, 0, 15>; // Next bit:15
+
+ // Template alias so that all Instruction storing alignment use the same
+ // definiton.
+ // Valid alignments are powers of two from 2^0 to 2^MaxAlignmentExponent =
+ // 2^29. We store them as Log2(Alignment), so we need 5 bits to encode the 30
+ // possible values.
+ template <unsigned Offset>
+ using AlignmentBitfieldElement =
+ typename Bitfield::Element<unsigned, Offset, 5,
+ Value::MaxAlignmentExponent>;
+
private:
// The last bit is used to store whether the instruction has metadata attached
// or not.
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index f719d0c1bee9..57ad0db6f3ce 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -60,7 +60,7 @@ class LLVMContext;
class AllocaInst : public UnaryInstruction {
Type *AllocatedType;
- using AlignmentField = Bitfield::Element<unsigned, 0, 5>; // Next bit:5
+ using AlignmentField = AlignmentBitfieldElement<0>; // Next bit:5
using UsedWithInAllocaField = Bitfield::Element<bool, 5, 1>; // Next bit:6
using SwiftErrorField = Bitfield::Element<bool, 6, 1>; // Next bit:7
@@ -113,11 +113,15 @@ class AllocaInst : public UnaryInstruction {
/// Return the alignment of the memory that is being allocated by the
/// instruction.
Align getAlign() const {
- return *decodeMaybeAlign(getSubclassData<AlignmentField>());
+ return Align(1ULL << getSubclassData<AlignmentField>());
}
+
+ void setAlignment(Align Align) {
+ setSubclassData<AlignmentField>(Log2(Align));
+ }
+
// FIXME: Remove this one transition to Align is over.
unsigned getAlignment() const { return getAlign().value(); }
- void setAlignment(Align Align);
/// Return true if this alloca is in the entry block of the function and is a
/// constant size. If so, the code generator will fold it into the
@@ -165,9 +169,9 @@ class AllocaInst : public UnaryInstruction {
/// Value to store whether or not the load is volatile.
class LoadInst : public UnaryInstruction {
using VolatileField = Bitfield::Element<bool, 0, 1>; // Next bit:1
- using AlignmentField = Bitfield::Element<unsigned, 1, 6>; // Next bit:7
- using OrderingField = Bitfield::Element<AtomicOrdering, 7, 3,
- AtomicOrdering::LAST>; // Next bit:10
+ using AlignmentField = AlignmentBitfieldElement<1>; // Next bit:6
+ using OrderingField = Bitfield::Element<AtomicOrdering, 6, 3,
+ AtomicOrdering::LAST>; // Next bit:9
void AssertOK();
@@ -210,10 +214,12 @@ class LoadInst : public UnaryInstruction {
/// Return the alignment of the access that is being performed.
Align getAlign() const {
- return *decodeMaybeAlign(getSubclassData<AlignmentField>());
+ return Align(1ULL << (getSubclassData<AlignmentField>()));
}
- void setAlignment(Align Alignment);
+ void setAlignment(Align Align) {
+ setSubclassData<AlignmentField>(Log2(Align));
+ }
/// Returns the ordering constraint of this load instruction.
AtomicOrdering getOrdering() const {
@@ -290,9 +296,9 @@ class LoadInst : public UnaryInstruction {
/// An instruction for storing to memory.
class StoreInst : public Instruction {
using VolatileField = Bitfield::Element<bool, 0, 1>; // Next bit:1
- using AlignmentField = Bitfield::Element<unsigned, 1, 6>; // Next bit:7
- using OrderingField = Bitfield::Element<AtomicOrdering, 7, 3,
- AtomicOrdering::LAST>; // Next bit:10
+ using AlignmentField = AlignmentBitfieldElement<1>; // Next bit:6
+ using OrderingField = Bitfield::Element<AtomicOrdering, 6, 3,
+ AtomicOrdering::LAST>; // Next bit:9
void AssertOK();
@@ -337,10 +343,12 @@ class StoreInst : public Instruction {
unsigned getAlignment() const { return getAlign().value(); }
Align getAlign() const {
- return *decodeMaybeAlign(getSubclassData<AlignmentField>());
+ return Align(1ULL << (getSubclassData<AlignmentField>()));
}
- void setAlignment(Align Alignment);
+ void setAlignment(Align Align) {
+ setSubclassData<AlignmentField>(Log2(Align));
+ }
/// Returns the ordering constraint of this store instruction.
AtomicOrdering getOrdering() const {
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index 8b333a5e2c0c..78887a63b726 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -1297,11 +1297,6 @@ AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
setName(Name);
}
-void AllocaInst::setAlignment(Align Align) {
- assert(Align <= MaximumAlignment &&
- "Alignment is greater than MaximumAlignment!");
- setSubclassData<AlignmentField>(encode(Align));
-}
bool AllocaInst::isArrayAllocation() const {
if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
@@ -1393,12 +1388,6 @@ LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
setName(Name);
}
-void LoadInst::setAlignment(Align Align) {
- assert(Align <= MaximumAlignment &&
- "Alignment is greater than MaximumAlignment!");
- setSubclassData<AlignmentField>(encode(Align));
-}
-
//===----------------------------------------------------------------------===//
// StoreInst Implementation
//===----------------------------------------------------------------------===//
@@ -1470,11 +1459,6 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align,
AssertOK();
}
-void StoreInst::setAlignment(Align Alignment) {
- assert(Alignment <= MaximumAlignment &&
- "Alignment is greater than MaximumAlignment!");
- setSubclassData<AlignmentField>(encode(Alignment));
-}
//===----------------------------------------------------------------------===//
// AtomicCmpXchgInst Implementation
More information about the llvm-commits
mailing list