Skip to content

Commit 20a631f

Browse files
committed
Refactor TargetMachine, pushing handling of TargetData into the target-specific subclasses. This has one caller-visible change: getTargetData() now returns a pointer instead of a reference.
This fixes PR 759. llvm-svn: 28074
1 parent 37c39b9 commit 20a631f

38 files changed

+145
-149
lines changed

Diff for: llvm/include/llvm/CodeGen/DwarfWriter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class DwarfWriter {
8585
AsmPrinter *Asm;
8686

8787
/// TD - Target data.
88-
const TargetData &TD;
88+
const TargetData *TD;
8989

9090
/// RI - Register Information.
9191
const MRegisterInfo *RI;

Diff for: llvm/include/llvm/CodeGen/MachineConstantPool.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ struct MachineConstantPoolEntry {
4242
};
4343

4444
class MachineConstantPool {
45-
const TargetData &TD;
45+
const TargetData *TD;
4646
unsigned PoolAlignment;
4747
std::vector<MachineConstantPoolEntry> Constants;
4848
public:
49-
MachineConstantPool(const TargetData &td) : TD(td), PoolAlignment(1) {}
49+
MachineConstantPool(const TargetData *td) : TD(td), PoolAlignment(1) {}
5050

5151
/// getConstantPoolAlignment - Return the log2 of the alignment required by
5252
/// the whole constant pool, of which the first element must be aligned.

Diff for: llvm/include/llvm/CodeGen/MachineJumpTableInfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ struct MachineJumpTableEntry {
3737
};
3838

3939
class MachineJumpTableInfo {
40-
const TargetData &TD;
40+
const TargetData *TD;
4141
std::vector<MachineJumpTableEntry> JumpTables;
4242
public:
43-
MachineJumpTableInfo(const TargetData &td) : TD(td) {}
43+
MachineJumpTableInfo(const TargetData *td) : TD(td) {}
4444

4545
/// getJumpTableIndex - Create a new jump table or return an existing one.
4646
///

Diff for: llvm/include/llvm/ExecutionEngine/ExecutionEngine.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class ExecutionEngine {
6767
protected:
6868
ModuleProvider *MP;
6969

70-
void setTargetData(const TargetData &td) {
71-
TD = &td;
70+
void setTargetData(const TargetData *td) {
71+
TD = td;
7272
}
7373

7474
// To avoid having libexecutionengine depend on the JIT and interpreter
@@ -88,7 +88,7 @@ class ExecutionEngine {
8888
virtual ~ExecutionEngine();
8989

9090
Module &getModule() const { return CurMod; }
91-
const TargetData &getTargetData() const { return *TD; }
91+
const TargetData *getTargetData() const { return TD; }
9292

9393
/// create - This is the factory method for creating an execution engine which
9494
/// is appropriate for the current machine.

Diff for: llvm/include/llvm/Target/TargetData.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ class TargetData : public ImmutablePass {
5454
unsigned char ByteAl = 1, unsigned char BoolAl = 1);
5555

5656
// Copy constructor
57-
TargetData (const TargetData &TD) :
57+
TargetData (const TargetData *TD) :
5858
ImmutablePass(),
59-
LittleEndian(TD.isLittleEndian()),
60-
BoolAlignment(TD.getBoolAlignment()),
61-
ByteAlignment(TD.getByteAlignment()),
62-
ShortAlignment(TD.getShortAlignment()),
63-
IntAlignment(TD.getIntAlignment()),
64-
LongAlignment(TD.getLongAlignment()),
65-
FloatAlignment(TD.getFloatAlignment()),
66-
DoubleAlignment(TD.getDoubleAlignment()),
67-
PointerSize(TD.getPointerSize()),
68-
PointerAlignment(TD.getPointerAlignment()) {
59+
LittleEndian(TD->isLittleEndian()),
60+
BoolAlignment(TD->getBoolAlignment()),
61+
ByteAlignment(TD->getByteAlignment()),
62+
ShortAlignment(TD->getShortAlignment()),
63+
IntAlignment(TD->getIntAlignment()),
64+
LongAlignment(TD->getLongAlignment()),
65+
FloatAlignment(TD->getFloatAlignment()),
66+
DoubleAlignment(TD->getDoubleAlignment()),
67+
PointerSize(TD->getPointerSize()),
68+
PointerAlignment(TD->getPointerAlignment()) {
6969
}
7070

7171
TargetData(const std::string &ToolName, const Module *M);

Diff for: llvm/include/llvm/Target/TargetLowering.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class TargetLowering {
7878
virtual ~TargetLowering();
7979

8080
TargetMachine &getTargetMachine() const { return TM; }
81-
const TargetData &getTargetData() const { return TD; }
81+
const TargetData *getTargetData() const { return TD; }
8282

8383
bool isLittleEndian() const { return IsLittleEndian; }
8484
MVT::ValueType getPointerTy() const { return PointerTy; }
@@ -648,7 +648,7 @@ class TargetLowering {
648648
std::vector<unsigned> LegalAddressScales;
649649

650650
TargetMachine &TM;
651-
const TargetData &TD;
651+
const TargetData *TD;
652652

653653
/// IsLittleEndian - True if this is a little endian target.
654654
///

Diff for: llvm/include/llvm/Target/TargetMachine.h

+2-10
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,11 @@ namespace Reloc {
5050
///
5151
class TargetMachine {
5252
const std::string Name;
53-
const TargetData DataLayout; // Calculates type size & alignment
5453

5554
TargetMachine(const TargetMachine&); // DO NOT IMPLEMENT
5655
void operator=(const TargetMachine&); // DO NOT IMPLEMENT
5756
protected: // Can only create subclasses...
58-
TargetMachine(const std::string &name, bool LittleEndian = false,
59-
unsigned char PtrSize = 8, unsigned char PtrAl = 8,
60-
unsigned char DoubleAl = 8, unsigned char FloatAl = 4,
61-
unsigned char LongAl = 8, unsigned char IntAl = 4,
62-
unsigned char ShortAl = 2, unsigned char ByteAl = 1,
63-
unsigned char BoolAl = 1);
64-
65-
TargetMachine(const std::string &name, const TargetData &TD);
57+
TargetMachine(const std::string &name) : Name(name) { };
6658

6759
/// This constructor is used for targets that support arbitrary TargetData
6860
/// layouts, like the C backend. It initializes the TargetData to match that
@@ -101,7 +93,7 @@ class TargetMachine {
10193
virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
10294
virtual const TargetFrameInfo *getFrameInfo() const { return 0; }
10395
virtual TargetLowering *getTargetLowering() const { return 0; }
104-
const TargetData &getTargetData() const { return DataLayout; }
96+
virtual const TargetData *getTargetData() const { return 0; }
10597

10698
/// getSubtarget - This method returns a pointer to the specified type of
10799
/// TargetSubtarget. In debug builds, it verifies that the object being

Diff for: llvm/lib/CodeGen/AsmPrinter.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
144144
void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
145145
const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
146146
if (CP.empty()) return;
147-
const TargetData &TD = TM.getTargetData();
147+
const TargetData *TD = TM.getTargetData();
148148

149149
SwitchSection(ConstantPoolSection, 0);
150150
EmitAlignment(MCP->getConstantPoolAlignment());
@@ -154,7 +154,7 @@ void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
154154
WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
155155
EmitGlobalConstant(CP[i].Val);
156156
if (i != e-1) {
157-
unsigned EntSize = TM.getTargetData().getTypeSize(CP[i].Val->getType());
157+
unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
158158
unsigned ValEnd = CP[i].Offset + EntSize;
159159
// Emit inter-object padding for alignment.
160160
EmitZeros(CP[i+1].Offset-ValEnd);
@@ -168,15 +168,15 @@ void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
168168
void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
169169
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
170170
if (JT.empty()) return;
171-
const TargetData &TD = TM.getTargetData();
171+
const TargetData *TD = TM.getTargetData();
172172

173173
// FIXME: someday we need to handle PIC jump tables
174174
assert((TM.getRelocationModel() == Reloc::Static ||
175175
TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
176176
"Unhandled relocation model emitting jump table information!");
177177

178178
SwitchSection(JumpTableSection, 0);
179-
EmitAlignment(Log2_32(TD.getPointerAlignment()));
179+
EmitAlignment(Log2_32(TD->getPointerAlignment()));
180180
for (unsigned i = 0, e = JT.size(); i != e; ++i) {
181181
O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
182182
<< ":\n";
@@ -242,7 +242,7 @@ void AsmPrinter::EmitXXStructorList(Constant *List) {
242242
/// specified global, returned in log form. This includes an explicitly
243243
/// requested alignment (if the global has one).
244244
unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
245-
unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
245+
unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType());
246246
if (GV->getAlignment() > (1U << Alignment))
247247
Alignment = Log2_32(GV->getAlignment());
248248

@@ -253,7 +253,7 @@ unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
253253
if (Alignment < 4) {
254254
// If the global is not external, see if it is large. If so, give it a
255255
// larger alignment.
256-
if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
256+
if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128)
257257
Alignment = 4; // 16-byte alignment.
258258
}
259259
}
@@ -310,13 +310,13 @@ void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
310310
else
311311
O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
312312
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
313-
const TargetData &TD = TM.getTargetData();
313+
const TargetData *TD = TM.getTargetData();
314314
switch(CE->getOpcode()) {
315315
case Instruction::GetElementPtr: {
316316
// generate a symbolic expression for the byte address
317317
const Constant *ptrVal = CE->getOperand(0);
318318
std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
319-
if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
319+
if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
320320
if (Offset)
321321
O << "(";
322322
EmitConstantValueOnly(ptrVal);
@@ -344,7 +344,7 @@ void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
344344
|| (isa<PointerType>(Ty)
345345
&& (OpTy == Type::LongTy || OpTy == Type::ULongTy
346346
|| OpTy == Type::IntTy || OpTy == Type::UIntTy))
347-
|| (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
347+
|| (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
348348
&& OpTy->isLosslesslyConvertibleTo(Ty))))
349349
&& "FIXME: Don't yet support this kind of constant cast expr");
350350
EmitConstantValueOnly(Op);
@@ -426,10 +426,10 @@ void AsmPrinter::EmitString(const ConstantArray *CVA) const {
426426
/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
427427
///
428428
void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
429-
const TargetData &TD = TM.getTargetData();
429+
const TargetData *TD = TM.getTargetData();
430430

431431
if (CV->isNullValue() || isa<UndefValue>(CV)) {
432-
EmitZeros(TD.getTypeSize(CV->getType()));
432+
EmitZeros(TD->getTypeSize(CV->getType()));
433433
return;
434434
} else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
435435
if (CVA->isString()) {
@@ -441,13 +441,13 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
441441
return;
442442
} else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
443443
// Print the fields in successive locations. Pad to align if needed!
444-
const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
444+
const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
445445
uint64_t sizeSoFar = 0;
446446
for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
447447
const Constant* field = CVS->getOperand(i);
448448

449449
// Check if padding is needed and insert one or more 0s.
450-
uint64_t fieldSize = TD.getTypeSize(field->getType());
450+
uint64_t fieldSize = TD->getTypeSize(field->getType());
451451
uint64_t padSize = ((i == e-1? cvsLayout->StructSize
452452
: cvsLayout->MemberOffsets[i+1])
453453
- cvsLayout->MemberOffsets[i]) - fieldSize;
@@ -470,7 +470,7 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
470470
if (Data64bitsDirective)
471471
O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
472472
<< " double value: " << Val << "\n";
473-
else if (TD.isBigEndian()) {
473+
else if (TD->isBigEndian()) {
474474
O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
475475
<< "\t" << CommentString << " double most significant word "
476476
<< Val << "\n";
@@ -497,7 +497,7 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
497497

498498
if (Data64bitsDirective)
499499
O << Data64bitsDirective << Val << "\n";
500-
else if (TD.isBigEndian()) {
500+
else if (TD->isBigEndian()) {
501501
O << Data32bitsDirective << unsigned(Val >> 32)
502502
<< "\t" << CommentString << " Double-word most significant word "
503503
<< Val << "\n";
@@ -533,7 +533,7 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
533533
O << Data16bitsDirective;
534534
break;
535535
case Type::PointerTyID:
536-
if (TD.getPointerSize() == 8) {
536+
if (TD->getPointerSize() == 8) {
537537
O << Data64bitsDirective;
538538
break;
539539
}

Diff for: llvm/lib/CodeGen/DwarfWriter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ void DwarfWriter::EmitInt64(uint64_t Value) const {
10751075
if (Asm->Data64bitsDirective) {
10761076
O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
10771077
} else {
1078-
if (TD.isBigEndian()) {
1078+
if (TD->isBigEndian()) {
10791079
EmitInt32(unsigned(Value >> 32)); O << "\n";
10801080
EmitInt32(unsigned(Value));
10811081
} else {
@@ -1361,7 +1361,7 @@ DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
13611361
Offset -= FieldOffset;
13621362

13631363
// Maybe we need to work from the other end.
1364-
if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
1364+
if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
13651365

13661366
Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
13671367
Member->AddUInt(DW_AT_bit_size, 0, Size);

Diff for: llvm/lib/CodeGen/ELFWriter.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
158158
e_machine = 0; // e_machine defaults to 'No Machine'
159159
e_flags = 0; // e_flags defaults to 0, no flags.
160160

161-
is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
162-
isLittleEndian = TM.getTargetData().isLittleEndian();
161+
is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
162+
isLittleEndian = TM.getTargetData()->isLittleEndian();
163163

164164
// Create the machine code emitter object for this target.
165165
MCE = new ELFCodeEmitter(*this);
@@ -233,8 +233,8 @@ void ELFWriter::EmitGlobal(GlobalVariable *GV) {
233233
}
234234

235235
const Type *GVType = (const Type*)GV->getType();
236-
unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
237-
unsigned Size = TM.getTargetData().getTypeSize(GVType);
236+
unsigned Align = TM.getTargetData()->getTypeAlignment(GVType);
237+
unsigned Size = TM.getTargetData()->getTypeSize(GVType);
238238

239239
// If this global has a zero initializer, it is part of the .bss or common
240240
// section.

Diff for: llvm/lib/CodeGen/MachineFunction.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,11 @@ void MachineJumpTableInfo::print(std::ostream &OS) const {
367367
}
368368

369369
unsigned MachineJumpTableInfo::getEntrySize() const {
370-
return TD.getPointerSize();
370+
return TD->getPointerSize();
371371
}
372372

373373
unsigned MachineJumpTableInfo::getAlignment() const {
374-
return TD.getPointerAlignment();
374+
return TD->getPointerAlignment();
375375
}
376376

377377
void MachineJumpTableInfo::dump() const { print(std::cerr); }
@@ -400,7 +400,7 @@ unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
400400
unsigned Offset = 0;
401401
if (!Constants.empty()) {
402402
Offset = Constants.back().Offset;
403-
Offset += TD.getTypeSize(Constants.back().Val->getType());
403+
Offset += TD->getTypeSize(Constants.back().Val->getType());
404404
Offset = (Offset+AlignMask)&~AlignMask;
405405
}
406406

Diff for: llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
19861986
// Otherwise, the target does not support this operation. Lower the
19871987
// operation to an explicit libcall as appropriate.
19881988
MVT::ValueType IntPtr = TLI.getPointerTy();
1989-
const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1989+
const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
19901990
std::vector<std::pair<SDOperand, const Type*> > Args;
19911991

19921992
const char *FnName = 0;
@@ -2781,8 +2781,8 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
27812781
// slots and always reusing the same one. We currently always create
27822782
// new ones, as reuse may inhibit scheduling.
27832783
const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2784-
unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2785-
unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2784+
unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
2785+
unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
27862786
MachineFunction &MF = DAG.getMachineFunction();
27872787
int SSFI =
27882788
MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);

Diff for: llvm/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
130130
Align = 3; // always 8-byte align doubles.
131131
else {
132132
Align = TM.getTargetData()
133-
.getTypeAlignmentShift(CP->get()->getType());
133+
->getTypeAlignmentShift(CP->get()->getType());
134134
if (Align == 0) {
135135
// Alignment of packed types. FIXME!
136-
Align = TM.getTargetData().getTypeSize(CP->get()->getType());
136+
Align = TM.getTargetData()->getTypeSize(CP->get()->getType());
137137
Align = Log2_64(Align);
138138
}
139139
}

0 commit comments

Comments
 (0)