blob: 5fe59d4e5a7eb8437583e55feeb9db14635457e4 [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"
Evan Cheng8b56a902008-09-22 22:21:38 +000016#include "llvm/CodeGen/Passes.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);
Chris Lattner4d557e42008-11-10 03:48:55 +0000366 if (GV && GV->hasInitializer()) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000367 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() << "), "
Evan Cheng94ea5be2008-12-10 00:15:44 +0000909 << "Encoding(" << Encoding << "),"
910 << "Flags(" << Flags << ")\n";
Bill Wendling10fff602008-07-03 22:53:42 +0000911}
912#endif
913
914//===----------------------------------------------------------------------===//
915
916DerivedTypeDesc::DerivedTypeDesc(unsigned T)
917: TypeDesc(T)
918, FromType(NULL)
919{}
920
921// Implement isa/cast/dyncast.
922bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
923 unsigned T = D->getTag();
924 switch (T) {
925 case DW_TAG_typedef:
926 case DW_TAG_pointer_type:
927 case DW_TAG_reference_type:
928 case DW_TAG_const_type:
929 case DW_TAG_volatile_type:
930 case DW_TAG_restrict_type:
931 case DW_TAG_member:
932 case DW_TAG_inheritance:
933 return true;
934 default: break;
935 }
936 return false;
937}
938
939/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
940///
941void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
942 TypeDesc::ApplyToFields(Visitor);
943
944 Visitor->Apply(FromType);
945}
946
947/// getDescString - Return a string used to compose global names and labels.
948///
949const char *DerivedTypeDesc::getDescString() const {
950 return "llvm.dbg.derivedtype";
951}
952
953/// getTypeString - Return a string used to label this descriptor's type.
954///
955const char *DerivedTypeDesc::getTypeString() const {
956 return "llvm.dbg.derivedtype.type";
957}
958
959#ifndef NDEBUG
960void DerivedTypeDesc::dump() {
961 cerr << getDescString() << " "
962 << "Version(" << getVersion() << "), "
963 << "Tag(" << getTag() << "), "
964 << "Context(" << getContext() << "), "
965 << "Name(\"" << getName() << "\"), "
966 << "Size(" << getSize() << "), "
967 << "File(" << getFile() << "), "
968 << "Line(" << getLine() << "), "
Evan Cheng94ea5be2008-12-10 00:15:44 +0000969 << "FromType(" << FromType << "),"
970 << "Flags(" << Flags << ")\n";
Bill Wendling10fff602008-07-03 22:53:42 +0000971}
972#endif
973
974//===----------------------------------------------------------------------===//
975
976CompositeTypeDesc::CompositeTypeDesc(unsigned T)
977: DerivedTypeDesc(T)
978, Elements()
979{}
980
981// Implement isa/cast/dyncast.
982bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
983 unsigned T = D->getTag();
984 switch (T) {
985 case DW_TAG_array_type:
986 case DW_TAG_structure_type:
987 case DW_TAG_union_type:
988 case DW_TAG_enumeration_type:
989 case DW_TAG_vector_type:
990 case DW_TAG_subroutine_type:
991 return true;
992 default: break;
993 }
994 return false;
995}
996
997/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
998///
999void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
1000 DerivedTypeDesc::ApplyToFields(Visitor);
1001
1002 Visitor->Apply(Elements);
1003}
1004
1005/// getDescString - Return a string used to compose global names and labels.
1006///
1007const char *CompositeTypeDesc::getDescString() const {
1008 return "llvm.dbg.compositetype";
1009}
1010
1011/// getTypeString - Return a string used to label this descriptor's type.
1012///
1013const char *CompositeTypeDesc::getTypeString() const {
1014 return "llvm.dbg.compositetype.type";
1015}
1016
1017#ifndef NDEBUG
1018void CompositeTypeDesc::dump() {
1019 cerr << getDescString() << " "
1020 << "Version(" << getVersion() << "), "
1021 << "Tag(" << getTag() << "), "
1022 << "Context(" << getContext() << "), "
1023 << "Name(\"" << getName() << "\"), "
1024 << "Size(" << getSize() << "), "
1025 << "File(" << getFile() << "), "
1026 << "Line(" << getLine() << "), "
1027 << "FromType(" << getFromType() << "), "
Evan Cheng94ea5be2008-12-10 00:15:44 +00001028 << "Elements.size(" << Elements.size() << "),"
1029 << "Flags(" << Flags << ")\n";
Bill Wendling10fff602008-07-03 22:53:42 +00001030}
1031#endif
1032
1033//===----------------------------------------------------------------------===//
1034
1035SubrangeDesc::SubrangeDesc()
1036: DebugInfoDesc(DW_TAG_subrange_type)
1037, Lo(0)
1038, Hi(0)
1039{}
1040
1041// Implement isa/cast/dyncast.
1042bool SubrangeDesc::classof(const DebugInfoDesc *D) {
1043 return D->getTag() == DW_TAG_subrange_type;
1044}
1045
1046/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
1047///
1048void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
1049 DebugInfoDesc::ApplyToFields(Visitor);
1050
1051 Visitor->Apply(Lo);
1052 Visitor->Apply(Hi);
1053}
1054
1055/// getDescString - Return a string used to compose global names and labels.
1056///
1057const char *SubrangeDesc::getDescString() const {
1058 return "llvm.dbg.subrange";
1059}
1060
1061/// getTypeString - Return a string used to label this descriptor's type.
1062///
1063const char *SubrangeDesc::getTypeString() const {
1064 return "llvm.dbg.subrange.type";
1065}
1066
1067#ifndef NDEBUG
1068void SubrangeDesc::dump() {
1069 cerr << getDescString() << " "
1070 << "Version(" << getVersion() << "), "
1071 << "Tag(" << getTag() << "), "
1072 << "Lo(" << Lo << "), "
1073 << "Hi(" << Hi << ")\n";
1074}
1075#endif
1076
1077//===----------------------------------------------------------------------===//
1078
1079EnumeratorDesc::EnumeratorDesc()
1080: DebugInfoDesc(DW_TAG_enumerator)
1081, Name("")
1082, Value(0)
1083{}
1084
1085// Implement isa/cast/dyncast.
1086bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
1087 return D->getTag() == DW_TAG_enumerator;
1088}
1089
1090/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
1091///
1092void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
1093 DebugInfoDesc::ApplyToFields(Visitor);
1094
1095 Visitor->Apply(Name);
1096 Visitor->Apply(Value);
1097}
1098
1099/// getDescString - Return a string used to compose global names and labels.
1100///
1101const char *EnumeratorDesc::getDescString() const {
1102 return "llvm.dbg.enumerator";
1103}
1104
1105/// getTypeString - Return a string used to label this descriptor's type.
1106///
1107const char *EnumeratorDesc::getTypeString() const {
1108 return "llvm.dbg.enumerator.type";
1109}
1110
1111#ifndef NDEBUG
1112void EnumeratorDesc::dump() {
1113 cerr << getDescString() << " "
1114 << "Version(" << getVersion() << "), "
1115 << "Tag(" << getTag() << "), "
1116 << "Name(" << Name << "), "
1117 << "Value(" << Value << ")\n";
1118}
1119#endif
1120
1121//===----------------------------------------------------------------------===//
1122
1123VariableDesc::VariableDesc(unsigned T)
1124: DebugInfoDesc(T)
1125, Context(NULL)
1126, Name("")
1127, File(NULL)
1128, Line(0)
1129, TyDesc(0)
1130{}
1131
1132// Implement isa/cast/dyncast.
1133bool VariableDesc::classof(const DebugInfoDesc *D) {
1134 unsigned T = D->getTag();
1135 switch (T) {
1136 case DW_TAG_auto_variable:
1137 case DW_TAG_arg_variable:
1138 case DW_TAG_return_variable:
1139 return true;
1140 default: break;
1141 }
1142 return false;
1143}
1144
1145/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1146///
1147void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1148 DebugInfoDesc::ApplyToFields(Visitor);
1149
1150 Visitor->Apply(Context);
1151 Visitor->Apply(Name);
1152 Visitor->Apply(File);
1153 Visitor->Apply(Line);
1154 Visitor->Apply(TyDesc);
1155}
1156
1157/// getDescString - Return a string used to compose global names and labels.
1158///
1159const char *VariableDesc::getDescString() const {
1160 return "llvm.dbg.variable";
1161}
1162
1163/// getTypeString - Return a string used to label this descriptor's type.
1164///
1165const char *VariableDesc::getTypeString() const {
1166 return "llvm.dbg.variable.type";
1167}
1168
1169#ifndef NDEBUG
1170void VariableDesc::dump() {
1171 cerr << getDescString() << " "
1172 << "Version(" << getVersion() << "), "
1173 << "Tag(" << getTag() << "), "
1174 << "Context(" << Context << "), "
1175 << "Name(\"" << Name << "\"), "
1176 << "File(" << File << "), "
1177 << "Line(" << Line << "), "
1178 << "TyDesc(" << TyDesc << ")\n";
1179}
1180#endif
1181
1182//===----------------------------------------------------------------------===//
1183
1184GlobalDesc::GlobalDesc(unsigned T)
1185: AnchoredDesc(T)
1186, Context(0)
1187, Name("")
1188, FullName("")
1189, LinkageName("")
1190, File(NULL)
1191, Line(0)
1192, TyDesc(NULL)
1193, IsStatic(false)
1194, IsDefinition(false)
1195{}
1196
1197/// ApplyToFields - Target the visitor to the fields of the global.
1198///
1199void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1200 AnchoredDesc::ApplyToFields(Visitor);
1201
1202 Visitor->Apply(Context);
1203 Visitor->Apply(Name);
1204 Visitor->Apply(FullName);
1205 Visitor->Apply(LinkageName);
1206 Visitor->Apply(File);
1207 Visitor->Apply(Line);
1208 Visitor->Apply(TyDesc);
1209 Visitor->Apply(IsStatic);
1210 Visitor->Apply(IsDefinition);
1211}
1212
1213//===----------------------------------------------------------------------===//
1214
1215GlobalVariableDesc::GlobalVariableDesc()
1216: GlobalDesc(DW_TAG_variable)
1217, Global(NULL)
1218{}
1219
1220// Implement isa/cast/dyncast.
1221bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1222 return D->getTag() == DW_TAG_variable;
1223}
1224
1225/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1226///
1227void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1228 GlobalDesc::ApplyToFields(Visitor);
1229
1230 Visitor->Apply(Global);
1231}
1232
1233/// getDescString - Return a string used to compose global names and labels.
1234///
1235const char *GlobalVariableDesc::getDescString() const {
1236 return "llvm.dbg.global_variable";
1237}
1238
1239/// getTypeString - Return a string used to label this descriptors type.
1240///
1241const char *GlobalVariableDesc::getTypeString() const {
1242 return "llvm.dbg.global_variable.type";
1243}
1244
1245/// getAnchorString - Return a string used to label this descriptor's anchor.
1246///
1247const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1248const char *GlobalVariableDesc::getAnchorString() const {
1249 return AnchorString;
1250}
1251
1252#ifndef NDEBUG
1253void GlobalVariableDesc::dump() {
1254 cerr << getDescString() << " "
1255 << "Version(" << getVersion() << "), "
1256 << "Tag(" << getTag() << "), "
1257 << "Anchor(" << getAnchor() << "), "
1258 << "Name(\"" << getName() << "\"), "
1259 << "FullName(\"" << getFullName() << "\"), "
1260 << "LinkageName(\"" << getLinkageName() << "\"), "
1261 << "File(" << getFile() << "),"
1262 << "Line(" << getLine() << "),"
1263 << "Type(" << getType() << "), "
1264 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1265 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1266 << "Global(" << Global << ")\n";
1267}
1268#endif
1269
1270//===----------------------------------------------------------------------===//
1271
1272SubprogramDesc::SubprogramDesc()
1273: GlobalDesc(DW_TAG_subprogram)
1274{}
1275
1276// Implement isa/cast/dyncast.
1277bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1278 return D->getTag() == DW_TAG_subprogram;
1279}
1280
1281/// ApplyToFields - Target the visitor to the fields of the
1282/// SubprogramDesc.
1283void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1284 GlobalDesc::ApplyToFields(Visitor);
1285}
1286
1287/// getDescString - Return a string used to compose global names and labels.
1288///
1289const char *SubprogramDesc::getDescString() const {
1290 return "llvm.dbg.subprogram";
1291}
1292
1293/// getTypeString - Return a string used to label this descriptors type.
1294///
1295const char *SubprogramDesc::getTypeString() const {
1296 return "llvm.dbg.subprogram.type";
1297}
1298
1299/// getAnchorString - Return a string used to label this descriptor's anchor.
1300///
1301const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1302const char *SubprogramDesc::getAnchorString() const {
1303 return AnchorString;
1304}
1305
1306#ifndef NDEBUG
1307void SubprogramDesc::dump() {
1308 cerr << getDescString() << " "
1309 << "Version(" << getVersion() << "), "
1310 << "Tag(" << getTag() << "), "
1311 << "Anchor(" << getAnchor() << "), "
1312 << "Name(\"" << getName() << "\"), "
1313 << "FullName(\"" << getFullName() << "\"), "
1314 << "LinkageName(\"" << getLinkageName() << "\"), "
1315 << "File(" << getFile() << "),"
1316 << "Line(" << getLine() << "),"
1317 << "Type(" << getType() << "), "
1318 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1319 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1320}
1321#endif
1322
1323//===----------------------------------------------------------------------===//
1324
1325BlockDesc::BlockDesc()
1326: DebugInfoDesc(DW_TAG_lexical_block)
1327, Context(NULL)
1328{}
1329
1330// Implement isa/cast/dyncast.
1331bool BlockDesc::classof(const DebugInfoDesc *D) {
1332 return D->getTag() == DW_TAG_lexical_block;
1333}
1334
1335/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1336///
1337void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1338 DebugInfoDesc::ApplyToFields(Visitor);
1339
1340 Visitor->Apply(Context);
1341}
1342
1343/// getDescString - Return a string used to compose global names and labels.
1344///
1345const char *BlockDesc::getDescString() const {
1346 return "llvm.dbg.block";
1347}
1348
1349/// getTypeString - Return a string used to label this descriptors type.
1350///
1351const char *BlockDesc::getTypeString() const {
1352 return "llvm.dbg.block.type";
1353}
1354
1355#ifndef NDEBUG
1356void BlockDesc::dump() {
1357 cerr << getDescString() << " "
1358 << "Version(" << getVersion() << "), "
1359 << "Tag(" << getTag() << "),"
1360 << "Context(" << Context << ")\n";
1361}
1362#endif
1363
1364//===----------------------------------------------------------------------===//
1365
Jim Laskey86cbdba2006-02-06 15:33:21 +00001366DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001367 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001368}
1369DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001370 // Handle NULL.
1371 if (!GV) return NULL;
1372
Jim Laskey86cbdba2006-02-06 15:33:21 +00001373 // Check to see if it has been already deserialized.
1374 DebugInfoDesc *&Slot = GlobalDescs[GV];
1375 if (Slot) return Slot;
1376
1377 // Get the Tag from the global.
1378 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1379
Jim Laskey86cbdba2006-02-06 15:33:21 +00001380 // Create an empty instance of the correct sort.
1381 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001382
Jim Laskey21407982006-03-14 18:37:57 +00001383 // If not a user defined descriptor.
1384 if (Slot) {
1385 // Deserialize the fields.
1386 DIDeserializeVisitor DRAM(*this, GV);
1387 DRAM.ApplyToFields(Slot);
1388 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001389
1390 return Slot;
1391}
1392
1393//===----------------------------------------------------------------------===//
1394
1395/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001396///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001397const PointerType *DISerializer::getStrPtrType() {
1398 // If not already defined.
1399 if (!StrPtrTy) {
1400 // Construct the pointer to signed bytes.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001401 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001402 }
1403
1404 return StrPtrTy;
1405}
1406
1407/// getEmptyStructPtrType - Return a "{ }*" type.
1408///
1409const PointerType *DISerializer::getEmptyStructPtrType() {
1410 // If not already defined.
Bill Wendling914c9702008-06-27 01:27:56 +00001411 if (EmptyStructPtrTy) return EmptyStructPtrTy;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001412
Bill Wendling914c9702008-06-27 01:27:56 +00001413 // Construct the pointer to empty structure type.
Bill Wendling49255672008-07-07 21:41:57 +00001414 const StructType *EmptyStructTy = StructType::get(NULL, NULL);
Bill Wendling0ac1b6d2008-06-27 01:32:08 +00001415
1416 // Construct the pointer to empty structure type.
1417 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001418 return EmptyStructPtrTy;
1419}
1420
1421/// getTagType - Return the type describing the specified descriptor (via tag.)
1422///
1423const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1424 // Attempt to get the previously defined type.
1425 StructType *&Ty = TagTypes[DD->getTag()];
1426
1427 // If not already defined.
1428 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001429 // Set up fields vector.
1430 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001431 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001432 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001433 GTAM.ApplyToFields(DD);
1434
1435 // Construct structured type.
1436 Ty = StructType::get(Fields);
1437
Jim Laskey86cbdba2006-02-06 15:33:21 +00001438 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001439 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001440 }
1441
1442 return Ty;
1443}
1444
1445/// getString - Construct the string as constant string global.
1446///
Jim Laskeyce72b172006-02-11 01:01:30 +00001447Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001448 // Check string cache for previous edition.
Bill Wendling667a68b2008-07-07 20:59:31 +00001449 Constant *&Slot = StringCache[String];
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001450
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001451 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001452 if (Slot) return Slot;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001453
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001454 // If empty string then use a sbyte* null instead.
1455 if (String.empty()) {
1456 Slot = ConstantPointerNull::get(getStrPtrType());
1457 } else {
1458 // Construct string as an llvm constant.
1459 Constant *ConstStr = ConstantArray::get(String);
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001460
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001461 // Otherwise create and return a new string global.
1462 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1463 GlobalVariable::InternalLinkage,
Devang Patel1e4c23a2007-05-11 23:14:43 +00001464 ConstStr, ".str", M);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001465 StrGV->setSection("llvm.metadata");
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001466
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001467 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001468 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001469 }
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001470
Jim Laskeyce72b172006-02-11 01:01:30 +00001471 return Slot;
1472
Jim Laskey86cbdba2006-02-06 15:33:21 +00001473}
1474
1475/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1476/// so that it can be serialized to a .bc or .ll file.
1477GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1478 // Check if the DebugInfoDesc is already in the map.
1479 GlobalVariable *&Slot = DescGlobals[DD];
1480
1481 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1482 if (Slot) return Slot;
1483
Jim Laskey86cbdba2006-02-06 15:33:21 +00001484 // Get the type associated with the Tag.
1485 const StructType *Ty = getTagType(DD);
1486
1487 // Create the GlobalVariable early to prevent infinite recursion.
Bill Wendling10fff602008-07-03 22:53:42 +00001488 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1489 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001490 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001491
1492 // Insert new GlobalVariable in DescGlobals map.
1493 Slot = GV;
1494
1495 // Set up elements vector
1496 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001497 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001498 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001499 SRAM.ApplyToFields(DD);
Bill Wendling10fff602008-07-03 22:53:42 +00001500
Jim Laskey86cbdba2006-02-06 15:33:21 +00001501 // Set the globals initializer.
1502 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1503
1504 return GV;
1505}
1506
Devang Patel962e0752007-11-30 00:51:33 +00001507/// addDescriptor - Directly connect DD with existing GV.
1508void DISerializer::addDescriptor(DebugInfoDesc *DD,
1509 GlobalVariable *GV) {
1510 DescGlobals[DD] = GV;
1511}
1512
Jim Laskey86cbdba2006-02-06 15:33:21 +00001513//===----------------------------------------------------------------------===//
1514
Jim Laskey86cbdba2006-02-06 15:33:21 +00001515/// Verify - Return true if the GlobalVariable appears to be a valid
1516/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001517bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001518 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001519}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001520bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001521 // NULLs are valid.
1522 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001523
Jim Laskey98e04102006-03-26 22:45:20 +00001524 // Check prior validity.
1525 unsigned &ValiditySlot = Validity[GV];
1526
1527 // If visited before then use old state.
1528 if (ValiditySlot) return ValiditySlot == Valid;
1529
1530 // Assume validity for the time being (recursion.)
1531 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001532
1533 // Make sure the global is internal or link once (anchor.)
1534 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1535 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1536 ValiditySlot = Invalid;
1537 return false;
1538 }
Jim Laskey98e04102006-03-26 22:45:20 +00001539
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001540 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001541 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001542
1543 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001544 if (Tag == DW_TAG_invalid) {
1545 ValiditySlot = Valid;
1546 return true;
1547 }
1548
1549 // Get the Version.
1550 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1551
1552 // Check for version mismatch.
1553 if (Version != LLVMDebugVersion) {
1554 ValiditySlot = Invalid;
1555 return false;
1556 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001557
Jim Laskey86cbdba2006-02-06 15:33:21 +00001558 // Construct an empty DebugInfoDesc.
1559 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001560
1561 // Allow for user defined descriptors.
1562 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001563
1564 // Get the initializer constant.
1565 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1566
1567 // Get the operand count.
1568 unsigned N = CI->getNumOperands();
1569
1570 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001571 unsigned &CountSlot = Counts[Tag];
Bill Wendling3e30cbb2008-07-09 06:02:33 +00001572
1573 if (!CountSlot)
Jim Laskey86cbdba2006-02-06 15:33:21 +00001574 // Check the operand count to the field count
Bill Wendling3e30cbb2008-07-09 06:02:33 +00001575 CountSlot = CountFields(DD);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001576
Jim Laskey21407982006-03-14 18:37:57 +00001577 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001578 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001579 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001580 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001581 return false;
1582 }
1583
1584 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001585 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001586 VRAM.ApplyToFields(DD);
1587
1588 // Release empty DebugInfoDesc.
1589 delete DD;
1590
Jim Laskey98e04102006-03-26 22:45:20 +00001591 // If fields are not valid.
1592 if (!VRAM.isValid()) {
1593 ValiditySlot = Invalid;
1594 return false;
1595 }
1596
1597 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001598}
1599
Evan Chenga844bde2008-02-02 04:07:54 +00001600/// isVerified - Return true if the specified GV has already been
1601/// verified as a debug information descriptor.
1602bool DIVerifier::isVerified(GlobalVariable *GV) {
1603 unsigned &ValiditySlot = Validity[GV];
1604 if (ValiditySlot) return ValiditySlot == Valid;
1605 return false;
1606}
1607
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001608//===----------------------------------------------------------------------===//
1609
Jim Laskeyb8509c52006-03-23 18:07:55 +00001610DebugScope::~DebugScope() {
Bill Wendling10fff602008-07-03 22:53:42 +00001611 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1612 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
Jim Laskeyb8509c52006-03-23 18:07:55 +00001613}
1614
1615//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001616
Jim Laskey6da18642007-01-26 21:38:26 +00001617MachineModuleInfo::MachineModuleInfo()
Dan Gohmanae73dc12008-09-04 17:05:41 +00001618: ImmutablePass(&ID)
Devang Patel794fd752007-05-01 21:15:47 +00001619, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001620, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001621, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001622, Directories()
1623, SourceFiles()
1624, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001625, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001626, ScopeMap()
1627, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001628, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001629, LandingPads()
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001630, Personalities()
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001631, CallsEHReturn(0)
1632, CallsUnwindInit(0)
Devang Patele2051622009-01-13 23:02:17 +00001633, DbgInfoAvailable(false)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001634{
1635 // Always emit "no personality" info
1636 Personalities.push_back(NULL);
1637}
Jim Laskey6da18642007-01-26 21:38:26 +00001638MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001639
1640}
1641
Jim Laskey6da18642007-01-26 21:38:26 +00001642/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001643///
Jim Laskey6da18642007-01-26 21:38:26 +00001644bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001645 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001646}
1647
Jim Laskey6da18642007-01-26 21:38:26 +00001648/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001649///
Jim Laskey6da18642007-01-26 21:38:26 +00001650bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001651 return false;
1652}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001653
Jim Laskey6da18642007-01-26 21:38:26 +00001654/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001655///
Jim Laskey6da18642007-01-26 21:38:26 +00001656void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001657 // Coming soon.
1658}
1659
Jim Laskey6da18642007-01-26 21:38:26 +00001660/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001661///
Jim Laskey6da18642007-01-26 21:38:26 +00001662void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001663 // Clean up scope information.
1664 if (RootScope) {
1665 delete RootScope;
1666 ScopeMap.clear();
1667 RootScope = NULL;
1668 }
1669
Jim Laskeyb82313f2007-02-01 16:31:34 +00001670 // Clean up line info.
1671 Lines.clear();
1672
Jim Laskey41886992006-04-07 16:34:46 +00001673 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001674 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001675
1676 // Clean up exception info.
1677 LandingPads.clear();
1678 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +00001679 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +00001680 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001681 CallsEHReturn = 0;
1682 CallsUnwindInit = 0;
Jim Laskey41886992006-04-07 16:34:46 +00001683}
1684
Jim Laskeyd96185a2006-02-13 12:50:39 +00001685/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001686///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001687// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001688DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001689 return DR.Deserialize(V);
1690}
1691
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001692/// AnalyzeModule - Scan the module for global debug information.
1693///
Jim Laskey6da18642007-01-26 21:38:26 +00001694void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001695 SetupCompileUnits(M);
Dale Johannesen48ae02f2008-01-16 19:59:28 +00001696
1697 // Insert functions in the llvm.used array into UsedFunctions.
1698 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1699 if (!GV || !GV->hasInitializer()) return;
1700
1701 // Should be an array of 'i8*'.
1702 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1703 if (InitList == 0) return;
1704
1705 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1706 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1707 if (CE->getOpcode() == Instruction::BitCast)
1708 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1709 UsedFunctions.insert(F);
1710 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001711}
1712
1713/// SetupCompileUnits - Set up the unique vector of compile units.
1714///
Jim Laskey6da18642007-01-26 21:38:26 +00001715void MachineModuleInfo::SetupCompileUnits(Module &M) {
Bill Wendlingc04f4652008-07-03 23:13:02 +00001716 std::vector<CompileUnitDesc *> CU;
1717 getAnchoredDescriptors<CompileUnitDesc>(M, CU);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001718
Bill Wendling10fff602008-07-03 22:53:42 +00001719 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1720 CompileUnits.insert(CU[i]);
1721 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001722}
1723
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001724/// getCompileUnits - Return a vector of debug compile units.
1725///
Jim Laskey6da18642007-01-26 21:38:26 +00001726const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001727 return CompileUnits;
1728}
1729
Jim Laskey0420f2a2006-02-22 19:02:11 +00001730/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1731/// named GlobalVariable.
Bill Wendlingc04f4652008-07-03 23:13:02 +00001732void
Jim Laskey6da18642007-01-26 21:38:26 +00001733MachineModuleInfo::getGlobalVariablesUsing(Module &M,
Bill Wendlingc04f4652008-07-03 23:13:02 +00001734 const std::string &RootName,
1735 std::vector<GlobalVariable*>&Result){
1736 return ::getGlobalVariablesUsing(M, RootName, Result);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001737}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001738
Evan Chenga647c922008-02-01 02:05:57 +00001739/// RecordSourceLine - Records location information and associates it with a
Jim Laskeyb8509c52006-03-23 18:07:55 +00001740/// debug label. Returns a unique label ID used to generate a label and
1741/// provide correspondence to the source line list.
Evan Chenga647c922008-02-01 02:05:57 +00001742unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1743 unsigned Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001744 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001745 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001746 return ID;
1747}
1748
1749/// RecordSource - Register a source file with debug info. Returns an source
1750/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001751unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1752 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001753 unsigned DirectoryID = Directories.insert(Directory);
1754 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1755}
Jim Laskey6da18642007-01-26 21:38:26 +00001756unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001757 return RecordSource(CompileUnit->getDirectory(),
1758 CompileUnit->getFileName());
1759}
1760
1761/// RecordRegionStart - Indicate the start of a region.
1762///
Jim Laskey6da18642007-01-26 21:38:26 +00001763unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001764 // FIXME - need to be able to handle split scopes because of bb cloning.
1765 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1766 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1767 unsigned ID = NextLabelID();
1768 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1769 return ID;
1770}
1771
1772/// RecordRegionEnd - Indicate the end of a region.
1773///
Jim Laskey6da18642007-01-26 21:38:26 +00001774unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001775 // FIXME - need to be able to handle split scopes because of bb cloning.
1776 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1777 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1778 unsigned ID = NextLabelID();
1779 Scope->setEndLabelID(ID);
1780 return ID;
1781}
1782
1783/// RecordVariable - Indicate the declaration of a local variable.
1784///
Evan Chenga844bde2008-02-02 04:07:54 +00001785void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1786 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001787 DebugScope *Scope = getOrCreateScope(VD->getContext());
1788 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1789 Scope->AddVariable(DV);
1790}
1791
1792/// getOrCreateScope - Returns the scope associated with the given descriptor.
1793///
Jim Laskey6da18642007-01-26 21:38:26 +00001794DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001795 DebugScope *&Slot = ScopeMap[ScopeDesc];
1796 if (!Slot) {
1797 // FIXME - breaks down when the context is an inlined function.
1798 DebugInfoDesc *ParentDesc = NULL;
1799 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1800 ParentDesc = Block->getContext();
1801 }
1802 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1803 Slot = new DebugScope(Parent, ScopeDesc);
1804 if (Parent) {
1805 Parent->AddScope(Slot);
1806 } else if (RootScope) {
1807 // FIXME - Add inlined function scopes to the root so we can delete
1808 // them later. Long term, handle inlined functions properly.
1809 RootScope->AddScope(Slot);
1810 } else {
1811 // First function is top level function.
1812 RootScope = Slot;
1813 }
1814 }
1815 return Slot;
1816}
1817
Jim Laskey59667fe2007-02-21 22:38:31 +00001818//===-EH-------------------------------------------------------------------===//
1819
1820/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1821/// specified MachineBasicBlock.
Bill Wendling10fff602008-07-03 22:53:42 +00001822LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1823 (MachineBasicBlock *LandingPad) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001824 unsigned N = LandingPads.size();
1825 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001826 LandingPadInfo &LP = LandingPads[i];
1827 if (LP.LandingPadBlock == LandingPad)
1828 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001829 }
1830
1831 LandingPads.push_back(LandingPadInfo(LandingPad));
1832 return LandingPads[N];
1833}
1834
1835/// addInvoke - Provide the begin and end labels of an invoke style call and
1836/// associate it with a try landing pad block.
1837void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1838 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001839 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001840 LP.BeginLabels.push_back(BeginLabel);
1841 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +00001842}
1843
1844/// addLandingPad - Provide the label of a try LandingPad block.
1845///
1846unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1847 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001848 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1849 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001850 return LandingPadLabel;
1851}
1852
1853/// addPersonality - Provide the personality function for the exception
1854/// information.
1855void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001856 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001857 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001858 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001859
Bill Wendling10fff602008-07-03 22:53:42 +00001860 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001861 if (Personalities[i] == Personality)
1862 return;
1863
1864 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +00001865}
1866
1867/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1868///
1869void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1870 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001871 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001872 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001873 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001874}
Duncan Sands73ef58a2007-06-02 16:53:42 +00001875
1876/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +00001877///
Duncan Sands73ef58a2007-06-02 16:53:42 +00001878void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1879 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001880 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling49255672008-07-07 21:41:57 +00001881 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling10fff602008-07-03 22:53:42 +00001882 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001883 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1884 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +00001885}
1886
Duncan Sands6590b042007-08-27 15:47:50 +00001887/// addCleanup - Add a cleanup action for a landing pad.
1888///
1889void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1890 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1891 LP.TypeIds.push_back(0);
1892}
1893
Jim Laskey59667fe2007-02-21 22:38:31 +00001894/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1895/// pads.
1896void MachineModuleInfo::TidyLandingPads() {
1897 for (unsigned i = 0; i != LandingPads.size(); ) {
1898 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001899 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001900
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001901 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +00001902 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001903 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001904 LandingPads.erase(LandingPads.begin() + i);
1905 continue;
1906 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001907
Bill Wendling10fff602008-07-03 22:53:42 +00001908 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001909 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1910 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands57810cd2007-09-05 11:27:52 +00001911
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001912 if (!BeginLabel || !EndLabel) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001913 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1914 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1915 continue;
1916 }
1917
1918 LandingPad.BeginLabels[j] = BeginLabel;
1919 LandingPad.EndLabels[j] = EndLabel;
1920 ++j;
1921 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001922
1923 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +00001924 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +00001925 LandingPads.erase(LandingPads.begin() + i);
1926 continue;
1927 }
1928
1929 // If there is no landing pad, ensure that the list of typeids is empty.
1930 // If the only typeid is a cleanup, this is the same as having no typeids.
1931 if (!LandingPad.LandingPadBlock ||
1932 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1933 LandingPad.TypeIds.clear();
1934
Jim Laskey59667fe2007-02-21 22:38:31 +00001935 ++i;
1936 }
1937}
1938
1939/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1940/// function wide.
1941unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling10fff602008-07-03 22:53:42 +00001942 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1943 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey59667fe2007-02-21 22:38:31 +00001944
1945 TypeInfos.push_back(TI);
1946 return TypeInfos.size();
1947}
1948
Duncan Sands73ef58a2007-06-02 16:53:42 +00001949/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1950/// function wide.
Bill Wendling914c9702008-06-27 01:27:56 +00001951int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +00001952 // If the new filter coincides with the tail of an existing filter, then
1953 // re-use the existing filter. Folding filters more than this requires
1954 // re-ordering filters and/or their elements - probably not worth it.
1955 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1956 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling914c9702008-06-27 01:27:56 +00001957 unsigned i = *I, j = TyIds.size();
Duncan Sands14da32a2007-07-05 15:15:01 +00001958
1959 while (i && j)
1960 if (FilterIds[--i] != TyIds[--j])
1961 goto try_next;
1962
1963 if (!j)
1964 // The new filter coincides with range [i, end) of the existing filter.
1965 return -(1 + i);
Bill Wendling914c9702008-06-27 01:27:56 +00001966
Duncan Sands14da32a2007-07-05 15:15:01 +00001967try_next:;
1968 }
1969
1970 // Add the new filter.
Bill Wendling914c9702008-06-27 01:27:56 +00001971 int FilterID = -(1 + FilterIds.size());
1972 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1973 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001974 FilterIds.push_back(TyIds[I]);
Bill Wendling914c9702008-06-27 01:27:56 +00001975 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +00001976 FilterIds.push_back(0); // terminator
1977 return FilterID;
1978}
1979
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001980/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +00001981Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001982 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001983 // function
1984 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +00001985}
1986
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001987/// getPersonalityIndex - Return unique index for current personality
1988/// function. NULL personality function should always get zero index.
1989unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001990 const Function* Personality = NULL;
1991
1992 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling10fff602008-07-03 22:53:42 +00001993 for (unsigned i = 0; i != LandingPads.size(); ++i)
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001994 if (LandingPads[i].Personality) {
1995 Personality = LandingPads[i].Personality;
1996 break;
1997 }
1998
Bill Wendling10fff602008-07-03 22:53:42 +00001999 for (unsigned i = 0; i < Personalities.size(); ++i) {
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002000 if (Personalities[i] == Personality)
2001 return i;
Bill Wendling10fff602008-07-03 22:53:42 +00002002 }
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002003
2004 // This should never happen
2005 assert(0 && "Personality function should be set!");
2006 return 0;
2007}
Jim Laskey59667fe2007-02-21 22:38:31 +00002008
Jim Laskey9d4209f2006-11-07 19:33:46 +00002009//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00002010/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
2011/// a info consumer to determine if the range of two labels is empty, by seeing
2012/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002013
2014namespace llvm {
2015
2016struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00002017 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +00002018 DebugLabelFolder() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +00002019
Evan Chengbbeeb2a2008-09-22 20:58:04 +00002020 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Evan Cheng8b56a902008-09-22 22:21:38 +00002021 AU.addPreservedID(MachineLoopInfoID);
2022 AU.addPreservedID(MachineDominatorsID);
Evan Chengbbeeb2a2008-09-22 20:58:04 +00002023 MachineFunctionPass::getAnalysisUsage(AU);
2024 }
2025
Jim Laskey9d4209f2006-11-07 19:33:46 +00002026 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00002027 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00002028};
2029
Devang Patel19974732007-05-03 01:11:54 +00002030char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00002031
Jim Laskey9d4209f2006-11-07 19:33:46 +00002032bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00002033 // Get machine module info.
2034 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
2035 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002036
2037 // Track if change is made.
2038 bool MadeChange = false;
2039 // No prior label to begin.
2040 unsigned PriorLabel = 0;
2041
2042 // Iterate through basic blocks.
2043 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
2044 BB != E; ++BB) {
2045 // Iterate through instructions.
2046 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00002047 // Is it a label.
Evan Chengbb81d972008-01-31 09:59:15 +00002048 if (I->isDebugLabel()) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002049 // The label ID # is always operand #0, an immediate.
2050 unsigned NextLabel = I->getOperand(0).getImm();
2051
2052 // If there was an immediate prior label.
2053 if (PriorLabel) {
2054 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00002055 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002056 // Delete the current label.
2057 I = BB->erase(I);
2058 // Indicate a change has been made.
2059 MadeChange = true;
2060 continue;
2061 } else {
2062 // Start a new round.
2063 PriorLabel = NextLabel;
2064 }
2065 } else {
2066 // No consecutive labels.
2067 PriorLabel = 0;
2068 }
2069
2070 ++I;
2071 }
2072 }
2073
2074 return MadeChange;
2075}
2076
2077FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
2078
2079}
Bill Wendling10fff602008-07-03 22:53:42 +00002080