blob: 1720745e4459fede06d5fbbd7a370ad2b9e71419 [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"
Eric Christopher0d2b0ab2008-06-26 00:31:12 +000013#include "llvm/Analysis/ValueTracking.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineFunction.h"
Jim Laskey41886992006-04-07 16:34:46 +000016#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000017#include "llvm/Target/TargetInstrInfo.h"
18#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000019#include "llvm/Target/TargetOptions.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000020#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000021#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000022#include "llvm/Intrinsics.h"
23#include "llvm/Instructions.h"
24#include "llvm/Module.h"
25#include "llvm/Support/Dwarf.h"
Bill Wendling832171c2006-12-07 20:04:42 +000026#include "llvm/Support/Streams.h"
Jim Laskey6af56812006-01-04 13:36:38 +000027using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000028using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000029
30// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman844731a2008-05-13 00:00:25 +000031static RegisterPass<MachineModuleInfo>
32X("machinemoduleinfo", "Module Information");
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++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000109 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000110 return NULL;
111 }
112 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000113 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000114 }
115 return NULL;
116}
117
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000118/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000119/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000120static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000121 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
122 return true;
123 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000124 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000125 return isa<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000126 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
127 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000128 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000129 return false;
130 }
131 return isa<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000132 }
133 }
134 return false;
135}
136
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000137/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000138///
Reid Spencerb83eb642006-10-20 07:07:24 +0000139static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000140 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000141 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000142
Jim Laskey86cbdba2006-02-06 15:33:21 +0000143 // Get the initializer constant.
144 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000145 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000146
Jim Laskey86cbdba2006-02-06 15:33:21 +0000147 // Check if there is at least i + 1 operands.
148 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000149 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000150
Jim Laskey86cbdba2006-02-06 15:33:21 +0000151 // Check constant.
Reid Spencerb83eb642006-10-20 07:07:24 +0000152 return dyn_cast<ConstantInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000153}
Reid Spencerb83eb642006-10-20 07:07:24 +0000154
Jim Laskey86cbdba2006-02-06 15:33:21 +0000155//===----------------------------------------------------------------------===//
156
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000157/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000158/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000159void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000160 DD->ApplyToFields(this);
161}
162
Dan Gohman844731a2008-05-13 00:00:25 +0000163namespace {
164
Jim Laskey86cbdba2006-02-06 15:33:21 +0000165//===----------------------------------------------------------------------===//
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++);
Eric Christopher0d2b0ab2008-06-26 00:31:12 +0000234 std::string S;
235 if (GetConstantStringInfo(C, S))
236 Field = S;
237 else
238 Field = "";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000239 }
240 virtual void Apply(DebugInfoDesc *&Field) {
241 Constant *C = CI->getOperand(I++);
242 Field = DR.Deserialize(C);
243 }
244 virtual void Apply(GlobalVariable *&Field) {
245 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000246 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000247 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000248 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000249 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000250 Constant *C = CI->getOperand(I++);
251 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000252 if (GV->hasInitializer()) {
253 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
254 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
255 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
256 DebugInfoDesc *DE = DR.Deserialize(GVE);
257 Field.push_back(DE);
258 }
259 } else if (GV->getInitializer()->isNullValue()) {
260 if (const ArrayType *T =
261 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
262 Field.resize(T->getNumElements());
263 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000264 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000265 }
266 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000267};
268
269//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000270/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000271/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000272class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000273private:
274 DISerializer &SR; // Active serializer.
275 std::vector<Constant*> &Elements; // Element accumulator.
276
277public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000278 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
279 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000280 , SR(S)
281 , Elements(E)
282 {}
283
284 /// Apply - Set the value of each of the fields.
285 ///
286 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000287 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000288 }
289 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000290 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000291 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000292 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000293 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000294 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000295 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000296 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000297 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000298 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000299 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000300 }
301 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000302 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000303 }
304 virtual void Apply(DebugInfoDesc *&Field) {
305 GlobalVariable *GV = NULL;
306
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000307 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000308 if (Field) GV = SR.Serialize(Field);
309
310 // FIXME - At some point should use specific type.
311 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
312
313 if (GV) {
314 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000315 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000316 } else {
317 // Use NULL.
318 Elements.push_back(ConstantPointerNull::get(EmptyTy));
319 }
320 }
321 virtual void Apply(GlobalVariable *&Field) {
322 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000323 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000324 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000325 } else {
326 Elements.push_back(ConstantPointerNull::get(EmptyTy));
327 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000328 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000329 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
330 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
331 unsigned N = Field.size();
332 ArrayType *AT = ArrayType::get(EmptyTy, N);
333 std::vector<Constant *> ArrayElements;
334
335 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000336 if (DebugInfoDesc *Element = Field[i]) {
337 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000338 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000339 ArrayElements.push_back(cast<Constant>(CE));
340 } else {
341 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
342 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000343 }
344
345 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000346 GlobalVariable *CAGV = new GlobalVariable(AT, true,
347 GlobalValue::InternalLinkage,
348 CA, "llvm.dbg.array",
349 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000350 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000351 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000352 Elements.push_back(CAE);
353 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000354};
355
356//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000357/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000358/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000359class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000360private:
361 DISerializer &SR; // Active serializer.
362 std::vector<const Type*> &Fields; // Type accumulator.
363
364public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000365 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
366 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000367 , SR(S)
368 , Fields(F)
369 {}
370
371 /// Apply - Set the value of each of the fields.
372 ///
373 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000374 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000375 }
376 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000377 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000378 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000379 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000380 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000381 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000382 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000383 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000384 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000385 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000386 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000387 }
388 virtual void Apply(std::string &Field) {
389 Fields.push_back(SR.getStrPtrType());
390 }
391 virtual void Apply(DebugInfoDesc *&Field) {
392 // FIXME - At some point should use specific type.
393 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
394 Fields.push_back(EmptyTy);
395 }
396 virtual void Apply(GlobalVariable *&Field) {
397 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
398 Fields.push_back(EmptyTy);
399 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000400 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
401 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
402 Fields.push_back(EmptyTy);
403 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000404};
405
406//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000407/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000408/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000409class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000410private:
411 DIVerifier &VR; // Active verifier.
412 bool IsValid; // Validity status.
413 unsigned I; // Current operand index.
414 ConstantStruct *CI; // GlobalVariable constant initializer.
415
416public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000417 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
418 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000419 , VR(V)
420 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000421 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000422 , CI(cast<ConstantStruct>(GV->getInitializer()))
423 {
424 }
425
426 // Accessors.
427 bool isValid() const { return IsValid; }
428
429 /// Apply - Set the value of each of the fields.
430 ///
431 virtual void Apply(int &Field) {
432 Constant *C = CI->getOperand(I++);
433 IsValid = IsValid && isa<ConstantInt>(C);
434 }
435 virtual void Apply(unsigned &Field) {
436 Constant *C = CI->getOperand(I++);
437 IsValid = IsValid && isa<ConstantInt>(C);
438 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000439 virtual void Apply(int64_t &Field) {
440 Constant *C = CI->getOperand(I++);
441 IsValid = IsValid && isa<ConstantInt>(C);
442 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000443 virtual void Apply(uint64_t &Field) {
444 Constant *C = CI->getOperand(I++);
445 IsValid = IsValid && isa<ConstantInt>(C);
446 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000447 virtual void Apply(bool &Field) {
448 Constant *C = CI->getOperand(I++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000449 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000450 }
451 virtual void Apply(std::string &Field) {
452 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000453 IsValid = IsValid &&
454 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000455 }
456 virtual void Apply(DebugInfoDesc *&Field) {
457 // FIXME - Prepare the correct descriptor.
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 }
461 virtual void Apply(GlobalVariable *&Field) {
462 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000463 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000464 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000465 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
466 Constant *C = CI->getOperand(I++);
467 IsValid = IsValid && isGlobalVariable(C);
468 if (!IsValid) return;
469
470 GlobalVariable *GV = getGlobalVariable(C);
471 IsValid = IsValid && GV && GV->hasInitializer();
472 if (!IsValid) return;
473
474 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
475 IsValid = IsValid && CA;
476 if (!IsValid) return;
477
478 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
479 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
480 if (!IsValid) return;
481
482 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
483 VR.Verify(GVE);
484 }
485 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000486};
487
Dan Gohman844731a2008-05-13 00:00:25 +0000488}
Jim Laskeyce72b172006-02-11 01:01:30 +0000489
Jim Laskey86cbdba2006-02-06 15:33:21 +0000490//===----------------------------------------------------------------------===//
491
Jim Laskeyed4e5662006-06-14 14:45:39 +0000492/// TagFromGlobal - Returns the tag number from a debug info descriptor
493/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000494unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000495 ConstantInt *C = getUIntOperand(GV, 0);
496 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000497 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000498}
499
500/// VersionFromGlobal - Returns the version number from a debug info
501/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
502/// int.
503unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000504 ConstantInt *C = getUIntOperand(GV, 0);
505 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000506 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000507}
508
509/// DescFactory - Create an instance of debug info descriptor based on Tag.
510/// Return NULL if not a recognized Tag.
511DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
512 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000513 case DW_TAG_anchor: return new AnchorDesc();
514 case DW_TAG_compile_unit: return new CompileUnitDesc();
515 case DW_TAG_variable: return new GlobalVariableDesc();
516 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000517 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000518 case DW_TAG_base_type: return new BasicTypeDesc();
519 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000520 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000521 case DW_TAG_reference_type:
522 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000523 case DW_TAG_volatile_type:
524 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000525 case DW_TAG_member:
526 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000527 case DW_TAG_array_type:
528 case DW_TAG_structure_type:
529 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000530 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000531 case DW_TAG_vector_type:
532 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000533 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000534 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000535 case DW_TAG_return_variable:
536 case DW_TAG_arg_variable:
537 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000538 default: break;
539 }
540 return NULL;
541}
542
543/// getLinkage - get linkage appropriate for this type of descriptor.
544///
545GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
546 return GlobalValue::InternalLinkage;
547}
548
549/// ApplyToFields - Target the vistor to the fields of the descriptor.
550///
551void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
552 Visitor->Apply(Tag);
553}
554
555//===----------------------------------------------------------------------===//
556
Jim Laskey9c4447a2006-03-01 20:39:36 +0000557AnchorDesc::AnchorDesc()
558: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000559, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000560{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000561AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000562: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000563, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000564{}
565
566// Implement isa/cast/dyncast.
567bool AnchorDesc::classof(const DebugInfoDesc *D) {
568 return D->getTag() == DW_TAG_anchor;
569}
570
Jim Laskeyce72b172006-02-11 01:01:30 +0000571/// getLinkage - get linkage appropriate for this type of descriptor.
572///
573GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
574 return GlobalValue::LinkOnceLinkage;
575}
576
577/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
578///
579void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
580 DebugInfoDesc::ApplyToFields(Visitor);
581
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000582 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000583}
584
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000585/// getDescString - Return a string used to compose global names and labels. A
586/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000587/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000588/// to the list of names left external in the internalizer.
589/// ExternalNames.insert("llvm.dbg.compile_units");
590/// ExternalNames.insert("llvm.dbg.global_variables");
591/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000592const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000593 switch (AnchorTag) {
594 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
595 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
596 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
597 default: break;
598 }
599
600 assert(0 && "Tag does not have a case for anchor string");
601 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000602}
603
604/// getTypeString - Return a string used to label this descriptors type.
605///
606const char *AnchorDesc::getTypeString() const {
607 return "llvm.dbg.anchor.type";
608}
609
610#ifndef NDEBUG
611void AnchorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000612 cerr << getDescString() << " "
613 << "Version(" << getVersion() << "), "
614 << "Tag(" << getTag() << "), "
615 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000616}
617#endif
618
619//===----------------------------------------------------------------------===//
620
621AnchoredDesc::AnchoredDesc(unsigned T)
622: DebugInfoDesc(T)
623, Anchor(NULL)
624{}
625
626/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
627///
628void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
629 DebugInfoDesc::ApplyToFields(Visitor);
630
Jim Laskey7089f452006-06-16 13:14:03 +0000631 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000632}
633
634//===----------------------------------------------------------------------===//
635
636CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000637: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000638, Language(0)
639, FileName("")
640, Directory("")
641, Producer("")
642{}
643
Jim Laskey9c4447a2006-03-01 20:39:36 +0000644// Implement isa/cast/dyncast.
645bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
646 return D->getTag() == DW_TAG_compile_unit;
647}
648
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000649/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
650///
651void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000652 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000653
654 // Handle cases out of sync with compiler.
655 if (getVersion() == 0) {
656 unsigned DebugVersion;
657 Visitor->Apply(DebugVersion);
658 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000659
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000660 Visitor->Apply(Language);
661 Visitor->Apply(FileName);
662 Visitor->Apply(Directory);
663 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000664}
665
Jim Laskeyce72b172006-02-11 01:01:30 +0000666/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000667///
Jim Laskeyce72b172006-02-11 01:01:30 +0000668const char *CompileUnitDesc::getDescString() const {
669 return "llvm.dbg.compile_unit";
670}
671
672/// getTypeString - Return a string used to label this descriptors type.
673///
674const char *CompileUnitDesc::getTypeString() const {
675 return "llvm.dbg.compile_unit.type";
676}
677
678/// getAnchorString - Return a string used to label this descriptor's anchor.
679///
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000680const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000681const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000682 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000683}
684
Jim Laskey86cbdba2006-02-06 15:33:21 +0000685#ifndef NDEBUG
686void CompileUnitDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000687 cerr << getDescString() << " "
688 << "Version(" << getVersion() << "), "
689 << "Tag(" << getTag() << "), "
690 << "Anchor(" << getAnchor() << "), "
691 << "Language(" << Language << "), "
692 << "FileName(\"" << FileName << "\"), "
693 << "Directory(\"" << Directory << "\"), "
694 << "Producer(\"" << Producer << "\")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000695}
696#endif
697
698//===----------------------------------------------------------------------===//
699
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000700TypeDesc::TypeDesc(unsigned T)
701: DebugInfoDesc(T)
702, Context(NULL)
703, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000704, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000705, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000706, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000707, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000708, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000709, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000710{}
711
Jim Laskey69906002006-02-24 16:46:40 +0000712/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000713///
714void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
715 DebugInfoDesc::ApplyToFields(Visitor);
716
717 Visitor->Apply(Context);
718 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000719 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000720 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000721 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000722 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000723 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000724 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000725}
726
727/// getDescString - Return a string used to compose global names and labels.
728///
729const char *TypeDesc::getDescString() const {
730 return "llvm.dbg.type";
731}
732
733/// getTypeString - Return a string used to label this descriptor's type.
734///
735const char *TypeDesc::getTypeString() const {
736 return "llvm.dbg.type.type";
737}
738
739#ifndef NDEBUG
740void TypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000741 cerr << getDescString() << " "
742 << "Version(" << getVersion() << "), "
743 << "Tag(" << getTag() << "), "
744 << "Context(" << Context << "), "
745 << "Name(\"" << Name << "\"), "
746 << "File(" << File << "), "
747 << "Line(" << Line << "), "
748 << "Size(" << Size << "), "
749 << "Align(" << Align << "), "
750 << "Offset(" << Offset << "), "
751 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000752}
753#endif
754
755//===----------------------------------------------------------------------===//
756
757BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000758: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000759, Encoding(0)
760{}
761
Jim Laskey9c4447a2006-03-01 20:39:36 +0000762// Implement isa/cast/dyncast.
763bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
764 return D->getTag() == DW_TAG_base_type;
765}
766
Jim Laskey69906002006-02-24 16:46:40 +0000767/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000768///
769void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
770 TypeDesc::ApplyToFields(Visitor);
771
772 Visitor->Apply(Encoding);
773}
774
Jim Laskeyf8913f12006-03-01 17:53:02 +0000775/// getDescString - Return a string used to compose global names and labels.
776///
777const char *BasicTypeDesc::getDescString() const {
778 return "llvm.dbg.basictype";
779}
780
781/// getTypeString - Return a string used to label this descriptor's type.
782///
783const char *BasicTypeDesc::getTypeString() const {
784 return "llvm.dbg.basictype.type";
785}
786
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000787#ifndef NDEBUG
788void BasicTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000789 cerr << getDescString() << " "
790 << "Version(" << getVersion() << "), "
791 << "Tag(" << getTag() << "), "
792 << "Context(" << getContext() << "), "
793 << "Name(\"" << getName() << "\"), "
794 << "Size(" << getSize() << "), "
795 << "Encoding(" << Encoding << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000796}
797#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000798
Jim Laskey434b40b2006-02-23 22:37:30 +0000799//===----------------------------------------------------------------------===//
800
Jim Laskey69906002006-02-24 16:46:40 +0000801DerivedTypeDesc::DerivedTypeDesc(unsigned T)
802: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000803, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000804{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000805
Jim Laskey9c4447a2006-03-01 20:39:36 +0000806// Implement isa/cast/dyncast.
807bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
808 unsigned T = D->getTag();
809 switch (T) {
810 case DW_TAG_typedef:
811 case DW_TAG_pointer_type:
812 case DW_TAG_reference_type:
813 case DW_TAG_const_type:
814 case DW_TAG_volatile_type:
815 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000816 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000817 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000818 return true;
819 default: break;
820 }
821 return false;
822}
823
Jim Laskey69906002006-02-24 16:46:40 +0000824/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000825///
Jim Laskey69906002006-02-24 16:46:40 +0000826void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000827 TypeDesc::ApplyToFields(Visitor);
828
Jim Laskey7089f452006-06-16 13:14:03 +0000829 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000830}
831
Jim Laskeyf8913f12006-03-01 17:53:02 +0000832/// getDescString - Return a string used to compose global names and labels.
833///
834const char *DerivedTypeDesc::getDescString() const {
835 return "llvm.dbg.derivedtype";
836}
837
838/// getTypeString - Return a string used to label this descriptor's type.
839///
840const char *DerivedTypeDesc::getTypeString() const {
841 return "llvm.dbg.derivedtype.type";
842}
843
Jim Laskey434b40b2006-02-23 22:37:30 +0000844#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000845void DerivedTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000846 cerr << getDescString() << " "
847 << "Version(" << getVersion() << "), "
848 << "Tag(" << getTag() << "), "
849 << "Context(" << getContext() << "), "
850 << "Name(\"" << getName() << "\"), "
851 << "Size(" << getSize() << "), "
852 << "File(" << getFile() << "), "
853 << "Line(" << getLine() << "), "
854 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000855}
856#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000857
858//===----------------------------------------------------------------------===//
859
Jim Laskeyf8913f12006-03-01 17:53:02 +0000860CompositeTypeDesc::CompositeTypeDesc(unsigned T)
861: DerivedTypeDesc(T)
862, Elements()
863{}
864
Jim Laskey9c4447a2006-03-01 20:39:36 +0000865// Implement isa/cast/dyncast.
866bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
867 unsigned T = D->getTag();
868 switch (T) {
869 case DW_TAG_array_type:
870 case DW_TAG_structure_type:
871 case DW_TAG_union_type:
872 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000873 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000874 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000875 return true;
876 default: break;
877 }
878 return false;
879}
880
Jim Laskeyf8913f12006-03-01 17:53:02 +0000881/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
882///
883void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000884 DerivedTypeDesc::ApplyToFields(Visitor);
885
Jim Laskeyf8913f12006-03-01 17:53:02 +0000886 Visitor->Apply(Elements);
887}
888
889/// getDescString - Return a string used to compose global names and labels.
890///
891const char *CompositeTypeDesc::getDescString() const {
892 return "llvm.dbg.compositetype";
893}
894
895/// getTypeString - Return a string used to label this descriptor's type.
896///
897const char *CompositeTypeDesc::getTypeString() const {
898 return "llvm.dbg.compositetype.type";
899}
900
901#ifndef NDEBUG
902void CompositeTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000903 cerr << getDescString() << " "
904 << "Version(" << getVersion() << "), "
905 << "Tag(" << getTag() << "), "
906 << "Context(" << getContext() << "), "
907 << "Name(\"" << getName() << "\"), "
908 << "Size(" << getSize() << "), "
909 << "File(" << getFile() << "), "
910 << "Line(" << getLine() << "), "
911 << "FromType(" << getFromType() << "), "
912 << "Elements.size(" << Elements.size() << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000913}
914#endif
915
916//===----------------------------------------------------------------------===//
917
918SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000919: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000920, Lo(0)
921, Hi(0)
922{}
923
Jim Laskey9c4447a2006-03-01 20:39:36 +0000924// Implement isa/cast/dyncast.
925bool SubrangeDesc::classof(const DebugInfoDesc *D) {
926 return D->getTag() == DW_TAG_subrange_type;
927}
928
Jim Laskeyf8913f12006-03-01 17:53:02 +0000929/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
930///
931void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
932 DebugInfoDesc::ApplyToFields(Visitor);
933
934 Visitor->Apply(Lo);
935 Visitor->Apply(Hi);
936}
937
938/// getDescString - Return a string used to compose global names and labels.
939///
940const char *SubrangeDesc::getDescString() const {
941 return "llvm.dbg.subrange";
942}
943
944/// getTypeString - Return a string used to label this descriptor's type.
945///
946const char *SubrangeDesc::getTypeString() const {
947 return "llvm.dbg.subrange.type";
948}
949
950#ifndef NDEBUG
951void SubrangeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000952 cerr << getDescString() << " "
953 << "Version(" << getVersion() << "), "
954 << "Tag(" << getTag() << "), "
955 << "Lo(" << Lo << "), "
956 << "Hi(" << Hi << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000957}
958#endif
959
960//===----------------------------------------------------------------------===//
961
Jim Laskey6a3eb012006-03-01 23:52:37 +0000962EnumeratorDesc::EnumeratorDesc()
963: DebugInfoDesc(DW_TAG_enumerator)
964, Name("")
965, Value(0)
966{}
967
968// Implement isa/cast/dyncast.
969bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
970 return D->getTag() == DW_TAG_enumerator;
971}
972
973/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
974///
975void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
976 DebugInfoDesc::ApplyToFields(Visitor);
977
978 Visitor->Apply(Name);
979 Visitor->Apply(Value);
980}
981
982/// getDescString - Return a string used to compose global names and labels.
983///
984const char *EnumeratorDesc::getDescString() const {
985 return "llvm.dbg.enumerator";
986}
987
988/// getTypeString - Return a string used to label this descriptor's type.
989///
990const char *EnumeratorDesc::getTypeString() const {
991 return "llvm.dbg.enumerator.type";
992}
993
994#ifndef NDEBUG
995void EnumeratorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000996 cerr << getDescString() << " "
997 << "Version(" << getVersion() << "), "
998 << "Tag(" << getTag() << "), "
999 << "Name(" << Name << "), "
1000 << "Value(" << Value << ")\n";
Jim Laskey6a3eb012006-03-01 23:52:37 +00001001}
1002#endif
1003
1004//===----------------------------------------------------------------------===//
1005
Jim Laskeyb8509c52006-03-23 18:07:55 +00001006VariableDesc::VariableDesc(unsigned T)
1007: DebugInfoDesc(T)
1008, Context(NULL)
1009, Name("")
1010, File(NULL)
1011, Line(0)
1012, TyDesc(0)
1013{}
1014
1015// Implement isa/cast/dyncast.
1016bool VariableDesc::classof(const DebugInfoDesc *D) {
1017 unsigned T = D->getTag();
1018 switch (T) {
1019 case DW_TAG_auto_variable:
1020 case DW_TAG_arg_variable:
1021 case DW_TAG_return_variable:
1022 return true;
1023 default: break;
1024 }
1025 return false;
1026}
1027
1028/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1029///
1030void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1031 DebugInfoDesc::ApplyToFields(Visitor);
1032
1033 Visitor->Apply(Context);
1034 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001035 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001036 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001037 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001038}
1039
1040/// getDescString - Return a string used to compose global names and labels.
1041///
1042const char *VariableDesc::getDescString() const {
1043 return "llvm.dbg.variable";
1044}
1045
1046/// getTypeString - Return a string used to label this descriptor's type.
1047///
1048const char *VariableDesc::getTypeString() const {
1049 return "llvm.dbg.variable.type";
1050}
1051
1052#ifndef NDEBUG
1053void VariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001054 cerr << getDescString() << " "
1055 << "Version(" << getVersion() << "), "
1056 << "Tag(" << getTag() << "), "
1057 << "Context(" << Context << "), "
1058 << "Name(\"" << Name << "\"), "
1059 << "File(" << File << "), "
1060 << "Line(" << Line << "), "
1061 << "TyDesc(" << TyDesc << ")\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001062}
1063#endif
1064
1065//===----------------------------------------------------------------------===//
1066
Jim Laskeyce72b172006-02-11 01:01:30 +00001067GlobalDesc::GlobalDesc(unsigned T)
1068: AnchoredDesc(T)
1069, Context(0)
1070, Name("")
Jim Laskey2172f962006-11-30 14:35:45 +00001071, FullName("")
1072, LinkageName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001073, File(NULL)
1074, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001075, TyDesc(NULL)
1076, IsStatic(false)
1077, IsDefinition(false)
1078{}
1079
1080/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001081///
Jim Laskeyce72b172006-02-11 01:01:30 +00001082void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1083 AnchoredDesc::ApplyToFields(Visitor);
1084
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001085 Visitor->Apply(Context);
1086 Visitor->Apply(Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001087 Visitor->Apply(FullName);
1088 Visitor->Apply(LinkageName);
Jim Laskey7089f452006-06-16 13:14:03 +00001089 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001090 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001091 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001092 Visitor->Apply(IsStatic);
1093 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001094}
1095
1096//===----------------------------------------------------------------------===//
1097
1098GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001099: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001100, Global(NULL)
1101{}
1102
Jim Laskey9c4447a2006-03-01 20:39:36 +00001103// Implement isa/cast/dyncast.
1104bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1105 return D->getTag() == DW_TAG_variable;
1106}
1107
Jim Laskeyce72b172006-02-11 01:01:30 +00001108/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1109///
1110void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1111 GlobalDesc::ApplyToFields(Visitor);
1112
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001113 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001114}
1115
Jim Laskeyce72b172006-02-11 01:01:30 +00001116/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001117///
Jim Laskeyce72b172006-02-11 01:01:30 +00001118const char *GlobalVariableDesc::getDescString() const {
1119 return "llvm.dbg.global_variable";
1120}
1121
1122/// getTypeString - Return a string used to label this descriptors type.
1123///
1124const char *GlobalVariableDesc::getTypeString() const {
1125 return "llvm.dbg.global_variable.type";
1126}
1127
1128/// getAnchorString - Return a string used to label this descriptor's anchor.
1129///
Dan Gohmancfbb2f02008-03-25 21:45:14 +00001130const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001131const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001132 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001133}
1134
Jim Laskey86cbdba2006-02-06 15:33:21 +00001135#ifndef NDEBUG
1136void GlobalVariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001137 cerr << getDescString() << " "
1138 << "Version(" << getVersion() << "), "
1139 << "Tag(" << getTag() << "), "
1140 << "Anchor(" << getAnchor() << "), "
1141 << "Name(\"" << getName() << "\"), "
1142 << "FullName(\"" << getFullName() << "\"), "
1143 << "LinkageName(\"" << getLinkageName() << "\"), "
1144 << "File(" << getFile() << "),"
1145 << "Line(" << getLine() << "),"
1146 << "Type(" << getType() << "), "
1147 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1148 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1149 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001150}
1151#endif
1152
1153//===----------------------------------------------------------------------===//
1154
Jim Laskeyce72b172006-02-11 01:01:30 +00001155SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001156: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001157{}
1158
Jim Laskey9c4447a2006-03-01 20:39:36 +00001159// Implement isa/cast/dyncast.
1160bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1161 return D->getTag() == DW_TAG_subprogram;
1162}
1163
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001164/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001165/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001166void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001167 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001168}
1169
Jim Laskeyce72b172006-02-11 01:01:30 +00001170/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001171///
Jim Laskeyce72b172006-02-11 01:01:30 +00001172const char *SubprogramDesc::getDescString() const {
1173 return "llvm.dbg.subprogram";
1174}
1175
1176/// getTypeString - Return a string used to label this descriptors type.
1177///
1178const char *SubprogramDesc::getTypeString() const {
1179 return "llvm.dbg.subprogram.type";
1180}
1181
1182/// getAnchorString - Return a string used to label this descriptor's anchor.
1183///
Dan Gohmancfbb2f02008-03-25 21:45:14 +00001184const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001185const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001186 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001187}
1188
Jim Laskey86cbdba2006-02-06 15:33:21 +00001189#ifndef NDEBUG
1190void SubprogramDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001191 cerr << getDescString() << " "
1192 << "Version(" << getVersion() << "), "
1193 << "Tag(" << getTag() << "), "
1194 << "Anchor(" << getAnchor() << "), "
1195 << "Name(\"" << getName() << "\"), "
1196 << "FullName(\"" << getFullName() << "\"), "
1197 << "LinkageName(\"" << getLinkageName() << "\"), "
1198 << "File(" << getFile() << "),"
1199 << "Line(" << getLine() << "),"
1200 << "Type(" << getType() << "), "
1201 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1202 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001203}
1204#endif
1205
Jim Laskey45ccae52006-02-28 20:15:07 +00001206//===----------------------------------------------------------------------===//
1207
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001208BlockDesc::BlockDesc()
1209: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001210, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001211{}
1212
1213// Implement isa/cast/dyncast.
1214bool BlockDesc::classof(const DebugInfoDesc *D) {
1215 return D->getTag() == DW_TAG_lexical_block;
1216}
1217
1218/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1219///
1220void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1221 DebugInfoDesc::ApplyToFields(Visitor);
1222
Jim Laskeyb8509c52006-03-23 18:07:55 +00001223 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001224}
1225
1226/// getDescString - Return a string used to compose global names and labels.
1227///
1228const char *BlockDesc::getDescString() const {
1229 return "llvm.dbg.block";
1230}
1231
1232/// getTypeString - Return a string used to label this descriptors type.
1233///
1234const char *BlockDesc::getTypeString() const {
1235 return "llvm.dbg.block.type";
1236}
1237
1238#ifndef NDEBUG
1239void BlockDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001240 cerr << getDescString() << " "
1241 << "Version(" << getVersion() << "), "
1242 << "Tag(" << getTag() << "),"
1243 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001244}
1245#endif
1246
1247//===----------------------------------------------------------------------===//
1248
Jim Laskey86cbdba2006-02-06 15:33:21 +00001249DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001250 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001251}
1252DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001253 // Handle NULL.
1254 if (!GV) return NULL;
1255
Jim Laskey86cbdba2006-02-06 15:33:21 +00001256 // Check to see if it has been already deserialized.
1257 DebugInfoDesc *&Slot = GlobalDescs[GV];
1258 if (Slot) return Slot;
1259
1260 // Get the Tag from the global.
1261 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1262
Jim Laskey86cbdba2006-02-06 15:33:21 +00001263 // Create an empty instance of the correct sort.
1264 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001265
Jim Laskey21407982006-03-14 18:37:57 +00001266 // If not a user defined descriptor.
1267 if (Slot) {
1268 // Deserialize the fields.
1269 DIDeserializeVisitor DRAM(*this, GV);
1270 DRAM.ApplyToFields(Slot);
1271 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001272
1273 return Slot;
1274}
1275
1276//===----------------------------------------------------------------------===//
1277
1278/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001279///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001280const PointerType *DISerializer::getStrPtrType() {
1281 // If not already defined.
1282 if (!StrPtrTy) {
1283 // Construct the pointer to signed bytes.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001284 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001285 }
1286
1287 return StrPtrTy;
1288}
1289
1290/// getEmptyStructPtrType - Return a "{ }*" type.
1291///
1292const PointerType *DISerializer::getEmptyStructPtrType() {
1293 // If not already defined.
1294 if (!EmptyStructPtrTy) {
1295 // Construct the empty structure type.
1296 const StructType *EmptyStructTy =
1297 StructType::get(std::vector<const Type*>());
1298 // Construct the pointer to empty structure type.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001299 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001300 }
1301
1302 return EmptyStructPtrTy;
1303}
1304
1305/// getTagType - Return the type describing the specified descriptor (via tag.)
1306///
1307const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1308 // Attempt to get the previously defined type.
1309 StructType *&Ty = TagTypes[DD->getTag()];
1310
1311 // If not already defined.
1312 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001313 // Set up fields vector.
1314 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001315 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001316 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001317 GTAM.ApplyToFields(DD);
1318
1319 // Construct structured type.
1320 Ty = StructType::get(Fields);
1321
Jim Laskey86cbdba2006-02-06 15:33:21 +00001322 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001323 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001324 }
1325
1326 return Ty;
1327}
1328
1329/// getString - Construct the string as constant string global.
1330///
Jim Laskeyce72b172006-02-11 01:01:30 +00001331Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001332 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001333 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001334 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001335 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001336 // If empty string then use a sbyte* null instead.
1337 if (String.empty()) {
1338 Slot = ConstantPointerNull::get(getStrPtrType());
1339 } else {
1340 // Construct string as an llvm constant.
1341 Constant *ConstStr = ConstantArray::get(String);
1342 // Otherwise create and return a new string global.
1343 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1344 GlobalVariable::InternalLinkage,
Devang Patel1e4c23a2007-05-11 23:14:43 +00001345 ConstStr, ".str", M);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001346 StrGV->setSection("llvm.metadata");
1347 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001348 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001349 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001350 return Slot;
1351
Jim Laskey86cbdba2006-02-06 15:33:21 +00001352}
1353
1354/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1355/// so that it can be serialized to a .bc or .ll file.
1356GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1357 // Check if the DebugInfoDesc is already in the map.
1358 GlobalVariable *&Slot = DescGlobals[DD];
1359
1360 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1361 if (Slot) return Slot;
1362
Jim Laskey86cbdba2006-02-06 15:33:21 +00001363 // Get the type associated with the Tag.
1364 const StructType *Ty = getTagType(DD);
1365
1366 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001367 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1368 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001369 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001370
1371 // Insert new GlobalVariable in DescGlobals map.
1372 Slot = GV;
1373
1374 // Set up elements vector
1375 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001376 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001377 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001378 SRAM.ApplyToFields(DD);
1379
1380 // Set the globals initializer.
1381 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1382
1383 return GV;
1384}
1385
Devang Patel962e0752007-11-30 00:51:33 +00001386/// addDescriptor - Directly connect DD with existing GV.
1387void DISerializer::addDescriptor(DebugInfoDesc *DD,
1388 GlobalVariable *GV) {
1389 DescGlobals[DD] = GV;
1390}
1391
Jim Laskey86cbdba2006-02-06 15:33:21 +00001392//===----------------------------------------------------------------------===//
1393
Jim Laskey86cbdba2006-02-06 15:33:21 +00001394/// Verify - Return true if the GlobalVariable appears to be a valid
1395/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001396bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001397 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001398}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001399bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001400 // NULLs are valid.
1401 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001402
Jim Laskey98e04102006-03-26 22:45:20 +00001403 // Check prior validity.
1404 unsigned &ValiditySlot = Validity[GV];
1405
1406 // If visited before then use old state.
1407 if (ValiditySlot) return ValiditySlot == Valid;
1408
1409 // Assume validity for the time being (recursion.)
1410 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001411
1412 // Make sure the global is internal or link once (anchor.)
1413 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1414 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1415 ValiditySlot = Invalid;
1416 return false;
1417 }
Jim Laskey98e04102006-03-26 22:45:20 +00001418
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001419 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001420 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001421
1422 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001423 if (Tag == DW_TAG_invalid) {
1424 ValiditySlot = Valid;
1425 return true;
1426 }
1427
1428 // Get the Version.
1429 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1430
1431 // Check for version mismatch.
1432 if (Version != LLVMDebugVersion) {
1433 ValiditySlot = Invalid;
1434 return false;
1435 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001436
Jim Laskey86cbdba2006-02-06 15:33:21 +00001437 // Construct an empty DebugInfoDesc.
1438 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001439
1440 // Allow for user defined descriptors.
1441 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001442
1443 // Get the initializer constant.
1444 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1445
1446 // Get the operand count.
1447 unsigned N = CI->getNumOperands();
1448
1449 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001450 unsigned &CountSlot = Counts[Tag];
1451 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001452 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001453 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001454 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001455 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001456 }
1457
Jim Laskey21407982006-03-14 18:37:57 +00001458 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001459 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001460 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001461 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001462 return false;
1463 }
1464
1465 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001466 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001467 VRAM.ApplyToFields(DD);
1468
1469 // Release empty DebugInfoDesc.
1470 delete DD;
1471
Jim Laskey98e04102006-03-26 22:45:20 +00001472 // If fields are not valid.
1473 if (!VRAM.isValid()) {
1474 ValiditySlot = Invalid;
1475 return false;
1476 }
1477
1478 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001479}
1480
Evan Chenga844bde2008-02-02 04:07:54 +00001481/// isVerified - Return true if the specified GV has already been
1482/// verified as a debug information descriptor.
1483bool DIVerifier::isVerified(GlobalVariable *GV) {
1484 unsigned &ValiditySlot = Validity[GV];
1485 if (ValiditySlot) return ValiditySlot == Valid;
1486 return false;
1487}
1488
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001489//===----------------------------------------------------------------------===//
1490
Jim Laskeyb8509c52006-03-23 18:07:55 +00001491DebugScope::~DebugScope() {
1492 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1493 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1494}
1495
1496//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001497
Jim Laskey6da18642007-01-26 21:38:26 +00001498MachineModuleInfo::MachineModuleInfo()
Devang Patel794fd752007-05-01 21:15:47 +00001499: ImmutablePass((intptr_t)&ID)
1500, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001501, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001502, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001503, Directories()
1504, SourceFiles()
1505, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001506, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001507, ScopeMap()
1508, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001509, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001510, LandingPads()
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001511, Personalities()
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001512, CallsEHReturn(0)
1513, CallsUnwindInit(0)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001514{
1515 // Always emit "no personality" info
1516 Personalities.push_back(NULL);
1517}
Jim Laskey6da18642007-01-26 21:38:26 +00001518MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001519
1520}
1521
Jim Laskey6da18642007-01-26 21:38:26 +00001522/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001523///
Jim Laskey6da18642007-01-26 21:38:26 +00001524bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001525 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001526}
1527
Jim Laskey6da18642007-01-26 21:38:26 +00001528/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001529///
Jim Laskey6da18642007-01-26 21:38:26 +00001530bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001531 return false;
1532}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001533
Jim Laskey6da18642007-01-26 21:38:26 +00001534/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001535///
Jim Laskey6da18642007-01-26 21:38:26 +00001536void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001537 // Coming soon.
1538}
1539
Jim Laskey6da18642007-01-26 21:38:26 +00001540/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001541///
Jim Laskey6da18642007-01-26 21:38:26 +00001542void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001543 // Clean up scope information.
1544 if (RootScope) {
1545 delete RootScope;
1546 ScopeMap.clear();
1547 RootScope = NULL;
1548 }
1549
Jim Laskeyb82313f2007-02-01 16:31:34 +00001550 // Clean up line info.
1551 Lines.clear();
1552
Jim Laskey41886992006-04-07 16:34:46 +00001553 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001554 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001555
1556 // Clean up exception info.
1557 LandingPads.clear();
1558 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +00001559 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +00001560 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001561 CallsEHReturn = 0;
1562 CallsUnwindInit = 0;
Jim Laskey41886992006-04-07 16:34:46 +00001563}
1564
Jim Laskeyd96185a2006-02-13 12:50:39 +00001565/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001566///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001567// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001568DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001569 return DR.Deserialize(V);
1570}
1571
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001572/// AnalyzeModule - Scan the module for global debug information.
1573///
Jim Laskey6da18642007-01-26 21:38:26 +00001574void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001575 SetupCompileUnits(M);
Dale Johannesen48ae02f2008-01-16 19:59:28 +00001576
1577 // Insert functions in the llvm.used array into UsedFunctions.
1578 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1579 if (!GV || !GV->hasInitializer()) return;
1580
1581 // Should be an array of 'i8*'.
1582 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1583 if (InitList == 0) return;
1584
1585 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1586 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1587 if (CE->getOpcode() == Instruction::BitCast)
1588 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1589 UsedFunctions.insert(F);
1590 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001591}
1592
1593/// SetupCompileUnits - Set up the unique vector of compile units.
1594///
Jim Laskey6da18642007-01-26 21:38:26 +00001595void MachineModuleInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001596 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001597
Jim Laskey0420f2a2006-02-22 19:02:11 +00001598 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1599 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001600 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001601}
1602
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001603/// getCompileUnits - Return a vector of debug compile units.
1604///
Jim Laskey6da18642007-01-26 21:38:26 +00001605const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001606 return CompileUnits;
1607}
1608
Jim Laskey0420f2a2006-02-22 19:02:11 +00001609/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1610/// named GlobalVariable.
1611std::vector<GlobalVariable*>
Jim Laskey6da18642007-01-26 21:38:26 +00001612MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1613 const std::string &RootName) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001614 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001615}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001616
Evan Chenga647c922008-02-01 02:05:57 +00001617/// RecordSourceLine - Records location information and associates it with a
Jim Laskeyb8509c52006-03-23 18:07:55 +00001618/// debug label. Returns a unique label ID used to generate a label and
1619/// provide correspondence to the source line list.
Evan Chenga647c922008-02-01 02:05:57 +00001620unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1621 unsigned Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001622 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001623 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001624 return ID;
1625}
1626
1627/// RecordSource - Register a source file with debug info. Returns an source
1628/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001629unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1630 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001631 unsigned DirectoryID = Directories.insert(Directory);
1632 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1633}
Jim Laskey6da18642007-01-26 21:38:26 +00001634unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001635 return RecordSource(CompileUnit->getDirectory(),
1636 CompileUnit->getFileName());
1637}
1638
1639/// RecordRegionStart - Indicate the start of a region.
1640///
Jim Laskey6da18642007-01-26 21:38:26 +00001641unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001642 // FIXME - need to be able to handle split scopes because of bb cloning.
1643 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1644 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1645 unsigned ID = NextLabelID();
1646 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1647 return ID;
1648}
1649
1650/// RecordRegionEnd - Indicate the end of a region.
1651///
Jim Laskey6da18642007-01-26 21:38:26 +00001652unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001653 // FIXME - need to be able to handle split scopes because of bb cloning.
1654 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1655 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1656 unsigned ID = NextLabelID();
1657 Scope->setEndLabelID(ID);
1658 return ID;
1659}
1660
1661/// RecordVariable - Indicate the declaration of a local variable.
1662///
Evan Chenga844bde2008-02-02 04:07:54 +00001663void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1664 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001665 DebugScope *Scope = getOrCreateScope(VD->getContext());
1666 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1667 Scope->AddVariable(DV);
1668}
1669
1670/// getOrCreateScope - Returns the scope associated with the given descriptor.
1671///
Jim Laskey6da18642007-01-26 21:38:26 +00001672DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001673 DebugScope *&Slot = ScopeMap[ScopeDesc];
1674 if (!Slot) {
1675 // FIXME - breaks down when the context is an inlined function.
1676 DebugInfoDesc *ParentDesc = NULL;
1677 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1678 ParentDesc = Block->getContext();
1679 }
1680 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1681 Slot = new DebugScope(Parent, ScopeDesc);
1682 if (Parent) {
1683 Parent->AddScope(Slot);
1684 } else if (RootScope) {
1685 // FIXME - Add inlined function scopes to the root so we can delete
1686 // them later. Long term, handle inlined functions properly.
1687 RootScope->AddScope(Slot);
1688 } else {
1689 // First function is top level function.
1690 RootScope = Slot;
1691 }
1692 }
1693 return Slot;
1694}
1695
Jim Laskey59667fe2007-02-21 22:38:31 +00001696//===-EH-------------------------------------------------------------------===//
1697
1698/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1699/// specified MachineBasicBlock.
1700LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1701 (MachineBasicBlock *LandingPad) {
1702 unsigned N = LandingPads.size();
1703 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001704 LandingPadInfo &LP = LandingPads[i];
1705 if (LP.LandingPadBlock == LandingPad)
1706 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001707 }
1708
1709 LandingPads.push_back(LandingPadInfo(LandingPad));
1710 return LandingPads[N];
1711}
1712
1713/// addInvoke - Provide the begin and end labels of an invoke style call and
1714/// associate it with a try landing pad block.
1715void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1716 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001717 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001718 LP.BeginLabels.push_back(BeginLabel);
1719 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +00001720}
1721
1722/// addLandingPad - Provide the label of a try LandingPad block.
1723///
1724unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1725 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001726 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1727 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001728 return LandingPadLabel;
1729}
1730
1731/// addPersonality - Provide the personality function for the exception
1732/// information.
1733void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001734 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001735 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001736 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001737
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001738 for (unsigned i = 0; i < Personalities.size(); ++i)
1739 if (Personalities[i] == Personality)
1740 return;
1741
1742 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +00001743}
1744
1745/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1746///
1747void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1748 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001749 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001750 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001751 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001752}
Duncan Sands73ef58a2007-06-02 16:53:42 +00001753
1754/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +00001755///
Duncan Sands73ef58a2007-06-02 16:53:42 +00001756void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1757 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001758 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Duncan Sands73ef58a2007-06-02 16:53:42 +00001759 std::vector<unsigned> IdsInFilter (TyInfo.size());
1760 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1761 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1762 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +00001763}
1764
Duncan Sands6590b042007-08-27 15:47:50 +00001765/// addCleanup - Add a cleanup action for a landing pad.
1766///
1767void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1768 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1769 LP.TypeIds.push_back(0);
1770}
1771
Jim Laskey59667fe2007-02-21 22:38:31 +00001772/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1773/// pads.
1774void MachineModuleInfo::TidyLandingPads() {
1775 for (unsigned i = 0; i != LandingPads.size(); ) {
1776 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001777 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001778
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001779 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +00001780 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001781 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001782 LandingPads.erase(LandingPads.begin() + i);
1783 continue;
1784 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001785
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001786 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1787 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1788 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands57810cd2007-09-05 11:27:52 +00001789
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001790 if (!BeginLabel || !EndLabel) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001791 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1792 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1793 continue;
1794 }
1795
1796 LandingPad.BeginLabels[j] = BeginLabel;
1797 LandingPad.EndLabels[j] = EndLabel;
1798 ++j;
1799 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001800
1801 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +00001802 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +00001803 LandingPads.erase(LandingPads.begin() + i);
1804 continue;
1805 }
1806
1807 // If there is no landing pad, ensure that the list of typeids is empty.
1808 // If the only typeid is a cleanup, this is the same as having no typeids.
1809 if (!LandingPad.LandingPadBlock ||
1810 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1811 LandingPad.TypeIds.clear();
1812
Jim Laskey59667fe2007-02-21 22:38:31 +00001813 ++i;
1814 }
1815}
1816
1817/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1818/// function wide.
1819unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1820 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1821 if (TypeInfos[i] == TI) return i + 1;
1822
1823 TypeInfos.push_back(TI);
1824 return TypeInfos.size();
1825}
1826
Duncan Sands73ef58a2007-06-02 16:53:42 +00001827/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1828/// function wide.
1829int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +00001830 // If the new filter coincides with the tail of an existing filter, then
1831 // re-use the existing filter. Folding filters more than this requires
1832 // re-ordering filters and/or their elements - probably not worth it.
1833 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1834 E = FilterEnds.end(); I != E; ++I) {
1835 unsigned i = *I, j = TyIds.size();
1836
1837 while (i && j)
1838 if (FilterIds[--i] != TyIds[--j])
1839 goto try_next;
1840
1841 if (!j)
1842 // The new filter coincides with range [i, end) of the existing filter.
1843 return -(1 + i);
1844
1845try_next:;
1846 }
1847
1848 // Add the new filter.
Duncan Sands73ef58a2007-06-02 16:53:42 +00001849 int FilterID = -(1 + FilterIds.size());
1850 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1851 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1852 FilterIds.push_back(TyIds[I]);
Duncan Sands14da32a2007-07-05 15:15:01 +00001853 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +00001854 FilterIds.push_back(0); // terminator
1855 return FilterID;
1856}
1857
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001858/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +00001859Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001860 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001861 // function
1862 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +00001863}
1864
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001865/// getPersonalityIndex - Return unique index for current personality
1866/// function. NULL personality function should always get zero index.
1867unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001868 const Function* Personality = NULL;
1869
1870 // Scan landing pads. If there is at least one non-NULL personality - use it.
1871 for (unsigned i = 0; i != LandingPads.size(); ++i)
1872 if (LandingPads[i].Personality) {
1873 Personality = LandingPads[i].Personality;
1874 break;
1875 }
1876
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001877 for (unsigned i = 0; i < Personalities.size(); ++i) {
1878 if (Personalities[i] == Personality)
1879 return i;
1880 }
1881
1882 // This should never happen
1883 assert(0 && "Personality function should be set!");
1884 return 0;
1885}
Jim Laskey59667fe2007-02-21 22:38:31 +00001886
Jim Laskey9d4209f2006-11-07 19:33:46 +00001887//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00001888/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1889/// a info consumer to determine if the range of two labels is empty, by seeing
1890/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00001891
1892namespace llvm {
1893
1894struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00001895 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +00001896 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1897
Jim Laskey9d4209f2006-11-07 19:33:46 +00001898 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00001899 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00001900};
1901
Devang Patel19974732007-05-03 01:11:54 +00001902char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00001903
Jim Laskey9d4209f2006-11-07 19:33:46 +00001904bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00001905 // Get machine module info.
1906 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1907 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001908
1909 // Track if change is made.
1910 bool MadeChange = false;
1911 // No prior label to begin.
1912 unsigned PriorLabel = 0;
1913
1914 // Iterate through basic blocks.
1915 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1916 BB != E; ++BB) {
1917 // Iterate through instructions.
1918 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00001919 // Is it a label.
Evan Chengbb81d972008-01-31 09:59:15 +00001920 if (I->isDebugLabel()) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00001921 // The label ID # is always operand #0, an immediate.
1922 unsigned NextLabel = I->getOperand(0).getImm();
1923
1924 // If there was an immediate prior label.
1925 if (PriorLabel) {
1926 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00001927 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00001928 // Delete the current label.
1929 I = BB->erase(I);
1930 // Indicate a change has been made.
1931 MadeChange = true;
1932 continue;
1933 } else {
1934 // Start a new round.
1935 PriorLabel = NextLabel;
1936 }
1937 } else {
1938 // No consecutive labels.
1939 PriorLabel = 0;
1940 }
1941
1942 ++I;
1943 }
1944 }
1945
1946 return MadeChange;
1947}
1948
1949FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1950
1951}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001952