blob: d37021e0a31b12d32cc7d35495ff1fac928f84c4 [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//
5// This file was developed by James M. Laskey and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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}
Jim Laskey063e7652006-01-17 17:31:53 +000033
Jim Laskeyb3e789a2006-01-26 20:21:46 +000034//===----------------------------------------------------------------------===//
35
Jim Laskey86cbdba2006-02-06 15:33:21 +000036/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000037/// specified value in their initializer somewhere.
38static void
39getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
40 // Scan though value users.
41 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
42 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000043 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000044 Result.push_back(GV);
45 } else if (Constant *C = dyn_cast<Constant>(*I)) {
46 // If the user is a constant variable then scan its users
47 getGlobalVariablesUsing(C, Result);
48 }
49 }
50}
51
Jim Laskey86cbdba2006-02-06 15:33:21 +000052/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
53/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000054static std::vector<GlobalVariable*>
55getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000056 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000057
58 std::vector<const Type*> FieldTypes;
Reid Spencer47857812006-12-31 05:55:36 +000059 FieldTypes.push_back(Type::Int32Ty);
60 FieldTypes.push_back(Type::Int32Ty);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000061
Jim Laskey86cbdba2006-02-06 15:33:21 +000062 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000063 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000064 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000065
66 // If present and linkonce then scan for users.
67 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
68 getGlobalVariablesUsing(UseRoot, Result);
69 }
70
71 return Result;
72}
73
Jim Laskey86cbdba2006-02-06 15:33:21 +000074/// isStringValue - Return true if the given value can be coerced to a string.
75///
76static bool isStringValue(Value *V) {
77 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
78 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
79 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
80 return Init->isString();
81 }
82 } else if (Constant *C = dyn_cast<Constant>(V)) {
83 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
84 return isStringValue(GV);
85 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
86 if (CE->getOpcode() == Instruction::GetElementPtr) {
87 if (CE->getNumOperands() == 3 &&
88 cast<Constant>(CE->getOperand(1))->isNullValue() &&
89 isa<ConstantInt>(CE->getOperand(2))) {
90 return isStringValue(CE->getOperand(0));
91 }
92 }
93 }
94 }
95 return false;
96}
97
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000098/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000099///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000100static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000101 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
102 return GV;
103 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000104 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000105 return dyn_cast<GlobalVariable>(CE->getOperand(0));
106 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000107 }
108 return NULL;
109}
110
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000111/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000112/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000113static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000114 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
115 return true;
116 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000117 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000118 return isa<GlobalVariable>(CE->getOperand(0));
119 }
120 }
121 return false;
122}
123
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000124/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000125///
Reid Spencerb83eb642006-10-20 07:07:24 +0000126static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000127 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000128 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000129
Jim Laskey86cbdba2006-02-06 15:33:21 +0000130 // Get the initializer constant.
131 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000132 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000133
Jim Laskey86cbdba2006-02-06 15:33:21 +0000134 // Check if there is at least i + 1 operands.
135 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000136 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000137
Jim Laskey86cbdba2006-02-06 15:33:21 +0000138 // Check constant.
Reid Spencerb83eb642006-10-20 07:07:24 +0000139 return dyn_cast<ConstantInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000140}
Reid Spencerb83eb642006-10-20 07:07:24 +0000141
Jim Laskey86cbdba2006-02-06 15:33:21 +0000142//===----------------------------------------------------------------------===//
143
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000144/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000145/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000146void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000147 DD->ApplyToFields(this);
148}
149
150//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000151/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
152/// the supplied DebugInfoDesc.
153class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000154private:
155 unsigned Count; // Running count of fields.
156
157public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000158 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000159
160 // Accessors.
161 unsigned getCount() const { return Count; }
162
163 /// Apply - Count each of the fields.
164 ///
165 virtual void Apply(int &Field) { ++Count; }
166 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000167 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000168 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000169 virtual void Apply(bool &Field) { ++Count; }
170 virtual void Apply(std::string &Field) { ++Count; }
171 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
172 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000173 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
174 ++Count;
175 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000176};
177
178//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000179/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
180/// supplied DebugInfoDesc.
181class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000182private:
183 DIDeserializer &DR; // Active deserializer.
184 unsigned I; // Current operand index.
185 ConstantStruct *CI; // GlobalVariable constant initializer.
186
187public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000188 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
189 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000190 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000191 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000192 , CI(cast<ConstantStruct>(GV->getInitializer()))
193 {}
194
195 /// Apply - Set the value of each of the fields.
196 ///
197 virtual void Apply(int &Field) {
198 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000199 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000200 }
201 virtual void Apply(unsigned &Field) {
202 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000203 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000204 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000205 virtual void Apply(int64_t &Field) {
206 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000207 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000208 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000209 virtual void Apply(uint64_t &Field) {
210 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000211 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000212 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000213 virtual void Apply(bool &Field) {
214 Constant *C = CI->getOperand(I++);
Reid Spencer579dca12007-01-12 04:24:46 +0000215 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000216 }
217 virtual void Apply(std::string &Field) {
218 Constant *C = CI->getOperand(I++);
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000219 Field = C->getStringValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000220 }
221 virtual void Apply(DebugInfoDesc *&Field) {
222 Constant *C = CI->getOperand(I++);
223 Field = DR.Deserialize(C);
224 }
225 virtual void Apply(GlobalVariable *&Field) {
226 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000227 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000228 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000229 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000230 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000231 Constant *C = CI->getOperand(I++);
232 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000233 if (GV->hasInitializer()) {
234 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
235 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
236 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
237 DebugInfoDesc *DE = DR.Deserialize(GVE);
238 Field.push_back(DE);
239 }
240 } else if (GV->getInitializer()->isNullValue()) {
241 if (const ArrayType *T =
242 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
243 Field.resize(T->getNumElements());
244 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000245 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000246 }
247 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000248};
249
250//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000251/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000252/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000253class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000254private:
255 DISerializer &SR; // Active serializer.
256 std::vector<Constant*> &Elements; // Element accumulator.
257
258public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000259 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
260 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000261 , SR(S)
262 , Elements(E)
263 {}
264
265 /// Apply - Set the value of each of the fields.
266 ///
267 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000268 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000269 }
270 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000271 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000272 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000273 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000274 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000275 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000276 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000277 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000278 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000279 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000280 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000281 }
282 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000283 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000284 }
285 virtual void Apply(DebugInfoDesc *&Field) {
286 GlobalVariable *GV = NULL;
287
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000288 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000289 if (Field) GV = SR.Serialize(Field);
290
291 // FIXME - At some point should use specific type.
292 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
293
294 if (GV) {
295 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000296 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000297 } else {
298 // Use NULL.
299 Elements.push_back(ConstantPointerNull::get(EmptyTy));
300 }
301 }
302 virtual void Apply(GlobalVariable *&Field) {
303 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000304 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000305 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000306 } else {
307 Elements.push_back(ConstantPointerNull::get(EmptyTy));
308 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000309 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000310 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
311 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
312 unsigned N = Field.size();
313 ArrayType *AT = ArrayType::get(EmptyTy, N);
314 std::vector<Constant *> ArrayElements;
315
316 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000317 if (DebugInfoDesc *Element = Field[i]) {
318 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000319 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000320 ArrayElements.push_back(cast<Constant>(CE));
321 } else {
322 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
323 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000324 }
325
326 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000327 GlobalVariable *CAGV = new GlobalVariable(AT, true,
328 GlobalValue::InternalLinkage,
329 CA, "llvm.dbg.array",
330 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000331 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000332 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000333 Elements.push_back(CAE);
334 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000335};
336
337//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000338/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000339/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000340class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000341private:
342 DISerializer &SR; // Active serializer.
343 std::vector<const Type*> &Fields; // Type accumulator.
344
345public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000346 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
347 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000348 , SR(S)
349 , Fields(F)
350 {}
351
352 /// Apply - Set the value of each of the fields.
353 ///
354 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000355 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000356 }
357 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000358 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000359 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000360 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000361 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000362 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000363 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000364 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000365 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000366 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000367 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000368 }
369 virtual void Apply(std::string &Field) {
370 Fields.push_back(SR.getStrPtrType());
371 }
372 virtual void Apply(DebugInfoDesc *&Field) {
373 // FIXME - At some point should use specific type.
374 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
375 Fields.push_back(EmptyTy);
376 }
377 virtual void Apply(GlobalVariable *&Field) {
378 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
379 Fields.push_back(EmptyTy);
380 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000381 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
382 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
383 Fields.push_back(EmptyTy);
384 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000385};
386
387//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000388/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000389/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000390class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000391private:
392 DIVerifier &VR; // Active verifier.
393 bool IsValid; // Validity status.
394 unsigned I; // Current operand index.
395 ConstantStruct *CI; // GlobalVariable constant initializer.
396
397public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000398 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
399 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000400 , VR(V)
401 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000402 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000403 , CI(cast<ConstantStruct>(GV->getInitializer()))
404 {
405 }
406
407 // Accessors.
408 bool isValid() const { return IsValid; }
409
410 /// Apply - Set the value of each of the fields.
411 ///
412 virtual void Apply(int &Field) {
413 Constant *C = CI->getOperand(I++);
414 IsValid = IsValid && isa<ConstantInt>(C);
415 }
416 virtual void Apply(unsigned &Field) {
417 Constant *C = CI->getOperand(I++);
418 IsValid = IsValid && isa<ConstantInt>(C);
419 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000420 virtual void Apply(int64_t &Field) {
421 Constant *C = CI->getOperand(I++);
422 IsValid = IsValid && isa<ConstantInt>(C);
423 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000424 virtual void Apply(uint64_t &Field) {
425 Constant *C = CI->getOperand(I++);
426 IsValid = IsValid && isa<ConstantInt>(C);
427 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000428 virtual void Apply(bool &Field) {
429 Constant *C = CI->getOperand(I++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000430 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000431 }
432 virtual void Apply(std::string &Field) {
433 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000434 IsValid = IsValid &&
435 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000436 }
437 virtual void Apply(DebugInfoDesc *&Field) {
438 // FIXME - Prepare the correct descriptor.
439 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000440 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000441 }
442 virtual void Apply(GlobalVariable *&Field) {
443 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000444 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000445 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000446 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
447 Constant *C = CI->getOperand(I++);
448 IsValid = IsValid && isGlobalVariable(C);
449 if (!IsValid) return;
450
451 GlobalVariable *GV = getGlobalVariable(C);
452 IsValid = IsValid && GV && GV->hasInitializer();
453 if (!IsValid) return;
454
455 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
456 IsValid = IsValid && CA;
457 if (!IsValid) return;
458
459 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
460 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
461 if (!IsValid) return;
462
463 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
464 VR.Verify(GVE);
465 }
466 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000467};
468
Jim Laskeyce72b172006-02-11 01:01:30 +0000469
Jim Laskey86cbdba2006-02-06 15:33:21 +0000470//===----------------------------------------------------------------------===//
471
Jim Laskeyed4e5662006-06-14 14:45:39 +0000472/// TagFromGlobal - Returns the tag number from a debug info descriptor
473/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000474unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000475 ConstantInt *C = getUIntOperand(GV, 0);
476 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000477 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000478}
479
480/// VersionFromGlobal - Returns the version number from a debug info
481/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
482/// int.
483unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000484 ConstantInt *C = getUIntOperand(GV, 0);
485 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000486 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000487}
488
489/// DescFactory - Create an instance of debug info descriptor based on Tag.
490/// Return NULL if not a recognized Tag.
491DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
492 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000493 case DW_TAG_anchor: return new AnchorDesc();
494 case DW_TAG_compile_unit: return new CompileUnitDesc();
495 case DW_TAG_variable: return new GlobalVariableDesc();
496 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000497 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000498 case DW_TAG_base_type: return new BasicTypeDesc();
499 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000500 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000501 case DW_TAG_reference_type:
502 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000503 case DW_TAG_volatile_type:
504 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000505 case DW_TAG_member:
506 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000507 case DW_TAG_array_type:
508 case DW_TAG_structure_type:
509 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000510 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000511 case DW_TAG_vector_type:
512 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000513 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000514 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000515 case DW_TAG_return_variable:
516 case DW_TAG_arg_variable:
517 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000518 default: break;
519 }
520 return NULL;
521}
522
523/// getLinkage - get linkage appropriate for this type of descriptor.
524///
525GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
526 return GlobalValue::InternalLinkage;
527}
528
529/// ApplyToFields - Target the vistor to the fields of the descriptor.
530///
531void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
532 Visitor->Apply(Tag);
533}
534
535//===----------------------------------------------------------------------===//
536
Jim Laskey9c4447a2006-03-01 20:39:36 +0000537AnchorDesc::AnchorDesc()
538: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000539, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000540{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000541AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000542: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000543, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000544{}
545
546// Implement isa/cast/dyncast.
547bool AnchorDesc::classof(const DebugInfoDesc *D) {
548 return D->getTag() == DW_TAG_anchor;
549}
550
Jim Laskeyce72b172006-02-11 01:01:30 +0000551/// getLinkage - get linkage appropriate for this type of descriptor.
552///
553GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
554 return GlobalValue::LinkOnceLinkage;
555}
556
557/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
558///
559void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
560 DebugInfoDesc::ApplyToFields(Visitor);
561
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000562 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000563}
564
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000565/// getDescString - Return a string used to compose global names and labels. A
566/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000567/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000568/// to the list of names left external in the internalizer.
569/// ExternalNames.insert("llvm.dbg.compile_units");
570/// ExternalNames.insert("llvm.dbg.global_variables");
571/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000572const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000573 switch (AnchorTag) {
574 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
575 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
576 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
577 default: break;
578 }
579
580 assert(0 && "Tag does not have a case for anchor string");
581 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000582}
583
584/// getTypeString - Return a string used to label this descriptors type.
585///
586const char *AnchorDesc::getTypeString() const {
587 return "llvm.dbg.anchor.type";
588}
589
590#ifndef NDEBUG
591void AnchorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000592 cerr << getDescString() << " "
593 << "Version(" << getVersion() << "), "
594 << "Tag(" << getTag() << "), "
595 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000596}
597#endif
598
599//===----------------------------------------------------------------------===//
600
601AnchoredDesc::AnchoredDesc(unsigned T)
602: DebugInfoDesc(T)
603, Anchor(NULL)
604{}
605
606/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
607///
608void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
609 DebugInfoDesc::ApplyToFields(Visitor);
610
Jim Laskey7089f452006-06-16 13:14:03 +0000611 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000612}
613
614//===----------------------------------------------------------------------===//
615
616CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000617: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000618, Language(0)
619, FileName("")
620, Directory("")
621, Producer("")
622{}
623
Jim Laskey9c4447a2006-03-01 20:39:36 +0000624// Implement isa/cast/dyncast.
625bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
626 return D->getTag() == DW_TAG_compile_unit;
627}
628
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000629/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
630///
631void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000632 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000633
634 // Handle cases out of sync with compiler.
635 if (getVersion() == 0) {
636 unsigned DebugVersion;
637 Visitor->Apply(DebugVersion);
638 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000639
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000640 Visitor->Apply(Language);
641 Visitor->Apply(FileName);
642 Visitor->Apply(Directory);
643 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000644}
645
Jim Laskeyce72b172006-02-11 01:01:30 +0000646/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000647///
Jim Laskeyce72b172006-02-11 01:01:30 +0000648const char *CompileUnitDesc::getDescString() const {
649 return "llvm.dbg.compile_unit";
650}
651
652/// getTypeString - Return a string used to label this descriptors type.
653///
654const char *CompileUnitDesc::getTypeString() const {
655 return "llvm.dbg.compile_unit.type";
656}
657
658/// getAnchorString - Return a string used to label this descriptor's anchor.
659///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000660const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000661const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000662 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000663}
664
Jim Laskey86cbdba2006-02-06 15:33:21 +0000665#ifndef NDEBUG
666void CompileUnitDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000667 cerr << getDescString() << " "
668 << "Version(" << getVersion() << "), "
669 << "Tag(" << getTag() << "), "
670 << "Anchor(" << getAnchor() << "), "
671 << "Language(" << Language << "), "
672 << "FileName(\"" << FileName << "\"), "
673 << "Directory(\"" << Directory << "\"), "
674 << "Producer(\"" << Producer << "\")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000675}
676#endif
677
678//===----------------------------------------------------------------------===//
679
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000680TypeDesc::TypeDesc(unsigned T)
681: DebugInfoDesc(T)
682, Context(NULL)
683, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000684, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000685, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000686, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000687, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000688, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000689, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000690{}
691
Jim Laskey69906002006-02-24 16:46:40 +0000692/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000693///
694void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
695 DebugInfoDesc::ApplyToFields(Visitor);
696
697 Visitor->Apply(Context);
698 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000699 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000700 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000701 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000702 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000703 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000704 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000705}
706
707/// getDescString - Return a string used to compose global names and labels.
708///
709const char *TypeDesc::getDescString() const {
710 return "llvm.dbg.type";
711}
712
713/// getTypeString - Return a string used to label this descriptor's type.
714///
715const char *TypeDesc::getTypeString() const {
716 return "llvm.dbg.type.type";
717}
718
719#ifndef NDEBUG
720void TypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000721 cerr << getDescString() << " "
722 << "Version(" << getVersion() << "), "
723 << "Tag(" << getTag() << "), "
724 << "Context(" << Context << "), "
725 << "Name(\"" << Name << "\"), "
726 << "File(" << File << "), "
727 << "Line(" << Line << "), "
728 << "Size(" << Size << "), "
729 << "Align(" << Align << "), "
730 << "Offset(" << Offset << "), "
731 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000732}
733#endif
734
735//===----------------------------------------------------------------------===//
736
737BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000738: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000739, Encoding(0)
740{}
741
Jim Laskey9c4447a2006-03-01 20:39:36 +0000742// Implement isa/cast/dyncast.
743bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
744 return D->getTag() == DW_TAG_base_type;
745}
746
Jim Laskey69906002006-02-24 16:46:40 +0000747/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000748///
749void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
750 TypeDesc::ApplyToFields(Visitor);
751
752 Visitor->Apply(Encoding);
753}
754
Jim Laskeyf8913f12006-03-01 17:53:02 +0000755/// getDescString - Return a string used to compose global names and labels.
756///
757const char *BasicTypeDesc::getDescString() const {
758 return "llvm.dbg.basictype";
759}
760
761/// getTypeString - Return a string used to label this descriptor's type.
762///
763const char *BasicTypeDesc::getTypeString() const {
764 return "llvm.dbg.basictype.type";
765}
766
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000767#ifndef NDEBUG
768void BasicTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000769 cerr << getDescString() << " "
770 << "Version(" << getVersion() << "), "
771 << "Tag(" << getTag() << "), "
772 << "Context(" << getContext() << "), "
773 << "Name(\"" << getName() << "\"), "
774 << "Size(" << getSize() << "), "
775 << "Encoding(" << Encoding << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000776}
777#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000778
Jim Laskey434b40b2006-02-23 22:37:30 +0000779//===----------------------------------------------------------------------===//
780
Jim Laskey69906002006-02-24 16:46:40 +0000781DerivedTypeDesc::DerivedTypeDesc(unsigned T)
782: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000783, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000784{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000785
Jim Laskey9c4447a2006-03-01 20:39:36 +0000786// Implement isa/cast/dyncast.
787bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
788 unsigned T = D->getTag();
789 switch (T) {
790 case DW_TAG_typedef:
791 case DW_TAG_pointer_type:
792 case DW_TAG_reference_type:
793 case DW_TAG_const_type:
794 case DW_TAG_volatile_type:
795 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000796 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000797 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000798 return true;
799 default: break;
800 }
801 return false;
802}
803
Jim Laskey69906002006-02-24 16:46:40 +0000804/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000805///
Jim Laskey69906002006-02-24 16:46:40 +0000806void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000807 TypeDesc::ApplyToFields(Visitor);
808
Jim Laskey7089f452006-06-16 13:14:03 +0000809 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000810}
811
Jim Laskeyf8913f12006-03-01 17:53:02 +0000812/// getDescString - Return a string used to compose global names and labels.
813///
814const char *DerivedTypeDesc::getDescString() const {
815 return "llvm.dbg.derivedtype";
816}
817
818/// getTypeString - Return a string used to label this descriptor's type.
819///
820const char *DerivedTypeDesc::getTypeString() const {
821 return "llvm.dbg.derivedtype.type";
822}
823
Jim Laskey434b40b2006-02-23 22:37:30 +0000824#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000825void DerivedTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000826 cerr << getDescString() << " "
827 << "Version(" << getVersion() << "), "
828 << "Tag(" << getTag() << "), "
829 << "Context(" << getContext() << "), "
830 << "Name(\"" << getName() << "\"), "
831 << "Size(" << getSize() << "), "
832 << "File(" << getFile() << "), "
833 << "Line(" << getLine() << "), "
834 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000835}
836#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000837
838//===----------------------------------------------------------------------===//
839
Jim Laskeyf8913f12006-03-01 17:53:02 +0000840CompositeTypeDesc::CompositeTypeDesc(unsigned T)
841: DerivedTypeDesc(T)
842, Elements()
843{}
844
Jim Laskey9c4447a2006-03-01 20:39:36 +0000845// Implement isa/cast/dyncast.
846bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
847 unsigned T = D->getTag();
848 switch (T) {
849 case DW_TAG_array_type:
850 case DW_TAG_structure_type:
851 case DW_TAG_union_type:
852 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000853 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000854 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000855 return true;
856 default: break;
857 }
858 return false;
859}
860
Jim Laskeyf8913f12006-03-01 17:53:02 +0000861/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
862///
863void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000864 DerivedTypeDesc::ApplyToFields(Visitor);
865
Jim Laskeyf8913f12006-03-01 17:53:02 +0000866 Visitor->Apply(Elements);
867}
868
869/// getDescString - Return a string used to compose global names and labels.
870///
871const char *CompositeTypeDesc::getDescString() const {
872 return "llvm.dbg.compositetype";
873}
874
875/// getTypeString - Return a string used to label this descriptor's type.
876///
877const char *CompositeTypeDesc::getTypeString() const {
878 return "llvm.dbg.compositetype.type";
879}
880
881#ifndef NDEBUG
882void CompositeTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000883 cerr << getDescString() << " "
884 << "Version(" << getVersion() << "), "
885 << "Tag(" << getTag() << "), "
886 << "Context(" << getContext() << "), "
887 << "Name(\"" << getName() << "\"), "
888 << "Size(" << getSize() << "), "
889 << "File(" << getFile() << "), "
890 << "Line(" << getLine() << "), "
891 << "FromType(" << getFromType() << "), "
892 << "Elements.size(" << Elements.size() << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000893}
894#endif
895
896//===----------------------------------------------------------------------===//
897
898SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000899: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000900, Lo(0)
901, Hi(0)
902{}
903
Jim Laskey9c4447a2006-03-01 20:39:36 +0000904// Implement isa/cast/dyncast.
905bool SubrangeDesc::classof(const DebugInfoDesc *D) {
906 return D->getTag() == DW_TAG_subrange_type;
907}
908
Jim Laskeyf8913f12006-03-01 17:53:02 +0000909/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
910///
911void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
912 DebugInfoDesc::ApplyToFields(Visitor);
913
914 Visitor->Apply(Lo);
915 Visitor->Apply(Hi);
916}
917
918/// getDescString - Return a string used to compose global names and labels.
919///
920const char *SubrangeDesc::getDescString() const {
921 return "llvm.dbg.subrange";
922}
923
924/// getTypeString - Return a string used to label this descriptor's type.
925///
926const char *SubrangeDesc::getTypeString() const {
927 return "llvm.dbg.subrange.type";
928}
929
930#ifndef NDEBUG
931void SubrangeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000932 cerr << getDescString() << " "
933 << "Version(" << getVersion() << "), "
934 << "Tag(" << getTag() << "), "
935 << "Lo(" << Lo << "), "
936 << "Hi(" << Hi << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000937}
938#endif
939
940//===----------------------------------------------------------------------===//
941
Jim Laskey6a3eb012006-03-01 23:52:37 +0000942EnumeratorDesc::EnumeratorDesc()
943: DebugInfoDesc(DW_TAG_enumerator)
944, Name("")
945, Value(0)
946{}
947
948// Implement isa/cast/dyncast.
949bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
950 return D->getTag() == DW_TAG_enumerator;
951}
952
953/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
954///
955void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
956 DebugInfoDesc::ApplyToFields(Visitor);
957
958 Visitor->Apply(Name);
959 Visitor->Apply(Value);
960}
961
962/// getDescString - Return a string used to compose global names and labels.
963///
964const char *EnumeratorDesc::getDescString() const {
965 return "llvm.dbg.enumerator";
966}
967
968/// getTypeString - Return a string used to label this descriptor's type.
969///
970const char *EnumeratorDesc::getTypeString() const {
971 return "llvm.dbg.enumerator.type";
972}
973
974#ifndef NDEBUG
975void EnumeratorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000976 cerr << getDescString() << " "
977 << "Version(" << getVersion() << "), "
978 << "Tag(" << getTag() << "), "
979 << "Name(" << Name << "), "
980 << "Value(" << Value << ")\n";
Jim Laskey6a3eb012006-03-01 23:52:37 +0000981}
982#endif
983
984//===----------------------------------------------------------------------===//
985
Jim Laskeyb8509c52006-03-23 18:07:55 +0000986VariableDesc::VariableDesc(unsigned T)
987: DebugInfoDesc(T)
988, Context(NULL)
989, Name("")
990, File(NULL)
991, Line(0)
992, TyDesc(0)
993{}
994
995// Implement isa/cast/dyncast.
996bool VariableDesc::classof(const DebugInfoDesc *D) {
997 unsigned T = D->getTag();
998 switch (T) {
999 case DW_TAG_auto_variable:
1000 case DW_TAG_arg_variable:
1001 case DW_TAG_return_variable:
1002 return true;
1003 default: break;
1004 }
1005 return false;
1006}
1007
1008/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1009///
1010void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1011 DebugInfoDesc::ApplyToFields(Visitor);
1012
1013 Visitor->Apply(Context);
1014 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001015 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001016 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001017 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001018}
1019
1020/// getDescString - Return a string used to compose global names and labels.
1021///
1022const char *VariableDesc::getDescString() const {
1023 return "llvm.dbg.variable";
1024}
1025
1026/// getTypeString - Return a string used to label this descriptor's type.
1027///
1028const char *VariableDesc::getTypeString() const {
1029 return "llvm.dbg.variable.type";
1030}
1031
1032#ifndef NDEBUG
1033void VariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001034 cerr << getDescString() << " "
1035 << "Version(" << getVersion() << "), "
1036 << "Tag(" << getTag() << "), "
1037 << "Context(" << Context << "), "
1038 << "Name(\"" << Name << "\"), "
1039 << "File(" << File << "), "
1040 << "Line(" << Line << "), "
1041 << "TyDesc(" << TyDesc << ")\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001042}
1043#endif
1044
1045//===----------------------------------------------------------------------===//
1046
Jim Laskeyce72b172006-02-11 01:01:30 +00001047GlobalDesc::GlobalDesc(unsigned T)
1048: AnchoredDesc(T)
1049, Context(0)
1050, Name("")
Jim Laskey2172f962006-11-30 14:35:45 +00001051, FullName("")
1052, LinkageName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001053, File(NULL)
1054, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001055, TyDesc(NULL)
1056, IsStatic(false)
1057, IsDefinition(false)
1058{}
1059
1060/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001061///
Jim Laskeyce72b172006-02-11 01:01:30 +00001062void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1063 AnchoredDesc::ApplyToFields(Visitor);
1064
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001065 Visitor->Apply(Context);
1066 Visitor->Apply(Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001067 Visitor->Apply(FullName);
1068 Visitor->Apply(LinkageName);
Jim Laskey7089f452006-06-16 13:14:03 +00001069 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001070 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001071 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001072 Visitor->Apply(IsStatic);
1073 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001074}
1075
1076//===----------------------------------------------------------------------===//
1077
1078GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001079: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001080, Global(NULL)
1081{}
1082
Jim Laskey9c4447a2006-03-01 20:39:36 +00001083// Implement isa/cast/dyncast.
1084bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1085 return D->getTag() == DW_TAG_variable;
1086}
1087
Jim Laskeyce72b172006-02-11 01:01:30 +00001088/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1089///
1090void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1091 GlobalDesc::ApplyToFields(Visitor);
1092
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001093 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001094}
1095
Jim Laskeyce72b172006-02-11 01:01:30 +00001096/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001097///
Jim Laskeyce72b172006-02-11 01:01:30 +00001098const char *GlobalVariableDesc::getDescString() const {
1099 return "llvm.dbg.global_variable";
1100}
1101
1102/// getTypeString - Return a string used to label this descriptors type.
1103///
1104const char *GlobalVariableDesc::getTypeString() const {
1105 return "llvm.dbg.global_variable.type";
1106}
1107
1108/// getAnchorString - Return a string used to label this descriptor's anchor.
1109///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001110const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001111const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001112 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001113}
1114
Jim Laskey86cbdba2006-02-06 15:33:21 +00001115#ifndef NDEBUG
1116void GlobalVariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001117 cerr << getDescString() << " "
1118 << "Version(" << getVersion() << "), "
1119 << "Tag(" << getTag() << "), "
1120 << "Anchor(" << getAnchor() << "), "
1121 << "Name(\"" << getName() << "\"), "
1122 << "FullName(\"" << getFullName() << "\"), "
1123 << "LinkageName(\"" << getLinkageName() << "\"), "
1124 << "File(" << getFile() << "),"
1125 << "Line(" << getLine() << "),"
1126 << "Type(" << getType() << "), "
1127 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1128 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1129 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001130}
1131#endif
1132
1133//===----------------------------------------------------------------------===//
1134
Jim Laskeyce72b172006-02-11 01:01:30 +00001135SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001136: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001137{}
1138
Jim Laskey9c4447a2006-03-01 20:39:36 +00001139// Implement isa/cast/dyncast.
1140bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1141 return D->getTag() == DW_TAG_subprogram;
1142}
1143
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001144/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001145/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001146void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001147 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001148}
1149
Jim Laskeyce72b172006-02-11 01:01:30 +00001150/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001151///
Jim Laskeyce72b172006-02-11 01:01:30 +00001152const char *SubprogramDesc::getDescString() const {
1153 return "llvm.dbg.subprogram";
1154}
1155
1156/// getTypeString - Return a string used to label this descriptors type.
1157///
1158const char *SubprogramDesc::getTypeString() const {
1159 return "llvm.dbg.subprogram.type";
1160}
1161
1162/// getAnchorString - Return a string used to label this descriptor's anchor.
1163///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001164const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001165const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001166 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001167}
1168
Jim Laskey86cbdba2006-02-06 15:33:21 +00001169#ifndef NDEBUG
1170void SubprogramDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001171 cerr << getDescString() << " "
1172 << "Version(" << getVersion() << "), "
1173 << "Tag(" << getTag() << "), "
1174 << "Anchor(" << getAnchor() << "), "
1175 << "Name(\"" << getName() << "\"), "
1176 << "FullName(\"" << getFullName() << "\"), "
1177 << "LinkageName(\"" << getLinkageName() << "\"), "
1178 << "File(" << getFile() << "),"
1179 << "Line(" << getLine() << "),"
1180 << "Type(" << getType() << "), "
1181 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1182 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001183}
1184#endif
1185
Jim Laskey45ccae52006-02-28 20:15:07 +00001186//===----------------------------------------------------------------------===//
1187
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001188BlockDesc::BlockDesc()
1189: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001190, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001191{}
1192
1193// Implement isa/cast/dyncast.
1194bool BlockDesc::classof(const DebugInfoDesc *D) {
1195 return D->getTag() == DW_TAG_lexical_block;
1196}
1197
1198/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1199///
1200void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1201 DebugInfoDesc::ApplyToFields(Visitor);
1202
Jim Laskeyb8509c52006-03-23 18:07:55 +00001203 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001204}
1205
1206/// getDescString - Return a string used to compose global names and labels.
1207///
1208const char *BlockDesc::getDescString() const {
1209 return "llvm.dbg.block";
1210}
1211
1212/// getTypeString - Return a string used to label this descriptors type.
1213///
1214const char *BlockDesc::getTypeString() const {
1215 return "llvm.dbg.block.type";
1216}
1217
1218#ifndef NDEBUG
1219void BlockDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001220 cerr << getDescString() << " "
1221 << "Version(" << getVersion() << "), "
1222 << "Tag(" << getTag() << "),"
1223 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001224}
1225#endif
1226
1227//===----------------------------------------------------------------------===//
1228
Jim Laskey86cbdba2006-02-06 15:33:21 +00001229DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001230 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001231}
1232DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001233 // Handle NULL.
1234 if (!GV) return NULL;
1235
Jim Laskey86cbdba2006-02-06 15:33:21 +00001236 // Check to see if it has been already deserialized.
1237 DebugInfoDesc *&Slot = GlobalDescs[GV];
1238 if (Slot) return Slot;
1239
1240 // Get the Tag from the global.
1241 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1242
Jim Laskey86cbdba2006-02-06 15:33:21 +00001243 // Create an empty instance of the correct sort.
1244 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001245
Jim Laskey21407982006-03-14 18:37:57 +00001246 // If not a user defined descriptor.
1247 if (Slot) {
1248 // Deserialize the fields.
1249 DIDeserializeVisitor DRAM(*this, GV);
1250 DRAM.ApplyToFields(Slot);
1251 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001252
1253 return Slot;
1254}
1255
1256//===----------------------------------------------------------------------===//
1257
1258/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001259///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001260const PointerType *DISerializer::getStrPtrType() {
1261 // If not already defined.
1262 if (!StrPtrTy) {
1263 // Construct the pointer to signed bytes.
Reid Spencer47857812006-12-31 05:55:36 +00001264 StrPtrTy = PointerType::get(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001265 }
1266
1267 return StrPtrTy;
1268}
1269
1270/// getEmptyStructPtrType - Return a "{ }*" type.
1271///
1272const PointerType *DISerializer::getEmptyStructPtrType() {
1273 // If not already defined.
1274 if (!EmptyStructPtrTy) {
1275 // Construct the empty structure type.
1276 const StructType *EmptyStructTy =
1277 StructType::get(std::vector<const Type*>());
1278 // Construct the pointer to empty structure type.
1279 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1280 }
1281
1282 return EmptyStructPtrTy;
1283}
1284
1285/// getTagType - Return the type describing the specified descriptor (via tag.)
1286///
1287const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1288 // Attempt to get the previously defined type.
1289 StructType *&Ty = TagTypes[DD->getTag()];
1290
1291 // If not already defined.
1292 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001293 // Set up fields vector.
1294 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001295 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001296 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001297 GTAM.ApplyToFields(DD);
1298
1299 // Construct structured type.
1300 Ty = StructType::get(Fields);
1301
Jim Laskey86cbdba2006-02-06 15:33:21 +00001302 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001303 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001304 }
1305
1306 return Ty;
1307}
1308
1309/// getString - Construct the string as constant string global.
1310///
Jim Laskeyce72b172006-02-11 01:01:30 +00001311Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001312 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001313 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001314 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001315 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001316 // If empty string then use a sbyte* null instead.
1317 if (String.empty()) {
1318 Slot = ConstantPointerNull::get(getStrPtrType());
1319 } else {
1320 // Construct string as an llvm constant.
1321 Constant *ConstStr = ConstantArray::get(String);
1322 // Otherwise create and return a new string global.
1323 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1324 GlobalVariable::InternalLinkage,
1325 ConstStr, "str", M);
1326 StrGV->setSection("llvm.metadata");
1327 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001328 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001329 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001330 return Slot;
1331
Jim Laskey86cbdba2006-02-06 15:33:21 +00001332}
1333
1334/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1335/// so that it can be serialized to a .bc or .ll file.
1336GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1337 // Check if the DebugInfoDesc is already in the map.
1338 GlobalVariable *&Slot = DescGlobals[DD];
1339
1340 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1341 if (Slot) return Slot;
1342
Jim Laskey86cbdba2006-02-06 15:33:21 +00001343 // Get the type associated with the Tag.
1344 const StructType *Ty = getTagType(DD);
1345
1346 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001347 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1348 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001349 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001350
1351 // Insert new GlobalVariable in DescGlobals map.
1352 Slot = GV;
1353
1354 // Set up elements vector
1355 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001356 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001357 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001358 SRAM.ApplyToFields(DD);
1359
1360 // Set the globals initializer.
1361 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1362
1363 return GV;
1364}
1365
1366//===----------------------------------------------------------------------===//
1367
Jim Laskey86cbdba2006-02-06 15:33:21 +00001368/// Verify - Return true if the GlobalVariable appears to be a valid
1369/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001370bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001371 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001372}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001373bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001374 // NULLs are valid.
1375 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001376
Jim Laskey98e04102006-03-26 22:45:20 +00001377 // Check prior validity.
1378 unsigned &ValiditySlot = Validity[GV];
1379
1380 // If visited before then use old state.
1381 if (ValiditySlot) return ValiditySlot == Valid;
1382
1383 // Assume validity for the time being (recursion.)
1384 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001385
1386 // Make sure the global is internal or link once (anchor.)
1387 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1388 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1389 ValiditySlot = Invalid;
1390 return false;
1391 }
Jim Laskey98e04102006-03-26 22:45:20 +00001392
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001393 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001394 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001395
1396 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001397 if (Tag == DW_TAG_invalid) {
1398 ValiditySlot = Valid;
1399 return true;
1400 }
1401
1402 // Get the Version.
1403 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1404
1405 // Check for version mismatch.
1406 if (Version != LLVMDebugVersion) {
1407 ValiditySlot = Invalid;
1408 return false;
1409 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001410
Jim Laskey86cbdba2006-02-06 15:33:21 +00001411 // Construct an empty DebugInfoDesc.
1412 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001413
1414 // Allow for user defined descriptors.
1415 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001416
1417 // Get the initializer constant.
1418 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1419
1420 // Get the operand count.
1421 unsigned N = CI->getNumOperands();
1422
1423 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001424 unsigned &CountSlot = Counts[Tag];
1425 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001426 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001427 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001428 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001429 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001430 }
1431
Jim Laskey21407982006-03-14 18:37:57 +00001432 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001433 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001434 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001435 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001436 return false;
1437 }
1438
1439 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001440 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001441 VRAM.ApplyToFields(DD);
1442
1443 // Release empty DebugInfoDesc.
1444 delete DD;
1445
Jim Laskey98e04102006-03-26 22:45:20 +00001446 // If fields are not valid.
1447 if (!VRAM.isValid()) {
1448 ValiditySlot = Invalid;
1449 return false;
1450 }
1451
1452 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001453}
1454
1455//===----------------------------------------------------------------------===//
1456
Jim Laskeyb8509c52006-03-23 18:07:55 +00001457DebugScope::~DebugScope() {
1458 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1459 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1460}
1461
1462//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001463
Jim Laskey6da18642007-01-26 21:38:26 +00001464MachineModuleInfo::MachineModuleInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001465: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001466, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001467, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001468, Directories()
1469, SourceFiles()
1470, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001471, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001472, ScopeMap()
1473, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001474, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001475, LandingPads()
Jim Laskey41886992006-04-07 16:34:46 +00001476{}
Jim Laskey6da18642007-01-26 21:38:26 +00001477MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001478
1479}
1480
Jim Laskey6da18642007-01-26 21:38:26 +00001481/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001482///
Jim Laskey6da18642007-01-26 21:38:26 +00001483bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001484 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001485}
1486
Jim Laskey6da18642007-01-26 21:38:26 +00001487/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001488///
Jim Laskey6da18642007-01-26 21:38:26 +00001489bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001490 return false;
1491}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001492
Jim Laskey6da18642007-01-26 21:38:26 +00001493/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001494///
Jim Laskey6da18642007-01-26 21:38:26 +00001495void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001496 // Coming soon.
1497}
1498
Jim Laskey6da18642007-01-26 21:38:26 +00001499/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001500///
Jim Laskey6da18642007-01-26 21:38:26 +00001501void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001502 // Clean up scope information.
1503 if (RootScope) {
1504 delete RootScope;
1505 ScopeMap.clear();
1506 RootScope = NULL;
1507 }
1508
Jim Laskeyb82313f2007-02-01 16:31:34 +00001509 // Clean up line info.
1510 Lines.clear();
1511
Jim Laskey41886992006-04-07 16:34:46 +00001512 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001513 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001514
1515 // Clean up exception info.
1516 LandingPads.clear();
1517 TypeInfos.clear();
Jim Laskey41886992006-04-07 16:34:46 +00001518}
1519
Jim Laskeyd96185a2006-02-13 12:50:39 +00001520/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001521///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001522// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001523DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001524 return DR.Deserialize(V);
1525}
1526
1527/// Verify - Verify that a Value is debug information descriptor.
1528///
Jim Laskey6da18642007-01-26 21:38:26 +00001529bool MachineModuleInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001530 return VR.Verify(V);
1531}
1532
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001533/// AnalyzeModule - Scan the module for global debug information.
1534///
Jim Laskey6da18642007-01-26 21:38:26 +00001535void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001536 SetupCompileUnits(M);
1537}
1538
Jim Laskeyc1c47c32007-01-29 23:40:33 +00001539/// needsFrameInfo - Returns true if we need to gather callee-saved register
1540/// move info for the frame.
1541bool MachineModuleInfo::needsFrameInfo() const {
1542 return hasDebugInfo() || ExceptionHandling;
1543}
1544
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001545/// SetupCompileUnits - Set up the unique vector of compile units.
1546///
Jim Laskey6da18642007-01-26 21:38:26 +00001547void MachineModuleInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001548 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001549
Jim Laskey0420f2a2006-02-22 19:02:11 +00001550 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1551 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001552 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001553}
1554
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001555/// getCompileUnits - Return a vector of debug compile units.
1556///
Jim Laskey6da18642007-01-26 21:38:26 +00001557const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001558 return CompileUnits;
1559}
1560
Jim Laskey0420f2a2006-02-22 19:02:11 +00001561/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1562/// named GlobalVariable.
1563std::vector<GlobalVariable*>
Jim Laskey6da18642007-01-26 21:38:26 +00001564MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1565 const std::string &RootName) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001566 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001567}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001568
1569/// RecordLabel - Records location information and associates it with a
1570/// debug label. Returns a unique label ID used to generate a label and
1571/// provide correspondence to the source line list.
Jim Laskey6da18642007-01-26 21:38:26 +00001572unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column,
Jim Laskeyb8509c52006-03-23 18:07:55 +00001573 unsigned Source) {
1574 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001575 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001576 return ID;
1577}
1578
1579/// RecordSource - Register a source file with debug info. Returns an source
1580/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001581unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1582 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001583 unsigned DirectoryID = Directories.insert(Directory);
1584 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1585}
Jim Laskey6da18642007-01-26 21:38:26 +00001586unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001587 return RecordSource(CompileUnit->getDirectory(),
1588 CompileUnit->getFileName());
1589}
1590
1591/// RecordRegionStart - Indicate the start of a region.
1592///
Jim Laskey6da18642007-01-26 21:38:26 +00001593unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001594 // FIXME - need to be able to handle split scopes because of bb cloning.
1595 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1596 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1597 unsigned ID = NextLabelID();
1598 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1599 return ID;
1600}
1601
1602/// RecordRegionEnd - Indicate the end of a region.
1603///
Jim Laskey6da18642007-01-26 21:38:26 +00001604unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001605 // FIXME - need to be able to handle split scopes because of bb cloning.
1606 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1607 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1608 unsigned ID = NextLabelID();
1609 Scope->setEndLabelID(ID);
1610 return ID;
1611}
1612
1613/// RecordVariable - Indicate the declaration of a local variable.
1614///
Jim Laskey6da18642007-01-26 21:38:26 +00001615void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001616 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1617 DebugScope *Scope = getOrCreateScope(VD->getContext());
1618 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1619 Scope->AddVariable(DV);
1620}
1621
1622/// getOrCreateScope - Returns the scope associated with the given descriptor.
1623///
Jim Laskey6da18642007-01-26 21:38:26 +00001624DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001625 DebugScope *&Slot = ScopeMap[ScopeDesc];
1626 if (!Slot) {
1627 // FIXME - breaks down when the context is an inlined function.
1628 DebugInfoDesc *ParentDesc = NULL;
1629 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1630 ParentDesc = Block->getContext();
1631 }
1632 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1633 Slot = new DebugScope(Parent, ScopeDesc);
1634 if (Parent) {
1635 Parent->AddScope(Slot);
1636 } else if (RootScope) {
1637 // FIXME - Add inlined function scopes to the root so we can delete
1638 // them later. Long term, handle inlined functions properly.
1639 RootScope->AddScope(Slot);
1640 } else {
1641 // First function is top level function.
1642 RootScope = Slot;
1643 }
1644 }
1645 return Slot;
1646}
1647
Jim Laskey59667fe2007-02-21 22:38:31 +00001648//===-EH-------------------------------------------------------------------===//
1649
1650/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1651/// specified MachineBasicBlock.
1652LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1653 (MachineBasicBlock *LandingPad) {
1654 unsigned N = LandingPads.size();
1655 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001656 LandingPadInfo &LP = LandingPads[i];
1657 if (LP.LandingPadBlock == LandingPad)
1658 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001659 }
1660
1661 LandingPads.push_back(LandingPadInfo(LandingPad));
1662 return LandingPads[N];
1663}
1664
1665/// addInvoke - Provide the begin and end labels of an invoke style call and
1666/// associate it with a try landing pad block.
1667void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1668 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001669 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1670 if (!LP.BeginLabel) LP.BeginLabel = BeginLabel;
1671 LP.EndLabel = EndLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001672}
1673
1674/// addLandingPad - Provide the label of a try LandingPad block.
1675///
1676unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1677 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001678 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1679 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001680 return LandingPadLabel;
1681}
1682
1683/// addPersonality - Provide the personality function for the exception
1684/// information.
1685void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1686 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001687 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1688 LP.Personality = Personality;
Jim Laskey59667fe2007-02-21 22:38:31 +00001689}
1690
1691/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1692///
1693void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1694 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001695 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001696 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001697 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001698}
1699
Jim Laskey59e84342007-03-01 20:25:32 +00001700/// setIsFilterLandingPad - Indicates that the landing pad is a throw filter.
1701///
1702void MachineModuleInfo::setIsFilterLandingPad(MachineBasicBlock *LandingPad) {
1703 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1704 LP.IsFilter = true;
1705}
1706
Jim Laskey59667fe2007-02-21 22:38:31 +00001707/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1708/// pads.
1709void MachineModuleInfo::TidyLandingPads() {
1710 for (unsigned i = 0; i != LandingPads.size(); ) {
1711 LandingPadInfo &LandingPad = LandingPads[i];
1712 LandingPad.BeginLabel = MappedLabel(LandingPad.BeginLabel);
1713 LandingPad.EndLabel = MappedLabel(LandingPad.EndLabel);
1714 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1715
1716 if (!LandingPad.BeginLabel ||
1717 !LandingPad.EndLabel ||
1718 !LandingPad.LandingPadLabel) {
1719 LandingPads.erase(LandingPads.begin() + i);
1720 continue;
1721 }
1722
1723 ++i;
1724 }
1725}
1726
1727/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1728/// function wide.
1729unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1730 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1731 if (TypeInfos[i] == TI) return i + 1;
1732
1733 TypeInfos.push_back(TI);
1734 return TypeInfos.size();
1735}
1736
1737/// getLandingPadInfos - Return a reference to the landing pad info for the
1738/// current function.
1739Function *MachineModuleInfo::getPersonality() const {
1740 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1741}
1742
1743
Jim Laskey9d4209f2006-11-07 19:33:46 +00001744//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00001745/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1746/// a info consumer to determine if the range of two labels is empty, by seeing
1747/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00001748
1749namespace llvm {
1750
1751struct DebugLabelFolder : public MachineFunctionPass {
1752 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00001753 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00001754};
1755
1756bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00001757 // Get machine module info.
1758 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1759 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001760 // Get target instruction info.
1761 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
1762 if (!TII) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001763
1764 // Track if change is made.
1765 bool MadeChange = false;
1766 // No prior label to begin.
1767 unsigned PriorLabel = 0;
1768
1769 // Iterate through basic blocks.
1770 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1771 BB != E; ++BB) {
1772 // Iterate through instructions.
1773 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00001774 // Is it a label.
Jim Laskey1ee29252007-01-26 14:34:52 +00001775 if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00001776 // The label ID # is always operand #0, an immediate.
1777 unsigned NextLabel = I->getOperand(0).getImm();
1778
1779 // If there was an immediate prior label.
1780 if (PriorLabel) {
1781 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00001782 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00001783 // Delete the current label.
1784 I = BB->erase(I);
1785 // Indicate a change has been made.
1786 MadeChange = true;
1787 continue;
1788 } else {
1789 // Start a new round.
1790 PriorLabel = NextLabel;
1791 }
1792 } else {
1793 // No consecutive labels.
1794 PriorLabel = 0;
1795 }
1796
1797 ++I;
1798 }
1799 }
1800
1801 return MadeChange;
1802}
1803
1804FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1805
1806}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001807