Skip to content

Commit 9f82ac5

Browse files
Remove GlobalObject::getAlign/setAlignment (#143188)
Currently, GlobalObject has an "alignment" property... but it's basically nonsense: alignment doesn't mean the same thing for variables and functions, and it's completely meaningless for ifuncs. This "removes" (actually marking protected) the methods from GlobalObject, adds the relevant methods to Function and GlobalVariable, and adjusts the code appropriately. This should make future alignment-related cleanups easier.
1 parent f9b98e3 commit 9f82ac5

File tree

18 files changed

+162
-121
lines changed

18 files changed

+162
-121
lines changed

llvm/include/llvm/IR/Function.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,21 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
10381038
/// Return value: true => null pointer dereference is not undefined.
10391039
bool nullPointerIsDefined() const;
10401040

1041+
/// Returns the alignment of the given function.
1042+
///
1043+
/// Note that this is the alignment of the code, not the alignment of a
1044+
/// function pointer.
1045+
MaybeAlign getAlign() const { return GlobalObject::getAlign(); }
1046+
1047+
/// Sets the alignment attribute of the Function.
1048+
void setAlignment(Align Align) { GlobalObject::setAlignment(Align); }
1049+
1050+
/// Sets the alignment attribute of the Function.
1051+
///
1052+
/// This method will be deprecated as the alignment property should always be
1053+
/// defined.
1054+
void setAlignment(MaybeAlign Align) { GlobalObject::setAlignment(Align); }
1055+
10411056
private:
10421057
void allocHungoffUselist();
10431058
template<int Idx> void setHungoffOperand(Constant *C);

llvm/include/llvm/IR/GlobalObject.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ class GlobalObject : public GlobalValue {
6767
public:
6868
GlobalObject(const GlobalObject &) = delete;
6969

70-
/// FIXME: Remove this function once transition to Align is over.
71-
uint64_t getAlignment() const {
72-
MaybeAlign Align = getAlign();
73-
return Align ? Align->value() : 0;
74-
}
75-
70+
protected:
7671
/// Returns the alignment of the given variable or function.
7772
///
7873
/// Note that for functions this is the alignment of the code, not the
@@ -103,6 +98,7 @@ class GlobalObject : public GlobalValue {
10398
assert(getGlobalObjectSubClassData() == Val && "representation error");
10499
}
105100

101+
public:
106102
/// Check if this global has a custom object file section.
107103
///
108104
/// This is more efficient than calling getSection() and checking for an empty

llvm/include/llvm/IR/GlobalVariable.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
298298
///
299299
LLVM_ABI void clearCodeModel();
300300

301+
/// FIXME: Remove this function once transition to Align is over.
302+
uint64_t getAlignment() const {
303+
MaybeAlign Align = getAlign();
304+
return Align ? Align->value() : 0;
305+
}
306+
307+
/// Returns the alignment of the given variable.
308+
MaybeAlign getAlign() const { return GlobalObject::getAlign(); }
309+
310+
/// Sets the alignment attribute of the GlobalVariable.
311+
void setAlignment(Align Align) { GlobalObject::setAlignment(Align); }
312+
313+
/// Sets the alignment attribute of the GlobalVariable.
314+
/// This method will be deprecated as the alignment property should always be
315+
/// defined.
316+
void setAlignment(MaybeAlign Align) { GlobalObject::setAlignment(Align); }
317+
301318
// Methods for support type inquiry through isa, cast, and dyn_cast:
302319
static bool classof(const Value *V) {
303320
return V->getValueID() == Value::GlobalVariableVal;

llvm/include/llvm/SandboxIR/Constant.h

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -976,32 +976,6 @@ class GlobalObject : public GlobalValue {
976976
}
977977
}
978978

979-
/// FIXME: Remove this function once transition to Align is over.
980-
uint64_t getAlignment() const {
981-
return cast<llvm::GlobalObject>(Val)->getAlignment();
982-
}
983-
984-
/// Returns the alignment of the given variable or function.
985-
///
986-
/// Note that for functions this is the alignment of the code, not the
987-
/// alignment of a function pointer.
988-
MaybeAlign getAlign() const {
989-
return cast<llvm::GlobalObject>(Val)->getAlign();
990-
}
991-
992-
// TODO: Add missing: setAlignment(Align)
993-
994-
/// Sets the alignment attribute of the GlobalObject.
995-
/// This method will be deprecated as the alignment property should always be
996-
/// defined.
997-
void setAlignment(MaybeAlign Align);
998-
999-
unsigned getGlobalObjectSubClassData() const {
1000-
return cast<llvm::GlobalObject>(Val)->getGlobalObjectSubClassData();
1001-
}
1002-
1003-
void setGlobalObjectSubClassData(unsigned V);
1004-
1005979
/// Check if this global has a custom object file section.
1006980
///
1007981
/// This is more efficient than calling getSection() and checking for an empty
@@ -1294,6 +1268,18 @@ class GlobalVariable final
12941268
return cast<llvm::GlobalVariable>(Val)->getCodeModel();
12951269
}
12961270

1271+
/// Returns the alignment of the given variable.
1272+
MaybeAlign getAlign() const {
1273+
return cast<llvm::GlobalVariable>(Val)->getAlign();
1274+
}
1275+
1276+
// TODO: Add missing: setAligment(Align)
1277+
1278+
/// Sets the alignment attribute of the GlobalVariable.
1279+
/// This method will be deprecated as the alignment property should always be
1280+
/// defined.
1281+
void setAlignment(MaybeAlign Align);
1282+
12971283
// TODO: Missing setCodeModel(). Requires custom tracker.
12981284

12991285
#ifndef NDEBUG

llvm/include/llvm/SandboxIR/Function.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ class Function : public GlobalWithNodeAPI<Function, llvm::Function,
5858
}
5959
FunctionType *getFunctionType() const;
6060

