Skip to content

Commit a77ce54

Browse files
committed
Address comments round five.
- Added support for looking through aliases to the aliasees objects code model, along with lit testing. - Split the shared code of 'getCodeModel' into PPCSubtarget and have PPCISelDAGToDAG and PPCAsmPrinter use that. - Undo unintended change to 'getMCSymbolForTOCPsudoMO' that was left when addressing earlier comments.
1 parent 39c7802 commit a77ce54

File tree

5 files changed

+91
-48
lines changed

5 files changed

+91
-48
lines changed

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,6 @@ static CodeModel::Model getCodeModel(const PPCSubtarget &S,
478478
const TargetMachine &TM,
479479
const MachineOperand &MO) {
480480
CodeModel::Model ModuleModel = TM.getCodeModel();
481-
// Per global code model is only supported on AIX.
482-
if (!S.isAIXABI())
483-
return ModuleModel;
484481

485482
// If the operand is not a global address then there is no
486483
// global variable to carry an attribute.
@@ -490,24 +487,7 @@ static CodeModel::Model getCodeModel(const PPCSubtarget &S,
490487
const GlobalValue *GV = MO.getGlobal();
491488
assert(GV && "expected global for MO_GlobalAddress");
492489

493-
if (!isa<GlobalVariable>(GV))
494-
return ModuleModel;
495-
496-
std::optional<CodeModel::Model> MaybeCodeModel =
497-
cast<GlobalVariable>(GV)->getCodeModel();
498-
if (MaybeCodeModel)
499-
return *MaybeCodeModel;
500-
501-
return ModuleModel;
502-
}
503-
504-
static std::optional<CodeModel::Model>
505-
getPerGlobalCodeModel(const GlobalValue *GV) {
506-
// Symbols that aren't global variables cannot have the attribute.
507-
if (!isa<GlobalVariable>(GV))
508-
return std::nullopt;
509-
510-
return cast<GlobalVariable>(GV)->getCodeModel();
490+
return S.getCodeModel(TM, GV);
511491
}
512492

513493
static void setOptionalCodeModel(MCSymbolXCOFF *XSym, CodeModel::Model CM) {
@@ -772,11 +752,8 @@ void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI,
772752
static MCSymbol *getMCSymbolForTOCPseudoMO(const MachineOperand &MO,
773753
AsmPrinter &AP) {
774754
switch (MO.getType()) {
775-
case MachineOperand::MO_GlobalAddress: {
776-
const GlobalValue *GV = MO.getGlobal();
777-
MCSymbol *Sym = AP.getSymbol(GV);
778-
return Sym;
779-
}
755+
case MachineOperand::MO_GlobalAddress:
756+
return AP.getSymbol(MO.getGlobal());
780757
case MachineOperand::MO_ConstantPoolIndex:
781758
return AP.GetCPISymbol(MO.getIndex());
782759
case MachineOperand::MO_JumpTableIndex:
@@ -3043,8 +3020,7 @@ bool PPCAIXAsmPrinter::doInitialization(Module &M) {
30433020
}
30443021

30453022
setCsectAlignment(&G);
3046-
std::optional<CodeModel::Model> OptionalCodeModel =
3047-
getPerGlobalCodeModel(&G);
3023+
std::optional<CodeModel::Model> OptionalCodeModel = G.getCodeModel();
30483024
if (OptionalCodeModel)
30493025
setOptionalCodeModel(cast<MCSymbolXCOFF>(getSymbol(&G)),
30503026
*OptionalCodeModel);
@@ -3069,6 +3045,15 @@ bool PPCAIXAsmPrinter::doInitialization(Module &M) {
30693045
false);
30703046
}
30713047

3048+
const GlobalVariable *GVar =
3049+
dyn_cast_or_null<GlobalVariable>(Alias.getAliaseeObject());
3050+
if (GVar) {
3051+
std::optional<CodeModel::Model> OptionalCodeModel = GVar->getCodeModel();
3052+
if (OptionalCodeModel)
3053+
setOptionalCodeModel(cast<MCSymbolXCOFF>(getSymbol(&Alias)),
3054+
*OptionalCodeModel);
3055+
}
3056+
30723057
GOAliasMap[Aliasee].push_back(&Alias);
30733058
}
30743059

llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -531,30 +531,15 @@ static CodeModel::Model getCodeModel(const PPCSubtarget &Subtarget,
531531
// this will be the effective code model.
532532
CodeModel::Model ModuleModel = TM.getCodeModel();
533533

534-
// Initially support per global code model for AIX only.
535-
if (!Subtarget.isAIXABI())
536-
return ModuleModel;
537-
538-
// If the operand is not a global address there is no
539-
// GlobalVariable to query for an attribute.
540-
SDValue Operand = Node->getOperand(0);
541-
if (!isa<GlobalAddressSDNode>(Operand))
534+
GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Node->getOperand(0));
535+
if (!GA)
542536
return ModuleModel;
543537

544-
const GlobalValue *GV = cast<GlobalAddressSDNode>(Operand)->getGlobal();
545-
if (!GV || !isa<GlobalVariable>(GV))
538+
const GlobalValue *GV = GA->getGlobal();
539+
if (!GV)
546540
return ModuleModel;
547541

548-
std::optional<CodeModel::Model> MaybeCodeModel =
549-
dyn_cast<GlobalVariable>(GV)->getCodeModel();
550-
if (MaybeCodeModel) {
551-
CodeModel::Model CM = *MaybeCodeModel;
552-
assert((CM == CodeModel::Small || CM == CodeModel::Large) &&
553-
"invalid code model for AIX");
554-
return CM;
555-
}
556-
557-
return ModuleModel;
542+
return Subtarget.getCodeModel(TM, GV);
558543
}
559544

560545
/// isInt32Immediate - This method tests to see if the node is a 32-bit constant

llvm/lib/Target/PowerPC/PPCSubtarget.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,46 @@ bool PPCSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
203203
return true;
204204
}
205205

