blob: c1823947b1dcca9da6c82864277ffe159805632e [file] [log] [blame]
Jim Laskey6da18642007-01-26 21:38:26 +00001//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
Jim Laskey6af56812006-01-04 13:36:38 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey6af56812006-01-04 13:36:38 +00007//
8//===----------------------------------------------------------------------===//
Jim Laskey6af56812006-01-04 13:36:38 +00009
Jim Laskey6da18642007-01-26 21:38:26 +000010#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey6af56812006-01-04 13:36:38 +000011
Jim Laskeyb3e789a2006-01-26 20:21:46 +000012#include "llvm/Constants.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000013#include "llvm/CodeGen/MachineFunctionPass.h"
14#include "llvm/CodeGen/MachineFunction.h"
Jim Laskey41886992006-04-07 16:34:46 +000015#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000016#include "llvm/Target/TargetInstrInfo.h"
17#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000018#include "llvm/Target/TargetOptions.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000019#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000020#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000021#include "llvm/Intrinsics.h"
22#include "llvm/Instructions.h"
23#include "llvm/Module.h"
24#include "llvm/Support/Dwarf.h"
Bill Wendling832171c2006-12-07 20:04:42 +000025#include "llvm/Support/Streams.h"
Jim Laskey6af56812006-01-04 13:36:38 +000026using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000027using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000028
29// Handle the Pass registration stuff necessary to use TargetData's.
30namespace {
Jim Laskey6da18642007-01-26 21:38:26 +000031 RegisterPass<MachineModuleInfo> X("machinemoduleinfo", "Module Information");
Jim Laskeyb2efb852006-01-04 22:28:25 +000032}
Devang Patel19974732007-05-03 01:11:54 +000033char MachineModuleInfo::ID = 0;
Jim Laskey063e7652006-01-17 17:31:53 +000034
Jim Laskeyb3e789a2006-01-26 20:21:46 +000035//===----------------------------------------------------------------------===//
36
Jim Laskey86cbdba2006-02-06 15:33:21 +000037/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000038/// specified value in their initializer somewhere.
39static void
40getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
41 // Scan though value users.
42 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
43 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000044 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000045 Result.push_back(GV);
46 } else if (Constant *C = dyn_cast<Constant>(*I)) {
47 // If the user is a constant variable then scan its users
48 getGlobalVariablesUsing(C, Result);
49 }
50 }
51}
52
Jim Laskey86cbdba2006-02-06 15:33:21 +000053/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
54/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000055static std::vector<GlobalVariable*>
56getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000057 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000058
59 std::vector<const Type*> FieldTypes;
Reid Spencer47857812006-12-31 05:55:36 +000060 FieldTypes.push_back(Type::Int32Ty);
61 FieldTypes.push_back(Type::Int32Ty);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000062
Jim Laskey86cbdba2006-02-06 15:33:21 +000063 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000064 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000065 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000066
67 // If present and linkonce then scan for users.
68 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
69 getGlobalVariablesUsing(UseRoot, Result);
70 }
71
72 return Result;
73}
74
Jim Laskey86cbdba2006-02-06 15:33:21 +000075/// isStringValue - Return true if the given value can be coerced to a string.
76///
77static bool isStringValue(Value *V) {
78 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
79 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
80 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
81 return Init->isString();
82 }
83 } else if (Constant *C = dyn_cast<Constant>(V)) {
84 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
85 return isStringValue(GV);
86 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
87 if (CE->getOpcode() == Instruction::GetElementPtr) {
88 if (CE->getNumOperands() == 3 &&
89 cast<Constant>(CE->getOperand(1))->isNullValue() &&
90 isa<ConstantInt>(CE->getOperand(2))) {
91 return isStringValue(CE->getOperand(0));
92 }
93 }
94 }
95 }
96 return false;
97}
98
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000099/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000100///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000101static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000102 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
103 return GV;
104 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000105 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000106 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000107 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
108 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
109 Constant* CI = cast<Constant>(CE->getOperand(i));
110 if (!CI || !CI->isNullValue())
111 return NULL;
112 }
113 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000114 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000115 }
116 return NULL;
117}
118
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000119/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000120/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000121static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000122 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
123 return true;
124 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000125 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000126 return isa<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000127 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
128 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
129 Constant* CI = cast<Constant>(CE->getOperand(i));
130 if (!CI || !CI->isNullValue())
131 return false;
132 }
133 return isa<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000134 }
135 }
136 return false;
137}
138
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000139/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000140///
Reid Spencerb83eb642006-10-20 07:07:24 +0000141static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000142 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000143 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000144
Jim Laskey86cbdba2006-02-06 15:33:21 +0000145 // Get the initializer constant.
146 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000147 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000148
Jim Laskey86cbdba2006-02-06 15:33:21 +0000149 // Check if there is at least i + 1 operands.
150 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000151 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000152
Jim Laskey86cbdba2006-02-06 15:33:21 +0000153 // Check constant.
Reid Spencerb83eb642006-10-20 07:07:24 +0000154 return dyn_cast<ConstantInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000155}
Reid Spencerb83eb642006-10-20 07:07:24 +0000156
Jim Laskey86cbdba2006-02-06 15:33:21 +0000157//===----------------------------------------------------------------------===//
158
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000159/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000160/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000161void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000162 DD->ApplyToFields(this);
163}
164
165//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000166/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
167/// the supplied DebugInfoDesc.
168class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000169private:
170 unsigned Count; // Running count of fields.
171
172public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000173 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000174
175 // Accessors.
176 unsigned getCount() const { return Count; }
177
178 /// Apply - Count each of the fields.
179 ///
180 virtual void Apply(int &Field) { ++Count; }
181 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000182 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000183 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000184 virtual void Apply(bool &Field) { ++Count; }
185 virtual void Apply(std::string &Field) { ++Count; }
186 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
187 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000188 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
189 ++Count;
190 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000191};
192
193//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000194/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
195/// supplied DebugInfoDesc.
196class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000197private:
198 DIDeserializer &DR; // Active deserializer.
199 unsigned I; // Current operand index.
200 ConstantStruct *CI; // GlobalVariable constant initializer.
201
202public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000203 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
204 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000205 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000206 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000207 , CI(cast<ConstantStruct>(GV->getInitializer()))
208 {}
209
210 /// Apply - Set the value of each of the fields.
211 ///
212 virtual void Apply(int &Field) {
213 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000214 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000215 }
216 virtual void Apply(unsigned &Field) {
217 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000218 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000219 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000220 virtual void Apply(int64_t &Field) {
221 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000222 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000223 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000224 virtual void Apply(uint64_t &Field) {
225 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000226 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000227 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000228 virtual void Apply(bool &Field) {
229 Constant *C = CI->getOperand(I++);
Reid Spencer579dca12007-01-12 04:24:46 +0000230 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000231 }
232 virtual void Apply(std::string &Field) {
233 Constant *C = CI->getOperand(I++);
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000234 Field = C->getStringValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000235 }
236 virtual void Apply(DebugInfoDesc *&Field) {
237 Constant *C = CI->getOperand(I++);
238 Field = DR.Deserialize(C);
239 }
240 virtual void Apply(GlobalVariable *&Field) {
241 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000242 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000243 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000244 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000245 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000246 Constant *C = CI->getOperand(I++);
247 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000248 if (GV->hasInitializer()) {
249 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
250 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
251 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
252 DebugInfoDesc *DE = DR.Deserialize(GVE);
253 Field.push_back(DE);
254 }
255 } else if (GV->getInitializer()->isNullValue()) {
256 if (const ArrayType *T =
257 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
258 Field.resize(T->getNumElements());
259 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000260 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000261 }
262 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000263};
264
265//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000266/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000267/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000268class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000269private:
270 DISerializer &SR; // Active serializer.
271 std::vector<Constant*> &Elements; // Element accumulator.
272
273public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000274 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
275 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000276 , SR(S)
277 , Elements(E)
278 {}
279
280 /// Apply - Set the value of each of the fields.
281 ///
282 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000283 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000284 }
285 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000286 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000287 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000288 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000289 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000290 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000291 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000292 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000293 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000294 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000295 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000296 }
297 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000298 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000299 }
300 virtual void Apply(DebugInfoDesc *&Field) {
301 GlobalVariable *GV = NULL;
302
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000303 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000304 if (Field) GV = SR.Serialize(Field);
305
306 // FIXME - At some point should use specific type.
307 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
308
309 if (GV) {
310 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000311 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000312 } else {
313 // Use NULL.
314 Elements.push_back(ConstantPointerNull::get(EmptyTy));
315 }
316 }
317 virtual void Apply(GlobalVariable *&Field) {
318 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000319 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000320 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000321 } else {
322 Elements.push_back(ConstantPointerNull::get(EmptyTy));
323 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000324 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000325 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
326 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
327 unsigned N = Field.size();
328 ArrayType *AT = ArrayType::get(EmptyTy, N);
329 std::vector<Constant *> ArrayElements;
330
331 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000332 if (DebugInfoDesc *Element = Field[i]) {
333 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000334 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000335 ArrayElements.push_back(cast<Constant>(CE));
336 } else {
337 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
338 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000339 }
340
341 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000342 GlobalVariable *CAGV = new GlobalVariable(AT, true,
343 GlobalValue::InternalLinkage,
344 CA, "llvm.dbg.array",
345 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000346 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000347 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000348 Elements.push_back(CAE);
349 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000350};
351
352//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000353/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000354/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000355class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000356private:
357 DISerializer &SR; // Active serializer.
358 std::vector<const Type*> &Fields; // Type accumulator.
359
360public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000361 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
362 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000363 , SR(S)
364 , Fields(F)
365 {}
366
367 /// Apply - Set the value of each of the fields.
368 ///
369 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000370 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000371 }
372 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000373 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000374 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000375 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000376 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000377 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000378 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000379 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000380 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000381 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000382 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000383 }
384 virtual void Apply(std::string &Field) {
385 Fields.push_back(SR.getStrPtrType());
386 }
387 virtual void Apply(DebugInfoDesc *&Field) {
388 // FIXME - At some point should use specific type.
389 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
390 Fields.push_back(EmptyTy);
391 }
392 virtual void Apply(GlobalVariable *&Field) {
393 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
394 Fields.push_back(EmptyTy);
395 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000396 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
397 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
398 Fields.push_back(EmptyTy);
399 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000400};
401
402//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000403/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000404/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000405class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000406private:
407 DIVerifier &VR; // Active verifier.
408 bool IsValid; // Validity status.
409 unsigned I; // Current operand index.
410 ConstantStruct *CI; // GlobalVariable constant initializer.
411
412public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000413 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
414 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000415 , VR(V)
416 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000417 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000418 , CI(cast<ConstantStruct>(GV->getInitializer()))
419 {
420 }
421
422 // Accessors.
423 bool isValid() const { return IsValid; }
424
425 /// Apply - Set the value of each of the fields.
426 ///
427 virtual void Apply(int &Field) {
428 Constant *C = CI->getOperand(I++);
429 IsValid = IsValid && isa<ConstantInt>(C);
430 }
431 virtual void Apply(unsigned &Field) {
432 Constant *C = CI->getOperand(I++);
433 IsValid = IsValid && isa<ConstantInt>(C);
434 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000435 virtual void Apply(int64_t &Field) {
436 Constant *C = CI->getOperand(I++);
437 IsValid = IsValid && isa<ConstantInt>(C);
438 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000439 virtual void Apply(uint64_t &Field) {
440 Constant *C = CI->getOperand(I++);
441 IsValid = IsValid && isa<ConstantInt>(C);
442 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000443 virtual void Apply(bool &Field) {
444 Constant *C = CI->getOperand(I++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000445 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000446 }
447 virtual void Apply(std::string &Field) {
448 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000449 IsValid = IsValid &&
450 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000451 }
452 virtual void Apply(DebugInfoDesc *&Field) {
453 // FIXME - Prepare the correct descriptor.
454 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000455 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000456 }
457 virtual void Apply(GlobalVariable *&Field) {
458 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000459 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000460 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000461 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
462 Constant *C = CI->getOperand(I++);
463 IsValid = IsValid && isGlobalVariable(C);
464 if (!IsValid) return;
465
466 GlobalVariable *GV = getGlobalVariable(C);
467 IsValid = IsValid && GV && GV->hasInitializer();
468 if (!IsValid) return;
469
470 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
471 IsValid = IsValid && CA;
472 if (!IsValid) return;
473
474 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
475 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
476 if (!IsValid) return;
477
478 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
479 VR.Verify(GVE);
480 }
481 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000482};
483
Jim Laskeyce72b172006-02-11 01:01:30 +0000484
Jim Laskey86cbdba2006-02-06 15:33:21 +0000485//===----------------------------------------------------------------------===//
486
Jim Laskeyed4e5662006-06-14 14:45:39 +0000487/// TagFromGlobal - Returns the tag number from a debug info descriptor
488/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000489unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000490 ConstantInt *C = getUIntOperand(GV, 0);
491 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000492 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000493}
494
495/// VersionFromGlobal - Returns the version number from a debug info
496/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
497/// int.
498unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000499 ConstantInt *C = getUIntOperand(GV, 0);
500 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000501 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000502}
503
504/// DescFactory - Create an instance of debug info descriptor based on Tag.
505/// Return NULL if not a recognized Tag.
506DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
507 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000508 case DW_TAG_anchor: return new AnchorDesc();
509 case DW_TAG_compile_unit: return new CompileUnitDesc();
510 case DW_TAG_variable: return new GlobalVariableDesc();
511 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000512 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000513 case DW_TAG_base_type: return new BasicTypeDesc();
514 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000515 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000516 case DW_TAG_reference_type:
517 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000518 case DW_TAG_volatile_type:
519 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000520 case DW_TAG_member:
521 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000522 case DW_TAG_array_type:
523 case DW_TAG_structure_type:
524 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000525 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000526 case DW_TAG_vector_type:
527 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000528 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000529 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000530 case DW_TAG_return_variable:
531 case DW_TAG_arg_variable:
532 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000533 default: break;
534 }
535 return NULL;
536}
537
538/// getLinkage - get linkage appropriate for this type of descriptor.
539///
540GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
541 return GlobalValue::InternalLinkage;
542}
543
544/// ApplyToFields - Target the vistor to the fields of the descriptor.
545///
546void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
547 Visitor->Apply(Tag);
548}
549
550//===----------------------------------------------------------------------===//
551
Jim Laskey9c4447a2006-03-01 20:39:36 +0000552AnchorDesc::AnchorDesc()
553: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000554, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000555{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000556AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000557: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000558, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000559{}
560
561// Implement isa/cast/dyncast.
562bool AnchorDesc::classof(const DebugInfoDesc *D) {
563 return D->getTag() == DW_TAG_anchor;
564}
565
Jim Laskeyce72b172006-02-11 01:01:30 +0000566/// getLinkage - get linkage appropriate for this type of descriptor.
567///
568GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
569 return GlobalValue::LinkOnceLinkage;
570}
571
572/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
573///
574void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
575 DebugInfoDesc::ApplyToFields(Visitor);
576
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000577 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000578}
579
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000580/// getDescString - Return a string used to compose global names and labels. A
581/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000582/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000583/// to the list of names left external in the internalizer.
584/// ExternalNames.insert("llvm.dbg.compile_units");
585/// ExternalNames.insert("llvm.dbg.global_variables");
586/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000587const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000588 switch (AnchorTag) {
589 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
590 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
591 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
592 default: break;
593 }
594
595 assert(0 && "Tag does not have a case for anchor string");
596 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000597}
598
599/// getTypeString - Return a string used to label this descriptors type.
600///
601const char *AnchorDesc::getTypeString() const {
602 return "llvm.dbg.anchor.type";
603}
604
605#ifndef NDEBUG
606void AnchorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000607 cerr << getDescString() << " "
608 << "Version(" << getVersion() << "), "
609 << "Tag(" << getTag() << "), "
610 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000611}
612#endif
613
614//===----------------------------------------------------------------------===//
615
616AnchoredDesc::AnchoredDesc(unsigned T)
617: DebugInfoDesc(T)
618, Anchor(NULL)
619{}
620
621/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
622///
623void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
624 DebugInfoDesc::ApplyToFields(Visitor);
625
Jim Laskey7089f452006-06-16 13:14:03 +0000626 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000627}
628
629//===----------------------------------------------------------------------===//
630
631CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000632: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000633, Language(0)
634, FileName("")
635, Directory("")
636, Producer("")
637{}
638
Jim Laskey9c4447a2006-03-01 20:39:36 +0000639// Implement isa/cast/dyncast.
640bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
641 return D->getTag() == DW_TAG_compile_unit;
642}
643
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000644/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
645///
646void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000647 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000648
649 // Handle cases out of sync with compiler.
650 if (getVersion() == 0) {
651 unsigned DebugVersion;
652 Visitor->Apply(DebugVersion);
653 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000654
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000655 Visitor->Apply(Language);
656 Visitor->Apply(FileName);
657 Visitor->Apply(Directory);
658 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000659}
660
Jim Laskeyce72b172006-02-11 01:01:30 +0000661/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000662///
Jim Laskeyce72b172006-02-11 01:01:30 +0000663const char *CompileUnitDesc::getDescString() const {
664 return "llvm.dbg.compile_unit";
665}
666
667/// getTypeString - Return a string used to label this descriptors type.
668///
669const char *CompileUnitDesc::getTypeString() const {
670 return "llvm.dbg.compile_unit.type";
671}
672
673/// getAnchorString - Return a string used to label this descriptor's anchor.
674///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000675const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000676const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000677 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000678}
679
Jim Laskey86cbdba2006-02-06 15:33:21 +0000680#ifndef NDEBUG
681void CompileUnitDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000682 cerr << getDescString() << " "
683 << "Version(" << getVersion() << "), "
684 << "Tag(" << getTag() << "), "
685 << "Anchor(" << getAnchor() << "), "
686 << "Language(" << Language << "), "
687 << "FileName(\"" << FileName << "\"), "
688 << "Directory(\"" << Directory << "\"), "
689 << "Producer(\"" << Producer << "\")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000690}
691#endif
692
693//===----------------------------------------------------------------------===//
694
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000695TypeDesc::TypeDesc(unsigned T)
696: DebugInfoDesc(T)
697, Context(NULL)
698, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000699, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000700, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000701, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000702, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000703, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000704, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000705{}
706
Jim Laskey69906002006-02-24 16:46:40 +0000707/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000708///
709void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
710 DebugInfoDesc::ApplyToFields(Visitor);
711
712 Visitor->Apply(Context);
713 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000714 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000715 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000716 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000717 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000718 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000719 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000720}
721
722/// getDescString - Return a string used to compose global names and labels.
723///
724const char *TypeDesc::getDescString() const {
725 return "llvm.dbg.type";
726}
727
728/// getTypeString - Return a string used to label this descriptor's type.
729///
730const char *TypeDesc::getTypeString() const {
731 return "llvm.dbg.type.type";
732}
733
734#ifndef NDEBUG
735void TypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000736 cerr << getDescString() << " "
737 << "Version(" << getVersion() << "), "
738 << "Tag(" << getTag() << "), "
739 << "Context(" << Context << "), "
740 << "Name(\"" << Name << "\"), "
741 << "File(" << File << "), "
742 << "Line(" << Line << "), "
743 << "Size(" << Size << "), "
744 << "Align(" << Align << "), "
745 << "Offset(" << Offset << "), "
746 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000747}
748#endif
749
750//===----------------------------------------------------------------------===//
751
752BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000753: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000754, Encoding(0)
755{}
756
Jim Laskey9c4447a2006-03-01 20:39:36 +0000757// Implement isa/cast/dyncast.
758bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
759 return D->getTag() == DW_TAG_base_type;
760}
761
Jim Laskey69906002006-02-24 16:46:40 +0000762/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000763///
764void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
765 TypeDesc::ApplyToFields(Visitor);
766
767 Visitor->Apply(Encoding);
768}
769
Jim Laskeyf8913f12006-03-01 17:53:02 +0000770/// getDescString - Return a string used to compose global names and labels.
771///
772const char *BasicTypeDesc::getDescString() const {
773 return "llvm.dbg.basictype";
774}
775
776/// getTypeString - Return a string used to label this descriptor's type.
777///
778const char *BasicTypeDesc::getTypeString() const {
779 return "llvm.dbg.basictype.type";
780}
781
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000782#ifndef NDEBUG
783void BasicTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000784 cerr << getDescString() << " "
785 << "Version(" << getVersion() << "), "
786 << "Tag(" << getTag() << "), "
787 << "Context(" << getContext() << "), "
788 << "Name(\"" << getName() << "\"), "
789 << "Size(" << getSize() << "), "
790 << "Encoding(" << Encoding << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000791}
792#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000793
Jim Laskey434b40b2006-02-23 22:37:30 +0000794//===----------------------------------------------------------------------===//
795
Jim Laskey69906002006-02-24 16:46:40 +0000796DerivedTypeDesc::DerivedTypeDesc(unsigned T)
797: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000798, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000799{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000800
Jim Laskey9c4447a2006-03-01 20:39:36 +0000801// Implement isa/cast/dyncast.
802bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
803 unsigned T = D->getTag();
804 switch (T) {
805 case DW_TAG_typedef:
806 case DW_TAG_pointer_type:
807 case DW_TAG_reference_type:
808 case DW_TAG_const_type:
809 case DW_TAG_volatile_type:
810 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000811 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000812 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000813 return true;
814 default: break;
815 }
816 return false;
817}
818
Jim Laskey69906002006-02-24 16:46:40 +0000819/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000820///
Jim Laskey69906002006-02-24 16:46:40 +0000821void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000822 TypeDesc::ApplyToFields(Visitor);
823
Jim Laskey7089f452006-06-16 13:14:03 +0000824 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000825}
826
Jim Laskeyf8913f12006-03-01 17:53:02 +0000827/// getDescString - Return a string used to compose global names and labels.
828///
829const char *DerivedTypeDesc::getDescString() const {
830 return "llvm.dbg.derivedtype";
831}
832
833/// getTypeString - Return a string used to label this descriptor's type.
834///
835const char *DerivedTypeDesc::getTypeString() const {
836 return "llvm.dbg.derivedtype.type";
837}
838
Jim Laskey434b40b2006-02-23 22:37:30 +0000839#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000840void DerivedTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000841 cerr << getDescString() << " "
842 << "Version(" << getVersion() << "), "
843 << "Tag(" << getTag() << "), "
844 << "Context(" << getContext() << "), "
845 << "Name(\"" << getName() << "\"), "
846 << "Size(" << getSize() << "), "
847 << "File(" << getFile() << "), "
848 << "Line(" << getLine() << "), "
849 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000850}
851#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000852
853//===----------------------------------------------------------------------===//
854
Jim Laskeyf8913f12006-03-01 17:53:02 +0000855CompositeTypeDesc::CompositeTypeDesc(unsigned T)
856: DerivedTypeDesc(T)
857, Elements()
858{}
859
Jim Laskey9c4447a2006-03-01 20:39:36 +0000860// Implement isa/cast/dyncast.
861bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
862 unsigned T = D->getTag();
863 switch (T) {
864 case DW_TAG_array_type:
865 case DW_TAG_structure_type:
866 case DW_TAG_union_type:
867 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000868 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000869 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000870 return true;
871 default: break;
872 }
873 return false;
874}
875
Jim Laskeyf8913f12006-03-01 17:53:02 +0000876/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
877///
878void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000879 DerivedTypeDesc::ApplyToFields(Visitor);
880
Jim Laskeyf8913f12006-03-01 17:53:02 +0000881 Visitor->Apply(Elements);
882}
883
884/// getDescString - Return a string used to compose global names and labels.
885///
886const char *CompositeTypeDesc::getDescString() const {
887 return "llvm.dbg.compositetype";
888}
889
890/// getTypeString - Return a string used to label this descriptor's type.
891///
892const char *CompositeTypeDesc::getTypeString() const {
893 return "llvm.dbg.compositetype.type";
894}
895
896#ifndef NDEBUG
897void CompositeTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000898 cerr << getDescString() << " "
899 << "Version(" << getVersion() << "), "
900 << "Tag(" << getTag() << "), "
901 << "Context(" << getContext() << "), "
902 << "Name(\"" << getName() << "\"), "
903 << "Size(" << getSize() << "), "
904 << "File(" << getFile() << "), "
905 << "Line(" << getLine() << "), "
906 << "FromType(" << getFromType() << "), "
907 << "Elements.size(" << Elements.size() << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000908}
909#endif
910
911//===----------------------------------------------------------------------===//
912
913SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000914: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000915, Lo(0)
916, Hi(0)
917{}
918
Jim Laskey9c4447a2006-03-01 20:39:36 +0000919// Implement isa/cast/dyncast.
920bool SubrangeDesc::classof(const DebugInfoDesc *D) {
921 return D->getTag() == DW_TAG_subrange_type;
922}
923
Jim Laskeyf8913f12006-03-01 17:53:02 +0000924/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
925///
926void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
927 DebugInfoDesc::ApplyToFields(Visitor);
928
929 Visitor->Apply(Lo);
930 Visitor->Apply(Hi);
931}
932
933/// getDescString - Return a string used to compose global names and labels.
934///
935const char *SubrangeDesc::getDescString() const {
936 return "llvm.dbg.subrange";
937}
938
939/// getTypeString - Return a string used to label this descriptor's type.
940///
941const char *SubrangeDesc::getTypeString() const {
942 return "llvm.dbg.subrange.type";
943}
944
945#ifndef NDEBUG
946void SubrangeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000947 cerr << getDescString() << " "
948 << "Version(" << getVersion() << "), "
949 << "Tag(" << getTag() << "), "
950 << "Lo(" << Lo << "), "
951 << "Hi(" << Hi << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000952}
953#endif
954
955//===----------------------------------------------------------------------===//
956
Jim Laskey6a3eb012006-03-01 23:52:37 +0000957EnumeratorDesc::EnumeratorDesc()
958: DebugInfoDesc(DW_TAG_enumerator)
959, Name("")
960, Value(0)
961{}
962
963// Implement isa/cast/dyncast.
964bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
965 return D->getTag() == DW_TAG_enumerator;
966}
967
968/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
969///
970void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
971 DebugInfoDesc::ApplyToFields(Visitor);
972
973 Visitor->Apply(Name);
974 Visitor->Apply(Value);
975}
976
977/// getDescString - Return a string used to compose global names and labels.
978///
979const char *EnumeratorDesc::getDescString() const {
980 return "llvm.dbg.enumerator";
981}
982
983/// getTypeString - Return a string used to label this descriptor's type.
984///
985const char *EnumeratorDesc::getTypeString() const {
986 return "llvm.dbg.enumerator.type";
987}
988
989#ifndef NDEBUG
990void EnumeratorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000991 cerr << getDescString() << " "
992 << "Version(" << getVersion() << "), "
993 << "Tag(" << getTag() << "), "
994 << "Name(" << Name << "), "
995 << "Value(" << Value << ")\n";
Jim Laskey6a3eb012006-03-01 23:52:37 +0000996}
997#endif
998
999//===----------------------------------------------------------------------===//
1000
Jim Laskeyb8509c52006-03-23 18:07:55 +00001001VariableDesc::VariableDesc(unsigned T)
1002: DebugInfoDesc(T)
1003, Context(NULL)
1004, Name("")
1005, File(NULL)
1006, Line(0)
1007, TyDesc(0)
1008{}
1009
1010// Implement isa/cast/dyncast.
1011bool VariableDesc::classof(const DebugInfoDesc *D) {
1012 unsigned T = D->getTag();
1013 switch (T) {
1014 case DW_TAG_auto_variable:
1015 case DW_TAG_arg_variable:
1016 case DW_TAG_return_variable:
1017 return true;
1018 default: break;
1019 }
1020 return false;
1021}
1022
1023/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1024///
1025void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1026 DebugInfoDesc::ApplyToFields(Visitor);
1027
1028 Visitor->Apply(Context);
1029 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001030 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001031 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001032 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001033}
1034
1035/// getDescString - Return a string used to compose global names and labels.
1036///
1037const char *VariableDesc::getDescString() const {
1038 return "llvm.dbg.variable";
1039}
1040
1041/// getTypeString - Return a string used to label this descriptor's type.
1042///
1043const char *VariableDesc::getTypeString() const {
1044 return "llvm.dbg.variable.type";
1045}
1046
1047#ifndef NDEBUG
1048void VariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001049 cerr << getDescString() << " "
1050 << "Version(" << getVersion() << "), "
1051 << "Tag(" << getTag() << "), "
1052 << "Context(" << Context << "), "
1053 << "Name(\"" << Name << "\"), "
1054 << "File(" << File << "), "
1055 << "Line(" << Line << "), "
1056 << "TyDesc(" << TyDesc << ")\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001057}
1058#endif
1059
1060//===----------------------------------------------------------------------===//
1061
Jim Laskeyce72b172006-02-11 01:01:30 +00001062GlobalDesc::GlobalDesc(unsigned T)
1063: AnchoredDesc(T)
1064, Context(0)
1065, Name("")
Jim Laskey2172f962006-11-30 14:35:45 +00001066, FullName("")
1067, LinkageName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001068, File(NULL)
1069, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001070, TyDesc(NULL)
1071, IsStatic(false)
1072, IsDefinition(false)
1073{}
1074
1075/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001076///
Jim Laskeyce72b172006-02-11 01:01:30 +00001077void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1078 AnchoredDesc::ApplyToFields(Visitor);
1079
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001080 Visitor->Apply(Context);
1081 Visitor->Apply(Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001082 Visitor->Apply(FullName);
1083 Visitor->Apply(LinkageName);
Jim Laskey7089f452006-06-16 13:14:03 +00001084 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001085 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001086 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001087 Visitor->Apply(IsStatic);
1088 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001089}
1090
1091//===----------------------------------------------------------------------===//
1092
1093GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001094: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001095, Global(NULL)
1096{}
1097
Jim Laskey9c4447a2006-03-01 20:39:36 +00001098// Implement isa/cast/dyncast.
1099bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1100 return D->getTag() == DW_TAG_variable;
1101}
1102
Jim Laskeyce72b172006-02-11 01:01:30 +00001103/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1104///
1105void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1106 GlobalDesc::ApplyToFields(Visitor);
1107
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001108 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001109}
1110
Jim Laskeyce72b172006-02-11 01:01:30 +00001111/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001112///
Jim Laskeyce72b172006-02-11 01:01:30 +00001113const char *GlobalVariableDesc::getDescString() const {
1114 return "llvm.dbg.global_variable";
1115}
1116
1117/// getTypeString - Return a string used to label this descriptors type.
1118///
1119const char *GlobalVariableDesc::getTypeString() const {
1120 return "llvm.dbg.global_variable.type";
1121}
1122
1123/// getAnchorString - Return a string used to label this descriptor's anchor.
1124///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001125const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001126const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001127 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001128}
1129
Jim Laskey86cbdba2006-02-06 15:33:21 +00001130#ifndef NDEBUG
1131void GlobalVariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001132 cerr << getDescString() << " "
1133 << "Version(" << getVersion() << "), "
1134 << "Tag(" << getTag() << "), "
1135 << "Anchor(" << getAnchor() << "), "
1136 << "Name(\"" << getName() << "\"), "
1137 << "FullName(\"" << getFullName() << "\"), "
1138 << "LinkageName(\"" << getLinkageName() << "\"), "
1139 << "File(" << getFile() << "),"
1140 << "Line(" << getLine() << "),"
1141 << "Type(" << getType() << "), "
1142 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1143 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1144 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001145}
1146#endif
1147
1148//===----------------------------------------------------------------------===//
1149
Jim Laskeyce72b172006-02-11 01:01:30 +00001150SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001151: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001152{}
1153
Jim Laskey9c4447a2006-03-01 20:39:36 +00001154// Implement isa/cast/dyncast.
1155bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1156 return D->getTag() == DW_TAG_subprogram;
1157}
1158
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001159/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001160/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001161void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001162 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001163}
1164
Jim Laskeyce72b172006-02-11 01:01:30 +00001165/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001166///
Jim Laskeyce72b172006-02-11 01:01:30 +00001167const char *SubprogramDesc::getDescString() const {
1168 return "llvm.dbg.subprogram";
1169}
1170
1171/// getTypeString - Return a string used to label this descriptors type.
1172///
1173const char *SubprogramDesc::getTypeString() const {
1174 return "llvm.dbg.subprogram.type";
1175}
1176
1177/// getAnchorString - Return a string used to label this descriptor's anchor.
1178///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001179const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001180const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001181 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001182}
1183
Jim Laskey86cbdba2006-02-06 15:33:21 +00001184#ifndef NDEBUG
1185void SubprogramDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001186 cerr << getDescString() << " "
1187 << "Version(" << getVersion() << "), "
1188 << "Tag(" << getTag() << "), "
1189 << "Anchor(" << getAnchor() << "), "
1190 << "Name(\"" << getName() << "\"), "
1191 << "FullName(\"" << getFullName() << "\"), "
1192 << "LinkageName(\"" << getLinkageName() << "\"), "
1193 << "File(" << getFile() << "),"
1194 << "Line(" << getLine() << "),"
1195 << "Type(" << getType() << "), "
1196 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1197 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001198}
1199#endif
1200
Jim Laskey45ccae52006-02-28 20:15:07 +00001201//===----------------------------------------------------------------------===//
1202
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001203BlockDesc::BlockDesc()
1204: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001205, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001206{}
1207
1208// Implement isa/cast/dyncast.
1209bool BlockDesc::classof(const DebugInfoDesc *D) {
1210 return D->getTag() == DW_TAG_lexical_block;
1211}
1212
1213/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1214///
1215void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1216 DebugInfoDesc::ApplyToFields(Visitor);
1217
Jim Laskeyb8509c52006-03-23 18:07:55 +00001218 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001219}
1220
1221/// getDescString - Return a string used to compose global names and labels.
1222///
1223const char *BlockDesc::getDescString() const {
1224 return "llvm.dbg.block";
1225}
1226
1227/// getTypeString - Return a string used to label this descriptors type.
1228///
1229const char *BlockDesc::getTypeString() const {
1230 return "llvm.dbg.block.type";
1231}
1232
1233#ifndef NDEBUG
1234void BlockDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001235 cerr << getDescString() << " "
1236 << "Version(" << getVersion() << "), "
1237 << "Tag(" << getTag() << "),"
1238 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001239}
1240#endif
1241
1242//===----------------------------------------------------------------------===//
1243
Jim Laskey86cbdba2006-02-06 15:33:21 +00001244DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001245 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001246}
1247DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001248 // Handle NULL.
1249 if (!GV) return NULL;
1250
Jim Laskey86cbdba2006-02-06 15:33:21 +00001251 // Check to see if it has been already deserialized.
1252 DebugInfoDesc *&Slot = GlobalDescs[GV];
1253 if (Slot) return Slot;
1254
1255 // Get the Tag from the global.
1256 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1257
Jim Laskey86cbdba2006-02-06 15:33:21 +00001258 // Create an empty instance of the correct sort.
1259 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001260
Jim Laskey21407982006-03-14 18:37:57 +00001261 // If not a user defined descriptor.
1262 if (Slot) {
1263 // Deserialize the fields.
1264 DIDeserializeVisitor DRAM(*this, GV);
1265 DRAM.ApplyToFields(Slot);
1266 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001267
1268 return Slot;
1269}
1270
1271//===----------------------------------------------------------------------===//
1272
1273/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001274///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001275const PointerType *DISerializer::getStrPtrType() {
1276 // If not already defined.
1277 if (!StrPtrTy) {
1278 // Construct the pointer to signed bytes.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001279 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001280 }
1281
1282 return StrPtrTy;
1283}
1284
1285/// getEmptyStructPtrType - Return a "{ }*" type.
1286///
1287const PointerType *DISerializer::getEmptyStructPtrType() {
1288 // If not already defined.
1289 if (!EmptyStructPtrTy) {
1290 // Construct the empty structure type.
1291 const StructType *EmptyStructTy =
1292 StructType::get(std::vector<const Type*>());
1293 // Construct the pointer to empty structure type.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001294 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001295 }
1296
1297 return EmptyStructPtrTy;
1298}
1299
1300/// getTagType - Return the type describing the specified descriptor (via tag.)
1301///
1302const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1303 // Attempt to get the previously defined type.
1304 StructType *&Ty = TagTypes[DD->getTag()];
1305
1306 // If not already defined.
1307 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001308 // Set up fields vector.
1309 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001310 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001311 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001312 GTAM.ApplyToFields(DD);
1313
1314 // Construct structured type.
1315 Ty = StructType::get(Fields);
1316
Jim Laskey86cbdba2006-02-06 15:33:21 +00001317 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001318 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001319 }
1320
1321 return Ty;
1322}
1323
1324/// getString - Construct the string as constant string global.
1325///
Jim Laskeyce72b172006-02-11 01:01:30 +00001326Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001327 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001328 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001329 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001330 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001331 // If empty string then use a sbyte* null instead.
1332 if (String.empty()) {
1333 Slot = ConstantPointerNull::get(getStrPtrType());
1334 } else {
1335 // Construct string as an llvm constant.
1336 Constant *ConstStr = ConstantArray::get(String);
1337 // Otherwise create and return a new string global.
1338 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1339 GlobalVariable::InternalLinkage,
Devang Patel1e4c23a2007-05-11 23:14:43 +00001340 ConstStr, ".str", M);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001341 StrGV->setSection("llvm.metadata");
1342 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001343 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001344 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001345 return Slot;
1346
Jim Laskey86cbdba2006-02-06 15:33:21 +00001347}
1348
1349/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1350/// so that it can be serialized to a .bc or .ll file.
1351GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1352 // Check if the DebugInfoDesc is already in the map.
1353 GlobalVariable *&Slot = DescGlobals[DD];
1354
1355 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1356 if (Slot) return Slot;
1357
Jim Laskey86cbdba2006-02-06 15:33:21 +00001358 // Get the type associated with the Tag.
1359 const StructType *Ty = getTagType(DD);
1360
1361 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001362 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1363 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001364 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001365
1366 // Insert new GlobalVariable in DescGlobals map.
1367 Slot = GV;
1368
1369 // Set up elements vector
1370 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001371 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001372 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001373 SRAM.ApplyToFields(DD);
1374
1375 // Set the globals initializer.
1376 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1377
1378 return GV;
1379}
1380
Devang Patel962e0752007-11-30 00:51:33 +00001381/// addDescriptor - Directly connect DD with existing GV.
1382void DISerializer::addDescriptor(DebugInfoDesc *DD,
1383 GlobalVariable *GV) {
1384 DescGlobals[DD] = GV;
1385}
1386
Jim Laskey86cbdba2006-02-06 15:33:21 +00001387//===----------------------------------------------------------------------===//
1388
Jim Laskey86cbdba2006-02-06 15:33:21 +00001389/// Verify - Return true if the GlobalVariable appears to be a valid
1390/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001391bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001392 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001393}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001394bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001395 // NULLs are valid.
1396 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001397
Jim Laskey98e04102006-03-26 22:45:20 +00001398 // Check prior validity.
1399 unsigned &ValiditySlot = Validity[GV];
1400
1401 // If visited before then use old state.
1402 if (ValiditySlot) return ValiditySlot == Valid;
1403
1404 // Assume validity for the time being (recursion.)
1405 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001406
1407 // Make sure the global is internal or link once (anchor.)
1408 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1409 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1410 ValiditySlot = Invalid;
1411 return false;
1412 }
Jim Laskey98e04102006-03-26 22:45:20 +00001413
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001414 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001415 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001416
1417 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001418 if (Tag == DW_TAG_invalid) {
1419 ValiditySlot = Valid;
1420 return true;
1421 }
1422
1423 // Get the Version.
1424 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1425
1426 // Check for version mismatch.
1427 if (Version != LLVMDebugVersion) {
1428 ValiditySlot = Invalid;
1429 return false;
1430 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001431
Jim Laskey86cbdba2006-02-06 15:33:21 +00001432 // Construct an empty DebugInfoDesc.
1433 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001434
1435 // Allow for user defined descriptors.
1436 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001437
1438 // Get the initializer constant.
1439 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1440
1441 // Get the operand count.
1442 unsigned N = CI->getNumOperands();
1443
1444 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001445 unsigned &CountSlot = Counts[Tag];
1446 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001447 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001448 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001449 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001450 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001451 }
1452
Jim Laskey21407982006-03-14 18:37:57 +00001453 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001454 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001455 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001456 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001457 return false;
1458 }
1459
1460 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001461 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001462 VRAM.ApplyToFields(DD);
1463
1464 // Release empty DebugInfoDesc.
1465 delete DD;
1466
Jim Laskey98e04102006-03-26 22:45:20 +00001467 // If fields are not valid.
1468 if (!VRAM.isValid()) {
1469 ValiditySlot = Invalid;
1470 return false;
1471 }
1472
1473 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001474}
1475
1476//===----------------------------------------------------------------------===//
1477
Jim Laskeyb8509c52006-03-23 18:07:55 +00001478DebugScope::~DebugScope() {
1479 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1480 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1481}
1482
1483//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001484
Jim Laskey6da18642007-01-26 21:38:26 +00001485MachineModuleInfo::MachineModuleInfo()
Devang Patel794fd752007-05-01 21:15:47 +00001486: ImmutablePass((intptr_t)&ID)
1487, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001488, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001489, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001490, Directories()
1491, SourceFiles()
1492, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001493, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001494, ScopeMap()
1495, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001496, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001497, LandingPads()
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001498, Personalities()
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001499, CallsEHReturn(0)
1500, CallsUnwindInit(0)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001501{
1502 // Always emit "no personality" info
1503 Personalities.push_back(NULL);
1504}
Jim Laskey6da18642007-01-26 21:38:26 +00001505MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001506
1507}
1508
Jim Laskey6da18642007-01-26 21:38:26 +00001509/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001510///
Jim Laskey6da18642007-01-26 21:38:26 +00001511bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001512 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001513}
1514
Jim Laskey6da18642007-01-26 21:38:26 +00001515/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001516///
Jim Laskey6da18642007-01-26 21:38:26 +00001517bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001518 return false;
1519}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001520
Jim Laskey6da18642007-01-26 21:38:26 +00001521/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001522///
Jim Laskey6da18642007-01-26 21:38:26 +00001523void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001524 // Coming soon.
1525}
1526
Jim Laskey6da18642007-01-26 21:38:26 +00001527/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001528///
Jim Laskey6da18642007-01-26 21:38:26 +00001529void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001530 // Clean up scope information.
1531 if (RootScope) {
1532 delete RootScope;
1533 ScopeMap.clear();
1534 RootScope = NULL;
1535 }
1536
Jim Laskeyb82313f2007-02-01 16:31:34 +00001537 // Clean up line info.
1538 Lines.clear();
1539
Jim Laskey41886992006-04-07 16:34:46 +00001540 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001541 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001542
1543 // Clean up exception info.
1544 LandingPads.clear();
1545 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +00001546 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +00001547 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001548 CallsEHReturn = 0;
1549 CallsUnwindInit = 0;
Jim Laskey41886992006-04-07 16:34:46 +00001550}
1551
Jim Laskeyd96185a2006-02-13 12:50:39 +00001552/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001553///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001554// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001555DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001556 return DR.Deserialize(V);
1557}
1558
1559/// Verify - Verify that a Value is debug information descriptor.
1560///
Jim Laskey6da18642007-01-26 21:38:26 +00001561bool MachineModuleInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001562 return VR.Verify(V);
1563}
1564
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001565/// AnalyzeModule - Scan the module for global debug information.
1566///
Jim Laskey6da18642007-01-26 21:38:26 +00001567void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001568 SetupCompileUnits(M);
Dale Johannesen48ae02f2008-01-16 19:59:28 +00001569
1570 // Insert functions in the llvm.used array into UsedFunctions.
1571 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1572 if (!GV || !GV->hasInitializer()) return;
1573
1574 // Should be an array of 'i8*'.
1575 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1576 if (InitList == 0) return;
1577
1578 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1579 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1580 if (CE->getOpcode() == Instruction::BitCast)
1581 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1582 UsedFunctions.insert(F);
1583 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001584}
1585
Jim Laskeyc1c47c32007-01-29 23:40:33 +00001586/// needsFrameInfo - Returns true if we need to gather callee-saved register
1587/// move info for the frame.
1588bool MachineModuleInfo::needsFrameInfo() const {
1589 return hasDebugInfo() || ExceptionHandling;
1590}
1591
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001592/// SetupCompileUnits - Set up the unique vector of compile units.
1593///
Jim Laskey6da18642007-01-26 21:38:26 +00001594void MachineModuleInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001595 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001596
Jim Laskey0420f2a2006-02-22 19:02:11 +00001597 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1598 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001599 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001600}
1601
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001602/// getCompileUnits - Return a vector of debug compile units.
1603///
Jim Laskey6da18642007-01-26 21:38:26 +00001604const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001605 return CompileUnits;
1606}
1607
Jim Laskey0420f2a2006-02-22 19:02:11 +00001608/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1609/// named GlobalVariable.
1610std::vector<GlobalVariable*>
Jim Laskey6da18642007-01-26 21:38:26 +00001611MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1612 const std::string &RootName) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001613 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001614}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001615
1616/// RecordLabel - Records location information and associates it with a
1617/// debug label. Returns a unique label ID used to generate a label and
1618/// provide correspondence to the source line list.
Jim Laskey6da18642007-01-26 21:38:26 +00001619unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column,
Jim Laskeyb8509c52006-03-23 18:07:55 +00001620 unsigned Source) {
1621 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001622 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001623 return ID;
1624}
1625
1626/// RecordSource - Register a source file with debug info. Returns an source
1627/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001628unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1629 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001630 unsigned DirectoryID = Directories.insert(Directory);
1631 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1632}
Jim Laskey6da18642007-01-26 21:38:26 +00001633unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001634 return RecordSource(CompileUnit->getDirectory(),
1635 CompileUnit->getFileName());
1636}
1637
1638/// RecordRegionStart - Indicate the start of a region.
1639///
Jim Laskey6da18642007-01-26 21:38:26 +00001640unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001641 // FIXME - need to be able to handle split scopes because of bb cloning.
1642 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1643 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1644 unsigned ID = NextLabelID();
1645 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1646 return ID;
1647}
1648
1649/// RecordRegionEnd - Indicate the end of a region.
1650///
Jim Laskey6da18642007-01-26 21:38:26 +00001651unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001652 // FIXME - need to be able to handle split scopes because of bb cloning.
1653 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1654 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1655 unsigned ID = NextLabelID();
1656 Scope->setEndLabelID(ID);
1657 return ID;
1658}
1659
1660/// RecordVariable - Indicate the declaration of a local variable.
1661///
Jim Laskey6da18642007-01-26 21:38:26 +00001662void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001663 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1664 DebugScope *Scope = getOrCreateScope(VD->getContext());
1665 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1666 Scope->AddVariable(DV);
1667}
1668
1669/// getOrCreateScope - Returns the scope associated with the given descriptor.
1670///
Jim Laskey6da18642007-01-26 21:38:26 +00001671DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001672 DebugScope *&Slot = ScopeMap[ScopeDesc];
1673 if (!Slot) {
1674 // FIXME - breaks down when the context is an inlined function.
1675 DebugInfoDesc *ParentDesc = NULL;
1676 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1677 ParentDesc = Block->getContext();
1678 }
1679 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1680 Slot = new DebugScope(Parent, ScopeDesc);
1681 if (Parent) {
1682 Parent->AddScope(Slot);
1683 } else if (RootScope) {
1684 // FIXME - Add inlined function scopes to the root so we can delete
1685 // them later. Long term, handle inlined functions properly.
1686 RootScope->AddScope(Slot);
1687 } else {
1688 // First function is top level function.
1689 RootScope = Slot;
1690 }
1691 }
1692 return Slot;
1693}
1694
Jim Laskey59667fe2007-02-21 22:38:31 +00001695//===-EH-------------------------------------------------------------------===//
1696
1697/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1698/// specified MachineBasicBlock.
1699LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1700 (MachineBasicBlock *LandingPad) {
1701 unsigned N = LandingPads.size();
1702 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001703 LandingPadInfo &LP = LandingPads[i];
1704 if (LP.LandingPadBlock == LandingPad)
1705 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001706 }
1707
1708 LandingPads.push_back(LandingPadInfo(LandingPad));
1709 return LandingPads[N];
1710}
1711
1712/// addInvoke - Provide the begin and end labels of an invoke style call and
1713/// associate it with a try landing pad block.
1714void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1715 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001716 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001717 LP.BeginLabels.push_back(BeginLabel);
1718 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +00001719}
1720
1721/// addLandingPad - Provide the label of a try LandingPad block.
1722///
1723unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1724 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001725 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1726 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001727 return LandingPadLabel;
1728}
1729
1730/// addPersonality - Provide the personality function for the exception
1731/// information.
1732void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001733 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001734 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001735 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001736
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001737 for (unsigned i = 0; i < Personalities.size(); ++i)
1738 if (Personalities[i] == Personality)
1739 return;
1740
1741 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +00001742}
1743
1744/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1745///
1746void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1747 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001748 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001749 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001750 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001751}
Duncan Sands73ef58a2007-06-02 16:53:42 +00001752
1753/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +00001754///
Duncan Sands73ef58a2007-06-02 16:53:42 +00001755void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1756 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001757 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Duncan Sands73ef58a2007-06-02 16:53:42 +00001758 std::vector<unsigned> IdsInFilter (TyInfo.size());
1759 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1760 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1761 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +00001762}
1763
Duncan Sands6590b042007-08-27 15:47:50 +00001764/// addCleanup - Add a cleanup action for a landing pad.
1765///
1766void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1767 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1768 LP.TypeIds.push_back(0);
1769}
1770
Jim Laskey59667fe2007-02-21 22:38:31 +00001771/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1772/// pads.
1773void MachineModuleInfo::TidyLandingPads() {
1774 for (unsigned i = 0; i != LandingPads.size(); ) {
1775 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001776 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001777
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001778 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +00001779 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001780 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001781 LandingPads.erase(LandingPads.begin() + i);
1782 continue;
1783 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001784
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001785 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1786 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1787 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands57810cd2007-09-05 11:27:52 +00001788
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001789 if (!BeginLabel || !EndLabel) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001790 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1791 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1792 continue;
1793 }
1794
1795 LandingPad.BeginLabels[j] = BeginLabel;
1796 LandingPad.EndLabels[j] = EndLabel;
1797 ++j;
1798 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001799
1800 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +00001801 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +00001802 LandingPads.erase(LandingPads.begin() + i);
1803 continue;
1804 }
1805
1806 // If there is no landing pad, ensure that the list of typeids is empty.
1807 // If the only typeid is a cleanup, this is the same as having no typeids.
1808 if (!LandingPad.LandingPadBlock ||
1809 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1810 LandingPad.TypeIds.clear();
1811
Jim Laskey59667fe2007-02-21 22:38:31 +00001812 ++i;
1813 }
1814}
1815
1816/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1817/// function wide.
1818unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1819 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1820 if (TypeInfos[i] == TI) return i + 1;
1821
1822 TypeInfos.push_back(TI);
1823 return TypeInfos.size();
1824}
1825
Duncan Sands73ef58a2007-06-02 16:53:42 +00001826/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1827/// function wide.
1828int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +00001829 // If the new filter coincides with the tail of an existing filter, then
1830 // re-use the existing filter. Folding filters more than this requires
1831 // re-ordering filters and/or their elements - probably not worth it.
1832 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1833 E = FilterEnds.end(); I != E; ++I) {
1834 unsigned i = *I, j = TyIds.size();
1835
1836 while (i && j)
1837 if (FilterIds[--i] != TyIds[--j])
1838 goto try_next;
1839
1840 if (!j)
1841 // The new filter coincides with range [i, end) of the existing filter.
1842 return -(1 + i);
1843
1844try_next:;
1845 }
1846
1847 // Add the new filter.
Duncan Sands73ef58a2007-06-02 16:53:42 +00001848 int FilterID = -(1 + FilterIds.size());
1849 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1850 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1851 FilterIds.push_back(TyIds[I]);
Duncan Sands14da32a2007-07-05 15:15:01 +00001852 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +00001853 FilterIds.push_back(0); // terminator
1854 return FilterID;
1855}
1856
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001857/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +00001858Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001859 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001860 // function
1861 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +00001862}
1863
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001864/// getPersonalityIndex - Return unique index for current personality
1865/// function. NULL personality function should always get zero index.
1866unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001867 const Function* Personality = NULL;
1868
1869 // Scan landing pads. If there is at least one non-NULL personality - use it.
1870 for (unsigned i = 0; i != LandingPads.size(); ++i)
1871 if (LandingPads[i].Personality) {
1872 Personality = LandingPads[i].Personality;
1873 break;
1874 }
1875
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001876 for (unsigned i = 0; i < Personalities.size(); ++i) {
1877 if (Personalities[i] == Personality)
1878 return i;
1879 }
1880
1881 // This should never happen
1882 assert(0 && "Personality function should be set!");
1883 return 0;
1884}
Jim Laskey59667fe2007-02-21 22:38:31 +00001885
Jim Laskey9d4209f2006-11-07 19:33:46 +00001886//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00001887/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1888/// a info consumer to determine if the range of two labels is empty, by seeing
1889/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00001890
1891namespace llvm {
1892
1893struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00001894 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +00001895 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1896
Jim Laskey9d4209f2006-11-07 19:33:46 +00001897 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00001898 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00001899};
1900
Devang Patel19974732007-05-03 01:11:54 +00001901char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00001902
Jim Laskey9d4209f2006-11-07 19:33:46 +00001903bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00001904 // Get machine module info.
1905 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1906 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001907
1908 // Track if change is made.
1909 bool MadeChange = false;
1910 // No prior label to begin.
1911 unsigned PriorLabel = 0;
1912
1913 // Iterate through basic blocks.
1914 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1915 BB != E; ++BB) {
1916 // Iterate through instructions.
1917 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00001918 // Is it a label.
Jim Laskey1ee29252007-01-26 14:34:52 +00001919 if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00001920 // The label ID # is always operand #0, an immediate.
1921 unsigned NextLabel = I->getOperand(0).getImm();
1922
1923 // If there was an immediate prior label.
1924 if (PriorLabel) {
1925 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00001926 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00001927 // Delete the current label.
1928 I = BB->erase(I);
1929 // Indicate a change has been made.
1930 MadeChange = true;
1931 continue;
1932 } else {
1933 // Start a new round.
1934 PriorLabel = NextLabel;
1935 }
1936 } else {
1937 // No consecutive labels.
1938 PriorLabel = 0;
1939 }
1940
1941 ++I;
1942 }
1943 }
1944
1945 return MadeChange;
1946}
1947
1948FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1949
1950}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001951