61+
/// Returns the alignment of the given function.
62+
MaybeAlign getAlign() const { return cast<llvm::Function>(Val)->getAlign(); }
63+
64+
// TODO: Add missing: setAligment(Align)
65+
66+
/// Sets the alignment attribute of the Function.
67+
/// This method will be deprecated as the alignment property should always be
68+
/// defined.
69+
void setAlignment(MaybeAlign Align);
70+
6171
#ifndef NDEBUG
6272
void verify() const final {
6373
assert(isa<llvm::Function>(Val) && "Expected Function!");

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,11 @@ Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
369369
Alignment = InAlign;
370370

371371
// If the GV has a specified alignment, take it into account.
372-
const MaybeAlign GVAlign(GV->getAlign());
372+
MaybeAlign GVAlign;
373+
if (auto *GVar = dyn_cast<GlobalVariable>(GV))
374+
GVAlign = GVar->getAlign();
375+
else if (auto *F = dyn_cast<Function>(GV))
376+
GVAlign = F->getAlign();
373377
if (!GVAlign)
374378
return Alignment;
375379

llvm/lib/IR/Core.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,8 +2108,10 @@ LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {
21082108

21092109
unsigned LLVMGetAlignment(LLVMValueRef V) {
21102110
Value *P = unwrap(V);
2111-
if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2111+
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P))
21122112
return GV->getAlign() ? GV->getAlign()->value() : 0;
2113+
if (Function *F = dyn_cast<Function>(P))
2114+
return F->getAlign() ? F->getAlign()->value() : 0;
21132115
if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
21142116
return AI->getAlign().value();
21152117
if (LoadInst *LI = dyn_cast<LoadInst>(P))
@@ -2128,8 +2130,10 @@ unsigned LLVMGetAlignment(LLVMValueRef V) {
21282130

21292131
void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
21302132
Value *P = unwrap(V);
2131-
if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2133+
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P))
21322134
GV->setAlignment(MaybeAlign(Bytes));
2135+
else if (Function *F = dyn_cast<Function>(P))
2136+
F->setAlignment(MaybeAlign(Bytes));
21332137
else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
21342138
AI->setAlignment(Align(Bytes));
21352139
else if (LoadInst *LI = dyn_cast<LoadInst>(P))

llvm/lib/IR/IRBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,10 @@ CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) {
457457
}
458458

459459
static MaybeAlign getAlign(Value *Ptr) {
460-
if (auto *O = dyn_cast<GlobalObject>(Ptr))
461-
return O->getAlign();
460+
if (auto *V = dyn_cast<GlobalVariable>(Ptr))
461+
return V->getAlign();
462462
if (auto *A = dyn_cast<GlobalAlias>(Ptr))
463-
return A->getAliaseeObject()->getAlign();
463+
return getAlign(A->getAliaseeObject());
464464
return {};
465465
}
466466

llvm/lib/IR/Value.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -957,30 +957,27 @@ uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,
957957

