blob: 60526643fdfc68c4cfbf8febef734ed46cf3e20e [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++);
Owen Anderson8342cff2008-06-26 17:06:02 +0000234 // Fills in the string if it succeeds
235 if (!GetConstantStringInfo(C, Field))
236 Field.clear();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000237 }
238 virtual void Apply(DebugInfoDesc *&Field) {
239 Constant *C = CI->getOperand(I++);
240 Field = DR.Deserialize(C);
241 }
242 virtual void Apply(GlobalVariable *&Field) {
243 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000244 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000245 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000246 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000247 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000248 Constant *C = CI->getOperand(I++);
249 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000250 if (GV->hasInitializer()) {
251 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
252 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
253 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
254 DebugInfoDesc *DE = DR.Deserialize(GVE);
255 Field.push_back(DE);
256 }
257 } else if (GV->getInitializer()->isNullValue()) {
258 if (const ArrayType *T =
259 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
260 Field.resize(T->getNumElements());
261 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000262 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000263 }
264 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000265};
266
267//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000268/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000269/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000270class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000271private:
272 DISerializer &SR; // Active serializer.
273 std::vector<Constant*> &Elements; // Element accumulator.
274
275public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000276 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
277 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000278 , SR(S)
279 , Elements(E)
280 {}
281
282 /// Apply - Set the value of each of the fields.
283 ///
284 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000285 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000286 }
287 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000288 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000289 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000290 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000291 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000292 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000293 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000294 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000295 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000296 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000297 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000298 }
299 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000300 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000301 }
302 virtual void Apply(DebugInfoDesc *&Field) {
303 GlobalVariable *GV = NULL;
304
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000305 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000306 if (Field) GV = SR.Serialize(Field);
307
308 // FIXME - At some point should use specific type.
309 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
310
311 if (GV) {
312 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000313 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000314 } else {
315 // Use NULL.
316 Elements.push_back(ConstantPointerNull::get(EmptyTy));
317 }
318 }
319 virtual void Apply(GlobalVariable *&Field) {
320 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000321 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000322 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000323 } else {
324 Elements.push_back(ConstantPointerNull::get(EmptyTy));
325 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000326 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000327 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
328 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
329 unsigned N = Field.size();
330 ArrayType *AT = ArrayType::get(EmptyTy, N);
331 std::vector<Constant *> ArrayElements;
332
333 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000334 if (DebugInfoDesc *Element = Field[i]) {
335 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000336 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000337 ArrayElements.push_back(cast<Constant>(CE));
338 } else {
339 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
340 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000341 }
342
343 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000344 GlobalVariable *CAGV = new GlobalVariable(AT, true,
345 GlobalValue::InternalLinkage,
346 CA, "llvm.dbg.array",
347 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000348 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000349 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000350 Elements.push_back(CAE);
351 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000352};
353
354//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000355/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000356/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000357class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000358private:
359 DISerializer &SR; // Active serializer.
360 std::vector<const Type*> &Fields; // Type accumulator.
361
362public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000363 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
364 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000365 , SR(S)
366 , Fields(F)
367 {}
368
369 /// Apply - Set the value of each of the fields.
370 ///
371 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000372 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000373 }
374 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000375 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000376 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000377 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000378 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000379 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000380 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000381 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000382 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000383 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000384 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000385 }
386 virtual void Apply(std::string &Field) {
387 Fields.push_back(SR.getStrPtrType());
388 }
389 virtual void Apply(DebugInfoDesc *&Field) {
390 // FIXME - At some point should use specific type.
391 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
392 Fields.push_back(EmptyTy);
393 }
394 virtual void Apply(GlobalVariable *&Field) {
395 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
396 Fields.push_back(EmptyTy);
397 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000398 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
399 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
400 Fields.push_back(EmptyTy);
401 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000402};
403
404//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000405/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000406/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000407class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000408private:
409 DIVerifier &VR; // Active verifier.
410 bool IsValid; // Validity status.
411 unsigned I; // Current operand index.
412 ConstantStruct *CI; // GlobalVariable constant initializer.
413
414public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000415 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
416 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000417 , VR(V)
418 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000419 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000420 , CI(cast<ConstantStruct>(GV->getInitializer()))
421 {
422 }
423
424 // Accessors.
425 bool isValid() const { return IsValid; }
426
427 /// Apply - Set the value of each of the fields.
428 ///
429 virtual void Apply(int &Field) {
430 Constant *C = CI->getOperand(I++);
431 IsValid = IsValid && isa<ConstantInt>(C);
432 }
433 virtual void Apply(unsigned &Field) {
434 Constant *C = CI->getOperand(I++);
435 IsValid = IsValid && isa<ConstantInt>(C);
436 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000437 virtual void Apply(int64_t &Field) {
438 Constant *C = CI->getOperand(I++);
439 IsValid = IsValid && isa<ConstantInt>(C);
440 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000441 virtual void Apply(uint64_t &Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isa<ConstantInt>(C);
444 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000445 virtual void Apply(bool &Field) {
446 Constant *C = CI->getOperand(I++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000447 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000448 }
449 virtual void Apply(std::string &Field) {
450 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000451 IsValid = IsValid &&
452 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000453 }
454 virtual void Apply(DebugInfoDesc *&Field) {
455 // FIXME - Prepare the correct descriptor.
456 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000457 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000458 }
459 virtual void Apply(GlobalVariable *&Field) {
460 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000461 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000462 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000463 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
464 Constant *C = CI->getOperand(I++);
465 IsValid = IsValid && isGlobalVariable(C);
466 if (!IsValid) return;
467
468 GlobalVariable *GV = getGlobalVariable(C);
469 IsValid = IsValid && GV && GV->hasInitializer();
470 if (!IsValid) return;
471
472 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
473 IsValid = IsValid && CA;
474 if (!IsValid) return;
475
476 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
477 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
478 if (!IsValid) return;
479
480 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
481 VR.Verify(GVE);
482 }
483 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000484};
485
Dan Gohman844731a2008-05-13 00:00:25 +0000486}
Jim Laskeyce72b172006-02-11 01:01:30 +0000487
Jim Laskey86cbdba2006-02-06 15:33:21 +0000488//===----------------------------------------------------------------------===//
489
Jim Laskeyed4e5662006-06-14 14:45:39 +0000490/// TagFromGlobal - Returns the tag number from a debug info descriptor
491/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000492unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000493 ConstantInt *C = getUIntOperand(GV, 0);
494 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000495 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000496}
497
498/// VersionFromGlobal - Returns the version number from a debug info
499/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
500/// int.
501unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000502 ConstantInt *C = getUIntOperand(GV, 0);
503 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000504 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000505}
506
507/// DescFactory - Create an instance of debug info descriptor based on Tag.
508/// Return NULL if not a recognized Tag.
509DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
510 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000511 case DW_TAG_anchor: return new AnchorDesc();
512 case DW_TAG_compile_unit: return new CompileUnitDesc();
513 case DW_TAG_variable: return new GlobalVariableDesc();
514 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000515 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000516 case DW_TAG_base_type: return new BasicTypeDesc();
517 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000518 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000519 case DW_TAG_reference_type:
520 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000521 case DW_TAG_volatile_type:
522 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000523 case DW_TAG_member:
524 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000525 case DW_TAG_array_type:
526 case DW_TAG_structure_type:
527 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000528 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000529 case DW_TAG_vector_type:
530 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000531 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000532 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000533 case DW_TAG_return_variable:
534 case DW_TAG_arg_variable:
535 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000536 default: break;
537 }
538 return NULL;
539}
540
541/// getLinkage - get linkage appropriate for this type of descriptor.
542///
543GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
544 return GlobalValue::InternalLinkage;
545}
546
547/// ApplyToFields - Target the vistor to the fields of the descriptor.
548///
549void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
550 Visitor->Apply(Tag);
551}
552
553//===----------------------------------------------------------------------===//
554
Jim Laskey9c4447a2006-03-01 20:39:36 +0000555AnchorDesc::AnchorDesc()
556: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000557, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000558{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000559AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000560: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000561, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000562{}
563
564// Implement isa/cast/dyncast.
565bool AnchorDesc::classof(const DebugInfoDesc *D) {
566 return D->getTag() == DW_TAG_anchor;
567}
568
Jim Laskeyce72b172006-02-11 01:01:30 +0000569/// getLinkage - get linkage appropriate for this type of descriptor.
570///
571GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
572 return GlobalValue::LinkOnceLinkage;
573}
574
575/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
576///
577void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
578 DebugInfoDesc::ApplyToFields(Visitor);
579
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000580 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000581}
582
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000583/// getDescString - Return a string used to compose global names and labels. A
584/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000585/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000586/// to the list of names left external in the internalizer.
587/// ExternalNames.insert("llvm.dbg.compile_units");
588/// ExternalNames.insert("llvm.dbg.global_variables");
589/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000590const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000591 switch (AnchorTag) {
592 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
593 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
594 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
595 default: break;
596 }
597
598 assert(0 && "Tag does not have a case for anchor string");
599 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000600}
601
602/// getTypeString - Return a string used to label this descriptors type.
603///
604const char *AnchorDesc::getTypeString() const {
605 return "llvm.dbg.anchor.type";
606}
607
608#ifndef NDEBUG
609void AnchorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000610 cerr << getDescString() << " "
611 << "Version(" << getVersion() << "), "
612 << "Tag(" << getTag() << "), "
613 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000614}
615#endif
616
617//===----------------------------------------------------------------------===//
618
619AnchoredDesc::AnchoredDesc(unsigned T)
620: DebugInfoDesc(T)
621, Anchor(NULL)
622{}
623
624/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
625///
626void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
627 DebugInfoDesc::ApplyToFields(Visitor);
628
Jim Laskey7089f452006-06-16 13:14:03 +0000629 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000630}
631
632//===----------------------------------------------------------------------===//
633
634CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000635: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000636, Language(0)
637, FileName("")
638, Directory("")
639, Producer("")
640{}
641
Jim Laskey9c4447a2006-03-01 20:39:36 +0000642// Implement isa/cast/dyncast.
643bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
644 return D->getTag() == DW_TAG_compile_unit;
645}
646
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000647/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
648///
649void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000650 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000651
652 // Handle cases out of sync with compiler.
653 if (getVersion() == 0) {
654 unsigned DebugVersion;
655 Visitor->Apply(DebugVersion);
656 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000657
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000658 Visitor->Apply(Language);
659 Visitor->Apply(FileName);
660 Visitor->Apply(Directory);
661 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000662}
663
Jim Laskeyce72b172006-02-11 01:01:30 +0000664/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000665///
Jim Laskeyce72b172006-02-11 01:01:30 +0000666const char *CompileUnitDesc::getDescString() const {
667 return "llvm.dbg.compile_unit";
668}
669
670/// getTypeString - Return a string used to label this descriptors type.
671///
672const char *CompileUnitDesc::getTypeString() const {
673 return "llvm.dbg.compile_unit.type";
674}
675
676/// getAnchorString - Return a string used to label this descriptor's anchor.
677///
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000678const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000679const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000680 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000681}
682
Jim Laskey86cbdba2006-02-06 15:33:21 +0000683#ifndef NDEBUG
684void CompileUnitDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000685 cerr << getDescString() << " "
686 << "Version(" << getVersion() << "), "
687 << "Tag(" << getTag() << "), "
688 << "Anchor(" << getAnchor() << "), "
689 << "Language(" << Language << "), "
690 << "FileName(\"" << FileName << "\"), "
691 << "Directory(\"" << Directory << "\"), "
692 << "Producer(\"" << Producer << "\")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000693}
694#endif
695
696//===----------------------------------------------------------------------===//
697
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000698TypeDesc::TypeDesc(unsigned T)
699: DebugInfoDesc(T)
700, Context(NULL)
701, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000702, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000703, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000704, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000705, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000706, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000707, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000708{}
709
Jim Laskey69906002006-02-24 16:46:40 +0000710/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000711///
712void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
713 DebugInfoDesc::ApplyToFields(Visitor);
714
715 Visitor->Apply(Context);
716 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000717 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000718 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000719 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000720 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000721 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000722 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000723}
724
725/// getDescString - Return a string used to compose global names and labels.
726///
727const char *TypeDesc::getDescString() const {
728 return "llvm.dbg.type";
729}
730
731/// getTypeString - Return a string used to label this descriptor's type.
732///
733const char *TypeDesc::getTypeString() const {
734 return "llvm.dbg.type.type";
735}
736
737#ifndef NDEBUG
738void TypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000739 cerr << getDescString() << " "
740 << "Version(" << getVersion() << "), "
741 << "Tag(" << getTag() << "), "
742 << "Context(" << Context << "), "
743 << "Name(\"" << Name << "\"), "
744 << "File(" << File << "), "
745 << "Line(" << Line << "), "
746 << "Size(" << Size << "), "
747 << "Align(" << Align << "), "
748 << "Offset(" << Offset << "), "
749 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000750}
751#endif
752
753//===----------------------------------------------------------------------===//
754
755BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000756: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000757, Encoding(0)
758{}
759
Jim Laskey9c4447a2006-03-01 20:39:36 +0000760// Implement isa/cast/dyncast.
761bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
762 return D->getTag() == DW_TAG_base_type;
763}
764
Jim Laskey69906002006-02-24 16:46:40 +0000765/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000766///
767void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
768 TypeDesc::ApplyToFields(Visitor);
769
770 Visitor->Apply(Encoding);
771}
772
Jim Laskeyf8913f12006-03-01 17:53:02 +0000773/// getDescString - Return a string used to compose global names and labels.
774///
775const char *BasicTypeDesc::getDescString() const {
776 return "llvm.dbg.basictype";
777}
778
779/// getTypeString - Return a string used to label this descriptor's type.
780///
781const char *BasicTypeDesc::getTypeString() const {
782 return "llvm.dbg.basictype.type";
783}
784
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000785#ifndef NDEBUG
786void BasicTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000787 cerr << getDescString() << " "
788 << "Version(" << getVersion() << "), "
789 << "Tag(" << getTag() << "), "
790 << "Context(" << getContext() << "), "
791 << "Name(\"" << getName() << "\"), "
792 << "Size(" << getSize() << "), "
793 << "Encoding(" << Encoding << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000794}
795#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000796
Jim Laskey434b40b2006-02-23 22:37:30 +0000797//===----------------------------------------------------------------------===//
798
Jim Laskey69906002006-02-24 16:46:40 +0000799DerivedTypeDesc::DerivedTypeDesc(unsigned T)
800: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000801, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000802{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000803
Jim Laskey9c4447a2006-03-01 20:39:36 +0000804// Implement isa/cast/dyncast.
805bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
806 unsigned T = D->getTag();
807 switch (T) {
808 case DW_TAG_typedef:
809 case DW_TAG_pointer_type:
810 case DW_TAG_reference_type:
811 case DW_TAG_const_type:
812 case DW_TAG_volatile_type:
813 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000814 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000815 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000816 return true;
817 default: break;
818 }
819 return false;
820}
821
Jim Laskey69906002006-02-24 16:46:40 +0000822/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000823///
Jim Laskey69906002006-02-24 16:46:40 +0000824void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000825 TypeDesc::ApplyToFields(Visitor);
826
Jim Laskey7089f452006-06-16 13:14:03 +0000827 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000828}
829
Jim Laskeyf8913f12006-03-01 17:53:02 +0000830/// getDescString - Return a string used to compose global names and labels.
831///
832const char *DerivedTypeDesc::getDescString() const {
833 return "llvm.dbg.derivedtype";
834}
835
836/// getTypeString - Return a string used to label this descriptor's type.
837///
838const char *DerivedTypeDesc::getTypeString() const {
839 return "llvm.dbg.derivedtype.type";
840}
841
Jim Laskey434b40b2006-02-23 22:37:30 +0000842#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000843void DerivedTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000844 cerr << getDescString() << " "
845 << "Version(" << getVersion() << "), "
846 << "Tag(" << getTag() << "), "
847 << "Context(" << getContext() << "), "
848 << "Name(\"" << getName() << "\"), "
849 << "Size(" << getSize() << "), "
850 << "File(" << getFile() << "), "
851 << "Line(" << getLine() << "), "
852 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000853}
854#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000855
856//===----------------------------------------------------------------------===//
857
Jim Laskeyf8913f12006-03-01 17:53:02 +0000858CompositeTypeDesc::CompositeTypeDesc(unsigned T)
859: DerivedTypeDesc(T)
860, Elements()
861{}
862
Jim Laskey9c4447a2006-03-01 20:39:36 +0000863// Implement isa/cast/dyncast.
864bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
865 unsigned T = D->getTag();
866 switch (T) {
867 case DW_TAG_array_type:
868 case DW_TAG_structure_type:
869 case DW_TAG_union_type:
870 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000871 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000872 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000873 return true;
874 default: break;
875 }
876 return false;
877}
878
Jim Laskeyf8913f12006-03-01 17:53:02 +0000879/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
880///
881void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000882 DerivedTypeDesc::ApplyToFields(Visitor);
883
Jim Laskeyf8913f12006-03-01 17:53:02 +0000884 Visitor->Apply(Elements);
885}
886
887/// getDescString - Return a string used to compose global names and labels.
888///
889const char *CompositeTypeDesc::getDescString() const {
890 return "llvm.dbg.compositetype";
891}
892
893/// getTypeString - Return a string used to label this descriptor's type.
894///
895const char *CompositeTypeDesc::getTypeString() const {
896 return "llvm.dbg.compositetype.type";
897}
898
899#ifndef NDEBUG
900void CompositeTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000901 cerr << getDescString() << " "
902 << "Version(" << getVersion() << "), "
903 << "Tag(" << getTag() << "), "
904 << "Context(" << getContext() << "), "
905 << "Name(\"" << getName() << "\"), "
906 << "Size(" << getSize() << "), "
907 << "File(" << getFile() << "), "
908 << "Line(" << getLine() << "), "
909 << "FromType(" << getFromType() << "), "
910 << "Elements.size(" << Elements.size() << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000911}
912#endif
913
914//===----------------------------------------------------------------------===//
915
916SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000917: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000918, Lo(0)
919, Hi(0)
920{}
921
Jim Laskey9c4447a2006-03-01 20:39:36 +0000922// Implement isa/cast/dyncast.
923bool SubrangeDesc::classof(const DebugInfoDesc *D) {
924 return D->getTag() == DW_TAG_subrange_type;
925}
926
Jim Laskeyf8913f12006-03-01 17:53:02 +0000927/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
928///
929void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
930 DebugInfoDesc::ApplyToFields(Visitor);
931
932 Visitor->Apply(Lo);
933 Visitor->Apply(Hi);
934}
935
936/// getDescString - Return a string used to compose global names and labels.
937///
938const char *SubrangeDesc::getDescString() const {
939 return "llvm.dbg.subrange";
940}
941
942/// getTypeString - Return a string used to label this descriptor's type.
943///
944const char *SubrangeDesc::getTypeString() const {
945 return "llvm.dbg.subrange.type";
946}
947
948#ifndef NDEBUG
949void SubrangeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000950 cerr << getDescString() << " "
951 << "Version(" << getVersion() << "), "
952 << "Tag(" << getTag() << "), "
953 << "Lo(" << Lo << "), "
954 << "Hi(" << Hi << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000955}
956#endif
957
958//===----------------------------------------------------------------------===//
959
Jim Laskey6a3eb012006-03-01 23:52:37 +0000960EnumeratorDesc::EnumeratorDesc()
961: DebugInfoDesc(DW_TAG_enumerator)
962, Name("")
963, Value(0)
964{}
965
966// Implement isa/cast/dyncast.
967bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
968 return D->getTag() == DW_TAG_enumerator;
969}
970
971/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
972///
973void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
974 DebugInfoDesc::ApplyToFields(Visitor);
975
976 Visitor->Apply(Name);
977 Visitor->Apply(Value);
978}
979
980/// getDescString - Return a string used to compose global names and labels.
981///
982const char *EnumeratorDesc::getDescString() const {
983 return "llvm.dbg.enumerator";
984}
985
986/// getTypeString - Return a string used to label this descriptor's type.
987///
988const char *EnumeratorDesc::getTypeString() const {
989 return "llvm.dbg.enumerator.type";
990}
991
992#ifndef NDEBUG
993void EnumeratorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000994 cerr << getDescString() << " "
995 << "Version(" << getVersion() << "), "
996 << "Tag(" << getTag() << "), "
997 << "Name(" << Name << "), "
998 << "Value(" << Value << ")\n";
Jim Laskey6a3eb012006-03-01 23:52:37 +0000999}
1000#endif
1001
1002//===----------------------------------------------------------------------===//
1003
Jim Laskeyb8509c52006-03-23 18:07:55 +00001004VariableDesc::VariableDesc(unsigned T)
1005: DebugInfoDesc(T)
1006, Context(NULL)
1007, Name("")
1008, File(NULL)
1009, Line(0)
1010, TyDesc(0)
1011{}
1012
1013// Implement isa/cast/dyncast.
1014bool VariableDesc::classof(const DebugInfoDesc *D) {
1015 unsigned T = D->getTag();
1016 switch (T) {
1017 case DW_TAG_auto_variable:
1018 case DW_TAG_arg_variable:
1019 case DW_TAG_return_variable:
1020 return true;
1021 default: break;
1022 }
1023 return false;
1024}
1025
1026/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1027///
1028void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1029 DebugInfoDesc::ApplyToFields(Visitor);
1030
1031 Visitor->Apply(Context);
1032 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001033 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001034 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001035 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001036}
1037
1038/// getDescString - Return a string used to compose global names and labels.
1039///
1040const char *VariableDesc::getDescString() const {
1041 return "llvm.dbg.variable";
1042}
1043
1044/// getTypeString - Return a string used to label this descriptor's type.
1045///
1046const char *VariableDesc::getTypeString() const {
1047 return "llvm.dbg.variable.type";
1048}
1049
1050#ifndef NDEBUG
1051void VariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001052 cerr << getDescString() << " "
1053 << "Version(" << getVersion() << "), "
1054 << "Tag(" << getTag() << "), "
1055 << "Context(" << Context << "), "
1056 << "Name(\"" << Name << "\"), "
1057 << "File(" << File << "), "
1058 << "Line(" << Line << "), "
1059 << "TyDesc(" << TyDesc << ")\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001060}
1061#endif
1062
1063//===----------------------------------------------------------------------===//
1064
Jim Laskeyce72b172006-02-11 01:01:30 +00001065GlobalDesc::GlobalDesc(unsigned T)
1066: AnchoredDesc(T)
1067, Context(0)
1068, Name("")
Jim Laskey2172f962006-11-30 14:35:45 +00001069, FullName("")
1070, LinkageName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001071, File(NULL)
1072, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001073, TyDesc(NULL)
1074, IsStatic(false)
1075, IsDefinition(false)
1076{}
1077
1078/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001079///
Jim Laskeyce72b172006-02-11 01:01:30 +00001080void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1081 AnchoredDesc::ApplyToFields(Visitor);
1082
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001083 Visitor->Apply(Context);
1084 Visitor->Apply(Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001085 Visitor->Apply(FullName);
1086 Visitor->Apply(LinkageName);
Jim Laskey7089f452006-06-16 13:14:03 +00001087 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001088 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001089 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001090 Visitor->Apply(IsStatic);
1091 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001092}
1093
1094//===----------------------------------------------------------------------===//
1095
1096GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001097: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001098, Global(NULL)
1099{}
1100
Jim Laskey9c4447a2006-03-01 20:39:36 +00001101// Implement isa/cast/dyncast.
1102bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1103 return D->getTag() == DW_TAG_variable;
1104}
1105
Jim Laskeyce72b172006-02-11 01:01:30 +00001106/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1107///
1108void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1109 GlobalDesc::ApplyToFields(Visitor);
1110
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001111 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001112}
1113
Jim Laskeyce72b172006-02-11 01:01:30 +00001114/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001115///
Jim Laskeyce72b172006-02-11 01:01:30 +00001116const char *GlobalVariableDesc::getDescString() const {
1117 return "llvm.dbg.global_variable";
1118}
1119
1120/// getTypeString - Return a string used to label this descriptors type.
1121///
1122const char *GlobalVariableDesc::getTypeString() const {
1123 return "llvm.dbg.global_variable.type";
1124}
1125
1126/// getAnchorString - Return a string used to label this descriptor's anchor.
1127///
Dan Gohmancfbb2f02008-03-25 21:45:14 +00001128const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001129const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001130 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001131}
1132
Jim Laskey86cbdba2006-02-06 15:33:21 +00001133#ifndef NDEBUG
1134void GlobalVariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001135 cerr << getDescString() << " "
1136 << "Version(" << getVersion() << "), "
1137 << "Tag(" << getTag() << "), "
1138 << "Anchor(" << getAnchor() << "), "
1139 << "Name(\"" << getName() << "\"), "
1140 << "FullName(\"" << getFullName() << "\"), "
1141 << "LinkageName(\"" << getLinkageName() << "\"), "
1142 << "File(" << getFile() << "),"
1143 << "Line(" << getLine() << "),"
1144 << "Type(" << getType() << "), "
1145 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1146 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1147 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001148}
1149#endif
1150
1151//===----------------------------------------------------------------------===//
1152
Jim Laskeyce72b172006-02-11 01:01:30 +00001153SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001154: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001155{}
1156
Jim Laskey9c4447a2006-03-01 20:39:36 +00001157// Implement isa/cast/dyncast.
1158bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1159 return D->getTag() == DW_TAG_subprogram;
1160}
1161
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001162/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001163/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001164void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001165 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001166}
1167
Jim Laskeyce72b172006-02-11 01:01:30 +00001168/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001169///
Jim Laskeyce72b172006-02-11 01:01:30 +00001170const char *SubprogramDesc::getDescString() const {
1171 return "llvm.dbg.subprogram";
1172}
1173
1174/// getTypeString - Return a string used to label this descriptors type.
1175///
1176const char *SubprogramDesc::getTypeString() const {
1177 return "llvm.dbg.subprogram.type";
1178}
1179
1180/// getAnchorString - Return a string used to label this descriptor's anchor.
1181///
Dan Gohmancfbb2f02008-03-25 21:45:14 +00001182const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001183const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001184 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001185}
1186
Jim Laskey86cbdba2006-02-06 15:33:21 +00001187#ifndef NDEBUG
1188void SubprogramDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001189 cerr << getDescString() << " "
1190 << "Version(" << getVersion() << "), "
1191 << "Tag(" << getTag() << "), "
1192 << "Anchor(" << getAnchor() << "), "
1193 << "Name(\"" << getName() << "\"), "
1194 << "FullName(\"" << getFullName() << "\"), "
1195 << "LinkageName(\"" << getLinkageName() << "\"), "
1196 << "File(" << getFile() << "),"
1197 << "Line(" << getLine() << "),"
1198 << "Type(" << getType() << "), "
1199 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1200 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001201}
1202#endif
1203
Jim Laskey45ccae52006-02-28 20:15:07 +00001204//===----------------------------------------------------------------------===//
1205
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001206BlockDesc::BlockDesc()
1207: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001208, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001209{}
1210
1211// Implement isa/cast/dyncast.
1212bool BlockDesc::classof(const DebugInfoDesc *D) {
1213 return D->getTag() == DW_TAG_lexical_block;
1214}
1215
1216/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1217///
1218void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1219 DebugInfoDesc::ApplyToFields(Visitor);
1220
Jim Laskeyb8509c52006-03-23 18:07:55 +00001221 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001222}
1223
1224/// getDescString - Return a string used to compose global names and labels.
1225///
1226const char *BlockDesc::getDescString() const {
1227 return "llvm.dbg.block";
1228}
1229
1230/// getTypeString - Return a string used to label this descriptors type.
1231///
1232const char *BlockDesc::getTypeString() const {
1233 return "llvm.dbg.block.type";
1234}
1235
1236#ifndef NDEBUG
1237void BlockDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001238 cerr << getDescString() << " "
1239 << "Version(" << getVersion() << "), "
1240 << "Tag(" << getTag() << "),"
1241 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001242}
1243#endif
1244
1245//===----------------------------------------------------------------------===//
1246
Jim Laskey86cbdba2006-02-06 15:33:21 +00001247DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001248 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001249}
1250DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001251 // Handle NULL.
1252 if (!GV) return NULL;
1253
Jim Laskey86cbdba2006-02-06 15:33:21 +00001254 // Check to see if it has been already deserialized.
1255 DebugInfoDesc *&Slot = GlobalDescs[GV];
1256 if (Slot) return Slot;
1257
1258 // Get the Tag from the global.
1259 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1260
Jim Laskey86cbdba2006-02-06 15:33:21 +00001261 // Create an empty instance of the correct sort.
1262 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001263
Jim Laskey21407982006-03-14 18:37:57 +00001264 // If not a user defined descriptor.
1265 if (Slot) {
1266 // Deserialize the fields.
1267 DIDeserializeVisitor DRAM(*this, GV);
1268 DRAM.ApplyToFields(Slot);
1269 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001270
1271 return Slot;
1272}
1273
1274//===----------------------------------------------------------------------===//
1275
1276/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001277///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001278const PointerType *DISerializer::getStrPtrType() {
1279 // If not already defined.
1280 if (!StrPtrTy) {
1281 // Construct the pointer to signed bytes.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001282 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001283 }
1284
1285 return StrPtrTy;
1286}
1287
1288/// getEmptyStructPtrType - Return a "{ }*" type.
1289///
1290const PointerType *DISerializer::getEmptyStructPtrType() {
1291 // If not already defined.
1292 if (!EmptyStructPtrTy) {
1293 // Construct the empty structure type.
1294 const StructType *EmptyStructTy =
1295 StructType::get(std::vector<const Type*>());
1296 // Construct the pointer to empty structure type.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001297 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001298 }
1299
1300 return EmptyStructPtrTy;
1301}
1302
1303/// getTagType - Return the type describing the specified descriptor (via tag.)
1304///
1305const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1306 // Attempt to get the previously defined type.
1307 StructType *&Ty = TagTypes[DD->getTag()];
1308
1309 // If not already defined.
1310 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001311 // Set up fields vector.
1312 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001313 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001314 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001315 GTAM.ApplyToFields(DD);
1316
1317 // Construct structured type.
1318 Ty = StructType::get(Fields);
1319
Jim Laskey86cbdba2006-02-06 15:33:21 +00001320 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001321 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001322 }
1323
1324 return Ty;
1325}
1326
1327/// getString - Construct the string as constant string global.
1328///
Jim Laskeyce72b172006-02-11 01:01:30 +00001329Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001330 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001331 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001332 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001333 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001334 // If empty string then use a sbyte* null instead.
1335 if (String.empty()) {
1336 Slot = ConstantPointerNull::get(getStrPtrType());
1337 } else {
1338 // Construct string as an llvm constant.
1339 Constant *ConstStr = ConstantArray::get(String);
1340 // Otherwise create and return a new string global.
1341 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1342 GlobalVariable::InternalLinkage,
Devang Patel1e4c23a2007-05-11 23:14:43 +00001343 ConstStr, ".str", M);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001344 StrGV->setSection("llvm.metadata");
1345 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001346 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001347 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001348 return Slot;
1349
Jim Laskey86cbdba2006-02-06 15:33:21 +00001350}
1351
1352/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1353/// so that it can be serialized to a .bc or .ll file.
1354GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1355 // Check if the DebugInfoDesc is already in the map.
1356 GlobalVariable *&Slot = DescGlobals[DD];
1357
1358 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1359 if (Slot) return Slot;
1360
Jim Laskey86cbdba2006-02-06 15:33:21 +00001361 // Get the type associated with the Tag.
1362 const StructType *Ty = getTagType(DD);
1363
1364 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001365 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1366 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001367 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001368
1369 // Insert new GlobalVariable in DescGlobals map.
1370 Slot = GV;
1371
1372 // Set up elements vector
1373 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001374 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001375 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001376 SRAM.ApplyToFields(DD);
1377
1378 // Set the globals initializer.
1379 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1380
1381 return GV;
1382}
1383
Devang Patel962e0752007-11-30 00:51:33 +00001384/// addDescriptor - Directly connect DD with existing GV.
1385void DISerializer::addDescriptor(DebugInfoDesc *DD,
1386 GlobalVariable *GV) {
1387 DescGlobals[DD] = GV;
1388}
1389
Jim Laskey86cbdba2006-02-06 15:33:21 +00001390//===----------------------------------------------------------------------===//
1391
Jim Laskey86cbdba2006-02-06 15:33:21 +00001392/// Verify - Return true if the GlobalVariable appears to be a valid
1393/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001394bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001395 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001396}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001397bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001398 // NULLs are valid.
1399 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001400
Jim Laskey98e04102006-03-26 22:45:20 +00001401 // Check prior validity.
1402 unsigned &ValiditySlot = Validity[GV];
1403
1404 // If visited before then use old state.
1405 if (ValiditySlot) return ValiditySlot == Valid;
1406
1407 // Assume validity for the time being (recursion.)
1408 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001409
1410 // Make sure the global is internal or link once (anchor.)
1411 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1412 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1413 ValiditySlot = Invalid;
1414 return false;
1415 }
Jim Laskey98e04102006-03-26 22:45:20 +00001416
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001417 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001418 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001419
1420 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001421 if (Tag == DW_TAG_invalid) {
1422 ValiditySlot = Valid;
1423 return true;
1424 }
1425
1426 // Get the Version.
1427 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1428
1429 // Check for version mismatch.
1430 if (Version != LLVMDebugVersion) {
1431 ValiditySlot = Invalid;
1432 return false;
1433 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001434
Jim Laskey86cbdba2006-02-06 15:33:21 +00001435 // Construct an empty DebugInfoDesc.
1436 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001437
1438 // Allow for user defined descriptors.
1439 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001440
1441 // Get the initializer constant.
1442 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1443
1444 // Get the operand count.
1445 unsigned N = CI->getNumOperands();
1446
1447 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001448 unsigned &CountSlot = Counts[Tag];
1449 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001450 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001451 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001452 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001453 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001454 }
1455
Jim Laskey21407982006-03-14 18:37:57 +00001456 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001457 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001458 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001459 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001460 return false;
1461 }
1462
1463 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001464 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001465 VRAM.ApplyToFields(DD);
1466
1467 // Release empty DebugInfoDesc.
1468 delete DD;
1469
Jim Laskey98e04102006-03-26 22:45:20 +00001470 // If fields are not valid.
1471 if (!VRAM.isValid()) {
1472 ValiditySlot = Invalid;
1473 return false;
1474 }
1475
1476 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001477}
1478
Evan Chenga844bde2008-02-02 04:07:54 +00001479/// isVerified - Return true if the specified GV has already been
1480/// verified as a debug information descriptor.
1481bool DIVerifier::isVerified(GlobalVariable *GV) {
1482 unsigned &ValiditySlot = Validity[GV];
1483 if (ValiditySlot) return ValiditySlot == Valid;
1484 return false;
1485}
1486
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001487//===----------------------------------------------------------------------===//
1488
Jim Laskeyb8509c52006-03-23 18:07:55 +00001489DebugScope::~DebugScope() {
1490 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1491 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1492}
1493
1494//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001495
Jim Laskey6da18642007-01-26 21:38:26 +00001496MachineModuleInfo::MachineModuleInfo()
Devang Patel794fd752007-05-01 21:15:47 +00001497: ImmutablePass((intptr_t)&ID)
1498, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001499, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001500, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001501, Directories()
1502, SourceFiles()
1503, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001504, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001505, ScopeMap()
1506, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001507, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001508, LandingPads()
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001509, Personalities()
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001510, CallsEHReturn(0)
1511, CallsUnwindInit(0)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001512{
1513 // Always emit "no personality" info
1514 Personalities.push_back(NULL);
1515}
Jim Laskey6da18642007-01-26 21:38:26 +00001516MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001517
1518}
1519
Jim Laskey6da18642007-01-26 21:38:26 +00001520/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001521///
Jim Laskey6da18642007-01-26 21:38:26 +00001522bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001523 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001524}
1525
Jim Laskey6da18642007-01-26 21:38:26 +00001526/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001527///
Jim Laskey6da18642007-01-26 21:38:26 +00001528bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001529 return false;
1530}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001531
Jim Laskey6da18642007-01-26 21:38:26 +00001532/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001533///
Jim Laskey6da18642007-01-26 21:38:26 +00001534void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001535 // Coming soon.
1536}
1537
Jim Laskey6da18642007-01-26 21:38:26 +00001538/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001539///
Jim Laskey6da18642007-01-26 21:38:26 +00001540void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001541 // Clean up scope information.
1542 if (RootScope) {
1543 delete RootScope;
1544 ScopeMap.clear();
1545 RootScope = NULL;
1546 }
1547
Jim Laskeyb82313f2007-02-01 16:31:34 +00001548 // Clean up line info.
1549 Lines.clear();
1550
Jim Laskey41886992006-04-07 16:34:46 +00001551 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001552 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001553
1554 // Clean up exception info.
1555 LandingPads.clear();
1556 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +00001557 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +00001558 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001559 CallsEHReturn = 0;
1560 CallsUnwindInit = 0;
Jim Laskey41886992006-04-07 16:34:46 +00001561}
1562
Jim Laskeyd96185a2006-02-13 12:50:39 +00001563/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001564///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001565// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001566DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001567 return DR.Deserialize(V);
1568}
1569
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001570/// AnalyzeModule - Scan the module for global debug information.
1571///
Jim Laskey6da18642007-01-26 21:38:26 +00001572void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001573 SetupCompileUnits(M);
Dale Johannesen48ae02f2008-01-16 19:59:28 +00001574
1575 // Insert functions in the llvm.used array into UsedFunctions.
1576 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1577 if (!GV || !GV->hasInitializer()) return;
1578
1579 // Should be an array of 'i8*'.
1580 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1581 if (InitList == 0) return;
1582
1583 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1584 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1585 if (CE->getOpcode() == Instruction::BitCast)
1586 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1587 UsedFunctions.insert(F);
1588 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001589}
1590
1591/// SetupCompileUnits - Set up the unique vector of compile units.
1592///
Jim Laskey6da18642007-01-26 21:38:26 +00001593void MachineModuleInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001594 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001595
Jim Laskey0420f2a2006-02-22 19:02:11 +00001596 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1597 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001598 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001599}
1600
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001601/// getCompileUnits - Return a vector of debug compile units.
1602///
Jim Laskey6da18642007-01-26 21:38:26 +00001603const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001604 return CompileUnits;
1605}
1606
Jim Laskey0420f2a2006-02-22 19:02:11 +00001607/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1608/// named GlobalVariable.
1609std::vector<GlobalVariable*>
Jim Laskey6da18642007-01-26 21:38:26 +00001610MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1611 const std::string &RootName) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001612 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001613}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001614
Evan Chenga647c922008-02-01 02:05:57 +00001615/// RecordSourceLine - Records location information and associates it with a
Jim Laskeyb8509c52006-03-23 18:07:55 +00001616/// debug label. Returns a unique label ID used to generate a label and
1617/// provide correspondence to the source line list.
Evan Chenga647c922008-02-01 02:05:57 +00001618unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1619 unsigned Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001620 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001621 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001622 return ID;
1623}
1624
1625/// RecordSource - Register a source file with debug info. Returns an source
1626/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001627unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1628 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001629 unsigned DirectoryID = Directories.insert(Directory);
1630 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1631}
Jim Laskey6da18642007-01-26 21:38:26 +00001632unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001633 return RecordSource(CompileUnit->getDirectory(),
1634 CompileUnit->getFileName());
1635}
1636
1637/// RecordRegionStart - Indicate the start of a region.
1638///
Jim Laskey6da18642007-01-26 21:38:26 +00001639unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001640 // FIXME - need to be able to handle split scopes because of bb cloning.
1641 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1642 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1643 unsigned ID = NextLabelID();
1644 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1645 return ID;
1646}
1647
1648/// RecordRegionEnd - Indicate the end of a region.
1649///
Jim Laskey6da18642007-01-26 21:38:26 +00001650unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001651 // FIXME - need to be able to handle split scopes because of bb cloning.
1652 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1653 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1654 unsigned ID = NextLabelID();
1655 Scope->setEndLabelID(ID);
1656 return ID;
1657}
1658
1659/// RecordVariable - Indicate the declaration of a local variable.
1660///
Evan Chenga844bde2008-02-02 04:07:54 +00001661void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1662 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001663 DebugScope *Scope = getOrCreateScope(VD->getContext());
1664 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1665 Scope->AddVariable(DV);
1666}
1667
1668/// getOrCreateScope - Returns the scope associated with the given descriptor.
1669///
Jim Laskey6da18642007-01-26 21:38:26 +00001670DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001671 DebugScope *&Slot = ScopeMap[ScopeDesc];
1672 if (!Slot) {
1673 // FIXME - breaks down when the context is an inlined function.
1674 DebugInfoDesc *ParentDesc = NULL;
1675 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1676 ParentDesc = Block->getContext();
1677 }
1678 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1679 Slot = new DebugScope(Parent, ScopeDesc);
1680 if (Parent) {
1681 Parent->AddScope(Slot);
1682 } else if (RootScope) {
1683 // FIXME - Add inlined function scopes to the root so we can delete
1684 // them later. Long term, handle inlined functions properly.
1685 RootScope->AddScope(Slot);
1686 } else {
1687 // First function is top level function.
1688 RootScope = Slot;
1689 }
1690 }
1691 return Slot;
1692}
1693
Jim Laskey59667fe2007-02-21 22:38:31 +00001694//===-EH-------------------------------------------------------------------===//
1695
1696/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1697/// specified MachineBasicBlock.
1698LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1699 (MachineBasicBlock *LandingPad) {
1700 unsigned N = LandingPads.size();
1701 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001702 LandingPadInfo &LP = LandingPads[i];
1703 if (LP.LandingPadBlock == LandingPad)
1704 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001705 }
1706
1707 LandingPads.push_back(LandingPadInfo(LandingPad));
1708 return LandingPads[N];
1709}
1710
1711/// addInvoke - Provide the begin and end labels of an invoke style call and
1712/// associate it with a try landing pad block.
1713void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1714 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001715 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001716 LP.BeginLabels.push_back(BeginLabel);
1717 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +00001718}
1719
1720/// addLandingPad - Provide the label of a try LandingPad block.
1721///
1722unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1723 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001724 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1725 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001726 return LandingPadLabel;
1727}
1728
1729/// addPersonality - Provide the personality function for the exception
1730/// information.
1731void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001732 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001733 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001734 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001735
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001736 for (unsigned i = 0; i < Personalities.size(); ++i)
1737 if (Personalities[i] == Personality)
1738 return;
1739
1740 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +00001741}
1742
1743/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1744///
1745void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1746 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001747 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001748 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001749 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001750}
Duncan Sands73ef58a2007-06-02 16:53:42 +00001751
1752/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +00001753///
Duncan Sands73ef58a2007-06-02 16:53:42 +00001754void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1755 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001756 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Duncan Sands73ef58a2007-06-02 16:53:42 +00001757 std::vector<unsigned> IdsInFilter (TyInfo.size());
1758 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
1759 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1760 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +00001761}
1762
Duncan Sands6590b042007-08-27 15:47:50 +00001763/// addCleanup - Add a cleanup action for a landing pad.
1764///
1765void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1766 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1767 LP.TypeIds.push_back(0);
1768}
1769
Jim Laskey59667fe2007-02-21 22:38:31 +00001770/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1771/// pads.
1772void MachineModuleInfo::TidyLandingPads() {
1773 for (unsigned i = 0; i != LandingPads.size(); ) {
1774 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001775 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001776
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001777 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +00001778 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001779 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001780 LandingPads.erase(LandingPads.begin() + i);
1781 continue;
1782 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001783
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001784 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1785 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1786 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands57810cd2007-09-05 11:27:52 +00001787
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001788 if (!BeginLabel || !EndLabel) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001789 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1790 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1791 continue;
1792 }
1793
1794 LandingPad.BeginLabels[j] = BeginLabel;
1795 LandingPad.EndLabels[j] = EndLabel;
1796 ++j;
1797 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001798
1799 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +00001800 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +00001801 LandingPads.erase(LandingPads.begin() + i);
1802 continue;
1803 }
1804
1805 // If there is no landing pad, ensure that the list of typeids is empty.
1806 // If the only typeid is a cleanup, this is the same as having no typeids.
1807 if (!LandingPad.LandingPadBlock ||
1808 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1809 LandingPad.TypeIds.clear();
1810
Jim Laskey59667fe2007-02-21 22:38:31 +00001811 ++i;
1812 }
1813}
1814
1815/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1816/// function wide.
1817unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1818 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1819 if (TypeInfos[i] == TI) return i + 1;
1820
1821 TypeInfos.push_back(TI);
1822 return TypeInfos.size();
1823}
1824
Duncan Sands73ef58a2007-06-02 16:53:42 +00001825/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1826/// function wide.
1827int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +00001828 // If the new filter coincides with the tail of an existing filter, then
1829 // re-use the existing filter. Folding filters more than this requires
1830 // re-ordering filters and/or their elements - probably not worth it.
1831 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1832 E = FilterEnds.end(); I != E; ++I) {
1833 unsigned i = *I, j = TyIds.size();
1834
1835 while (i && j)
1836 if (FilterIds[--i] != TyIds[--j])
1837 goto try_next;
1838
1839 if (!j)
1840 // The new filter coincides with range [i, end) of the existing filter.
1841 return -(1 + i);
1842
1843try_next:;
1844 }
1845
1846 // Add the new filter.
Duncan Sands73ef58a2007-06-02 16:53:42 +00001847 int FilterID = -(1 + FilterIds.size());
1848 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1849 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
1850 FilterIds.push_back(TyIds[I]);
Duncan Sands14da32a2007-07-05 15:15:01 +00001851 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +00001852 FilterIds.push_back(0); // terminator
1853 return FilterID;
1854}
1855
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001856/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +00001857Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001858 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001859 // function
1860 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +00001861}
1862
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001863/// getPersonalityIndex - Return unique index for current personality
1864/// function. NULL personality function should always get zero index.
1865unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001866 const Function* Personality = NULL;
1867
1868 // Scan landing pads. If there is at least one non-NULL personality - use it.
1869 for (unsigned i = 0; i != LandingPads.size(); ++i)
1870 if (LandingPads[i].Personality) {
1871 Personality = LandingPads[i].Personality;
1872 break;
1873 }
1874
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001875 for (unsigned i = 0; i < Personalities.size(); ++i) {
1876 if (Personalities[i] == Personality)
1877 return i;
1878 }
1879
1880 // This should never happen
1881 assert(0 && "Personality function should be set!");
1882 return 0;
1883}
Jim Laskey59667fe2007-02-21 22:38:31 +00001884
Jim Laskey9d4209f2006-11-07 19:33:46 +00001885//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00001886/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1887/// a info consumer to determine if the range of two labels is empty, by seeing
1888/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00001889
1890namespace llvm {
1891
1892struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00001893 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +00001894 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1895
Jim Laskey9d4209f2006-11-07 19:33:46 +00001896 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00001897 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00001898};
1899
Devang Patel19974732007-05-03 01:11:54 +00001900char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00001901
Jim Laskey9d4209f2006-11-07 19:33:46 +00001902bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00001903 // Get machine module info.
1904 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1905 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001906
1907 // Track if change is made.
1908 bool MadeChange = false;
1909 // No prior label to begin.
1910 unsigned PriorLabel = 0;
1911
1912 // Iterate through basic blocks.
1913 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1914 BB != E; ++BB) {
1915 // Iterate through instructions.
1916 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00001917 // Is it a label.
Evan Chengbb81d972008-01-31 09:59:15 +00001918 if (I->isDebugLabel()) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00001919 // The label ID # is always operand #0, an immediate.
1920 unsigned NextLabel = I->getOperand(0).getImm();
1921
1922 // If there was an immediate prior label.
1923 if (PriorLabel) {
1924 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00001925 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00001926 // Delete the current label.
1927 I = BB->erase(I);
1928 // Indicate a change has been made.
1929 MadeChange = true;
1930 continue;
1931 } else {
1932 // Start a new round.
1933 PriorLabel = NextLabel;
1934 }
1935 } else {
1936 // No consecutive labels.
1937 PriorLabel = 0;
1938 }
1939
1940 ++I;
1941 }
1942 }
1943
1944 return MadeChange;
1945}
1946
1947FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1948
1949}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001950