206+
CodeModel::Model PPCSubtarget::getCodeModel(const TargetMachine &TM,
207+
const GlobalValue *GV) const {
208+
// If there isn't an attribute to override the module code model
209+
// this will be the effective code model.
210+
CodeModel::Model ModuleModel = TM.getCodeModel();
211+
212+
// Initially support per global code model for AIX only.
213+
if (!isAIXABI())
214+
return ModuleModel;
215+
216+
// Only GlobalVariables carry an attribute which can override the module code
217+
// model.
218+
assert(GV && "Unexpected NULL GlobalValue");
219+
const GlobalVariable *GlobalVar =
220+
[](const GlobalValue *GV) -> const GlobalVariable * {
221+
const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV);
222+
if (Var)
223+
return Var;
224+
225+
const GlobalAlias *Alias = dyn_cast<GlobalAlias>(GV);
226+
if (Alias)
227+
return dyn_cast<GlobalVariable>(Alias->getAliaseeObject());
228+
229+
return nullptr;
230+
}(GV);
231+
232+
if (!GlobalVar)
233+
return ModuleModel;
234+
235+
std::optional<CodeModel::Model> MaybeCodeModel = GlobalVar->getCodeModel();
236+
if (MaybeCodeModel) {
237+
CodeModel::Model CM = *MaybeCodeModel;
238+
assert((CM == CodeModel::Small || CM == CodeModel::Large) &&
239+
"invalid code model for AIX");
240+
return CM;
241+
}
242+
243+
return ModuleModel;
244+
}
245+
206246
bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
207247
bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }
208248

llvm/lib/Target/PowerPC/PPCSubtarget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ class PPCSubtarget : public PPCGenSubtargetInfo {
245245
/// True if the GV will be accessed via an indirect symbol.
246246
bool isGVIndirectSymbol(const GlobalValue *GV) const;
247247

248+
/// Calculates the effective code model for argument GV.
249+
CodeModel::Model getCodeModel(const TargetMachine &TM,
250+
const GlobalValue *GV) const;
251+
248252
/// True if the ABI is descriptor based.
249253
bool usesFunctionDescriptors() const {
250254
// Both 32-bit and 64-bit AIX are descriptor based. For ELF only the 64-bit

llvm/test/CodeGen/PowerPC/aix-codemodel-attr.ll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
@e = external dso_local global i32, align 4
1818
@f = dso_local global i32 2748, align 4
1919

20+
@large_aliasee = global i32 10, code_model "large", align 4
21+
@small_aliasee = global i32 171, code_model "small", align 4
22+
@normal_aliasee = global i32 2748, align 4
23+
24+
@al = alias i32, ptr @large_aliasee
25+
@as = alias i32, ptr @small_aliasee
26+
@an = alias i32, ptr @normal_aliasee
27+
2028
define i32 @A() local_unnamed_addr {
2129
entry:
2230
%0 = load i32, ptr @a, align 4
@@ -120,6 +128,23 @@ entry:
120128
; CHECK64: ld 3, L..C[[TL_D]]@l([[HI]])
121129
; CHECK: blr
122130

131+
define i32 @G() {
132+
%tmp = load i32, ptr @al
133+
ret i32 %tmp
134+
}
135+
; CHECK: addis [[HI:[0-9]+]], L..C[[TL_AL:[0-9]+]]@u(2)
136+
; CHECK32: lwz [[ADDR:[0-9]+]], L..C[[TL_AL]]@l([[HI]])
137+
; CHECK64: ld [[ADDR:[0-9]+]], L..C[[TL_AL]]@l([[HI]])
138+
; CHECK: lwz 3, 0([[ADDR]])
139+
140+
define i32 @H() {
141+
%tmp = load i32, ptr @as
142+
ret i32 %tmp
143+
}
144+
; CHECK32: lwz [[ADDR:[0-9]+]], L..C[[TL_AS:[0-9]+]](2)
145+
; CHECK64: ld [[ADDR:[0-9]+]], L..C[[TL_AS:[0-9]+]](2)
146+
; CHECK: lwz 3, 0([[ADDR]])
147+
123148
;; Check TOC entires have correct storage mapping class
124149
; CHECK: L..C[[TL_A]]:
125150
; CHECK: .tc a[TC],a[UA]
@@ -135,3 +160,7 @@ entry:
135160
; CHECK: L..C[[TL_F]]:
136161
; CHECK-SMALL: .tc f[TC],f[RW]
137162
; CHECK-LARGE: .tc f[TE],f[RW]
163+
; CHECK: L..C[[TL_AL]]:
164+
; CHECK: .tc al[TE],al
165+
; CHECK: L..C[[TL_AS]]:
166+
; CHECK: .tc as[TC],as

0 commit comments

Comments
 (0)