blob: a83da0a232a09faaa6492caa262c844655e64abb [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"
Evan Cheng8b56a902008-09-22 22:21:38 +000017#include "llvm/CodeGen/Passes.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000018#include "llvm/Target/TargetInstrInfo.h"
19#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000020#include "llvm/Target/TargetOptions.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000021#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000022#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000023#include "llvm/Intrinsics.h"
24#include "llvm/Instructions.h"
25#include "llvm/Module.h"
26#include "llvm/Support/Dwarf.h"
Bill Wendling832171c2006-12-07 20:04:42 +000027#include "llvm/Support/Streams.h"
Jim Laskey6af56812006-01-04 13:36:38 +000028using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000029using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000030
31// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman844731a2008-05-13 00:00:25 +000032static RegisterPass<MachineModuleInfo>
33X("machinemoduleinfo", "Module Information");
Devang Patel19974732007-05-03 01:11:54 +000034char MachineModuleInfo::ID = 0;
Jim Laskey063e7652006-01-17 17:31:53 +000035
Jim Laskeyb3e789a2006-01-26 20:21:46 +000036//===----------------------------------------------------------------------===//
37
Jim Laskey86cbdba2006-02-06 15:33:21 +000038/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000039/// specified value in their initializer somewhere.
40static void
Bill Wendling0ac1b6d2008-06-27 01:32:08 +000041getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +000042 // Scan though value users.
43 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
44 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000045 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000046 Result.push_back(GV);
47 } else if (Constant *C = dyn_cast<Constant>(*I)) {
48 // If the user is a constant variable then scan its users
49 getGlobalVariablesUsing(C, Result);
50 }
51 }
52}
53
Jim Laskey86cbdba2006-02-06 15:33:21 +000054/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
55/// named GlobalVariable.
Bill Wendlingc04f4652008-07-03 23:13:02 +000056static void
57getGlobalVariablesUsing(Module &M, const std::string &RootName,
58 std::vector<GlobalVariable*> &Result) {
Jim Laskeyce72b172006-02-11 01:01:30 +000059 std::vector<const Type*> FieldTypes;
Reid Spencer47857812006-12-31 05:55:36 +000060 FieldTypes.push_back(Type::Int32Ty);
61 FieldTypes.push_back(Type::Int32Ty);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000062
Jim Laskey86cbdba2006-02-06 15:33:21 +000063 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000064 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000065 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000066
67 // If present and linkonce then scan for users.
Bill Wendlingc04f4652008-07-03 23:13:02 +000068 if (UseRoot && UseRoot->hasLinkOnceLinkage())
Jim Laskeyb3e789a2006-01-26 20:21:46 +000069 getGlobalVariablesUsing(UseRoot, Result);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000070}
71
Jim Laskey86cbdba2006-02-06 15:33:21 +000072/// isStringValue - Return true if the given value can be coerced to a string.
73///
74static bool isStringValue(Value *V) {
75 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
76 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
77 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
78 return Init->isString();
79 }
80 } else if (Constant *C = dyn_cast<Constant>(V)) {
81 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
82 return isStringValue(GV);
83 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
84 if (CE->getOpcode() == Instruction::GetElementPtr) {
85 if (CE->getNumOperands() == 3 &&
86 cast<Constant>(CE->getOperand(1))->isNullValue() &&
87 isa<ConstantInt>(CE->getOperand(2))) {
88 return isStringValue(CE->getOperand(0));
89 }
90 }
91 }
92 }
93 return false;
94}
95
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000096/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000097///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000098static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000099 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
100 return GV;
101 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000102 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000103 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000104 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
105 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000106 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000107 return NULL;
108 }
109 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000110 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000111 }
112 return NULL;
113}
114
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000115/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000116/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000117static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000118 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
119 return true;
120 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000121 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000122 return isa<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000123 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
124 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000125 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000126 return false;
127 }
128 return isa<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000129 }
130 }
131 return false;
132}
133
Bill Wendling10fff602008-07-03 22:53:42 +0000134/// getUIntOperand - Return ith operand if it is an unsigned integer.
135///
136static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
137 // Make sure the GlobalVariable has an initializer.
138 if (!GV->hasInitializer()) return NULL;
139
140 // Get the initializer constant.
141 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
142 if (!CI) return NULL;
143
144 // Check if there is at least i + 1 operands.
145 unsigned N = CI->getNumOperands();
146 if (i >= N) return NULL;
147
148 // Check constant.
149 return dyn_cast<ConstantInt>(CI->getOperand(i));
150}
151
Jim Laskey86cbdba2006-02-06 15:33:21 +0000152//===----------------------------------------------------------------------===//
153
Bill Wendling3e30cbb2008-07-09 06:02:33 +0000154static unsigned CountFields(DebugInfoDesc *DD) {
155 unsigned Count = 0;
156
157 switch (DD->getTag()) {
158 case DW_TAG_anchor: // AnchorDesc
159 // Tag
160 // AnchorTag
161 Count = 2;
162 break;
163 case DW_TAG_compile_unit: // CompileUnitDesc
164 // [DW_TAG_anchor]
165 // if (Version == 0) DebugVersion
166 // Language
167 // FileName
168 // Directory
169 // Producer
170 Count = 6;
171
172 // Handle cases out of sync with compiler.
173 if (DD->getVersion() == 0)
174 ++Count;
175
176 break;
177 case DW_TAG_variable: // GlobalVariableDesc
178 // [DW_TAG_anchor]
179 // Context
180 // Name
181 // FullName
182 // LinkageName
183 // File
184 // Line
185 // TyDesc
186 // IsStatic
187 // IsDefinition
188 // Global
189 Count = 12;
190 break;
191 case DW_TAG_subprogram: // SubprogramDesc
192 // [DW_TAG_anchor]
193 // Context
194 // Name
195 // FullName
196 // LinkageName
197 // File
198 // Line
199 // TyDesc
200 // IsStatic
201 // IsDefinition
202 Count = 11;
203 break;
204 case DW_TAG_lexical_block: // BlockDesc
205 // Tag
206 // Context
207 Count = 2;
208 break;
209 case DW_TAG_base_type: // BasicTypeDesc
210 // Tag
211 // Context
212 // Name
213 // File
214 // Line
215 // Size
216 // Align
217 // Offset
218 // if (Version > LLVMDebugVersion4) Flags
219 // Encoding
220 Count = 9;
221
222 if (DD->getVersion() > LLVMDebugVersion4)
223 ++Count;
224
225 break;
226 case DW_TAG_typedef:
227 case DW_TAG_pointer_type:
228 case DW_TAG_reference_type:
229 case DW_TAG_const_type:
230 case DW_TAG_volatile_type:
231 case DW_TAG_restrict_type:
232 case DW_TAG_member:
233 case DW_TAG_inheritance: // DerivedTypeDesc
234 // Tag
235 // Context
236 // Name
237 // File
238 // Line
239 // Size
240 // Align
241 // Offset
242 // if (Version > LLVMDebugVersion4) Flags
243 // FromType
244 Count = 9;
245
246 if (DD->getVersion() > LLVMDebugVersion4)
247 ++Count;
248
249 break;
250 case DW_TAG_array_type:
251 case DW_TAG_structure_type:
252 case DW_TAG_union_type:
253 case DW_TAG_enumeration_type:
254 case DW_TAG_vector_type:
255 case DW_TAG_subroutine_type: // CompositeTypeDesc
256 // Tag
257 // Context
258 // Name
259 // File
260 // Line
261 // Size
262 // Align
263 // Offset
264 // if (Version > LLVMDebugVersion4) Flags
265 // FromType
266 // Elements
267 Count = 10;
268
269 if (DD->getVersion() > LLVMDebugVersion4)
270 ++Count;
271
272 break;
273 case DW_TAG_subrange_type: // SubrangeDesc
274 // Tag
275 // Lo
276 // Hi
277 Count = 3;
278 break;
279 case DW_TAG_enumerator: // EnumeratorDesc
280 // Tag
281 // Name
282 // Value
283 Count = 3;
284 break;
285 case DW_TAG_return_variable:
286 case DW_TAG_arg_variable:
287 case DW_TAG_auto_variable: // VariableDesc
288 // Tag
289 // Context
290 // Name
291 // File
292 // Line
293 // TyDesc
294 Count = 6;
295 break;
296 default:
297 break;
298 }
299
300 return Count;
301}
302
303//===----------------------------------------------------------------------===//
304
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000305/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000306/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000307void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000308 DD->ApplyToFields(this);
309}
310
Dan Gohman844731a2008-05-13 00:00:25 +0000311namespace {
312
Jim Laskey86cbdba2006-02-06 15:33:21 +0000313//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000314/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
315/// supplied DebugInfoDesc.
316class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000317private:
318 DIDeserializer &DR; // Active deserializer.
319 unsigned I; // Current operand index.
320 ConstantStruct *CI; // GlobalVariable constant initializer.
321
322public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000323 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
Bill Wendling914c9702008-06-27 01:27:56 +0000324 : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer()))
Jim Laskey86cbdba2006-02-06 15:33:21 +0000325 {}
326
327 /// Apply - Set the value of each of the fields.
328 ///
329 virtual void Apply(int &Field) {
330 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000331 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000332 }
333 virtual void Apply(unsigned &Field) {
334 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000335 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000336 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000337 virtual void Apply(int64_t &Field) {
338 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000339 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000340 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000341 virtual void Apply(uint64_t &Field) {
342 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000343 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000344 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000345 virtual void Apply(bool &Field) {
346 Constant *C = CI->getOperand(I++);
Reid Spencer579dca12007-01-12 04:24:46 +0000347 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000348 }
349 virtual void Apply(std::string &Field) {
350 Constant *C = CI->getOperand(I++);
Evan Cheng0ff39b32008-06-30 07:31:25 +0000351 // Fills in the string if it succeeds
352 if (!GetConstantStringInfo(C, Field))
353 Field.clear();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000354 }
355 virtual void Apply(DebugInfoDesc *&Field) {
356 Constant *C = CI->getOperand(I++);
357 Field = DR.Deserialize(C);
358 }
359 virtual void Apply(GlobalVariable *&Field) {
360 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000361 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000362 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000363 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000364 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000365 Constant *C = CI->getOperand(I++);
366 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000367 if (GV->hasInitializer()) {
368 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
369 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
370 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
371 DebugInfoDesc *DE = DR.Deserialize(GVE);
372 Field.push_back(DE);
373 }
374 } else if (GV->getInitializer()->isNullValue()) {
375 if (const ArrayType *T =
376 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
377 Field.resize(T->getNumElements());
378 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000379 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000380 }
381 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000382};
383
384//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000385/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000386/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000387class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000388private:
389 DISerializer &SR; // Active serializer.
390 std::vector<Constant*> &Elements; // Element accumulator.
391
392public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000393 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
Bill Wendling10fff602008-07-03 22:53:42 +0000394 : DIVisitor()
395 , SR(S)
396 , Elements(E)
397 {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000398
399 /// Apply - Set the value of each of the fields.
400 ///
401 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000402 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000403 }
404 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000405 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000406 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000407 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000408 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000409 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000410 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000411 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000412 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000413 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000414 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000415 }
416 virtual void Apply(std::string &Field) {
Bill Wendling914c9702008-06-27 01:27:56 +0000417 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000418 }
419 virtual void Apply(DebugInfoDesc *&Field) {
420 GlobalVariable *GV = NULL;
421
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000422 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000423 if (Field) GV = SR.Serialize(Field);
424
425 // FIXME - At some point should use specific type.
426 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
427
428 if (GV) {
429 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000430 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000431 } else {
432 // Use NULL.
433 Elements.push_back(ConstantPointerNull::get(EmptyTy));
434 }
435 }
436 virtual void Apply(GlobalVariable *&Field) {
437 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000438 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000439 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000440 } else {
441 Elements.push_back(ConstantPointerNull::get(EmptyTy));
442 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000443 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000444 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
445 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
446 unsigned N = Field.size();
447 ArrayType *AT = ArrayType::get(EmptyTy, N);
448 std::vector<Constant *> ArrayElements;
449
Bill Wendling10fff602008-07-03 22:53:42 +0000450 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000451 if (DebugInfoDesc *Element = Field[i]) {
452 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000453 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000454 ArrayElements.push_back(cast<Constant>(CE));
455 } else {
456 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
457 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000458 }
459
460 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000461 GlobalVariable *CAGV = new GlobalVariable(AT, true,
462 GlobalValue::InternalLinkage,
463 CA, "llvm.dbg.array",
464 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000465 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000466 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000467 Elements.push_back(CAE);
468 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000469};
470
471//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000472/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000473/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000474class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000475private:
476 DISerializer &SR; // Active serializer.
477 std::vector<const Type*> &Fields; // Type accumulator.
478
479public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000480 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
Bill Wendling10fff602008-07-03 22:53:42 +0000481 : DIVisitor()
482 , SR(S)
483 , Fields(F)
484 {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000485
486 /// Apply - Set the value of each of the fields.
487 ///
488 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000489 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000490 }
491 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000492 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000493 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000494 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000495 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000496 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000497 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000498 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000499 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000500 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000501 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000502 }
503 virtual void Apply(std::string &Field) {
504 Fields.push_back(SR.getStrPtrType());
505 }
506 virtual void Apply(DebugInfoDesc *&Field) {
507 // FIXME - At some point should use specific type.
508 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
509 Fields.push_back(EmptyTy);
510 }
511 virtual void Apply(GlobalVariable *&Field) {
512 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
513 Fields.push_back(EmptyTy);
514 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000515 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
516 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
517 Fields.push_back(EmptyTy);
518 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000519};
520
521//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000522/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000523/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000524class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000525private:
526 DIVerifier &VR; // Active verifier.
527 bool IsValid; // Validity status.
528 unsigned I; // Current operand index.
529 ConstantStruct *CI; // GlobalVariable constant initializer.
530
531public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000532 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
533 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000534 , VR(V)
535 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000536 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000537 , CI(cast<ConstantStruct>(GV->getInitializer()))
538 {
539 }
540
541 // Accessors.
542 bool isValid() const { return IsValid; }
543
544 /// Apply - Set the value of each of the fields.
545 ///
546 virtual void Apply(int &Field) {
547 Constant *C = CI->getOperand(I++);
548 IsValid = IsValid && isa<ConstantInt>(C);
549 }
550 virtual void Apply(unsigned &Field) {
551 Constant *C = CI->getOperand(I++);
552 IsValid = IsValid && isa<ConstantInt>(C);
553 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000554 virtual void Apply(int64_t &Field) {
555 Constant *C = CI->getOperand(I++);
556 IsValid = IsValid && isa<ConstantInt>(C);
557 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000558 virtual void Apply(uint64_t &Field) {
559 Constant *C = CI->getOperand(I++);
560 IsValid = IsValid && isa<ConstantInt>(C);
561 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000562 virtual void Apply(bool &Field) {
563 Constant *C = CI->getOperand(I++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000564 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000565 }
566 virtual void Apply(std::string &Field) {
567 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000568 IsValid = IsValid &&
569 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000570 }
571 virtual void Apply(DebugInfoDesc *&Field) {
572 // FIXME - Prepare the correct descriptor.
573 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000574 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000575 }
576 virtual void Apply(GlobalVariable *&Field) {
577 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000578 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000579 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000580 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
581 Constant *C = CI->getOperand(I++);
582 IsValid = IsValid && isGlobalVariable(C);
583 if (!IsValid) return;
584
585 GlobalVariable *GV = getGlobalVariable(C);
586 IsValid = IsValid && GV && GV->hasInitializer();
587 if (!IsValid) return;
588
589 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
590 IsValid = IsValid && CA;
591 if (!IsValid) return;
592
593 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
594 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
595 if (!IsValid) return;
596
597 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
598 VR.Verify(GVE);
599 }
600 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000601};
602
Dan Gohman844731a2008-05-13 00:00:25 +0000603}
Jim Laskeyce72b172006-02-11 01:01:30 +0000604
Jim Laskey86cbdba2006-02-06 15:33:21 +0000605//===----------------------------------------------------------------------===//
606
Bill Wendling10fff602008-07-03 22:53:42 +0000607/// TagFromGlobal - Returns the tag number from a debug info descriptor
608/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
609unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
610 ConstantInt *C = getUIntOperand(GV, 0);
611 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
612 (unsigned)DW_TAG_invalid;
613}
614
615/// VersionFromGlobal - Returns the version number from a debug info
616/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
617/// int.
618unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
619 ConstantInt *C = getUIntOperand(GV, 0);
620 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
621 (unsigned)DW_TAG_invalid;
622}
623
624/// DescFactory - Create an instance of debug info descriptor based on Tag.
625/// Return NULL if not a recognized Tag.
626DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
627 switch (Tag) {
628 case DW_TAG_anchor: return new AnchorDesc();
629 case DW_TAG_compile_unit: return new CompileUnitDesc();
630 case DW_TAG_variable: return new GlobalVariableDesc();
631 case DW_TAG_subprogram: return new SubprogramDesc();
632 case DW_TAG_lexical_block: return new BlockDesc();
633 case DW_TAG_base_type: return new BasicTypeDesc();
634 case DW_TAG_typedef:
635 case DW_TAG_pointer_type:
636 case DW_TAG_reference_type:
637 case DW_TAG_const_type:
638 case DW_TAG_volatile_type:
639 case DW_TAG_restrict_type:
640 case DW_TAG_member:
641 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
642 case DW_TAG_array_type:
643 case DW_TAG_structure_type:
644 case DW_TAG_union_type:
645 case DW_TAG_enumeration_type:
646 case DW_TAG_vector_type:
647 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
648 case DW_TAG_subrange_type: return new SubrangeDesc();
649 case DW_TAG_enumerator: return new EnumeratorDesc();
650 case DW_TAG_return_variable:
651 case DW_TAG_arg_variable:
652 case DW_TAG_auto_variable: return new VariableDesc(Tag);
653 default: break;
654 }
655 return NULL;
656}
657
658/// getLinkage - get linkage appropriate for this type of descriptor.
659///
660GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
661 return GlobalValue::InternalLinkage;
662}
663
664/// ApplyToFields - Target the vistor to the fields of the descriptor.
665///
666void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
667 Visitor->Apply(Tag);
668}
669
670//===----------------------------------------------------------------------===//
671
672AnchorDesc::AnchorDesc()
673: DebugInfoDesc(DW_TAG_anchor)
674, AnchorTag(0)
675{}
676AnchorDesc::AnchorDesc(AnchoredDesc *D)
677: DebugInfoDesc(DW_TAG_anchor)
678, AnchorTag(D->getTag())
679{}
680
681// Implement isa/cast/dyncast.
682bool AnchorDesc::classof(const DebugInfoDesc *D) {
683 return D->getTag() == DW_TAG_anchor;
684}
685
686/// getLinkage - get linkage appropriate for this type of descriptor.
687///
688GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
689 return GlobalValue::LinkOnceLinkage;
690}
691
692/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
693///
694void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
695 DebugInfoDesc::ApplyToFields(Visitor);
696
697 Visitor->Apply(AnchorTag);
698}
699
700/// getDescString - Return a string used to compose global names and labels. A
701/// A global variable name needs to be defined for each debug descriptor that is
702/// anchored. NOTE: that each global variable named here also needs to be added
703/// to the list of names left external in the internalizer.
704/// ExternalNames.insert("llvm.dbg.compile_units");
705/// ExternalNames.insert("llvm.dbg.global_variables");
706/// ExternalNames.insert("llvm.dbg.subprograms");
707const char *AnchorDesc::getDescString() const {
708 switch (AnchorTag) {
709 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
710 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
711 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
712 default: break;
713 }
714
715 assert(0 && "Tag does not have a case for anchor string");
716 return "";
717}
718
719/// getTypeString - Return a string used to label this descriptors type.
720///
721const char *AnchorDesc::getTypeString() const {
722 return "llvm.dbg.anchor.type";
723}
724
725#ifndef NDEBUG
726void AnchorDesc::dump() {
727 cerr << getDescString() << " "
728 << "Version(" << getVersion() << "), "
729 << "Tag(" << getTag() << "), "
730 << "AnchorTag(" << AnchorTag << ")\n";
731}
732#endif
733
734//===----------------------------------------------------------------------===//
735
736AnchoredDesc::AnchoredDesc(unsigned T)
737: DebugInfoDesc(T)
738, Anchor(NULL)
739{}
740
741/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
742///
743void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
744 DebugInfoDesc::ApplyToFields(Visitor);
745
746 Visitor->Apply(Anchor);
747}
748
749//===----------------------------------------------------------------------===//
750
751CompileUnitDesc::CompileUnitDesc()
752: AnchoredDesc(DW_TAG_compile_unit)
753, Language(0)
754, FileName("")
755, Directory("")
756, Producer("")
757{}
758
759// Implement isa/cast/dyncast.
760bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
761 return D->getTag() == DW_TAG_compile_unit;
762}
763
764/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
765///
766void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
767 AnchoredDesc::ApplyToFields(Visitor);
768
769 // Handle cases out of sync with compiler.
770 if (getVersion() == 0) {
771 unsigned DebugVersion;
772 Visitor->Apply(DebugVersion);
773 }
774
775 Visitor->Apply(Language);
776 Visitor->Apply(FileName);
777 Visitor->Apply(Directory);
778 Visitor->Apply(Producer);
779}
780
781/// getDescString - Return a string used to compose global names and labels.
782///
783const char *CompileUnitDesc::getDescString() const {
784 return "llvm.dbg.compile_unit";
785}
786
787/// getTypeString - Return a string used to label this descriptors type.
788///
789const char *CompileUnitDesc::getTypeString() const {
790 return "llvm.dbg.compile_unit.type";
791}
792
793/// getAnchorString - Return a string used to label this descriptor's anchor.
794///
795const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
796const char *CompileUnitDesc::getAnchorString() const {
797 return AnchorString;
798}
799
800#ifndef NDEBUG
801void CompileUnitDesc::dump() {
802 cerr << getDescString() << " "
803 << "Version(" << getVersion() << "), "
804 << "Tag(" << getTag() << "), "
805 << "Anchor(" << getAnchor() << "), "
806 << "Language(" << Language << "), "
807 << "FileName(\"" << FileName << "\"), "
808 << "Directory(\"" << Directory << "\"), "
809 << "Producer(\"" << Producer << "\")\n";
810}
811#endif
812
813//===----------------------------------------------------------------------===//
814
815TypeDesc::TypeDesc(unsigned T)
816: DebugInfoDesc(T)
817, Context(NULL)
818, Name("")
819, File(NULL)
820, Line(0)
821, Size(0)
822, Align(0)
823, Offset(0)
824, Flags(0)
825{}
826
827/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
828///
829void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
830 DebugInfoDesc::ApplyToFields(Visitor);
831
832 Visitor->Apply(Context);
833 Visitor->Apply(Name);
834 Visitor->Apply(File);
835 Visitor->Apply(Line);
836 Visitor->Apply(Size);
837 Visitor->Apply(Align);
838 Visitor->Apply(Offset);
839 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
840}
841
842/// getDescString - Return a string used to compose global names and labels.
843///
844const char *TypeDesc::getDescString() const {
845 return "llvm.dbg.type";
846}
847
848/// getTypeString - Return a string used to label this descriptor's type.
849///
850const char *TypeDesc::getTypeString() const {
851 return "llvm.dbg.type.type";
852}
853
854#ifndef NDEBUG
855void TypeDesc::dump() {
856 cerr << getDescString() << " "
857 << "Version(" << getVersion() << "), "
858 << "Tag(" << getTag() << "), "
859 << "Context(" << Context << "), "
860 << "Name(\"" << Name << "\"), "
861 << "File(" << File << "), "
862 << "Line(" << Line << "), "
863 << "Size(" << Size << "), "
864 << "Align(" << Align << "), "
865 << "Offset(" << Offset << "), "
866 << "Flags(" << Flags << ")\n";
867}
868#endif
869
870//===----------------------------------------------------------------------===//
871
872BasicTypeDesc::BasicTypeDesc()
873: TypeDesc(DW_TAG_base_type)
874, Encoding(0)
875{}
876
877// Implement isa/cast/dyncast.
878bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
879 return D->getTag() == DW_TAG_base_type;
880}
881
882/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
883///
884void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
885 TypeDesc::ApplyToFields(Visitor);
886
887 Visitor->Apply(Encoding);
888}
889
890/// getDescString - Return a string used to compose global names and labels.
891///
892const char *BasicTypeDesc::getDescString() const {
893 return "llvm.dbg.basictype";
894}
895
896/// getTypeString - Return a string used to label this descriptor's type.
897///
898const char *BasicTypeDesc::getTypeString() const {
899 return "llvm.dbg.basictype.type";
900}
901
902#ifndef NDEBUG
903void BasicTypeDesc::dump() {
904 cerr << getDescString() << " "
905 << "Version(" << getVersion() << "), "
906 << "Tag(" << getTag() << "), "
907 << "Context(" << getContext() << "), "
908 << "Name(\"" << getName() << "\"), "
909 << "Size(" << getSize() << "), "
910 << "Encoding(" << Encoding << ")\n";
911}
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() << "), "
969 << "FromType(" << FromType << ")\n";
970}
971#endif
972
973//===----------------------------------------------------------------------===//
974
975CompositeTypeDesc::CompositeTypeDesc(unsigned T)
976: DerivedTypeDesc(T)
977, Elements()
978{}
979
980// Implement isa/cast/dyncast.
981bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
982 unsigned T = D->getTag();
983 switch (T) {
984 case DW_TAG_array_type:
985 case DW_TAG_structure_type:
986 case DW_TAG_union_type:
987 case DW_TAG_enumeration_type:
988 case DW_TAG_vector_type:
989 case DW_TAG_subroutine_type:
990 return true;
991 default: break;
992 }
993 return false;
994}
995
996/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
997///
998void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
999 DerivedTypeDesc::ApplyToFields(Visitor);
1000
1001 Visitor->Apply(Elements);
1002}
1003
1004/// getDescString - Return a string used to compose global names and labels.
1005///
1006const char *CompositeTypeDesc::getDescString() const {
1007 return "llvm.dbg.compositetype";
1008}
1009
1010/// getTypeString - Return a string used to label this descriptor's type.
1011///
1012const char *CompositeTypeDesc::getTypeString() const {
1013 return "llvm.dbg.compositetype.type";
1014}
1015
1016#ifndef NDEBUG
1017void CompositeTypeDesc::dump() {
1018 cerr << getDescString() << " "
1019 << "Version(" << getVersion() << "), "
1020 << "Tag(" << getTag() << "), "
1021 << "Context(" << getContext() << "), "
1022 << "Name(\"" << getName() << "\"), "
1023 << "Size(" << getSize() << "), "
1024 << "File(" << getFile() << "), "
1025 << "Line(" << getLine() << "), "
1026 << "FromType(" << getFromType() << "), "
1027 << "Elements.size(" << Elements.size() << ")\n";
1028}
1029#endif
1030
1031//===----------------------------------------------------------------------===//
1032
1033SubrangeDesc::SubrangeDesc()
1034: DebugInfoDesc(DW_TAG_subrange_type)
1035, Lo(0)
1036, Hi(0)
1037{}
1038
1039// Implement isa/cast/dyncast.
1040bool SubrangeDesc::classof(const DebugInfoDesc *D) {
1041 return D->getTag() == DW_TAG_subrange_type;
1042}
1043
1044/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
1045///
1046void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
1047 DebugInfoDesc::ApplyToFields(Visitor);
1048
1049 Visitor->Apply(Lo);
1050 Visitor->Apply(Hi);
1051}
1052
1053/// getDescString - Return a string used to compose global names and labels.
1054///
1055const char *SubrangeDesc::getDescString() const {
1056 return "llvm.dbg.subrange";
1057}
1058
1059/// getTypeString - Return a string used to label this descriptor's type.
1060///
1061const char *SubrangeDesc::getTypeString() const {
1062 return "llvm.dbg.subrange.type";
1063}
1064
1065#ifndef NDEBUG
1066void SubrangeDesc::dump() {
1067 cerr << getDescString() << " "
1068 << "Version(" << getVersion() << "), "
1069 << "Tag(" << getTag() << "), "
1070 << "Lo(" << Lo << "), "
1071 << "Hi(" << Hi << ")\n";
1072}
1073#endif
1074
1075//===----------------------------------------------------------------------===//
1076
1077EnumeratorDesc::EnumeratorDesc()
1078: DebugInfoDesc(DW_TAG_enumerator)
1079, Name("")
1080, Value(0)
1081{}
1082
1083// Implement isa/cast/dyncast.
1084bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
1085 return D->getTag() == DW_TAG_enumerator;
1086}
1087
1088/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
1089///
1090void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
1091 DebugInfoDesc::ApplyToFields(Visitor);
1092
1093 Visitor->Apply(Name);
1094 Visitor->Apply(Value);
1095}
1096
1097/// getDescString - Return a string used to compose global names and labels.
1098///
1099const char *EnumeratorDesc::getDescString() const {
1100 return "llvm.dbg.enumerator";
1101}
1102
1103/// getTypeString - Return a string used to label this descriptor's type.
1104///
1105const char *EnumeratorDesc::getTypeString() const {
1106 return "llvm.dbg.enumerator.type";
1107}
1108
1109#ifndef NDEBUG
1110void EnumeratorDesc::dump() {
1111 cerr << getDescString() << " "
1112 << "Version(" << getVersion() << "), "
1113 << "Tag(" << getTag() << "), "
1114 << "Name(" << Name << "), "
1115 << "Value(" << Value << ")\n";
1116}
1117#endif
1118
1119//===----------------------------------------------------------------------===//
1120
1121VariableDesc::VariableDesc(unsigned T)
1122: DebugInfoDesc(T)
1123, Context(NULL)
1124, Name("")
1125, File(NULL)
1126, Line(0)
1127, TyDesc(0)
1128{}
1129
1130// Implement isa/cast/dyncast.
1131bool VariableDesc::classof(const DebugInfoDesc *D) {
1132 unsigned T = D->getTag();
1133 switch (T) {
1134 case DW_TAG_auto_variable:
1135 case DW_TAG_arg_variable:
1136 case DW_TAG_return_variable:
1137 return true;
1138 default: break;
1139 }
1140 return false;
1141}
1142
1143/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1144///
1145void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1146 DebugInfoDesc::ApplyToFields(Visitor);
1147
1148 Visitor->Apply(Context);
1149 Visitor->Apply(Name);
1150 Visitor->Apply(File);
1151 Visitor->Apply(Line);
1152 Visitor->Apply(TyDesc);
1153}
1154
1155/// getDescString - Return a string used to compose global names and labels.
1156///
1157const char *VariableDesc::getDescString() const {
1158 return "llvm.dbg.variable";
1159}
1160
1161/// getTypeString - Return a string used to label this descriptor's type.
1162///
1163const char *VariableDesc::getTypeString() const {
1164 return "llvm.dbg.variable.type";
1165}
1166
1167#ifndef NDEBUG
1168void VariableDesc::dump() {
1169 cerr << getDescString() << " "
1170 << "Version(" << getVersion() << "), "
1171 << "Tag(" << getTag() << "), "
1172 << "Context(" << Context << "), "
1173 << "Name(\"" << Name << "\"), "
1174 << "File(" << File << "), "
1175 << "Line(" << Line << "), "
1176 << "TyDesc(" << TyDesc << ")\n";
1177}
1178#endif
1179
1180//===----------------------------------------------------------------------===//
1181
1182GlobalDesc::GlobalDesc(unsigned T)
1183: AnchoredDesc(T)
1184, Context(0)
1185, Name("")
1186, FullName("")
1187, LinkageName("")
1188, File(NULL)
1189, Line(0)
1190, TyDesc(NULL)
1191, IsStatic(false)
1192, IsDefinition(false)
1193{}
1194
1195/// ApplyToFields - Target the visitor to the fields of the global.
1196///
1197void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1198 AnchoredDesc::ApplyToFields(Visitor);
1199
1200 Visitor->Apply(Context);
1201 Visitor->Apply(Name);
1202 Visitor->Apply(FullName);
1203 Visitor->Apply(LinkageName);
1204 Visitor->Apply(File);
1205 Visitor->Apply(Line);
1206 Visitor->Apply(TyDesc);
1207 Visitor->Apply(IsStatic);
1208 Visitor->Apply(IsDefinition);
1209}
1210
1211//===----------------------------------------------------------------------===//
1212
1213GlobalVariableDesc::GlobalVariableDesc()
1214: GlobalDesc(DW_TAG_variable)
1215, Global(NULL)
1216{}
1217
1218// Implement isa/cast/dyncast.
1219bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1220 return D->getTag() == DW_TAG_variable;
1221}
1222
1223/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1224///
1225void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1226 GlobalDesc::ApplyToFields(Visitor);
1227
1228 Visitor->Apply(Global);
1229}
1230
1231/// getDescString - Return a string used to compose global names and labels.
1232///
1233const char *GlobalVariableDesc::getDescString() const {
1234 return "llvm.dbg.global_variable";
1235}
1236
1237/// getTypeString - Return a string used to label this descriptors type.
1238///
1239const char *GlobalVariableDesc::getTypeString() const {
1240 return "llvm.dbg.global_variable.type";
1241}
1242
1243/// getAnchorString - Return a string used to label this descriptor's anchor.
1244///
1245const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1246const char *GlobalVariableDesc::getAnchorString() const {
1247 return AnchorString;
1248}
1249
1250#ifndef NDEBUG
1251void GlobalVariableDesc::dump() {
1252 cerr << getDescString() << " "
1253 << "Version(" << getVersion() << "), "
1254 << "Tag(" << getTag() << "), "
1255 << "Anchor(" << getAnchor() << "), "
1256 << "Name(\"" << getName() << "\"), "
1257 << "FullName(\"" << getFullName() << "\"), "
1258 << "LinkageName(\"" << getLinkageName() << "\"), "
1259 << "File(" << getFile() << "),"
1260 << "Line(" << getLine() << "),"
1261 << "Type(" << getType() << "), "
1262 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1263 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1264 << "Global(" << Global << ")\n";
1265}
1266#endif
1267
1268//===----------------------------------------------------------------------===//
1269
1270SubprogramDesc::SubprogramDesc()
1271: GlobalDesc(DW_TAG_subprogram)
1272{}
1273
1274// Implement isa/cast/dyncast.
1275bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1276 return D->getTag() == DW_TAG_subprogram;
1277}
1278
1279/// ApplyToFields - Target the visitor to the fields of the
1280/// SubprogramDesc.
1281void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1282 GlobalDesc::ApplyToFields(Visitor);
1283}
1284
1285/// getDescString - Return a string used to compose global names and labels.
1286///
1287const char *SubprogramDesc::getDescString() const {
1288 return "llvm.dbg.subprogram";
1289}
1290
1291/// getTypeString - Return a string used to label this descriptors type.
1292///
1293const char *SubprogramDesc::getTypeString() const {
1294 return "llvm.dbg.subprogram.type";
1295}
1296
1297/// getAnchorString - Return a string used to label this descriptor's anchor.
1298///
1299const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1300const char *SubprogramDesc::getAnchorString() const {
1301 return AnchorString;
1302}
1303
1304#ifndef NDEBUG
1305void SubprogramDesc::dump() {
1306 cerr << getDescString() << " "
1307 << "Version(" << getVersion() << "), "
1308 << "Tag(" << getTag() << "), "
1309 << "Anchor(" << getAnchor() << "), "
1310 << "Name(\"" << getName() << "\"), "
1311 << "FullName(\"" << getFullName() << "\"), "
1312 << "LinkageName(\"" << getLinkageName() << "\"), "
1313 << "File(" << getFile() << "),"
1314 << "Line(" << getLine() << "),"
1315 << "Type(" << getType() << "), "
1316 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1317 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1318}
1319#endif
1320
1321//===----------------------------------------------------------------------===//
1322
1323BlockDesc::BlockDesc()
1324: DebugInfoDesc(DW_TAG_lexical_block)
1325, Context(NULL)
1326{}
1327
1328// Implement isa/cast/dyncast.
1329bool BlockDesc::classof(const DebugInfoDesc *D) {
1330 return D->getTag() == DW_TAG_lexical_block;
1331}
1332
1333/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1334///
1335void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1336 DebugInfoDesc::ApplyToFields(Visitor);
1337
1338 Visitor->Apply(Context);
1339}
1340
1341/// getDescString - Return a string used to compose global names and labels.
1342///
1343const char *BlockDesc::getDescString() const {
1344 return "llvm.dbg.block";
1345}
1346
1347/// getTypeString - Return a string used to label this descriptors type.
1348///
1349const char *BlockDesc::getTypeString() const {
1350 return "llvm.dbg.block.type";
1351}
1352
1353#ifndef NDEBUG
1354void BlockDesc::dump() {
1355 cerr << getDescString() << " "
1356 << "Version(" << getVersion() << "), "
1357 << "Tag(" << getTag() << "),"
1358 << "Context(" << Context << ")\n";
1359}
1360#endif
1361
1362//===----------------------------------------------------------------------===//
1363
Jim Laskey86cbdba2006-02-06 15:33:21 +00001364DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001365 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001366}
1367DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001368 // Handle NULL.
1369 if (!GV) return NULL;
1370
Jim Laskey86cbdba2006-02-06 15:33:21 +00001371 // Check to see if it has been already deserialized.
1372 DebugInfoDesc *&Slot = GlobalDescs[GV];
1373 if (Slot) return Slot;
1374
1375 // Get the Tag from the global.
1376 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1377
Jim Laskey86cbdba2006-02-06 15:33:21 +00001378 // Create an empty instance of the correct sort.
1379 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001380
Jim Laskey21407982006-03-14 18:37:57 +00001381 // If not a user defined descriptor.
1382 if (Slot) {
1383 // Deserialize the fields.
1384 DIDeserializeVisitor DRAM(*this, GV);
1385 DRAM.ApplyToFields(Slot);
1386 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001387
1388 return Slot;
1389}
1390
1391//===----------------------------------------------------------------------===//
1392
1393/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001394///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001395const PointerType *DISerializer::getStrPtrType() {
1396 // If not already defined.
1397 if (!StrPtrTy) {
1398 // Construct the pointer to signed bytes.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001399 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001400 }
1401
1402 return StrPtrTy;
1403}
1404
1405/// getEmptyStructPtrType - Return a "{ }*" type.
1406///
1407const PointerType *DISerializer::getEmptyStructPtrType() {
1408 // If not already defined.
Bill Wendling914c9702008-06-27 01:27:56 +00001409 if (EmptyStructPtrTy) return EmptyStructPtrTy;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001410
Bill Wendling914c9702008-06-27 01:27:56 +00001411 // Construct the pointer to empty structure type.
Bill Wendling49255672008-07-07 21:41:57 +00001412 const StructType *EmptyStructTy = StructType::get(NULL, NULL);
Bill Wendling0ac1b6d2008-06-27 01:32:08 +00001413
1414 // Construct the pointer to empty structure type.
1415 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001416 return EmptyStructPtrTy;
1417}
1418
1419/// getTagType - Return the type describing the specified descriptor (via tag.)
1420///
1421const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1422 // Attempt to get the previously defined type.
1423 StructType *&Ty = TagTypes[DD->getTag()];
1424
1425 // If not already defined.
1426 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001427 // Set up fields vector.
1428 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001429 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001430 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001431 GTAM.ApplyToFields(DD);
1432
1433 // Construct structured type.
1434 Ty = StructType::get(Fields);
1435
Jim Laskey86cbdba2006-02-06 15:33:21 +00001436 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001437 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001438 }
1439
1440 return Ty;
1441}
1442
1443/// getString - Construct the string as constant string global.
1444///
Jim Laskeyce72b172006-02-11 01:01:30 +00001445Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001446 // Check string cache for previous edition.
Bill Wendling667a68b2008-07-07 20:59:31 +00001447 Constant *&Slot = StringCache[String];
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001448
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001449 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001450 if (Slot) return Slot;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001451
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001452 // If empty string then use a sbyte* null instead.
1453 if (String.empty()) {
1454 Slot = ConstantPointerNull::get(getStrPtrType());
1455 } else {
1456 // Construct string as an llvm constant.
1457 Constant *ConstStr = ConstantArray::get(String);
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001458
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001459 // Otherwise create and return a new string global.
1460 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1461 GlobalVariable::InternalLinkage,
Devang Patel1e4c23a2007-05-11 23:14:43 +00001462 ConstStr, ".str", M);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001463 StrGV->setSection("llvm.metadata");
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001464
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001465 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001466 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001467 }
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001468
Jim Laskeyce72b172006-02-11 01:01:30 +00001469 return Slot;
1470
Jim Laskey86cbdba2006-02-06 15:33:21 +00001471}
1472
1473/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1474/// so that it can be serialized to a .bc or .ll file.
1475GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1476 // Check if the DebugInfoDesc is already in the map.
1477 GlobalVariable *&Slot = DescGlobals[DD];
1478
1479 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1480 if (Slot) return Slot;
1481
Jim Laskey86cbdba2006-02-06 15:33:21 +00001482 // Get the type associated with the Tag.
1483 const StructType *Ty = getTagType(DD);
1484
1485 // Create the GlobalVariable early to prevent infinite recursion.
Bill Wendling10fff602008-07-03 22:53:42 +00001486 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1487 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001488 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001489
1490 // Insert new GlobalVariable in DescGlobals map.
1491 Slot = GV;
1492
1493 // Set up elements vector
1494 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001495 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001496 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001497 SRAM.ApplyToFields(DD);
Bill Wendling10fff602008-07-03 22:53:42 +00001498
Jim Laskey86cbdba2006-02-06 15:33:21 +00001499 // Set the globals initializer.
1500 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1501
1502 return GV;
1503}
1504
Devang Patel962e0752007-11-30 00:51:33 +00001505/// addDescriptor - Directly connect DD with existing GV.
1506void DISerializer::addDescriptor(DebugInfoDesc *DD,
1507 GlobalVariable *GV) {
1508 DescGlobals[DD] = GV;
1509}
1510
Jim Laskey86cbdba2006-02-06 15:33:21 +00001511//===----------------------------------------------------------------------===//
1512
Jim Laskey86cbdba2006-02-06 15:33:21 +00001513/// Verify - Return true if the GlobalVariable appears to be a valid
1514/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001515bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001516 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001517}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001518bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001519 // NULLs are valid.
1520 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001521
Jim Laskey98e04102006-03-26 22:45:20 +00001522 // Check prior validity.
1523 unsigned &ValiditySlot = Validity[GV];
1524
1525 // If visited before then use old state.
1526 if (ValiditySlot) return ValiditySlot == Valid;
1527
1528 // Assume validity for the time being (recursion.)
1529 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001530
1531 // Make sure the global is internal or link once (anchor.)
1532 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1533 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1534 ValiditySlot = Invalid;
1535 return false;
1536 }
Jim Laskey98e04102006-03-26 22:45:20 +00001537
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001538 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001539 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001540
1541 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001542 if (Tag == DW_TAG_invalid) {
1543 ValiditySlot = Valid;
1544 return true;
1545 }
1546
1547 // Get the Version.
1548 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1549
1550 // Check for version mismatch.
1551 if (Version != LLVMDebugVersion) {
1552 ValiditySlot = Invalid;
1553 return false;
1554 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001555
Jim Laskey86cbdba2006-02-06 15:33:21 +00001556 // Construct an empty DebugInfoDesc.
1557 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001558
1559 // Allow for user defined descriptors.
1560 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001561
1562 // Get the initializer constant.
1563 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1564
1565 // Get the operand count.
1566 unsigned N = CI->getNumOperands();
1567
1568 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001569 unsigned &CountSlot = Counts[Tag];
Bill Wendling3e30cbb2008-07-09 06:02:33 +00001570
1571 if (!CountSlot)
Jim Laskey86cbdba2006-02-06 15:33:21 +00001572 // Check the operand count to the field count
Bill Wendling3e30cbb2008-07-09 06:02:33 +00001573 CountSlot = CountFields(DD);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001574
Jim Laskey21407982006-03-14 18:37:57 +00001575 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001576 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001577 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001578 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001579 return false;
1580 }
1581
1582 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001583 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001584 VRAM.ApplyToFields(DD);
1585
1586 // Release empty DebugInfoDesc.
1587 delete DD;
1588
Jim Laskey98e04102006-03-26 22:45:20 +00001589 // If fields are not valid.
1590 if (!VRAM.isValid()) {
1591 ValiditySlot = Invalid;
1592 return false;
1593 }
1594
1595 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001596}
1597
Evan Chenga844bde2008-02-02 04:07:54 +00001598/// isVerified - Return true if the specified GV has already been
1599/// verified as a debug information descriptor.
1600bool DIVerifier::isVerified(GlobalVariable *GV) {
1601 unsigned &ValiditySlot = Validity[GV];
1602 if (ValiditySlot) return ValiditySlot == Valid;
1603 return false;
1604}
1605
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001606//===----------------------------------------------------------------------===//
1607
Jim Laskeyb8509c52006-03-23 18:07:55 +00001608DebugScope::~DebugScope() {
Bill Wendling10fff602008-07-03 22:53:42 +00001609 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1610 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
Jim Laskeyb8509c52006-03-23 18:07:55 +00001611}
1612
1613//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001614
Jim Laskey6da18642007-01-26 21:38:26 +00001615MachineModuleInfo::MachineModuleInfo()
Dan Gohmanae73dc12008-09-04 17:05:41 +00001616: ImmutablePass(&ID)
Devang Patel794fd752007-05-01 21:15:47 +00001617, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001618, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001619, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001620, Directories()
1621, SourceFiles()
1622, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001623, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001624, ScopeMap()
1625, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001626, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001627, LandingPads()
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001628, Personalities()
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001629, CallsEHReturn(0)
1630, CallsUnwindInit(0)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001631{
1632 // Always emit "no personality" info
1633 Personalities.push_back(NULL);
1634}
Jim Laskey6da18642007-01-26 21:38:26 +00001635MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001636
1637}
1638
Jim Laskey6da18642007-01-26 21:38:26 +00001639/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001640///
Jim Laskey6da18642007-01-26 21:38:26 +00001641bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001642 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001643}
1644
Jim Laskey6da18642007-01-26 21:38:26 +00001645/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001646///
Jim Laskey6da18642007-01-26 21:38:26 +00001647bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001648 return false;
1649}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001650
Jim Laskey6da18642007-01-26 21:38:26 +00001651/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001652///
Jim Laskey6da18642007-01-26 21:38:26 +00001653void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001654 // Coming soon.
1655}
1656
Jim Laskey6da18642007-01-26 21:38:26 +00001657/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001658///
Jim Laskey6da18642007-01-26 21:38:26 +00001659void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001660 // Clean up scope information.
1661 if (RootScope) {
1662 delete RootScope;
1663 ScopeMap.clear();
1664 RootScope = NULL;
1665 }
1666
Jim Laskeyb82313f2007-02-01 16:31:34 +00001667 // Clean up line info.
1668 Lines.clear();
1669
Jim Laskey41886992006-04-07 16:34:46 +00001670 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001671 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001672
1673 // Clean up exception info.
1674 LandingPads.clear();
1675 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +00001676 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +00001677 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001678 CallsEHReturn = 0;
1679 CallsUnwindInit = 0;
Jim Laskey41886992006-04-07 16:34:46 +00001680}
1681
Jim Laskeyd96185a2006-02-13 12:50:39 +00001682/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001683///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001684// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001685DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001686 return DR.Deserialize(V);
1687}
1688
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001689/// AnalyzeModule - Scan the module for global debug information.
1690///
Jim Laskey6da18642007-01-26 21:38:26 +00001691void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001692 SetupCompileUnits(M);
Dale Johannesen48ae02f2008-01-16 19:59:28 +00001693
1694 // Insert functions in the llvm.used array into UsedFunctions.
1695 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1696 if (!GV || !GV->hasInitializer()) return;
1697
1698 // Should be an array of 'i8*'.
1699 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1700 if (InitList == 0) return;
1701
1702 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1703 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1704 if (CE->getOpcode() == Instruction::BitCast)
1705 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1706 UsedFunctions.insert(F);
1707 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001708}
1709
1710/// SetupCompileUnits - Set up the unique vector of compile units.
1711///
Jim Laskey6da18642007-01-26 21:38:26 +00001712void MachineModuleInfo::SetupCompileUnits(Module &M) {
Bill Wendlingc04f4652008-07-03 23:13:02 +00001713 std::vector<CompileUnitDesc *> CU;
1714 getAnchoredDescriptors<CompileUnitDesc>(M, CU);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001715
Bill Wendling10fff602008-07-03 22:53:42 +00001716 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1717 CompileUnits.insert(CU[i]);
1718 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001719}
1720
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001721/// getCompileUnits - Return a vector of debug compile units.
1722///
Jim Laskey6da18642007-01-26 21:38:26 +00001723const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001724 return CompileUnits;
1725}
1726
Jim Laskey0420f2a2006-02-22 19:02:11 +00001727/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1728/// named GlobalVariable.
Bill Wendlingc04f4652008-07-03 23:13:02 +00001729void
Jim Laskey6da18642007-01-26 21:38:26 +00001730MachineModuleInfo::getGlobalVariablesUsing(Module &M,
Bill Wendlingc04f4652008-07-03 23:13:02 +00001731 const std::string &RootName,
1732 std::vector<GlobalVariable*>&Result){
1733 return ::getGlobalVariablesUsing(M, RootName, Result);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001734}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001735
Evan Chenga647c922008-02-01 02:05:57 +00001736/// RecordSourceLine - Records location information and associates it with a
Jim Laskeyb8509c52006-03-23 18:07:55 +00001737/// debug label. Returns a unique label ID used to generate a label and
1738/// provide correspondence to the source line list.
Evan Chenga647c922008-02-01 02:05:57 +00001739unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1740 unsigned Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001741 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001742 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001743 return ID;
1744}
1745
1746/// RecordSource - Register a source file with debug info. Returns an source
1747/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001748unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1749 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001750 unsigned DirectoryID = Directories.insert(Directory);
1751 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1752}
Jim Laskey6da18642007-01-26 21:38:26 +00001753unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001754 return RecordSource(CompileUnit->getDirectory(),
1755 CompileUnit->getFileName());
1756}
1757
1758/// RecordRegionStart - Indicate the start of a region.
1759///
Jim Laskey6da18642007-01-26 21:38:26 +00001760unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001761 // FIXME - need to be able to handle split scopes because of bb cloning.
1762 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1763 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1764 unsigned ID = NextLabelID();
1765 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1766 return ID;
1767}
1768
1769/// RecordRegionEnd - Indicate the end of a region.
1770///
Jim Laskey6da18642007-01-26 21:38:26 +00001771unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001772 // FIXME - need to be able to handle split scopes because of bb cloning.
1773 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1774 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1775 unsigned ID = NextLabelID();
1776 Scope->setEndLabelID(ID);
1777 return ID;
1778}
1779
1780/// RecordVariable - Indicate the declaration of a local variable.
1781///
Evan Chenga844bde2008-02-02 04:07:54 +00001782void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1783 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001784 DebugScope *Scope = getOrCreateScope(VD->getContext());
1785 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1786 Scope->AddVariable(DV);
1787}
1788
1789/// getOrCreateScope - Returns the scope associated with the given descriptor.
1790///
Jim Laskey6da18642007-01-26 21:38:26 +00001791DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001792 DebugScope *&Slot = ScopeMap[ScopeDesc];
1793 if (!Slot) {
1794 // FIXME - breaks down when the context is an inlined function.
1795 DebugInfoDesc *ParentDesc = NULL;
1796 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1797 ParentDesc = Block->getContext();
1798 }
1799 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1800 Slot = new DebugScope(Parent, ScopeDesc);
1801 if (Parent) {
1802 Parent->AddScope(Slot);
1803 } else if (RootScope) {
1804 // FIXME - Add inlined function scopes to the root so we can delete
1805 // them later. Long term, handle inlined functions properly.
1806 RootScope->AddScope(Slot);
1807 } else {
1808 // First function is top level function.
1809 RootScope = Slot;
1810 }
1811 }
1812 return Slot;
1813}
1814
Jim Laskey59667fe2007-02-21 22:38:31 +00001815//===-EH-------------------------------------------------------------------===//
1816
1817/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1818/// specified MachineBasicBlock.
Bill Wendling10fff602008-07-03 22:53:42 +00001819LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1820 (MachineBasicBlock *LandingPad) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001821 unsigned N = LandingPads.size();
1822 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001823 LandingPadInfo &LP = LandingPads[i];
1824 if (LP.LandingPadBlock == LandingPad)
1825 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001826 }
1827
1828 LandingPads.push_back(LandingPadInfo(LandingPad));
1829 return LandingPads[N];
1830}
1831
1832/// addInvoke - Provide the begin and end labels of an invoke style call and
1833/// associate it with a try landing pad block.
1834void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1835 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001836 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001837 LP.BeginLabels.push_back(BeginLabel);
1838 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +00001839}
1840
1841/// addLandingPad - Provide the label of a try LandingPad block.
1842///
1843unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1844 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001845 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1846 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001847 return LandingPadLabel;
1848}
1849
1850/// addPersonality - Provide the personality function for the exception
1851/// information.
1852void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001853 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001854 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001855 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001856
Bill Wendling10fff602008-07-03 22:53:42 +00001857 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001858 if (Personalities[i] == Personality)
1859 return;
1860
1861 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +00001862}
1863
1864/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1865///
1866void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1867 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001868 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001869 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001870 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001871}
Duncan Sands73ef58a2007-06-02 16:53:42 +00001872
1873/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +00001874///
Duncan Sands73ef58a2007-06-02 16:53:42 +00001875void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1876 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001877 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling49255672008-07-07 21:41:57 +00001878 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling10fff602008-07-03 22:53:42 +00001879 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001880 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1881 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +00001882}
1883
Duncan Sands6590b042007-08-27 15:47:50 +00001884/// addCleanup - Add a cleanup action for a landing pad.
1885///
1886void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1887 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1888 LP.TypeIds.push_back(0);
1889}
1890
Jim Laskey59667fe2007-02-21 22:38:31 +00001891/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1892/// pads.
1893void MachineModuleInfo::TidyLandingPads() {
1894 for (unsigned i = 0; i != LandingPads.size(); ) {
1895 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001896 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001897
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001898 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +00001899 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001900 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001901 LandingPads.erase(LandingPads.begin() + i);
1902 continue;
1903 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001904
Bill Wendling10fff602008-07-03 22:53:42 +00001905 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001906 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1907 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands57810cd2007-09-05 11:27:52 +00001908
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001909 if (!BeginLabel || !EndLabel) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001910 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1911 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1912 continue;
1913 }
1914
1915 LandingPad.BeginLabels[j] = BeginLabel;
1916 LandingPad.EndLabels[j] = EndLabel;
1917 ++j;
1918 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001919
1920 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +00001921 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +00001922 LandingPads.erase(LandingPads.begin() + i);
1923 continue;
1924 }
1925
1926 // If there is no landing pad, ensure that the list of typeids is empty.
1927 // If the only typeid is a cleanup, this is the same as having no typeids.
1928 if (!LandingPad.LandingPadBlock ||
1929 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1930 LandingPad.TypeIds.clear();
1931
Jim Laskey59667fe2007-02-21 22:38:31 +00001932 ++i;
1933 }
1934}
1935
1936/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1937/// function wide.
1938unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling10fff602008-07-03 22:53:42 +00001939 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1940 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey59667fe2007-02-21 22:38:31 +00001941
1942 TypeInfos.push_back(TI);
1943 return TypeInfos.size();
1944}
1945
Duncan Sands73ef58a2007-06-02 16:53:42 +00001946/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1947/// function wide.
Bill Wendling914c9702008-06-27 01:27:56 +00001948int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +00001949 // If the new filter coincides with the tail of an existing filter, then
1950 // re-use the existing filter. Folding filters more than this requires
1951 // re-ordering filters and/or their elements - probably not worth it.
1952 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1953 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling914c9702008-06-27 01:27:56 +00001954 unsigned i = *I, j = TyIds.size();
Duncan Sands14da32a2007-07-05 15:15:01 +00001955
1956 while (i && j)
1957 if (FilterIds[--i] != TyIds[--j])
1958 goto try_next;
1959
1960 if (!j)
1961 // The new filter coincides with range [i, end) of the existing filter.
1962 return -(1 + i);
Bill Wendling914c9702008-06-27 01:27:56 +00001963
Duncan Sands14da32a2007-07-05 15:15:01 +00001964try_next:;
1965 }
1966
1967 // Add the new filter.
Bill Wendling914c9702008-06-27 01:27:56 +00001968 int FilterID = -(1 + FilterIds.size());
1969 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1970 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001971 FilterIds.push_back(TyIds[I]);
Bill Wendling914c9702008-06-27 01:27:56 +00001972 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +00001973 FilterIds.push_back(0); // terminator
1974 return FilterID;
1975}
1976
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001977/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +00001978Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001979 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001980 // function
1981 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +00001982}
1983
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001984/// getPersonalityIndex - Return unique index for current personality
1985/// function. NULL personality function should always get zero index.
1986unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001987 const Function* Personality = NULL;
1988
1989 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling10fff602008-07-03 22:53:42 +00001990 for (unsigned i = 0; i != LandingPads.size(); ++i)
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001991 if (LandingPads[i].Personality) {
1992 Personality = LandingPads[i].Personality;
1993 break;
1994 }
1995
Bill Wendling10fff602008-07-03 22:53:42 +00001996 for (unsigned i = 0; i < Personalities.size(); ++i) {
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001997 if (Personalities[i] == Personality)
1998 return i;
Bill Wendling10fff602008-07-03 22:53:42 +00001999 }
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002000
2001 // This should never happen
2002 assert(0 && "Personality function should be set!");
2003 return 0;
2004}
Jim Laskey59667fe2007-02-21 22:38:31 +00002005
Jim Laskey9d4209f2006-11-07 19:33:46 +00002006//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00002007/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
2008/// a info consumer to determine if the range of two labels is empty, by seeing
2009/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002010
2011namespace llvm {
2012
2013struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00002014 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +00002015 DebugLabelFolder() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +00002016
Evan Chengbbeeb2a2008-09-22 20:58:04 +00002017 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Evan Cheng8b56a902008-09-22 22:21:38 +00002018 AU.addPreservedID(MachineLoopInfoID);
2019 AU.addPreservedID(MachineDominatorsID);
Evan Chengbbeeb2a2008-09-22 20:58:04 +00002020 MachineFunctionPass::getAnalysisUsage(AU);
2021 }
2022
Jim Laskey9d4209f2006-11-07 19:33:46 +00002023 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00002024 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00002025};
2026
Devang Patel19974732007-05-03 01:11:54 +00002027char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00002028
Jim Laskey9d4209f2006-11-07 19:33:46 +00002029bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00002030 // Get machine module info.
2031 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
2032 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002033
2034 // Track if change is made.
2035 bool MadeChange = false;
2036 // No prior label to begin.
2037 unsigned PriorLabel = 0;
2038
2039 // Iterate through basic blocks.
2040 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
2041 BB != E; ++BB) {
2042 // Iterate through instructions.
2043 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00002044 // Is it a label.
Evan Chengbb81d972008-01-31 09:59:15 +00002045 if (I->isDebugLabel()) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002046 // The label ID # is always operand #0, an immediate.
2047 unsigned NextLabel = I->getOperand(0).getImm();
2048
2049 // If there was an immediate prior label.
2050 if (PriorLabel) {
2051 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00002052 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002053 // Delete the current label.
2054 I = BB->erase(I);
2055 // Indicate a change has been made.
2056 MadeChange = true;
2057 continue;
2058 } else {
2059 // Start a new round.
2060 PriorLabel = NextLabel;
2061 }
2062 } else {
2063 // No consecutive labels.
2064 PriorLabel = 0;
2065 }
2066
2067 ++I;
2068 }
2069 }
2070
2071 return MadeChange;
2072}
2073
2074FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
2075
2076}
Bill Wendling10fff602008-07-03 22:53:42 +00002077