llvm.org GIT mirror llvm / a69571c
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28074 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 14 years ago
38 changed file(s) with 145 addition(s) and 149 deletion(s). Raw diff Collapse all Expand all
8484 AsmPrinter *Asm;
8585
8686 /// TD - Target data.
87 const TargetData &TD;
87 const TargetData *TD;
8888
8989 /// RI - Register Information.
9090 const MRegisterInfo *RI;
4141 };
4242
4343 class MachineConstantPool {
44 const TargetData &TD;
44 const TargetData *TD;
4545 unsigned PoolAlignment;
4646 std::vector Constants;
4747 public:
48 MachineConstantPool(const TargetData &td) : TD(td), PoolAlignment(1) {}
48 MachineConstantPool(const TargetData *td) : TD(td), PoolAlignment(1) {}
4949
5050 /// getConstantPoolAlignment - Return the log2 of the alignment required by
5151 /// the whole constant pool, of which the first element must be aligned.
3636 };
3737
3838 class MachineJumpTableInfo {
39 const TargetData &TD;
39 const TargetData *TD;
4040 std::vector JumpTables;
4141 public:
42 MachineJumpTableInfo(const TargetData &td) : TD(td) {}
42 MachineJumpTableInfo(const TargetData *td) : TD(td) {}
4343
4444 /// getJumpTableIndex - Create a new jump table or return an existing one.
4545 ///
6666 protected:
6767 ModuleProvider *MP;
6868
69 void setTargetData(const TargetData &td) {
70 TD = &td;
69 void setTargetData(const TargetData *td) {
70 TD = td;
7171 }
7272
7373 // To avoid having libexecutionengine depend on the JIT and interpreter
8787 virtual ~ExecutionEngine();
8888
8989 Module &getModule() const { return CurMod; }
90 const TargetData &getTargetData() const { return *TD; }
90 const TargetData *getTargetData() const { return TD; }
9191
9292 /// create - This is the factory method for creating an execution engine which
9393 /// is appropriate for the current machine.
5353 unsigned char ByteAl = 1, unsigned char BoolAl = 1);
5454
5555 // Copy constructor
56 TargetData (const TargetData &TD) :
56 TargetData (const TargetData *TD) :
5757 ImmutablePass(),
58 LittleEndian(TD.isLittleEndian()),
59 BoolAlignment(TD.getBoolAlignment()),
60 ByteAlignment(TD.getByteAlignment()),
61 ShortAlignment(TD.getShortAlignment()),
62 IntAlignment(TD.getIntAlignment()),
63 LongAlignment(TD.getLongAlignment()),
64 FloatAlignment(TD.getFloatAlignment()),
65 DoubleAlignment(TD.getDoubleAlignment()),
66 PointerSize(TD.getPointerSize()),
67 PointerAlignment(TD.getPointerAlignment()) {
58 LittleEndian(TD->isLittleEndian()),
59 BoolAlignment(TD->getBoolAlignment()),
60 ByteAlignment(TD->getByteAlignment()),
61 ShortAlignment(TD->getShortAlignment()),
62 IntAlignment(TD->getIntAlignment()),
63 LongAlignment(TD->getLongAlignment()),
64 FloatAlignment(TD->getFloatAlignment()),
65 DoubleAlignment(TD->getDoubleAlignment()),
66 PointerSize(TD->getPointerSize()),
67 PointerAlignment(TD->getPointerAlignment()) {
6868 }
6969
7070 TargetData(const std::string &ToolName, const Module *M);
7777 virtual ~TargetLowering();
7878
7979 TargetMachine &getTargetMachine() const { return TM; }
80 const TargetData &getTargetData() const { return TD; }
80 const TargetData *getTargetData() const { return TD; }
8181
8282 bool isLittleEndian() const { return IsLittleEndian; }
8383 MVT::ValueType getPointerTy() const { return PointerTy; }
647647 std::vector LegalAddressScales;
648648
649649 TargetMachine &TM;
650 const TargetData &TD;
650 const TargetData *TD;
651651
652652 /// IsLittleEndian - True if this is a little endian target.
653653 ///
4949 ///
5050 class TargetMachine {
5151 const std::string Name;
52 const TargetData DataLayout; // Calculates type size & alignment
5352
5453 TargetMachine(const TargetMachine&); // DO NOT IMPLEMENT
5554 void operator=(const TargetMachine&); // DO NOT IMPLEMENT
5655 protected: // Can only create subclasses...
57 TargetMachine(const std::string &name, bool LittleEndian = false,
58 unsigned char PtrSize = 8, unsigned char PtrAl = 8,
59 unsigned char DoubleAl = 8, unsigned char FloatAl = 4,
60 unsigned char LongAl = 8, unsigned char IntAl = 4,
61 unsigned char ShortAl = 2, unsigned char ByteAl = 1,
62 unsigned char BoolAl = 1);
63
64 TargetMachine(const std::string &name, const TargetData &TD);
56 TargetMachine(const std::string &name) : Name(name) { };
6557
6658 /// This constructor is used for targets that support arbitrary TargetData
6759 /// layouts, like the C backend. It initializes the TargetData to match that
10092 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
10193 virtual const TargetFrameInfo *getFrameInfo() const { return 0; }
10294 virtual TargetLowering *getTargetLowering() const { return 0; }
103 const TargetData &getTargetData() const { return DataLayout; }
95 virtual const TargetData *getTargetData() const { return 0; }
10496
10597 /// getSubtarget - This method returns a pointer to the specified type of
10698 /// TargetSubtarget. In debug builds, it verifies that the object being
143143 void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
144144 const std::vector &CP = MCP->getConstants();
145145 if (CP.empty()) return;
146 const TargetData &TD = TM.getTargetData();
146 const TargetData *TD = TM.getTargetData();
147147
148148 SwitchSection(ConstantPoolSection, 0);
149149 EmitAlignment(MCP->getConstantPoolAlignment());
153153 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
154154 EmitGlobalConstant(CP[i].Val);
155155 if (i != e-1) {
156 unsigned EntSize = TM.getTargetData().getTypeSize(CP[i].Val->getType());
156 unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
157157 unsigned ValEnd = CP[i].Offset + EntSize;
158158 // Emit inter-object padding for alignment.
159159 EmitZeros(CP[i+1].Offset-ValEnd);
167167 void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
168168 const std::vector &JT = MJTI->getJumpTables();
169169 if (JT.empty()) return;
170 const TargetData &TD = TM.getTargetData();
170 const TargetData *TD = TM.getTargetData();
171171
172172 // FIXME: someday we need to handle PIC jump tables
173173 assert((TM.getRelocationModel() == Reloc::Static ||
175175 "Unhandled relocation model emitting jump table information!");
176176
177177 SwitchSection(JumpTableSection, 0);
178 EmitAlignment(Log2_32(TD.getPointerAlignment()));
178 EmitAlignment(Log2_32(TD->getPointerAlignment()));
179179 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
180180 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
181181 << ":\n";
241241 /// specified global, returned in log form. This includes an explicitly
242242 /// requested alignment (if the global has one).
243243 unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
244 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
244 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType());
245245 if (GV->getAlignment() > (1U << Alignment))
246246 Alignment = Log2_32(GV->getAlignment());
247247
252252 if (Alignment < 4) {
253253 // If the global is not external, see if it is large. If so, give it a
254254 // larger alignment.
255 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
255 if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128)
256256 Alignment = 4; // 16-byte alignment.
257257 }
258258 }
309309 else
310310 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
311311 } else if (const ConstantExpr *CE = dyn_cast(CV)) {
312 const TargetData &TD = TM.getTargetData();
312 const TargetData *TD = TM.getTargetData();
313313 switch(CE->getOpcode()) {
314314 case Instruction::GetElementPtr: {
315315 // generate a symbolic expression for the byte address
316316 const Constant *ptrVal = CE->getOperand(0);
317317 std::vector idxVec(CE->op_begin()+1, CE->op_end());
318 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
318 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
319319 if (Offset)
320320 O << "(";
321321 EmitConstantValueOnly(ptrVal);
343343 || (isa(Ty)
344344 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
345345 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
346 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
346 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
347347 && OpTy->isLosslesslyConvertibleTo(Ty))))
348348 && "FIXME: Don't yet support this kind of constant cast expr");
349349 EmitConstantValueOnly(Op);
425425 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
426426 ///
427427 void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
428 const TargetData &TD = TM.getTargetData();
428 const TargetData *TD = TM.getTargetData();
429429
430430 if (CV->isNullValue() || isa(CV)) {
431 EmitZeros(TD.getTypeSize(CV->getType()));
431 EmitZeros(TD->getTypeSize(CV->getType()));
432432 return;
433433 } else if (const ConstantArray *CVA = dyn_cast(CV)) {
434434 if (CVA->isString()) {
440440 return;
441441 } else if (const ConstantStruct *CVS = dyn_cast(CV)) {
442442 // Print the fields in successive locations. Pad to align if needed!
443 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
443 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
444444 uint64_t sizeSoFar = 0;
445445 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
446446 const Constant* field = CVS->getOperand(i);
447447
448448 // Check if padding is needed and insert one or more 0s.
449 uint64_t fieldSize = TD.getTypeSize(field->getType());
449 uint64_t fieldSize = TD->getTypeSize(field->getType());
450450 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
451451 : cvsLayout->MemberOffsets[i+1])
452452 - cvsLayout->MemberOffsets[i]) - fieldSize;
469469 if (Data64bitsDirective)
470470 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
471471 << " double value: " << Val << "\n";
472 else if (TD.isBigEndian()) {
472 else if (TD->isBigEndian()) {
473473 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
474474 << "\t" << CommentString << " double most significant word "
475475 << Val << "\n";
496496
497497 if (Data64bitsDirective)
498498 O << Data64bitsDirective << Val << "\n";
499 else if (TD.isBigEndian()) {
499 else if (TD->isBigEndian()) {
500500 O << Data32bitsDirective << unsigned(Val >> 32)
501501 << "\t" << CommentString << " Double-word most significant word "
502502 << Val << "\n";
532532 O << Data16bitsDirective;
533533 break;
534534 case Type::PointerTyID:
535 if (TD.getPointerSize() == 8) {
535 if (TD->getPointerSize() == 8) {
536536 O << Data64bitsDirective;
537537 break;
538538 }
10741074 if (Asm->Data64bitsDirective) {
10751075 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
10761076 } else {
1077 if (TD.isBigEndian()) {
1077 if (TD->isBigEndian()) {
10781078 EmitInt32(unsigned(Value >> 32)); O << "\n";
10791079 EmitInt32(unsigned(Value));
10801080 } else {
13601360 Offset -= FieldOffset;
13611361
13621362 // Maybe we need to work from the other end.
1363 if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
1363 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
13641364
13651365 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
13661366 Member->AddUInt(DW_AT_bit_size, 0, Size);
157157 e_machine = 0; // e_machine defaults to 'No Machine'
158158 e_flags = 0; // e_flags defaults to 0, no flags.
159159
160 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
161 isLittleEndian = TM.getTargetData().isLittleEndian();
160 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
161 isLittleEndian = TM.getTargetData()->isLittleEndian();
162162
163163 // Create the machine code emitter object for this target.
164164 MCE = new ELFCodeEmitter(*this);
232232 }
233233
234234 const Type *GVType = (const Type*)GV->getType();
235 unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
236 unsigned Size = TM.getTargetData().getTypeSize(GVType);
235 unsigned Align = TM.getTargetData()->getTypeAlignment(GVType);
236 unsigned Size = TM.getTargetData()->getTypeSize(GVType);
237237
238238 // If this global has a zero initializer, it is part of the .bss or common
239239 // section.
366366 }
367367
368368 unsigned MachineJumpTableInfo::getEntrySize() const {
369 return TD.getPointerSize();
369 return TD->getPointerSize();
370370 }
371371
372372 unsigned MachineJumpTableInfo::getAlignment() const {
373 return TD.getPointerAlignment();
373 return TD->getPointerAlignment();
374374 }
375375
376376 void MachineJumpTableInfo::dump() const { print(std::cerr); }
399399 unsigned Offset = 0;
400400 if (!Constants.empty()) {
401401 Offset = Constants.back().Offset;
402 Offset += TD.getTypeSize(Constants.back().Val->getType());
402 Offset += TD->getTypeSize(Constants.back().Val->getType());
403403 Offset = (Offset+AlignMask)&~AlignMask;
404404 }
405405
19851985 // Otherwise, the target does not support this operation. Lower the
19861986 // operation to an explicit libcall as appropriate.
19871987 MVT::ValueType IntPtr = TLI.getPointerTy();
1988 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1988 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
19891989 std::vector > Args;
19901990
19911991 const char *FnName = 0;
27802780 // slots and always reusing the same one. We currently always create
27812781 // new ones, as reuse may inhibit scheduling.
27822782 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2783 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2784 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2783 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
2784 unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
27852785 MachineFunction &MF = DAG.getMachineFunction();
27862786 int SSFI =
27872787 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
129129 Align = 3; // always 8-byte align doubles.
130130 else {
131131 Align = TM.getTargetData()
132 .getTypeAlignmentShift(CP->get()->getType());
132 ->getTypeAlignmentShift(CP->get()->getType());
133133 if (Align == 0) {
134134 // Alignment of packed types. FIXME!
135 Align = TM.getTargetData().getTypeSize(CP->get()->getType());
135 Align = TM.getTargetData()->getTypeSize(CP->get()->getType());
136136 Align = Log2_64(Align);
137137 }
138138 }
224224 if (AllocaInst *AI = dyn_cast(I))
225225 if (ConstantUInt *CUI = dyn_cast(AI->getArraySize())) {
226226 const Type *Ty = AI->getAllocatedType();
227 uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
227 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
228228 unsigned Align =
229 std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty),
229 std::max((unsigned)TLI.getTargetData()->getTypeAlignment(Ty),
230230 AI->getAlignment());
231231
232232 // If the alignment of the value is smaller than the size of the value,
393393 // implemented with a libcall, etc.
394394 TargetLowering &TLI;
395395 SelectionDAG &DAG;
396 const TargetData &TD;
396 const TargetData *TD;
397397
398398 /// SwitchCases - Vector of CaseBlock structures used to communicate
399399 /// SwitchInst code generation information.
12011201 void SelectionDAGLowering::visitGetElementPtr(User &I) {
12021202 SDOperand N = getValue(I.getOperand(0));
12031203 const Type *Ty = I.getOperand(0)->getType();
1204 const Type *UIntPtrTy = TD.getIntPtrType();
1204 const Type *UIntPtrTy = TD->getIntPtrType();
12051205
12061206 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
12071207 OI != E; ++OI) {
12101210 unsigned Field = cast(Idx)->getValue();
12111211 if (Field) {
12121212 // N = N + Offset
1213 uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field];
1213 uint64_t Offset = TD->getStructLayout(StTy)->MemberOffsets[Field];
12141214 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
12151215 getIntPtrConstant(Offset));
12161216 }
12241224
12251225 uint64_t Offs;
12261226 if (ConstantSInt *CSI = dyn_cast(CI))
1227 Offs = (int64_t)TD.getTypeSize(Ty)*CSI->getValue();
1227 Offs = (int64_t)TD->getTypeSize(Ty)*CSI->getValue();
12281228 else
1229 Offs = TD.getTypeSize(Ty)*cast(CI)->getValue();
1229 Offs = TD->getTypeSize(Ty)*cast(CI)->getValue();
12301230 N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs));
12311231 continue;
12321232 }
12331233
12341234 // N = N + Idx * ElementSize;
1235 uint64_t ElementSize = TD.getTypeSize(Ty);
1235 uint64_t ElementSize = TD->getTypeSize(Ty);
12361236 SDOperand IdxN = getValue(Idx);
12371237
12381238 // If the index is smaller or larger than intptr_t, truncate or extend
12701270 return; // getValue will auto-populate this.
12711271
12721272 const Type *Ty = I.getAllocatedType();
1273 uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
1274 unsigned Align = std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty),
1273 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
1274 unsigned Align = std::max((unsigned)TLI.getTargetData()->getTypeAlignment(Ty),
12751275 I.getAlignment());
12761276
12771277 SDOperand AllocSize = getValue(I.getArraySize());
22662266 Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
22672267
22682268 // Scale the source by the type size.
2269 uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType());
2269 uint64_t ElementSize = TD->getTypeSize(I.getType()->getElementType());
22702270 Src = DAG.getNode(ISD::MUL, Src.getValueType(),
22712271 Src, getIntPtrConstant(ElementSize));
22722272
22732273 std::vector > Args;
2274 Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType()));
2274 Args.push_back(std::make_pair(Src, TLI.getTargetData()->getIntPtrType()));
22752275
22762276 std::pair Result =
22772277 TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true,
22842284 void SelectionDAGLowering::visitFree(FreeInst &I) {
22852285 std::vector > Args;
22862286 Args.push_back(std::make_pair(getValue(I.getOperand(0)),
2287 TLI.getTargetData().getIntPtrType()));
2287 TLI.getTargetData()->getIntPtrType()));
22882288 MVT::ValueType IntPtr = TLI.getPointerTy();
22892289 std::pair Result =
22902290 TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true,
27652765 /// stores that use it. In this case, decompose the GEP and move constant
27662766 /// indices into blocks that use it.
27672767 static void OptimizeGEPExpression(GetElementPtrInst *GEPI,
2768 const TargetData &TD) {
2768 const TargetData *TD) {
27692769 // If this GEP is only used inside the block it is defined in, there is no
27702770 // need to rewrite it.
27712771 bool isUsedOutsideDefBB = false;
27962796 // Otherwise, decompose the GEP instruction into multiplies and adds. Sum the
27972797 // constant offset (which we now know is non-zero) and deal with it later.
27982798 uint64_t ConstantOffset = 0;
2799 const Type *UIntPtrTy = TD.getIntPtrType();
2799 const Type *UIntPtrTy = TD->getIntPtrType();
28002800 Value *Ptr = new CastInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI);
28012801 const Type *Ty = GEPI->getOperand(0)->getType();
28022802
28062806 if (const StructType *StTy = dyn_cast(Ty)) {
28072807 unsigned Field = cast(Idx)->getValue();
28082808 if (Field)
2809 ConstantOffset += TD.getStructLayout(StTy)->MemberOffsets[Field];
2809 ConstantOffset += TD->getStructLayout(StTy)->MemberOffsets[Field];
28102810 Ty = StTy->getElementType(Field);
28112811 } else {
28122812 Ty = cast(Ty)->getElementType();
28162816 if (CI->getRawValue() == 0) continue;
28172817
28182818 if (ConstantSInt *CSI = dyn_cast(CI))
2819 ConstantOffset += (int64_t)TD.getTypeSize(Ty)*CSI->getValue();
2819 ConstantOffset += (int64_t)TD->getTypeSize(Ty)*CSI->getValue();
28202820 else
2821 ConstantOffset+=TD.getTypeSize(Ty)*cast(CI)->getValue();
2821 ConstantOffset+=TD->getTypeSize(Ty)*cast(CI)->getValue();
28222822 continue;
28232823 }
28242824
28272827 // Cast Idx to UIntPtrTy if needed.
28282828 Idx = new CastInst(Idx, UIntPtrTy, "", GEPI);
28292829
2830 uint64_t ElementSize = TD.getTypeSize(Ty);
2830 uint64_t ElementSize = TD->getTypeSize(Ty);
28312831 // Mask off bits that should not be set.
28322832 ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
28332833 Constant *SizeCst = ConstantUInt::get(UIntPtrTy, ElementSize);
2626 // All operations default to being supported.
2727 memset(OpActions, 0, sizeof(OpActions));
2828
29 IsLittleEndian = TD.isLittleEndian();
30 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
29 IsLittleEndian = TD->isLittleEndian();
30 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType());
3131 ShiftAmtHandling = Undefined;
3232 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
3333 memset(TargetDAGCombineArray, 0,
6969 //
7070 static void *CreateArgv(ExecutionEngine *EE,
7171 const std::vector &InputArgv) {
72 unsigned PtrSize = EE->getTargetData().getPointerSize();
72 unsigned PtrSize = EE->getTargetData()->getPointerSize();
7373 char *Result = new char[(InputArgv.size()+1)*PtrSize];
7474
7575 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
217217 uint64_t Offset =
218218 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
219219
220 if (getTargetData().getPointerSize() == 4)
220 if (getTargetData()->getPointerSize() == 4)
221221 Result.IntVal += Offset;
222222 else
223223 Result.LongVal += Offset;
334334 ///
335335 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
336336 const Type *Ty) {
337 if (getTargetData().isLittleEndian()) {
337 if (getTargetData()->isLittleEndian()) {
338338 switch (Ty->getTypeID()) {
339339 case Type::BoolTyID:
340340 case Type::UByteTyID:
351351 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
352352 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
353353 break;
354 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
354 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
355355 goto Store4BytesLittleEndian;
356356 case Type::DoubleTyID:
357357 case Type::ULongTyID:
385385 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
386386 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
387387 break;
388 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
388 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
389389 goto Store4BytesBigEndian;
390390 case Type::DoubleTyID:
391391 case Type::ULongTyID:
410410 GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
411411 const Type *Ty) {
412412 GenericValue Result;
413 if (getTargetData().isLittleEndian()) {
413 if (getTargetData()->isLittleEndian()) {
414414 switch (Ty->getTypeID()) {
415415 case Type::BoolTyID:
416416 case Type::UByteTyID:
427427 ((unsigned)Ptr->Untyped[2] << 16) |
428428 ((unsigned)Ptr->Untyped[3] << 24);
429429 break;
430 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
430 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
431431 goto Load4BytesLittleEndian;
432432 case Type::DoubleTyID:
433433 case Type::ULongTyID:
461461 ((unsigned)Ptr->Untyped[1] << 16) |
462462 ((unsigned)Ptr->Untyped[0] << 24);
463463 break;
464 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
464 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
465465 goto Load4BytesBigEndian;
466466 case Type::DoubleTyID:
467467 case Type::ULongTyID:
490490 return;
491491 } else if (const ConstantPacked *CP = dyn_cast(Init)) {
492492 unsigned ElementSize =
493 getTargetData().getTypeSize(CP->getType()->getElementType());
493 getTargetData()->getTypeSize(CP->getType()->getElementType());
494494 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
495495 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
496496 return;
499499 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
500500 return;
501501 } else if (isa(Init)) {
502 memset(Addr, 0, (size_t)getTargetData().getTypeSize(Init->getType()));
502 memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType()));
503503 return;
504504 }
505505
507507 case Type::ArrayTyID: {
508508 const ConstantArray *CPA = cast(Init);
509509 unsigned ElementSize =
510 getTargetData().getTypeSize(CPA->getType()->getElementType());
510 getTargetData()->getTypeSize(CPA->getType()->getElementType());
511511 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
512512 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
513513 return;
516516 case Type::StructTyID: {
517517 const ConstantStruct *CPS = cast(Init);
518518 const StructLayout *SL =
519 getTargetData().getStructLayout(cast(CPS->getType()));
519 getTargetData()->getStructLayout(cast(CPS->getType()));
520520 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
521521 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
522522 return;
533533 /// their initializers into the memory.
534534 ///
535535 void ExecutionEngine::emitGlobals() {
536 const TargetData &TD = getTargetData();
536 const TargetData *TD = getTargetData();
537537
538538 // Loop over all of the global variables in the program, allocating the memory
539539 // to hold them.
545545 const Type *Ty = I->getType()->getElementType();
546546
547547 // Allocate some memory for it!
548 unsigned Size = TD.getTypeSize(Ty);
548 unsigned Size = TD->getTypeSize(Ty);
549549 addGlobalMapping(I, new char[Size]);
550550 } else {
551551 // External variable reference. Try to use the dynamic loader to
576576 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
577577
578578 const Type *ElTy = GV->getType()->getElementType();
579 size_t GVSize = (size_t)getTargetData().getTypeSize(ElTy);
579 size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy);
580580 if (GA == 0) {
581581 // If it's not already specified, allocate memory for the global.
582582 GA = new char[GVSize];
7070 isLongPointer ? 8 : 4) {
7171
7272 memset(&ExitValue, 0, sizeof(ExitValue));
73 setTargetData(TD);
73 setTargetData(&TD);
7474 // Initialize the "backend"
7575 initializeExecutionEngine();
7676 initializeExternalFunctions();
301301 // actually initialize the global after current function has finished
302302 // compilation.
303303 const Type *GlobalType = GV->getType()->getElementType();
304 size_t S = getTargetData().getTypeSize(GlobalType);
305 size_t A = getTargetData().getTypeAlignment(GlobalType);
304 size_t S = getTargetData()->getTypeSize(GlobalType);
305 size_t A = getTargetData()->getTypeAlignment(GlobalType);
306306 if (A <= 8) {
307307 Ptr = malloc(S);
308308 } else {
517517 if (Constants.empty()) return;
518518
519519 unsigned Size = Constants.back().Offset;
520 Size += TheJIT->getTargetData().getTypeSize(Constants.back().Val->getType());
520 Size += TheJIT->getTargetData()->getTypeSize(Constants.back().Val->getType());
521521
522522 ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment());
523523 ConstantPool = MCP;
220220 }
221221
222222 bool AlphaAsmPrinter::doFinalization(Module &M) {
223 const TargetData &TD = TM.getTargetData();
223 const TargetData *TD = TM.getTargetData();
224224
225225 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
226226 if (I->hasInitializer()) { // External global require no code
231231 O << "\n\n";
232232 std::string name = Mang->getValueName(I);
233233 Constant *C = I->getInitializer();
234 unsigned Size = TD.getTypeSize(C->getType());
235 // unsigned Align = TD.getTypeAlignmentShift(C->getType());
234 unsigned Size = TD->getTypeSize(C->getType());
235 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
236236 unsigned Align = getPreferredAlignmentLog(I);
237237
238238 if (C->isNullValue() &&
242242 if (I->hasInternalLinkage())
243243 O << "\t.local " << name << "\n";
244244
245 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
245 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
246246 << "," << (1 << Align)
247247 << "\n";
248248 } else {
5353 }
5454
5555 AlphaTargetMachine::AlphaTargetMachine(const Module &M, const std::string &FS)
56 : TargetMachine("alpha", true),
56 : TargetMachine("alpha"),
57 DataLayout("alpha", true),
5758 FrameInfo(TargetFrameInfo::StackGrowsDown, 16, 0),
5859 JITInfo(*this),
5960 Subtarget(M, FS)
2525 class GlobalValue;
2626
2727 class AlphaTargetMachine : public TargetMachine {
28 const TargetData DataLayout; // Calculates type size & alignment
2829 AlphaInstrInfo InstrInfo;
2930 TargetFrameInfo FrameInfo;
3031 AlphaJITInfo JITInfo;
3132 AlphaSubtarget Subtarget;
32
33
3334 public:
3435 AlphaTargetMachine(const Module &M, const std::string &FS);
3536
3940 virtual const MRegisterInfo *getRegisterInfo() const {
4041 return &InstrInfo.getRegisterInfo();
4142 }
43 virtual const TargetData *getTargetData() const { return &DataLayout; }
4244 virtual TargetJITInfo* getJITInfo() {
4345 return &JITInfo;
4446 }
1818 namespace llvm {
1919
2020 struct CTargetMachine : public TargetMachine {
21 const TargetData DataLayout; // Calculates type size & alignment
22
2123 CTargetMachine(const Module &M, const std::string &FS)
22 : TargetMachine("CBackend", M) {}
24 : TargetMachine("CBackend", M),
25 DataLayout("CBackend") {}
2326
2427 // This is the only thing that actually does anything here.
2528 virtual bool addPassesToEmitFile(PassManager &PM, std::ostream &Out,
2730
2831 // This class always works, but shouldn't be the default in most cases.
2932 static unsigned getModuleMatchQuality(const Module &M) { return 1; }
33
34 virtual const TargetData *getTargetData() const { return &DataLayout; }
3035 };
3136
3237 } // End llvm namespace
276276 }
277277
278278 bool IA64AsmPrinter::doFinalization(Module &M) {
279 const TargetData &TD = TM.getTargetData();
279 const TargetData *TD = TM.getTargetData();
280280
281281 // Print out module-level global variables here.
282282 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
289289 O << "\n\n";
290290 std::string name = Mang->getValueName(I);
291291 Constant *C = I->getInitializer();
292 unsigned Size = TD.getTypeSize(C->getType());
293 unsigned Align = TD.getTypeAlignmentShift(C->getType());
292 unsigned Size = TD->getTypeSize(C->getType());
293 unsigned Align = TD->getTypeAlignmentShift(C->getType());
294294
295295 if (C->isNullValue() &&
296296 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
297297 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
298298 SwitchSection(".data", I);
299299 if (I->hasInternalLinkage()) {
300 O << "\t.lcomm " << name << "#," << TD.getTypeSize(C->getType())
300 O << "\t.lcomm " << name << "#," << TD->getTypeSize(C->getType())
301301 << "," << (1 << Align);
302302 O << "\t\t// ";
303303 } else {
304 O << "\t.common " << name << "#," << TD.getTypeSize(C->getType())
304 O << "\t.common " << name << "#," << TD->getTypeSize(C->getType())
305305 << "," << (1 << Align);
306306 O << "\t\t// ";
307307 }
7575 /// IA64TargetMachine ctor - Create an LP64 architecture model
7676 ///
7777 IA64TargetMachine::IA64TargetMachine(const Module &M, const std::string &FS)
78 : TargetMachine("IA64", true),
78 : TargetMachine("IA64"), DataLayout("IA64", true),
7979 FrameInfo(TargetFrameInfo::StackGrowsDown, 16, 0),
8080 TLInfo(*this) { // FIXME? check this stuff
8181 }
2222 namespace llvm {
2323
2424 class IA64TargetMachine : public TargetMachine {
25 const TargetData DataLayout; // Calculates type size & alignment
2526 IA64InstrInfo InstrInfo;
2627 TargetFrameInfo FrameInfo;
2728 //IA64JITInfo JITInfo;
3536 virtual const MRegisterInfo *getRegisterInfo() const {
3637 return &InstrInfo.getRegisterInfo();
3738 }
39 virtual const TargetData *getTargetData() const { return &DataLayout; }
3840
3941 virtual bool addPassesToEmitFile(PassManager &PM, std::ostream &Out,
4042 CodeGenFileType FileType, bool Fast);
581581 }
582582
583583 bool DarwinAsmPrinter::doFinalization(Module &M) {
584 const TargetData &TD = TM.getTargetData();
584 const TargetData *TD = TM.getTargetData();
585585
586586 // Print out module-level global variables here.
587587 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
594594
595595 std::string name = Mang->getValueName(I);
596596 Constant *C = I->getInitializer();
597 unsigned Size = TD.getTypeSize(C->getType());
597 unsigned Size = TD->getTypeSize(C->getType());
598598 unsigned Align = getPreferredAlignmentLog(I);
599599
600600 if (C->isNullValue() && /* FIXME: Verify correct */
760760
761761 bool AIXAsmPrinter::doInitialization(Module &M) {
762762 SwitchSection("", 0);
763 const TargetData &TD = TM.getTargetData();
763 const TargetData *TD = TM.getTargetData();
764764
765765 O << "\t.machine \"ppc64\"\n"
766766 << "\t.toc\n"
809809 }
810810
811811 bool AIXAsmPrinter::doFinalization(Module &M) {
812 const TargetData &TD = TM.getTargetData();
812 const TargetData *TD = TM.getTargetData();
813813 // Print out module-level global variables
814814 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
815815 I != E; ++I) {
820820 if (I->hasInternalLinkage()) {
821821 O << "\t.lcomm " << Name << ",16,_global.bss_c";
822822 } else {
823 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
824 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
823 O << "\t.comm " << Name << "," << TD->getTypeSize(I->getType())
824 << "," << Log2_32((unsigned)TD->getTypeAlignment(I->getType()));
825825 }
826826 O << "\t\t" << CommentString << " ";
827827 WriteAsOperand(O, I, false, true, &M);
5757 }
5858
5959 PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS)
60 : TargetMachine("PowerPC", false, 4, 4, 4, 4, 4, 4, 2, 1, 1),
60 : TargetMachine("PowerPC"),
61 DataLayout("PowerPC", false, 4, 4, 4, 4, 4),
6162 Subtarget(M, FS), FrameInfo(*this, false), JITInfo(*this),
6263 TLInfo(*this), InstrItins(Subtarget.getInstrItineraryData()) {
6364 if (TargetDefault == PPCTarget) {
2525 class GlobalValue;
2626
2727 class PPCTargetMachine : public TargetMachine {
28 const TargetData DataLayout; // Calculates type size & alignment
2829 PPCInstrInfo InstrInfo;
2930 PPCSubtarget Subtarget;
3031 PPCFrameInfo FrameInfo;
4243 virtual const MRegisterInfo *getRegisterInfo() const {
4344 return &InstrInfo.getRegisterInfo();
4445 }
46 virtual const TargetData *getTargetData() const { return &DataLayout; }
4547 virtual const InstrItineraryData getInstrItineraryData() const {
4648 return InstrItins;
4749 }
231231 }
232232
233233 bool SparcAsmPrinter::doFinalization(Module &M) {
234 const TargetData &TD = TM.getTargetData();
234 const TargetData *TD = TM.getTargetData();
235235
236236 // Print out module-level global variables here.
237237 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
244244 O << "\n\n";
245245 std::string name = Mang->getValueName(I);
246246 Constant *C = I->getInitializer();
247 unsigned Size = TD.getTypeSize(C->getType());
248 unsigned Align = TD.getTypeAlignment(C->getType());
247 unsigned Size = TD->getTypeSize(C->getType());
248 unsigned Align = TD->getTypeAlignment(C->getType());
249249
250250 if (C->isNullValue() &&
251251 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
254254 if (I->hasInternalLinkage())
255255 O << "\t.local " << name << "\n";
256256
257 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
258 << "," << (unsigned)TD.getTypeAlignment(C->getType());
257 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
258 << "," << (unsigned)TD->getTypeAlignment(C->getType());
259259 O << "\t\t! ";
260260 WriteAsOperand(O, I, true, true, &M);
261261 O << "\n";
3030 /// SparcTargetMachine ctor - Create an ILP32 architecture model
3131 ///
3232 SparcTargetMachine::SparcTargetMachine(const Module &M, const std::string &FS)
33 : TargetMachine("Sparc", false, 4, 4),
33 : TargetMachine("Sparc"),
34 DataLayout("Sparc", false, 4, 4),
3435 Subtarget(M, FS), InstrInfo(Subtarget),
3536 FrameInfo(TargetFrameInfo::StackGrowsDown, 8, 0) {
3637 }
2424 class Module;
2525
2626 class SparcTargetMachine : public TargetMachine {
27 const TargetData DataLayout; // Calculates type size & alignment
2728 SparcSubtarget Subtarget;
2829 SparcInstrInfo InstrInfo;
2930 TargetFrameInfo FrameInfo;
3637 virtual const MRegisterInfo *getRegisterInfo() const {
3738 return &InstrInfo.getRegisterInfo();
3839 }
39
40 virtual const TargetData *getTargetData() const { return &DataLayout; }
4041 static unsigned getModuleMatchQuality(const Module &M);
4142
4243 virtual bool addPassesToEmitFile(PassManager &PM, std::ostream &Out,
6868 //---------------------------------------------------------------------------
6969 // TargetMachine Class
7070 //
71 TargetMachine::TargetMachine(const std::string &name, bool LittleEndian,
72 unsigned char PtrSize, unsigned char PtrAl,
73 unsigned char DoubleAl, unsigned char FloatAl,
74 unsigned char LongAl, unsigned char IntAl,
75 unsigned char ShortAl, unsigned char ByteAl,
76 unsigned char BoolAl)
77 : Name(name), DataLayout(name, LittleEndian,
78 PtrSize, PtrAl, DoubleAl, FloatAl, LongAl,
79 IntAl, ShortAl, ByteAl, BoolAl) {
80 }
81
82 TargetMachine::TargetMachine(const std::string &name, const TargetData &TD)
83 : Name(name), DataLayout(TD) {
84 }
8571
8672 TargetMachine::TargetMachine(const std::string &name, const Module &M)
87 : Name(name), DataLayout(name, &M) {
73 : Name(name) {
8874 }
8975
9076 TargetMachine::~TargetMachine() {
8383 }
8484
8585 bool X86SharedAsmPrinter::doFinalization(Module &M) {
86 const TargetData &TD = TM.getTargetData();
86 const TargetData *TD = TM.getTargetData();
8787
8888 // Print out module-level global variables here.
8989 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
9696
9797 std::string name = Mang->getValueName(I);
9898 Constant *C = I->getInitializer();
99 unsigned Size = TD.getTypeSize(C->getType());
99 unsigned Size = TD->getTypeSize(C->getType());
100100 unsigned Align = getPreferredAlignmentLog(I);
101101
102102 if (C->isNullValue() && /* FIXME: Verify correct */
35213521 if ((Align & 3) != 0 ||
35223522 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
35233523 MVT::ValueType IntPtr = getPointerTy();
3524 const Type *IntPtrTy = getTargetData().getIntPtrType();
3524 const Type *IntPtrTy = getTargetData()->getIntPtrType();
35253525 std::vector > Args;
35263526 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
35273527 // Extend the ubyte argument to be an int value for the call.
36543654 if ((Align & 3) != 0 ||
36553655 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
36563656 MVT::ValueType IntPtr = getPointerTy();
3657 const Type *IntPtrTy = getTargetData().getIntPtrType();
3657 const Type *IntPtrTy = getTargetData()->getIntPtrType();
36583658 std::vector > Args;
36593659 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
36603660 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
6767 /// X86TargetMachine ctor - Create an ILP32 architecture model
6868 ///
6969 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS)
70 : TargetMachine("X86", true, 4, 4, 4, 4, 4),
70 : TargetMachine("X86"),
71 DataLayout("X86", true, 4, 4, 4, 4, 4),
7172 Subtarget(M, FS),
7273 FrameInfo(TargetFrameInfo::StackGrowsDown,
7374 Subtarget.getStackAlignment(), -4),
2525 namespace llvm {
2626
2727 class X86TargetMachine : public TargetMachine {
28 const TargetData DataLayout; // Calculates type size & alignment
2829 X86InstrInfo InstrInfo;
2930 X86Subtarget Subtarget;
3031 TargetFrameInfo FrameInfo;
4142 virtual const MRegisterInfo *getRegisterInfo() const {
4243 return &InstrInfo.getRegisterInfo();
4344 }
45 virtual const TargetData *getTargetData() const { return &DataLayout; }
4446
4547 virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM,
4648 MachineCodeEmitter &MCE);
142142 std::auto_ptr target(MArch->CtorFn(mod, FeaturesStr));
143143 assert(target.get() && "Could not allocate target machine!");
144144 TargetMachine &Target = *target.get();
145 const TargetData &TD = Target.getTargetData();
145 const TargetData *TD = Target.getTargetData();
146146
147147 // Build up all of the passes that we want to do to the module...
148148 PassManager Passes;