958958
Align Value::getPointerAlignment(const DataLayout &DL) const {
959959
assert(getType()->isPointerTy() && "must be pointer");
960-
if (auto *GO = dyn_cast<GlobalObject>(this)) {
961-
if (isa<Function>(GO)) {
962-
Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
963-
switch (DL.getFunctionPtrAlignType()) {
964-
case DataLayout::FunctionPtrAlignType::Independent:
965-
return FunctionPtrAlign;
966-
case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
967-
return std::max(FunctionPtrAlign, GO->getAlign().valueOrOne());
968-
}
969-
llvm_unreachable("Unhandled FunctionPtrAlignType");
960+
if (const Function *F = dyn_cast<Function>(this)) {
961+
Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
962+
switch (DL.getFunctionPtrAlignType()) {
963+
case DataLayout::FunctionPtrAlignType::Independent:
964+
return FunctionPtrAlign;
965+
case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
966+
return std::max(FunctionPtrAlign, F->getAlign().valueOrOne());
970967
}
971-
const MaybeAlign Alignment(GO->getAlign());
968+
llvm_unreachable("Unhandled FunctionPtrAlignType");
969+
} else if (auto *GVar = dyn_cast<GlobalVariable>(this)) {
970+
const MaybeAlign Alignment(GVar->getAlign());
972971
if (!Alignment) {
973-
if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
974-
Type *ObjectType = GVar->getValueType();
975-
if (ObjectType->isSized()) {
976-
// If the object is defined in the current Module, we'll be giving
977-
// it the preferred alignment. Otherwise, we have to assume that it
978-
// may only have the minimum ABI alignment.
979-
if (GVar->isStrongDefinitionForLinker())
980-
return DL.getPreferredAlign(GVar);
981-
else
982-
return DL.getABITypeAlign(ObjectType);
983-
}
972+
Type *ObjectType = GVar->getValueType();
973+
if (ObjectType->isSized()) {
974+
// If the object is defined in the current Module, we'll be giving
975+
// it the preferred alignment. Otherwise, we have to assume that it
976+
// may only have the minimum ABI alignment.
977+
if (GVar->isStrongDefinitionForLinker())
978+
return DL.getPreferredAlign(GVar);
979+
else
980+
return DL.getABITypeAlign(ObjectType);
984981
}
985982
}
986983
return Alignment.valueOrOne();

llvm/lib/IR/Verifier.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -735,12 +735,6 @@ void Verifier::visitGlobalValue(const GlobalValue &GV) {
735735
"Global is external, but doesn't have external or weak linkage!", &GV);
736736

737737
if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV)) {
738-
739-
if (MaybeAlign A = GO->getAlign()) {
740-
Check(A->value() <= Value::MaximumAlignment,
741-
"huge alignment values are unsupported", GO);
742-
}
743-
744738
if (const MDNode *Associated =
745739
GO->getMetadata(LLVMContext::MD_associated)) {
746740
Check(Associated->getNumOperands() == 1,
@@ -830,6 +824,11 @@ void Verifier::visitGlobalValue(const GlobalValue &GV) {
830824
void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
831825
Type *GVType = GV.getValueType();
832826

827+
if (MaybeAlign A = GV.getAlign()) {
828+
Check(A->value() <= Value::MaximumAlignment,
829+
"huge alignment values are unsupported", &GV);
830+
}
831+
833832
if (GV.hasInitializer()) {
834833
Check(GV.getInitializer()->getType() == GVType,
835834
"Global variable initializer type does not match global "
@@ -2869,6 +2868,11 @@ void Verifier::visitFunction(const Function &F) {
28692868
Check(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
28702869
"Invalid struct return type!", &F);
28712870

2871+
if (MaybeAlign A = F.getAlign()) {
2872+
Check(A->value() <= Value::MaximumAlignment,
2873+
"huge alignment values are unsupported", &F);
2874+
}
2875+
28722876
AttributeList Attrs = F.getAttributes();
28732877

28742878
Check(verifyAttributeCount(Attrs, FT->getNumParams()),

llvm/lib/LTO/LTOModule.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,11 @@ void LTOModule::addDefinedFunctionSymbol(StringRef Name, const GlobalValue *F) {
414414

415415
void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,
416416
bool isFunction) {
417-
const GlobalObject *go = dyn_cast<GlobalObject>(def);
418-
uint32_t attr = go ? Log2(go->getAlign().valueOrOne()) : 0;
417+
uint32_t attr = 0;
418+
if (auto *gv = dyn_cast<GlobalVariable>(def))
419+
attr = Log2(gv->getAlign().valueOrOne());
420+
else if (auto *f = dyn_cast<Function>(def))
421+
attr = Log2(f->getAlign().valueOrOne());
419422

420423
// set permissions part
421424
if (isFunction) {

llvm/lib/SandboxIR/Constant.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,11 @@ PoisonValue *PoisonValue::getElementValue(unsigned Idx) const {
282282
cast<llvm::PoisonValue>(Val)->getElementValue(Idx)));
283283
}
284284

285-
void GlobalObject::setAlignment(MaybeAlign Align) {
285+
void GlobalVariable::setAlignment(MaybeAlign Align) {
286286
Ctx.getTracker()
287-
.emplaceIfTracking<
288-
GenericSetter<&GlobalObject::getAlign, &GlobalObject::setAlignment>>(
289-
this);
290-
cast<llvm::GlobalObject>(Val)->setAlignment(Align);
291-
}
292-
293-
void GlobalObject::setGlobalObjectSubClassData(unsigned V) {
294-
Ctx.getTracker()
295-
.emplaceIfTracking<
296-
GenericSetter<&GlobalObject::getGlobalObjectSubClassData,
297-
&GlobalObject::setGlobalObjectSubClassData>>(this);
298-
cast<llvm::GlobalObject>(Val)->setGlobalObjectSubClassData(V);
287+
.emplaceIfTracking<GenericSetter<&GlobalVariable::getAlign,
288+
&GlobalVariable::setAlignment>>(this);
289+
cast<llvm::GlobalVariable>(Val)->setAlignment(Align);
299290
}
300291

301292
void GlobalObject::setSection(StringRef S) {

llvm/lib/SandboxIR/Function.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ FunctionType *Function::getFunctionType() const {
1717
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
1818
}
1919

20+
void Function::setAlignment(MaybeAlign Align) {
21+
Ctx.getTracker()
22+
.emplaceIfTracking<
23+
GenericSetter<&Function::getAlign, &Function::setAlignment>>(this);
24+
cast<llvm::Function>(Val)->setAlignment(Align);
25+
}
26+
2027
#ifndef NDEBUG
2128
void Function::dumpNameAndArgs(raw_ostream &OS) const {
2229
auto *F = cast<llvm::Function>(Val);

llvm/lib/Target/PowerPC/PPCMIPeephole.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "llvm/CodeGen/MachineInstrBuilder.h"
4343
#include "llvm/CodeGen/MachinePostDominators.h"
4444
#include "llvm/CodeGen/MachineRegisterInfo.h"
45+
#include "llvm/IR/GlobalVariable.h"
4546
#include "llvm/Support/Debug.h"
4647
#include "llvm/Support/DebugCounter.h"
4748

@@ -997,9 +998,9 @@ bool PPCMIPeephole::simplifyCode() {
997998
// the transformation.
998999
bool IsWordAligned = false;
9991000
if (SrcMI->getOperand(1).isGlobal()) {
1000-
const GlobalObject *GO =
1001-
dyn_cast<GlobalObject>(SrcMI->getOperand(1).getGlobal());
1002-
if (GO && GO->getAlign() && *GO->getAlign() >= 4 &&
1001+
const GlobalVariable *GV =
1002+
dyn_cast<GlobalVariable>(SrcMI->getOperand(1).getGlobal());
1003+
if (GV && GV->getAlign() && *GV->getAlign() >= 4 &&
10031004
(SrcMI->getOperand(1).getOffset() % 4 == 0))
10041005
IsWordAligned = true;
10051006
} else if (SrcMI->getOperand(1).isImm()) {

llvm/lib/Target/SystemZ/SystemZSubtarget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "SystemZSubtarget.h"
1010
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
11-
#include "llvm/IR/GlobalValue.h"
11+
#include "llvm/IR/GlobalVariable.h"
1212
#include "llvm/Target/TargetMachine.h"
1313

1414
using namespace llvm;
@@ -83,9 +83,9 @@ bool SystemZSubtarget::isAddressedViaADA(const GlobalValue *GV) const {
8383
// least two byte alignment, then generated code can use relative
8484
// instructions to address the variable. Otherwise, use the ADA to address
8585
// the variable.
86-
if (GO->getAlignment() & 0x1) {
87-
return true;
88-
}
86+
if (auto *GV = dyn_cast<GlobalVariable>(GO))
87+
if (GV->getAlign() && (*GV->getAlign()).value() & 0x1)
88+
return true;
8989

9090
// getKindForGlobal only works with definitions
9191
if (GO->isDeclaration()) {

0 commit comments

Comments
 (0)