blob: 69f8d3c014c852c9192f5a981579bce937c90029 [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"
Evan Cheng0ff39b32008-06-30 07:31:25 +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
Bill Wendling0ac1b6d2008-06-27 01:32:08 +000040getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +000041 // 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.
Bill Wendlingc04f4652008-07-03 23:13:02 +000055static void
56getGlobalVariablesUsing(Module &M, const std::string &RootName,
57 std::vector<GlobalVariable*> &Result) {
Jim Laskeyce72b172006-02-11 01:01:30 +000058 std::vector<const Type*> FieldTypes;
Reid Spencer47857812006-12-31 05:55:36 +000059 FieldTypes.push_back(Type::Int32Ty);
60 FieldTypes.push_back(Type::Int32Ty);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000061
Jim Laskey86cbdba2006-02-06 15:33:21 +000062 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000063 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000064 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000065
66 // If present and linkonce then scan for users.
Bill Wendlingc04f4652008-07-03 23:13:02 +000067 if (UseRoot && UseRoot->hasLinkOnceLinkage())
Jim Laskeyb3e789a2006-01-26 20:21:46 +000068 getGlobalVariablesUsing(UseRoot, Result);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000069}
70
Jim Laskey86cbdba2006-02-06 15:33:21 +000071/// isStringValue - Return true if the given value can be coerced to a string.
72///
73static bool isStringValue(Value *V) {
74 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 return Init->isString();
78 }
79 } else if (Constant *C = dyn_cast<Constant>(V)) {
80 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
81 return isStringValue(GV);
82 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
83 if (CE->getOpcode() == Instruction::GetElementPtr) {
84 if (CE->getNumOperands() == 3 &&
85 cast<Constant>(CE->getOperand(1))->isNullValue() &&
86 isa<ConstantInt>(CE->getOperand(2))) {
87 return isStringValue(CE->getOperand(0));
88 }
89 }
90 }
91 }
92 return false;
93}
94
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000095/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000096///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000097static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000098 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
99 return GV;
100 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000101 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000102 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000103 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
104 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000105 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000106 return NULL;
107 }
108 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000109 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000110 }
111 return NULL;
112}
113
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000114/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000115/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000116static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000117 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
118 return true;
119 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000120 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000121 return isa<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000122 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
123 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000124 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000125 return false;
126 }
127 return isa<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000128 }
129 }
130 return false;
131}
132
Bill Wendling10fff602008-07-03 22:53:42 +0000133/// getUIntOperand - Return ith operand if it is an unsigned integer.
134///
135static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
136 // Make sure the GlobalVariable has an initializer.
137 if (!GV->hasInitializer()) return NULL;
138
139 // Get the initializer constant.
140 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
141 if (!CI) return NULL;
142
143 // Check if there is at least i + 1 operands.
144 unsigned N = CI->getNumOperands();
145 if (i >= N) return NULL;
146
147 // Check constant.
148 return dyn_cast<ConstantInt>(CI->getOperand(i));
149}
150
Jim Laskey86cbdba2006-02-06 15:33:21 +0000151//===----------------------------------------------------------------------===//
152
Bill Wendling3e30cbb2008-07-09 06:02:33 +0000153static unsigned CountFields(DebugInfoDesc *DD) {
154 unsigned Count = 0;
155
156 switch (DD->getTag()) {
157 case DW_TAG_anchor: // AnchorDesc
158 // Tag
159 // AnchorTag
160 Count = 2;
161 break;
162 case DW_TAG_compile_unit: // CompileUnitDesc
163 // [DW_TAG_anchor]
164 // if (Version == 0) DebugVersion
165 // Language
166 // FileName
167 // Directory
168 // Producer
169 Count = 6;
170
171 // Handle cases out of sync with compiler.
172 if (DD->getVersion() == 0)
173 ++Count;
174
175 break;
176 case DW_TAG_variable: // GlobalVariableDesc
177 // [DW_TAG_anchor]
178 // Context
179 // Name
180 // FullName
181 // LinkageName
182 // File
183 // Line
184 // TyDesc
185 // IsStatic
186 // IsDefinition
187 // Global
188 Count = 12;
189 break;
190 case DW_TAG_subprogram: // SubprogramDesc
191 // [DW_TAG_anchor]
192 // Context
193 // Name
194 // FullName
195 // LinkageName
196 // File
197 // Line
198 // TyDesc
199 // IsStatic
200 // IsDefinition
201 Count = 11;
202 break;
203 case DW_TAG_lexical_block: // BlockDesc
204 // Tag
205 // Context
206 Count = 2;
207 break;
208 case DW_TAG_base_type: // BasicTypeDesc
209 // Tag
210 // Context
211 // Name
212 // File
213 // Line
214 // Size
215 // Align
216 // Offset
217 // if (Version > LLVMDebugVersion4) Flags
218 // Encoding
219 Count = 9;
220
221 if (DD->getVersion() > LLVMDebugVersion4)
222 ++Count;
223
224 break;
225 case DW_TAG_typedef:
226 case DW_TAG_pointer_type:
227 case DW_TAG_reference_type:
228 case DW_TAG_const_type:
229 case DW_TAG_volatile_type:
230 case DW_TAG_restrict_type:
231 case DW_TAG_member:
232 case DW_TAG_inheritance: // DerivedTypeDesc
233 // Tag
234 // Context
235 // Name
236 // File
237 // Line
238 // Size
239 // Align
240 // Offset
241 // if (Version > LLVMDebugVersion4) Flags
242 // FromType
243 Count = 9;
244
245 if (DD->getVersion() > LLVMDebugVersion4)
246 ++Count;
247
248 break;
249 case DW_TAG_array_type:
250 case DW_TAG_structure_type:
251 case DW_TAG_union_type:
252 case DW_TAG_enumeration_type:
253 case DW_TAG_vector_type:
254 case DW_TAG_subroutine_type: // CompositeTypeDesc
255 // Tag
256 // Context
257 // Name
258 // File
259 // Line
260 // Size
261 // Align
262 // Offset
263 // if (Version > LLVMDebugVersion4) Flags
264 // FromType
265 // Elements
266 Count = 10;
267
268 if (DD->getVersion() > LLVMDebugVersion4)
269 ++Count;
270
271 break;
272 case DW_TAG_subrange_type: // SubrangeDesc
273 // Tag
274 // Lo
275 // Hi
276 Count = 3;
277 break;
278 case DW_TAG_enumerator: // EnumeratorDesc
279 // Tag
280 // Name
281 // Value
282 Count = 3;
283 break;
284 case DW_TAG_return_variable:
285 case DW_TAG_arg_variable:
286 case DW_TAG_auto_variable: // VariableDesc
287 // Tag
288 // Context
289 // Name
290 // File
291 // Line
292 // TyDesc
293 Count = 6;
294 break;
295 default:
296 break;
297 }
298
299 return Count;
300}
301
302//===----------------------------------------------------------------------===//
303
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000304/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000305/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000306void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000307 DD->ApplyToFields(this);
308}
309
Dan Gohman844731a2008-05-13 00:00:25 +0000310namespace {
311
Jim Laskey86cbdba2006-02-06 15:33:21 +0000312//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000313/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
314/// supplied DebugInfoDesc.
315class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000316private:
317 DIDeserializer &DR; // Active deserializer.
318 unsigned I; // Current operand index.
319 ConstantStruct *CI; // GlobalVariable constant initializer.
320
321public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000322 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
Bill Wendling914c9702008-06-27 01:27:56 +0000323 : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer()))
Jim Laskey86cbdba2006-02-06 15:33:21 +0000324 {}
325
326 /// Apply - Set the value of each of the fields.
327 ///
328 virtual void Apply(int &Field) {
329 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000330 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000331 }
332 virtual void Apply(unsigned &Field) {
333 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000334 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000335 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000336 virtual void Apply(int64_t &Field) {
337 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000338 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000339 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000340 virtual void Apply(uint64_t &Field) {
341 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000342 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000343 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000344 virtual void Apply(bool &Field) {
345 Constant *C = CI->getOperand(I++);
Reid Spencer579dca12007-01-12 04:24:46 +0000346 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000347 }
348 virtual void Apply(std::string &Field) {
349 Constant *C = CI->getOperand(I++);
Evan Cheng0ff39b32008-06-30 07:31:25 +0000350 // Fills in the string if it succeeds
351 if (!GetConstantStringInfo(C, Field))
352 Field.clear();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000353 }
354 virtual void Apply(DebugInfoDesc *&Field) {
355 Constant *C = CI->getOperand(I++);
356 Field = DR.Deserialize(C);
357 }
358 virtual void Apply(GlobalVariable *&Field) {
359 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000360 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000361 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000362 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000363 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000364 Constant *C = CI->getOperand(I++);
365 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000366 if (GV->hasInitializer()) {
367 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
368 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
369 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
370 DebugInfoDesc *DE = DR.Deserialize(GVE);
371 Field.push_back(DE);
372 }
373 } else if (GV->getInitializer()->isNullValue()) {
374 if (const ArrayType *T =
375 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
376 Field.resize(T->getNumElements());
377 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000378 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000379 }
380 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000381};
382
383//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000384/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000385/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000386class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000387private:
388 DISerializer &SR; // Active serializer.
389 std::vector<Constant*> &Elements; // Element accumulator.
390
391public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000392 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
Bill Wendling10fff602008-07-03 22:53:42 +0000393 : DIVisitor()
394 , SR(S)
395 , Elements(E)
396 {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000397
398 /// Apply - Set the value of each of the fields.
399 ///
400 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000401 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000402 }
403 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000404 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000405 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000406 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000407 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000408 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000409 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000410 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000411 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000412 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000413 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000414 }
415 virtual void Apply(std::string &Field) {
Bill Wendling914c9702008-06-27 01:27:56 +0000416 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000417 }
418 virtual void Apply(DebugInfoDesc *&Field) {
419 GlobalVariable *GV = NULL;
420
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000421 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000422 if (Field) GV = SR.Serialize(Field);
423
424 // FIXME - At some point should use specific type.
425 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
426
427 if (GV) {
428 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000429 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000430 } else {
431 // Use NULL.
432 Elements.push_back(ConstantPointerNull::get(EmptyTy));
433 }
434 }
435 virtual void Apply(GlobalVariable *&Field) {
436 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000437 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000438 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000439 } else {
440 Elements.push_back(ConstantPointerNull::get(EmptyTy));
441 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000442 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000443 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
444 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
445 unsigned N = Field.size();
446 ArrayType *AT = ArrayType::get(EmptyTy, N);
447 std::vector<Constant *> ArrayElements;
448
Bill Wendling10fff602008-07-03 22:53:42 +0000449 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000450 if (DebugInfoDesc *Element = Field[i]) {
451 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000452 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000453 ArrayElements.push_back(cast<Constant>(CE));
454 } else {
455 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
456 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000457 }
458
459 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000460 GlobalVariable *CAGV = new GlobalVariable(AT, true,
461 GlobalValue::InternalLinkage,
462 CA, "llvm.dbg.array",
463 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000464 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000465 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000466 Elements.push_back(CAE);
467 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000468};
469
470//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000471/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000472/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000473class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000474private:
475 DISerializer &SR; // Active serializer.
476 std::vector<const Type*> &Fields; // Type accumulator.
477
478public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000479 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
Bill Wendling10fff602008-07-03 22:53:42 +0000480 : DIVisitor()
481 , SR(S)
482 , Fields(F)
483 {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000484
485 /// Apply - Set the value of each of the fields.
486 ///
487 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000488 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000489 }
490 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000491 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000492 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000493 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000494 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000495 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000496 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000497 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000498 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000499 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000500 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000501 }
502 virtual void Apply(std::string &Field) {
503 Fields.push_back(SR.getStrPtrType());
504 }
505 virtual void Apply(DebugInfoDesc *&Field) {
506 // FIXME - At some point should use specific type.
507 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
508 Fields.push_back(EmptyTy);
509 }
510 virtual void Apply(GlobalVariable *&Field) {
511 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
512 Fields.push_back(EmptyTy);
513 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000514 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
515 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
516 Fields.push_back(EmptyTy);
517 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000518};
519
520//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000521/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000522/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000523class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000524private:
525 DIVerifier &VR; // Active verifier.
526 bool IsValid; // Validity status.
527 unsigned I; // Current operand index.
528 ConstantStruct *CI; // GlobalVariable constant initializer.
529
530public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000531 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
532 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000533 , VR(V)
534 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000535 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000536 , CI(cast<ConstantStruct>(GV->getInitializer()))
537 {
538 }
539
540 // Accessors.
541 bool isValid() const { return IsValid; }
542
543 /// Apply - Set the value of each of the fields.
544 ///
545 virtual void Apply(int &Field) {
546 Constant *C = CI->getOperand(I++);
547 IsValid = IsValid && isa<ConstantInt>(C);
548 }
549 virtual void Apply(unsigned &Field) {
550 Constant *C = CI->getOperand(I++);
551 IsValid = IsValid && isa<ConstantInt>(C);
552 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000553 virtual void Apply(int64_t &Field) {
554 Constant *C = CI->getOperand(I++);
555 IsValid = IsValid && isa<ConstantInt>(C);
556 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000557 virtual void Apply(uint64_t &Field) {
558 Constant *C = CI->getOperand(I++);
559 IsValid = IsValid && isa<ConstantInt>(C);
560 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000561 virtual void Apply(bool &Field) {
562 Constant *C = CI->getOperand(I++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000563 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000564 }
565 virtual void Apply(std::string &Field) {
566 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000567 IsValid = IsValid &&
568 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000569 }
570 virtual void Apply(DebugInfoDesc *&Field) {
571 // FIXME - Prepare the correct descriptor.
572 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000573 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000574 }
575 virtual void Apply(GlobalVariable *&Field) {
576 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000577 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000578 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000579 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
580 Constant *C = CI->getOperand(I++);
581 IsValid = IsValid && isGlobalVariable(C);
582 if (!IsValid) return;
583
584 GlobalVariable *GV = getGlobalVariable(C);
585 IsValid = IsValid && GV && GV->hasInitializer();
586 if (!IsValid) return;
587
588 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
589 IsValid = IsValid && CA;
590 if (!IsValid) return;
591
592 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
593 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
594 if (!IsValid) return;
595
596 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
597 VR.Verify(GVE);
598 }
599 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000600};
601
Dan Gohman844731a2008-05-13 00:00:25 +0000602}
Jim Laskeyce72b172006-02-11 01:01:30 +0000603
Jim Laskey86cbdba2006-02-06 15:33:21 +0000604//===----------------------------------------------------------------------===//
605
Bill Wendling10fff602008-07-03 22:53:42 +0000606/// TagFromGlobal - Returns the tag number from a debug info descriptor
607/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
608unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
609 ConstantInt *C = getUIntOperand(GV, 0);
610 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
611 (unsigned)DW_TAG_invalid;
612}
613
614/// VersionFromGlobal - Returns the version number from a debug info
615/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
616/// int.
617unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
618 ConstantInt *C = getUIntOperand(GV, 0);
619 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
620 (unsigned)DW_TAG_invalid;
621}
622
623/// DescFactory - Create an instance of debug info descriptor based on Tag.
624/// Return NULL if not a recognized Tag.
625DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
626 switch (Tag) {
627 case DW_TAG_anchor: return new AnchorDesc();
628 case DW_TAG_compile_unit: return new CompileUnitDesc();
629 case DW_TAG_variable: return new GlobalVariableDesc();
630 case DW_TAG_subprogram: return new SubprogramDesc();
631 case DW_TAG_lexical_block: return new BlockDesc();
632 case DW_TAG_base_type: return new BasicTypeDesc();
633 case DW_TAG_typedef:
634 case DW_TAG_pointer_type:
635 case DW_TAG_reference_type:
636 case DW_TAG_const_type:
637 case DW_TAG_volatile_type:
638 case DW_TAG_restrict_type:
639 case DW_TAG_member:
640 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
641 case DW_TAG_array_type:
642 case DW_TAG_structure_type:
643 case DW_TAG_union_type:
644 case DW_TAG_enumeration_type:
645 case DW_TAG_vector_type:
646 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
647 case DW_TAG_subrange_type: return new SubrangeDesc();
648 case DW_TAG_enumerator: return new EnumeratorDesc();
649 case DW_TAG_return_variable:
650 case DW_TAG_arg_variable:
651 case DW_TAG_auto_variable: return new VariableDesc(Tag);
652 default: break;
653 }
654 return NULL;
655}
656
657/// getLinkage - get linkage appropriate for this type of descriptor.
658///
659GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
660 return GlobalValue::InternalLinkage;
661}
662
663/// ApplyToFields - Target the vistor to the fields of the descriptor.
664///
665void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
666 Visitor->Apply(Tag);
667}
668
669//===----------------------------------------------------------------------===//
670
671AnchorDesc::AnchorDesc()
672: DebugInfoDesc(DW_TAG_anchor)
673, AnchorTag(0)
674{}
675AnchorDesc::AnchorDesc(AnchoredDesc *D)
676: DebugInfoDesc(DW_TAG_anchor)
677, AnchorTag(D->getTag())
678{}
679
680// Implement isa/cast/dyncast.
681bool AnchorDesc::classof(const DebugInfoDesc *D) {
682 return D->getTag() == DW_TAG_anchor;
683}
684
685/// getLinkage - get linkage appropriate for this type of descriptor.
686///
687GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
688 return GlobalValue::LinkOnceLinkage;
689}
690
691/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
692///
693void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
694 DebugInfoDesc::ApplyToFields(Visitor);
695
696 Visitor->Apply(AnchorTag);
697}
698
699/// getDescString - Return a string used to compose global names and labels. A
700/// A global variable name needs to be defined for each debug descriptor that is
701/// anchored. NOTE: that each global variable named here also needs to be added
702/// to the list of names left external in the internalizer.
703/// ExternalNames.insert("llvm.dbg.compile_units");
704/// ExternalNames.insert("llvm.dbg.global_variables");
705/// ExternalNames.insert("llvm.dbg.subprograms");
706const char *AnchorDesc::getDescString() const {
707 switch (AnchorTag) {
708 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
709 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
710 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
711 default: break;
712 }
713
714 assert(0 && "Tag does not have a case for anchor string");
715 return "";
716}
717
718/// getTypeString - Return a string used to label this descriptors type.
719///
720const char *AnchorDesc::getTypeString() const {
721 return "llvm.dbg.anchor.type";
722}
723
724#ifndef NDEBUG
725void AnchorDesc::dump() {
726 cerr << getDescString() << " "
727 << "Version(" << getVersion() << "), "
728 << "Tag(" << getTag() << "), "
729 << "AnchorTag(" << AnchorTag << ")\n";
730}
731#endif
732
733//===----------------------------------------------------------------------===//
734
735AnchoredDesc::AnchoredDesc(unsigned T)
736: DebugInfoDesc(T)
737, Anchor(NULL)
738{}
739
740/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
741///
742void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
743 DebugInfoDesc::ApplyToFields(Visitor);
744
745 Visitor->Apply(Anchor);
746}
747
748//===----------------------------------------------------------------------===//
749
750CompileUnitDesc::CompileUnitDesc()
751: AnchoredDesc(DW_TAG_compile_unit)
752, Language(0)
753, FileName("")
754, Directory("")
755, Producer("")
756{}
757
758// Implement isa/cast/dyncast.
759bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
760 return D->getTag() == DW_TAG_compile_unit;
761}
762
763/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
764///
765void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
766 AnchoredDesc::ApplyToFields(Visitor);
767
768 // Handle cases out of sync with compiler.
769 if (getVersion() == 0) {
770 unsigned DebugVersion;
771 Visitor->Apply(DebugVersion);
772 }
773
774 Visitor->Apply(Language);
775 Visitor->Apply(FileName);
776 Visitor->Apply(Directory);
777 Visitor->Apply(Producer);
778}
779
780/// getDescString - Return a string used to compose global names and labels.
781///
782const char *CompileUnitDesc::getDescString() const {
783 return "llvm.dbg.compile_unit";
784}
785
786/// getTypeString - Return a string used to label this descriptors type.
787///
788const char *CompileUnitDesc::getTypeString() const {
789 return "llvm.dbg.compile_unit.type";
790}
791
792/// getAnchorString - Return a string used to label this descriptor's anchor.
793///
794const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
795const char *CompileUnitDesc::getAnchorString() const {
796 return AnchorString;
797}
798
799#ifndef NDEBUG
800void CompileUnitDesc::dump() {
801 cerr << getDescString() << " "
802 << "Version(" << getVersion() << "), "
803 << "Tag(" << getTag() << "), "
804 << "Anchor(" << getAnchor() << "), "
805 << "Language(" << Language << "), "
806 << "FileName(\"" << FileName << "\"), "
807 << "Directory(\"" << Directory << "\"), "
808 << "Producer(\"" << Producer << "\")\n";
809}
810#endif
811
812//===----------------------------------------------------------------------===//
813
814TypeDesc::TypeDesc(unsigned T)
815: DebugInfoDesc(T)
816, Context(NULL)
817, Name("")
818, File(NULL)
819, Line(0)
820, Size(0)
821, Align(0)
822, Offset(0)
823, Flags(0)
824{}
825
826/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
827///
828void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
829 DebugInfoDesc::ApplyToFields(Visitor);
830
831 Visitor->Apply(Context);
832 Visitor->Apply(Name);
833 Visitor->Apply(File);
834 Visitor->Apply(Line);
835 Visitor->Apply(Size);
836 Visitor->Apply(Align);
837 Visitor->Apply(Offset);
838 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
839}
840
841/// getDescString - Return a string used to compose global names and labels.
842///
843const char *TypeDesc::getDescString() const {
844 return "llvm.dbg.type";
845}
846
847/// getTypeString - Return a string used to label this descriptor's type.
848///
849const char *TypeDesc::getTypeString() const {
850 return "llvm.dbg.type.type";
851}
852
853#ifndef NDEBUG
854void TypeDesc::dump() {
855 cerr << getDescString() << " "
856 << "Version(" << getVersion() << "), "
857 << "Tag(" << getTag() << "), "
858 << "Context(" << Context << "), "
859 << "Name(\"" << Name << "\"), "
860 << "File(" << File << "), "
861 << "Line(" << Line << "), "
862 << "Size(" << Size << "), "
863 << "Align(" << Align << "), "
864 << "Offset(" << Offset << "), "
865 << "Flags(" << Flags << ")\n";
866}
867#endif
868
869//===----------------------------------------------------------------------===//
870
871BasicTypeDesc::BasicTypeDesc()
872: TypeDesc(DW_TAG_base_type)
873, Encoding(0)
874{}
875
876// Implement isa/cast/dyncast.
877bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
878 return D->getTag() == DW_TAG_base_type;
879}
880
881/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
882///
883void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
884 TypeDesc::ApplyToFields(Visitor);
885
886 Visitor->Apply(Encoding);
887}
888
889/// getDescString - Return a string used to compose global names and labels.
890///
891const char *BasicTypeDesc::getDescString() const {
892 return "llvm.dbg.basictype";
893}
894
895/// getTypeString - Return a string used to label this descriptor's type.
896///
897const char *BasicTypeDesc::getTypeString() const {
898 return "llvm.dbg.basictype.type";
899}
900
901#ifndef NDEBUG
902void BasicTypeDesc::dump() {
903 cerr << getDescString() << " "
904 << "Version(" << getVersion() << "), "
905 << "Tag(" << getTag() << "), "
906 << "Context(" << getContext() << "), "
907 << "Name(\"" << getName() << "\"), "
908 << "Size(" << getSize() << "), "
909 << "Encoding(" << Encoding << ")\n";
910}
911#endif
912
913//===----------------------------------------------------------------------===//
914
915DerivedTypeDesc::DerivedTypeDesc(unsigned T)
916: TypeDesc(T)
917, FromType(NULL)
918{}
919
920// Implement isa/cast/dyncast.
921bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
922 unsigned T = D->getTag();
923 switch (T) {
924 case DW_TAG_typedef:
925 case DW_TAG_pointer_type:
926 case DW_TAG_reference_type:
927 case DW_TAG_const_type:
928 case DW_TAG_volatile_type:
929 case DW_TAG_restrict_type:
930 case DW_TAG_member:
931 case DW_TAG_inheritance:
932 return true;
933 default: break;
934 }
935 return false;
936}
937
938/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
939///
940void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
941 TypeDesc::ApplyToFields(Visitor);
942
943 Visitor->Apply(FromType);
944}
945
946/// getDescString - Return a string used to compose global names and labels.
947///
948const char *DerivedTypeDesc::getDescString() const {
949 return "llvm.dbg.derivedtype";
950}
951
952/// getTypeString - Return a string used to label this descriptor's type.
953///
954const char *DerivedTypeDesc::getTypeString() const {
955 return "llvm.dbg.derivedtype.type";
956}
957
958#ifndef NDEBUG
959void DerivedTypeDesc::dump() {
960 cerr << getDescString() << " "
961 << "Version(" << getVersion() << "), "
962 << "Tag(" << getTag() << "), "
963 << "Context(" << getContext() << "), "
964 << "Name(\"" << getName() << "\"), "
965 << "Size(" << getSize() << "), "
966 << "File(" << getFile() << "), "
967 << "Line(" << getLine() << "), "
968 << "FromType(" << FromType << ")\n";
969}
970#endif
971
972//===----------------------------------------------------------------------===//
973
974CompositeTypeDesc::CompositeTypeDesc(unsigned T)
975: DerivedTypeDesc(T)
976, Elements()
977{}
978
979// Implement isa/cast/dyncast.
980bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
981 unsigned T = D->getTag();
982 switch (T) {
983 case DW_TAG_array_type:
984 case DW_TAG_structure_type:
985 case DW_TAG_union_type:
986 case DW_TAG_enumeration_type:
987 case DW_TAG_vector_type:
988 case DW_TAG_subroutine_type:
989 return true;
990 default: break;
991 }
992 return false;
993}
994
995/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
996///
997void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
998 DerivedTypeDesc::ApplyToFields(Visitor);
999
1000 Visitor->Apply(Elements);
1001}
1002
1003/// getDescString - Return a string used to compose global names and labels.
1004///
1005const char *CompositeTypeDesc::getDescString() const {
1006 return "llvm.dbg.compositetype";
1007}
1008
1009/// getTypeString - Return a string used to label this descriptor's type.
1010///
1011const char *CompositeTypeDesc::getTypeString() const {
1012 return "llvm.dbg.compositetype.type";
1013}
1014
1015#ifndef NDEBUG
1016void CompositeTypeDesc::dump() {
1017 cerr << getDescString() << " "
1018 << "Version(" << getVersion() << "), "
1019 << "Tag(" << getTag() << "), "
1020 << "Context(" << getContext() << "), "
1021 << "Name(\"" << getName() << "\"), "
1022 << "Size(" << getSize() << "), "
1023 << "File(" << getFile() << "), "
1024 << "Line(" << getLine() << "), "
1025 << "FromType(" << getFromType() << "), "
1026 << "Elements.size(" << Elements.size() << ")\n";
1027}
1028#endif
1029
1030//===----------------------------------------------------------------------===//
1031
1032SubrangeDesc::SubrangeDesc()
1033: DebugInfoDesc(DW_TAG_subrange_type)
1034, Lo(0)
1035, Hi(0)
1036{}
1037
1038// Implement isa/cast/dyncast.
1039bool SubrangeDesc::classof(const DebugInfoDesc *D) {
1040 return D->getTag() == DW_TAG_subrange_type;
1041}
1042
1043/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
1044///
1045void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
1046 DebugInfoDesc::ApplyToFields(Visitor);
1047
1048 Visitor->Apply(Lo);
1049 Visitor->Apply(Hi);
1050}
1051
1052/// getDescString - Return a string used to compose global names and labels.
1053///
1054const char *SubrangeDesc::getDescString() const {
1055 return "llvm.dbg.subrange";
1056}
1057
1058/// getTypeString - Return a string used to label this descriptor's type.
1059///
1060const char *SubrangeDesc::getTypeString() const {
1061 return "llvm.dbg.subrange.type";
1062}
1063
1064#ifndef NDEBUG
1065void SubrangeDesc::dump() {
1066 cerr << getDescString() << " "
1067 << "Version(" << getVersion() << "), "
1068 << "Tag(" << getTag() << "), "
1069 << "Lo(" << Lo << "), "
1070 << "Hi(" << Hi << ")\n";
1071}
1072#endif
1073
1074//===----------------------------------------------------------------------===//
1075
1076EnumeratorDesc::EnumeratorDesc()
1077: DebugInfoDesc(DW_TAG_enumerator)
1078, Name("")
1079, Value(0)
1080{}
1081
1082// Implement isa/cast/dyncast.
1083bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
1084 return D->getTag() == DW_TAG_enumerator;
1085}
1086
1087/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
1088///
1089void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
1090 DebugInfoDesc::ApplyToFields(Visitor);
1091
1092 Visitor->Apply(Name);
1093 Visitor->Apply(Value);
1094}
1095
1096/// getDescString - Return a string used to compose global names and labels.
1097///
1098const char *EnumeratorDesc::getDescString() const {
1099 return "llvm.dbg.enumerator";
1100}
1101
1102/// getTypeString - Return a string used to label this descriptor's type.
1103///
1104const char *EnumeratorDesc::getTypeString() const {
1105 return "llvm.dbg.enumerator.type";
1106}
1107
1108#ifndef NDEBUG
1109void EnumeratorDesc::dump() {
1110 cerr << getDescString() << " "
1111 << "Version(" << getVersion() << "), "
1112 << "Tag(" << getTag() << "), "
1113 << "Name(" << Name << "), "
1114 << "Value(" << Value << ")\n";
1115}
1116#endif
1117
1118//===----------------------------------------------------------------------===//
1119
1120VariableDesc::VariableDesc(unsigned T)
1121: DebugInfoDesc(T)
1122, Context(NULL)
1123, Name("")
1124, File(NULL)
1125, Line(0)
1126, TyDesc(0)
1127{}
1128
1129// Implement isa/cast/dyncast.
1130bool VariableDesc::classof(const DebugInfoDesc *D) {
1131 unsigned T = D->getTag();
1132 switch (T) {
1133 case DW_TAG_auto_variable:
1134 case DW_TAG_arg_variable:
1135 case DW_TAG_return_variable:
1136 return true;
1137 default: break;
1138 }
1139 return false;
1140}
1141
1142/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1143///
1144void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1145 DebugInfoDesc::ApplyToFields(Visitor);
1146
1147 Visitor->Apply(Context);
1148 Visitor->Apply(Name);
1149 Visitor->Apply(File);
1150 Visitor->Apply(Line);
1151 Visitor->Apply(TyDesc);
1152}
1153
1154/// getDescString - Return a string used to compose global names and labels.
1155///
1156const char *VariableDesc::getDescString() const {
1157 return "llvm.dbg.variable";
1158}
1159
1160/// getTypeString - Return a string used to label this descriptor's type.
1161///
1162const char *VariableDesc::getTypeString() const {
1163 return "llvm.dbg.variable.type";
1164}
1165
1166#ifndef NDEBUG
1167void VariableDesc::dump() {
1168 cerr << getDescString() << " "
1169 << "Version(" << getVersion() << "), "
1170 << "Tag(" << getTag() << "), "
1171 << "Context(" << Context << "), "
1172 << "Name(\"" << Name << "\"), "
1173 << "File(" << File << "), "
1174 << "Line(" << Line << "), "
1175 << "TyDesc(" << TyDesc << ")\n";
1176}
1177#endif
1178
1179//===----------------------------------------------------------------------===//
1180
1181GlobalDesc::GlobalDesc(unsigned T)
1182: AnchoredDesc(T)
1183, Context(0)
1184, Name("")
1185, FullName("")
1186, LinkageName("")
1187, File(NULL)
1188, Line(0)
1189, TyDesc(NULL)
1190, IsStatic(false)
1191, IsDefinition(false)
1192{}
1193
1194/// ApplyToFields - Target the visitor to the fields of the global.
1195///
1196void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1197 AnchoredDesc::ApplyToFields(Visitor);
1198
1199 Visitor->Apply(Context);
1200 Visitor->Apply(Name);
1201 Visitor->Apply(FullName);
1202 Visitor->Apply(LinkageName);
1203 Visitor->Apply(File);
1204 Visitor->Apply(Line);
1205 Visitor->Apply(TyDesc);
1206 Visitor->Apply(IsStatic);
1207 Visitor->Apply(IsDefinition);
1208}
1209
1210//===----------------------------------------------------------------------===//
1211
1212GlobalVariableDesc::GlobalVariableDesc()
1213: GlobalDesc(DW_TAG_variable)
1214, Global(NULL)
1215{}
1216
1217// Implement isa/cast/dyncast.
1218bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1219 return D->getTag() == DW_TAG_variable;
1220}
1221
1222/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1223///
1224void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1225 GlobalDesc::ApplyToFields(Visitor);
1226
1227 Visitor->Apply(Global);
1228}
1229
1230/// getDescString - Return a string used to compose global names and labels.
1231///
1232const char *GlobalVariableDesc::getDescString() const {
1233 return "llvm.dbg.global_variable";
1234}
1235
1236/// getTypeString - Return a string used to label this descriptors type.
1237///
1238const char *GlobalVariableDesc::getTypeString() const {
1239 return "llvm.dbg.global_variable.type";
1240}
1241
1242/// getAnchorString - Return a string used to label this descriptor's anchor.
1243///
1244const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1245const char *GlobalVariableDesc::getAnchorString() const {
1246 return AnchorString;
1247}
1248
1249#ifndef NDEBUG
1250void GlobalVariableDesc::dump() {
1251 cerr << getDescString() << " "
1252 << "Version(" << getVersion() << "), "
1253 << "Tag(" << getTag() << "), "
1254 << "Anchor(" << getAnchor() << "), "
1255 << "Name(\"" << getName() << "\"), "
1256 << "FullName(\"" << getFullName() << "\"), "
1257 << "LinkageName(\"" << getLinkageName() << "\"), "
1258 << "File(" << getFile() << "),"
1259 << "Line(" << getLine() << "),"
1260 << "Type(" << getType() << "), "
1261 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1262 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1263 << "Global(" << Global << ")\n";
1264}
1265#endif
1266
1267//===----------------------------------------------------------------------===//
1268
1269SubprogramDesc::SubprogramDesc()
1270: GlobalDesc(DW_TAG_subprogram)
1271{}
1272
1273// Implement isa/cast/dyncast.
1274bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1275 return D->getTag() == DW_TAG_subprogram;
1276}
1277
1278/// ApplyToFields - Target the visitor to the fields of the
1279/// SubprogramDesc.
1280void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1281 GlobalDesc::ApplyToFields(Visitor);
1282}
1283
1284/// getDescString - Return a string used to compose global names and labels.
1285///
1286const char *SubprogramDesc::getDescString() const {
1287 return "llvm.dbg.subprogram";
1288}
1289
1290/// getTypeString - Return a string used to label this descriptors type.
1291///
1292const char *SubprogramDesc::getTypeString() const {
1293 return "llvm.dbg.subprogram.type";
1294}
1295
1296/// getAnchorString - Return a string used to label this descriptor's anchor.
1297///
1298const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1299const char *SubprogramDesc::getAnchorString() const {
1300 return AnchorString;
1301}
1302
1303#ifndef NDEBUG
1304void SubprogramDesc::dump() {
1305 cerr << getDescString() << " "
1306 << "Version(" << getVersion() << "), "
1307 << "Tag(" << getTag() << "), "
1308 << "Anchor(" << getAnchor() << "), "
1309 << "Name(\"" << getName() << "\"), "
1310 << "FullName(\"" << getFullName() << "\"), "
1311 << "LinkageName(\"" << getLinkageName() << "\"), "
1312 << "File(" << getFile() << "),"
1313 << "Line(" << getLine() << "),"
1314 << "Type(" << getType() << "), "
1315 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1316 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1317}
1318#endif
1319
1320//===----------------------------------------------------------------------===//
1321
1322BlockDesc::BlockDesc()
1323: DebugInfoDesc(DW_TAG_lexical_block)
1324, Context(NULL)
1325{}
1326
1327// Implement isa/cast/dyncast.
1328bool BlockDesc::classof(const DebugInfoDesc *D) {
1329 return D->getTag() == DW_TAG_lexical_block;
1330}
1331
1332/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1333///
1334void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1335 DebugInfoDesc::ApplyToFields(Visitor);
1336
1337 Visitor->Apply(Context);
1338}
1339
1340/// getDescString - Return a string used to compose global names and labels.
1341///
1342const char *BlockDesc::getDescString() const {
1343 return "llvm.dbg.block";
1344}
1345
1346/// getTypeString - Return a string used to label this descriptors type.
1347///
1348const char *BlockDesc::getTypeString() const {
1349 return "llvm.dbg.block.type";
1350}
1351
1352#ifndef NDEBUG
1353void BlockDesc::dump() {
1354 cerr << getDescString() << " "
1355 << "Version(" << getVersion() << "), "
1356 << "Tag(" << getTag() << "),"
1357 << "Context(" << Context << ")\n";
1358}
1359#endif
1360
1361//===----------------------------------------------------------------------===//
1362
Jim Laskey86cbdba2006-02-06 15:33:21 +00001363DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001364 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001365}
1366DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001367 // Handle NULL.
1368 if (!GV) return NULL;
1369
Jim Laskey86cbdba2006-02-06 15:33:21 +00001370 // Check to see if it has been already deserialized.
1371 DebugInfoDesc *&Slot = GlobalDescs[GV];
1372 if (Slot) return Slot;
1373
1374 // Get the Tag from the global.
1375 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1376
Jim Laskey86cbdba2006-02-06 15:33:21 +00001377 // Create an empty instance of the correct sort.
1378 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001379
Jim Laskey21407982006-03-14 18:37:57 +00001380 // If not a user defined descriptor.
1381 if (Slot) {
1382 // Deserialize the fields.
1383 DIDeserializeVisitor DRAM(*this, GV);
1384 DRAM.ApplyToFields(Slot);
1385 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001386
1387 return Slot;
1388}
1389
1390//===----------------------------------------------------------------------===//
1391
1392/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001393///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001394const PointerType *DISerializer::getStrPtrType() {
1395 // If not already defined.
1396 if (!StrPtrTy) {
1397 // Construct the pointer to signed bytes.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001398 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001399 }
1400
1401 return StrPtrTy;
1402}
1403
1404/// getEmptyStructPtrType - Return a "{ }*" type.
1405///
1406const PointerType *DISerializer::getEmptyStructPtrType() {
1407 // If not already defined.
Bill Wendling914c9702008-06-27 01:27:56 +00001408 if (EmptyStructPtrTy) return EmptyStructPtrTy;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001409
Bill Wendling914c9702008-06-27 01:27:56 +00001410 // Construct the pointer to empty structure type.
Bill Wendling49255672008-07-07 21:41:57 +00001411 const StructType *EmptyStructTy = StructType::get(NULL, NULL);
Bill Wendling0ac1b6d2008-06-27 01:32:08 +00001412
1413 // Construct the pointer to empty structure type.
1414 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001415 return EmptyStructPtrTy;
1416}
1417
1418/// getTagType - Return the type describing the specified descriptor (via tag.)
1419///
1420const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1421 // Attempt to get the previously defined type.
1422 StructType *&Ty = TagTypes[DD->getTag()];
1423
1424 // If not already defined.
1425 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001426 // Set up fields vector.
1427 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001428 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001429 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001430 GTAM.ApplyToFields(DD);
1431
1432 // Construct structured type.
1433 Ty = StructType::get(Fields);
1434
Jim Laskey86cbdba2006-02-06 15:33:21 +00001435 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001436 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001437 }
1438
1439 return Ty;
1440}
1441
1442/// getString - Construct the string as constant string global.
1443///
Jim Laskeyce72b172006-02-11 01:01:30 +00001444Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001445 // Check string cache for previous edition.
Bill Wendling667a68b2008-07-07 20:59:31 +00001446 Constant *&Slot = StringCache[String];
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001447
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001448 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001449 if (Slot) return Slot;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001450
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001451 // If empty string then use a sbyte* null instead.
1452 if (String.empty()) {
1453 Slot = ConstantPointerNull::get(getStrPtrType());
1454 } else {
1455 // Construct string as an llvm constant.
1456 Constant *ConstStr = ConstantArray::get(String);
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001457
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001458 // Otherwise create and return a new string global.
1459 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1460 GlobalVariable::InternalLinkage,
Devang Patel1e4c23a2007-05-11 23:14:43 +00001461 ConstStr, ".str", M);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001462 StrGV->setSection("llvm.metadata");
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001463
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001464 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001465 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001466 }
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001467
Jim Laskeyce72b172006-02-11 01:01:30 +00001468 return Slot;
1469
Jim Laskey86cbdba2006-02-06 15:33:21 +00001470}
1471
1472/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1473/// so that it can be serialized to a .bc or .ll file.
1474GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1475 // Check if the DebugInfoDesc is already in the map.
1476 GlobalVariable *&Slot = DescGlobals[DD];
1477
1478 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1479 if (Slot) return Slot;
1480
Jim Laskey86cbdba2006-02-06 15:33:21 +00001481 // Get the type associated with the Tag.
1482 const StructType *Ty = getTagType(DD);
1483
1484 // Create the GlobalVariable early to prevent infinite recursion.
Bill Wendling10fff602008-07-03 22:53:42 +00001485 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1486 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001487 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001488
1489 // Insert new GlobalVariable in DescGlobals map.
1490 Slot = GV;
1491
1492 // Set up elements vector
1493 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001494 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001495 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001496 SRAM.ApplyToFields(DD);
Bill Wendling10fff602008-07-03 22:53:42 +00001497
Jim Laskey86cbdba2006-02-06 15:33:21 +00001498 // Set the globals initializer.
1499 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1500
1501 return GV;
1502}
1503
Devang Patel962e0752007-11-30 00:51:33 +00001504/// addDescriptor - Directly connect DD with existing GV.
1505void DISerializer::addDescriptor(DebugInfoDesc *DD,
1506 GlobalVariable *GV) {
1507 DescGlobals[DD] = GV;
1508}
1509
Jim Laskey86cbdba2006-02-06 15:33:21 +00001510//===----------------------------------------------------------------------===//
1511
Jim Laskey86cbdba2006-02-06 15:33:21 +00001512/// Verify - Return true if the GlobalVariable appears to be a valid
1513/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001514bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001515 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001516}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001517bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001518 // NULLs are valid.
1519 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001520
Jim Laskey98e04102006-03-26 22:45:20 +00001521 // Check prior validity.
1522 unsigned &ValiditySlot = Validity[GV];
1523
1524 // If visited before then use old state.
1525 if (ValiditySlot) return ValiditySlot == Valid;
1526
1527 // Assume validity for the time being (recursion.)
1528 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001529
1530 // Make sure the global is internal or link once (anchor.)
1531 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1532 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1533 ValiditySlot = Invalid;
1534 return false;
1535 }
Jim Laskey98e04102006-03-26 22:45:20 +00001536
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001537 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001538 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001539
1540 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001541 if (Tag == DW_TAG_invalid) {
1542 ValiditySlot = Valid;
1543 return true;
1544 }
1545
1546 // Get the Version.
1547 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1548
1549 // Check for version mismatch.
1550 if (Version != LLVMDebugVersion) {
1551 ValiditySlot = Invalid;
1552 return false;
1553 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001554
Jim Laskey86cbdba2006-02-06 15:33:21 +00001555 // Construct an empty DebugInfoDesc.
1556 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001557
1558 // Allow for user defined descriptors.
1559 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001560
1561 // Get the initializer constant.
1562 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1563
1564 // Get the operand count.
1565 unsigned N = CI->getNumOperands();
1566
1567 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001568 unsigned &CountSlot = Counts[Tag];
Bill Wendling3e30cbb2008-07-09 06:02:33 +00001569
1570 if (!CountSlot)
Jim Laskey86cbdba2006-02-06 15:33:21 +00001571 // Check the operand count to the field count
Bill Wendling3e30cbb2008-07-09 06:02:33 +00001572 CountSlot = CountFields(DD);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001573
Jim Laskey21407982006-03-14 18:37:57 +00001574 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001575 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001576 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001577 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001578 return false;
1579 }
1580
1581 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001582 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001583 VRAM.ApplyToFields(DD);
1584
1585 // Release empty DebugInfoDesc.
1586 delete DD;
1587
Jim Laskey98e04102006-03-26 22:45:20 +00001588 // If fields are not valid.
1589 if (!VRAM.isValid()) {
1590 ValiditySlot = Invalid;
1591 return false;
1592 }
1593
1594 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001595}
1596
Evan Chenga844bde2008-02-02 04:07:54 +00001597/// isVerified - Return true if the specified GV has already been
1598/// verified as a debug information descriptor.
1599bool DIVerifier::isVerified(GlobalVariable *GV) {
1600 unsigned &ValiditySlot = Validity[GV];
1601 if (ValiditySlot) return ValiditySlot == Valid;
1602 return false;
1603}
1604
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001605//===----------------------------------------------------------------------===//
1606
Jim Laskeyb8509c52006-03-23 18:07:55 +00001607DebugScope::~DebugScope() {
Bill Wendling10fff602008-07-03 22:53:42 +00001608 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1609 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
Jim Laskeyb8509c52006-03-23 18:07:55 +00001610}
1611
1612//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001613
Jim Laskey6da18642007-01-26 21:38:26 +00001614MachineModuleInfo::MachineModuleInfo()
Dan Gohmanae73dc12008-09-04 17:05:41 +00001615: ImmutablePass(&ID)
Devang Patel794fd752007-05-01 21:15:47 +00001616, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001617, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001618, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001619, Directories()
1620, SourceFiles()
1621, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001622, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001623, ScopeMap()
1624, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001625, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001626, LandingPads()
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001627, Personalities()
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001628, CallsEHReturn(0)
1629, CallsUnwindInit(0)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001630{
1631 // Always emit "no personality" info
1632 Personalities.push_back(NULL);
1633}
Jim Laskey6da18642007-01-26 21:38:26 +00001634MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001635
1636}
1637
Jim Laskey6da18642007-01-26 21:38:26 +00001638/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001639///
Jim Laskey6da18642007-01-26 21:38:26 +00001640bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001641 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001642}
1643
Jim Laskey6da18642007-01-26 21:38:26 +00001644/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001645///
Jim Laskey6da18642007-01-26 21:38:26 +00001646bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001647 return false;
1648}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001649
Jim Laskey6da18642007-01-26 21:38:26 +00001650/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001651///
Jim Laskey6da18642007-01-26 21:38:26 +00001652void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001653 // Coming soon.
1654}
1655
Jim Laskey6da18642007-01-26 21:38:26 +00001656/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001657///
Jim Laskey6da18642007-01-26 21:38:26 +00001658void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001659 // Clean up scope information.
1660 if (RootScope) {
1661 delete RootScope;
1662 ScopeMap.clear();
1663 RootScope = NULL;
1664 }
1665
Jim Laskeyb82313f2007-02-01 16:31:34 +00001666 // Clean up line info.
1667 Lines.clear();
1668
Jim Laskey41886992006-04-07 16:34:46 +00001669 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001670 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001671
1672 // Clean up exception info.
1673 LandingPads.clear();
1674 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +00001675 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +00001676 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001677 CallsEHReturn = 0;
1678 CallsUnwindInit = 0;
Jim Laskey41886992006-04-07 16:34:46 +00001679}
1680
Jim Laskeyd96185a2006-02-13 12:50:39 +00001681/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001682///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001683// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001684DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001685 return DR.Deserialize(V);
1686}
1687
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001688/// AnalyzeModule - Scan the module for global debug information.
1689///
Jim Laskey6da18642007-01-26 21:38:26 +00001690void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001691 SetupCompileUnits(M);
Dale Johannesen48ae02f2008-01-16 19:59:28 +00001692
1693 // Insert functions in the llvm.used array into UsedFunctions.
1694 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1695 if (!GV || !GV->hasInitializer()) return;
1696
1697 // Should be an array of 'i8*'.
1698 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1699 if (InitList == 0) return;
1700
1701 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1702 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1703 if (CE->getOpcode() == Instruction::BitCast)
1704 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1705 UsedFunctions.insert(F);
1706 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001707}
1708
1709/// SetupCompileUnits - Set up the unique vector of compile units.
1710///
Jim Laskey6da18642007-01-26 21:38:26 +00001711void MachineModuleInfo::SetupCompileUnits(Module &M) {
Bill Wendlingc04f4652008-07-03 23:13:02 +00001712 std::vector<CompileUnitDesc *> CU;
1713 getAnchoredDescriptors<CompileUnitDesc>(M, CU);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001714
Bill Wendling10fff602008-07-03 22:53:42 +00001715 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1716 CompileUnits.insert(CU[i]);
1717 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001718}
1719
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001720/// getCompileUnits - Return a vector of debug compile units.
1721///
Jim Laskey6da18642007-01-26 21:38:26 +00001722const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001723 return CompileUnits;
1724}
1725
Jim Laskey0420f2a2006-02-22 19:02:11 +00001726/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1727/// named GlobalVariable.
Bill Wendlingc04f4652008-07-03 23:13:02 +00001728void
Jim Laskey6da18642007-01-26 21:38:26 +00001729MachineModuleInfo::getGlobalVariablesUsing(Module &M,
Bill Wendlingc04f4652008-07-03 23:13:02 +00001730 const std::string &RootName,
1731 std::vector<GlobalVariable*>&Result){
1732 return ::getGlobalVariablesUsing(M, RootName, Result);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001733}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001734
Evan Chenga647c922008-02-01 02:05:57 +00001735/// RecordSourceLine - Records location information and associates it with a
Jim Laskeyb8509c52006-03-23 18:07:55 +00001736/// debug label. Returns a unique label ID used to generate a label and
1737/// provide correspondence to the source line list.
Evan Chenga647c922008-02-01 02:05:57 +00001738unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1739 unsigned Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001740 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001741 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001742 return ID;
1743}
1744
1745/// RecordSource - Register a source file with debug info. Returns an source
1746/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001747unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1748 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001749 unsigned DirectoryID = Directories.insert(Directory);
1750 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1751}
Jim Laskey6da18642007-01-26 21:38:26 +00001752unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001753 return RecordSource(CompileUnit->getDirectory(),
1754 CompileUnit->getFileName());
1755}
1756
1757/// RecordRegionStart - Indicate the start of a region.
1758///
Jim Laskey6da18642007-01-26 21:38:26 +00001759unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001760 // FIXME - need to be able to handle split scopes because of bb cloning.
1761 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1762 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1763 unsigned ID = NextLabelID();
1764 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1765 return ID;
1766}
1767
1768/// RecordRegionEnd - Indicate the end of a region.
1769///
Jim Laskey6da18642007-01-26 21:38:26 +00001770unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001771 // FIXME - need to be able to handle split scopes because of bb cloning.
1772 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1773 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1774 unsigned ID = NextLabelID();
1775 Scope->setEndLabelID(ID);
1776 return ID;
1777}
1778
1779/// RecordVariable - Indicate the declaration of a local variable.
1780///
Evan Chenga844bde2008-02-02 04:07:54 +00001781void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1782 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001783 DebugScope *Scope = getOrCreateScope(VD->getContext());
1784 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1785 Scope->AddVariable(DV);
1786}
1787
1788/// getOrCreateScope - Returns the scope associated with the given descriptor.
1789///
Jim Laskey6da18642007-01-26 21:38:26 +00001790DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001791 DebugScope *&Slot = ScopeMap[ScopeDesc];
1792 if (!Slot) {
1793 // FIXME - breaks down when the context is an inlined function.
1794 DebugInfoDesc *ParentDesc = NULL;
1795 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1796 ParentDesc = Block->getContext();
1797 }
1798 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1799 Slot = new DebugScope(Parent, ScopeDesc);
1800 if (Parent) {
1801 Parent->AddScope(Slot);
1802 } else if (RootScope) {
1803 // FIXME - Add inlined function scopes to the root so we can delete
1804 // them later. Long term, handle inlined functions properly.
1805 RootScope->AddScope(Slot);
1806 } else {
1807 // First function is top level function.
1808 RootScope = Slot;
1809 }
1810 }
1811 return Slot;
1812}
1813
Jim Laskey59667fe2007-02-21 22:38:31 +00001814//===-EH-------------------------------------------------------------------===//
1815
1816/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1817/// specified MachineBasicBlock.
Bill Wendling10fff602008-07-03 22:53:42 +00001818LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1819 (MachineBasicBlock *LandingPad) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001820 unsigned N = LandingPads.size();
1821 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001822 LandingPadInfo &LP = LandingPads[i];
1823 if (LP.LandingPadBlock == LandingPad)
1824 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001825 }
1826
1827 LandingPads.push_back(LandingPadInfo(LandingPad));
1828 return LandingPads[N];
1829}
1830
1831/// addInvoke - Provide the begin and end labels of an invoke style call and
1832/// associate it with a try landing pad block.
1833void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1834 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001835 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001836 LP.BeginLabels.push_back(BeginLabel);
1837 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +00001838}
1839
1840/// addLandingPad - Provide the label of a try LandingPad block.
1841///
1842unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1843 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001844 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1845 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001846 return LandingPadLabel;
1847}
1848
1849/// addPersonality - Provide the personality function for the exception
1850/// information.
1851void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001852 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001853 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001854 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001855
Bill Wendling10fff602008-07-03 22:53:42 +00001856 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001857 if (Personalities[i] == Personality)
1858 return;
1859
1860 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +00001861}
1862
1863/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1864///
1865void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1866 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001867 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001868 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001869 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001870}
Duncan Sands73ef58a2007-06-02 16:53:42 +00001871
1872/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +00001873///
Duncan Sands73ef58a2007-06-02 16:53:42 +00001874void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1875 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001876 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling49255672008-07-07 21:41:57 +00001877 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling10fff602008-07-03 22:53:42 +00001878 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001879 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1880 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +00001881}
1882
Duncan Sands6590b042007-08-27 15:47:50 +00001883/// addCleanup - Add a cleanup action for a landing pad.
1884///
1885void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1886 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1887 LP.TypeIds.push_back(0);
1888}
1889
Jim Laskey59667fe2007-02-21 22:38:31 +00001890/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1891/// pads.
1892void MachineModuleInfo::TidyLandingPads() {
1893 for (unsigned i = 0; i != LandingPads.size(); ) {
1894 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001895 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001896
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001897 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +00001898 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001899 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001900 LandingPads.erase(LandingPads.begin() + i);
1901 continue;
1902 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001903
Bill Wendling10fff602008-07-03 22:53:42 +00001904 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001905 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1906 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands57810cd2007-09-05 11:27:52 +00001907
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001908 if (!BeginLabel || !EndLabel) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001909 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1910 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1911 continue;
1912 }
1913
1914 LandingPad.BeginLabels[j] = BeginLabel;
1915 LandingPad.EndLabels[j] = EndLabel;
1916 ++j;
1917 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001918
1919 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +00001920 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +00001921 LandingPads.erase(LandingPads.begin() + i);
1922 continue;
1923 }
1924
1925 // If there is no landing pad, ensure that the list of typeids is empty.
1926 // If the only typeid is a cleanup, this is the same as having no typeids.
1927 if (!LandingPad.LandingPadBlock ||
1928 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1929 LandingPad.TypeIds.clear();
1930
Jim Laskey59667fe2007-02-21 22:38:31 +00001931 ++i;
1932 }
1933}
1934
1935/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1936/// function wide.
1937unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling10fff602008-07-03 22:53:42 +00001938 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1939 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey59667fe2007-02-21 22:38:31 +00001940
1941 TypeInfos.push_back(TI);
1942 return TypeInfos.size();
1943}
1944
Duncan Sands73ef58a2007-06-02 16:53:42 +00001945/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1946/// function wide.
Bill Wendling914c9702008-06-27 01:27:56 +00001947int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +00001948 // If the new filter coincides with the tail of an existing filter, then
1949 // re-use the existing filter. Folding filters more than this requires
1950 // re-ordering filters and/or their elements - probably not worth it.
1951 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1952 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling914c9702008-06-27 01:27:56 +00001953 unsigned i = *I, j = TyIds.size();
Duncan Sands14da32a2007-07-05 15:15:01 +00001954
1955 while (i && j)
1956 if (FilterIds[--i] != TyIds[--j])
1957 goto try_next;
1958
1959 if (!j)
1960 // The new filter coincides with range [i, end) of the existing filter.
1961 return -(1 + i);
Bill Wendling914c9702008-06-27 01:27:56 +00001962
Duncan Sands14da32a2007-07-05 15:15:01 +00001963try_next:;
1964 }
1965
1966 // Add the new filter.
Bill Wendling914c9702008-06-27 01:27:56 +00001967 int FilterID = -(1 + FilterIds.size());
1968 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1969 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001970 FilterIds.push_back(TyIds[I]);
Bill Wendling914c9702008-06-27 01:27:56 +00001971 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +00001972 FilterIds.push_back(0); // terminator
1973 return FilterID;
1974}
1975
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001976/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +00001977Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001978 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001979 // function
1980 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +00001981}
1982
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001983/// getPersonalityIndex - Return unique index for current personality
1984/// function. NULL personality function should always get zero index.
1985unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001986 const Function* Personality = NULL;
1987
1988 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling10fff602008-07-03 22:53:42 +00001989 for (unsigned i = 0; i != LandingPads.size(); ++i)
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001990 if (LandingPads[i].Personality) {
1991 Personality = LandingPads[i].Personality;
1992 break;
1993 }
1994
Bill Wendling10fff602008-07-03 22:53:42 +00001995 for (unsigned i = 0; i < Personalities.size(); ++i) {
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001996 if (Personalities[i] == Personality)
1997 return i;
Bill Wendling10fff602008-07-03 22:53:42 +00001998 }
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001999
2000 // This should never happen
2001 assert(0 && "Personality function should be set!");
2002 return 0;
2003}
Jim Laskey59667fe2007-02-21 22:38:31 +00002004
Jim Laskey9d4209f2006-11-07 19:33:46 +00002005//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00002006/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
2007/// a info consumer to determine if the range of two labels is empty, by seeing
2008/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002009
2010namespace llvm {
2011
2012struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00002013 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +00002014 DebugLabelFolder() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +00002015
Jim Laskey9d4209f2006-11-07 19:33:46 +00002016 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00002017 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00002018};
2019
Devang Patel19974732007-05-03 01:11:54 +00002020char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00002021
Jim Laskey9d4209f2006-11-07 19:33:46 +00002022bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00002023 // Get machine module info.
2024 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
2025 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002026
2027 // Track if change is made.
2028 bool MadeChange = false;
2029 // No prior label to begin.
2030 unsigned PriorLabel = 0;
2031
2032 // Iterate through basic blocks.
2033 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
2034 BB != E; ++BB) {
2035 // Iterate through instructions.
2036 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00002037 // Is it a label.
Evan Chengbb81d972008-01-31 09:59:15 +00002038 if (I->isDebugLabel()) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002039 // The label ID # is always operand #0, an immediate.
2040 unsigned NextLabel = I->getOperand(0).getImm();
2041
2042 // If there was an immediate prior label.
2043 if (PriorLabel) {
2044 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00002045 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002046 // Delete the current label.
2047 I = BB->erase(I);
2048 // Indicate a change has been made.
2049 MadeChange = true;
2050 continue;
2051 } else {
2052 // Start a new round.
2053 PriorLabel = NextLabel;
2054 }
2055 } else {
2056 // No consecutive labels.
2057 PriorLabel = 0;
2058 }
2059
2060 ++I;
2061 }
2062 }
2063
2064 return MadeChange;
2065}
2066
2067FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
2068
2069}
Bill Wendling10fff602008-07-03 22:53:42 +00002070