blob: bdd771bb2b1e9914dff8eb5a68d13ff389366cfd [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/CodeGen/MachineModuleInfo.h"
11
12#include "llvm/Constants.h"
Evan Cheng833501d2008-06-30 07:31:25 +000013#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineLocation.h"
Bill Wendlinge15b1192008-06-27 00:09:40 +000017#include "llvm/CodeGen/MachineDebugInfoDesc.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Target/TargetInstrInfo.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetOptions.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/GlobalVariable.h"
23#include "llvm/Intrinsics.h"
24#include "llvm/Instructions.h"
25#include "llvm/Module.h"
26#include "llvm/Support/Dwarf.h"
27#include "llvm/Support/Streams.h"
28using namespace llvm;
29using namespace llvm::dwarf;
30
31// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman089efff2008-05-13 00:00:25 +000032static RegisterPass<MachineModuleInfo>
33X("machinemoduleinfo", "Module Information");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034char MachineModuleInfo::ID = 0;
35
36//===----------------------------------------------------------------------===//
37
38/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
39/// specified value in their initializer somewhere.
40static void
Bill Wendlingff424a92008-06-27 01:32:08 +000041getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
Dan Gohmanf17a25c2007-07-18 16:29: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)) {
45 // If the user is a GlobalVariable then add to result.
46 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
54/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
55/// named GlobalVariable.
Bill Wendlinge15b1192008-06-27 00:09:40 +000056static void
57getGlobalVariablesUsing(Module &M, const std::string &RootName,
Bill Wendlingff424a92008-06-27 01:32:08 +000058 std::vector<GlobalVariable*> &Result) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 std::vector<const Type*> FieldTypes;
60 FieldTypes.push_back(Type::Int32Ty);
61 FieldTypes.push_back(Type::Int32Ty);
62
63 // Get the GlobalVariable root.
64 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
65 StructType::get(FieldTypes));
66
67 // If present and linkonce then scan for users.
Bill Wendlinge15b1192008-06-27 00:09:40 +000068 if (UseRoot && UseRoot->hasLinkOnceLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 getGlobalVariablesUsing(UseRoot, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070}
71
72/// 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
96/// getGlobalVariable - Return either a direct or cast Global value.
97///
98static GlobalVariable *getGlobalVariable(Value *V) {
99 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
100 return GV;
101 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
102 if (CE->getOpcode() == Instruction::BitCast) {
103 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Dale Johannesen3bb9df92008-01-30 19:00:21 +0000104 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
105 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesenc59be552008-01-30 19:44:39 +0000106 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen3bb9df92008-01-30 19:00:21 +0000107 return NULL;
108 }
109 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 }
111 }
112 return NULL;
113}
114
115/// isGlobalVariable - Return true if the given value can be coerced to a
116/// GlobalVariable.
117static bool isGlobalVariable(Value *V) {
118 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
119 return true;
120 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
121 if (CE->getOpcode() == Instruction::BitCast) {
122 return isa<GlobalVariable>(CE->getOperand(0));
Dale Johannesen3bb9df92008-01-30 19:00:21 +0000123 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
124 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesenc59be552008-01-30 19:44:39 +0000125 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen3bb9df92008-01-30 19:00:21 +0000126 return false;
127 }
128 return isa<GlobalVariable>(CE->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 }
130 }
131 return false;
132}
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134//===----------------------------------------------------------------------===//
135
136/// ApplyToFields - Target the visitor to each field of the debug information
137/// descriptor.
138void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
139 DD->ApplyToFields(this);
140}
141
Dan Gohman089efff2008-05-13 00:00:25 +0000142namespace {
143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144//===----------------------------------------------------------------------===//
145/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
146/// the supplied DebugInfoDesc.
147class DICountVisitor : public DIVisitor {
148private:
149 unsigned Count; // Running count of fields.
150
151public:
152 DICountVisitor() : DIVisitor(), Count(0) {}
153
154 // Accessors.
155 unsigned getCount() const { return Count; }
156
157 /// Apply - Count each of the fields.
158 ///
159 virtual void Apply(int &Field) { ++Count; }
160 virtual void Apply(unsigned &Field) { ++Count; }
161 virtual void Apply(int64_t &Field) { ++Count; }
162 virtual void Apply(uint64_t &Field) { ++Count; }
163 virtual void Apply(bool &Field) { ++Count; }
164 virtual void Apply(std::string &Field) { ++Count; }
165 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
166 virtual void Apply(GlobalVariable *&Field) { ++Count; }
167 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
168 ++Count;
169 }
170};
171
172//===----------------------------------------------------------------------===//
173/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
174/// supplied DebugInfoDesc.
175class DIDeserializeVisitor : public DIVisitor {
176private:
177 DIDeserializer &DR; // Active deserializer.
178 unsigned I; // Current operand index.
179 ConstantStruct *CI; // GlobalVariable constant initializer.
180
181public:
182 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
Bill Wendling0e679b82008-06-27 01:27:56 +0000183 : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 {}
185
186 /// Apply - Set the value of each of the fields.
187 ///
188 virtual void Apply(int &Field) {
189 Constant *C = CI->getOperand(I++);
190 Field = cast<ConstantInt>(C)->getSExtValue();
191 }
192 virtual void Apply(unsigned &Field) {
193 Constant *C = CI->getOperand(I++);
194 Field = cast<ConstantInt>(C)->getZExtValue();
195 }
196 virtual void Apply(int64_t &Field) {
197 Constant *C = CI->getOperand(I++);
198 Field = cast<ConstantInt>(C)->getSExtValue();
199 }
200 virtual void Apply(uint64_t &Field) {
201 Constant *C = CI->getOperand(I++);
202 Field = cast<ConstantInt>(C)->getZExtValue();
203 }
204 virtual void Apply(bool &Field) {
205 Constant *C = CI->getOperand(I++);
206 Field = cast<ConstantInt>(C)->getZExtValue();
207 }
208 virtual void Apply(std::string &Field) {
209 Constant *C = CI->getOperand(I++);
Evan Cheng833501d2008-06-30 07:31:25 +0000210 // Fills in the string if it succeeds
211 if (!GetConstantStringInfo(C, Field))
212 Field.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 }
214 virtual void Apply(DebugInfoDesc *&Field) {
215 Constant *C = CI->getOperand(I++);
216 Field = DR.Deserialize(C);
217 }
218 virtual void Apply(GlobalVariable *&Field) {
219 Constant *C = CI->getOperand(I++);
220 Field = getGlobalVariable(C);
221 }
222 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
223 Field.resize(0);
224 Constant *C = CI->getOperand(I++);
225 GlobalVariable *GV = getGlobalVariable(C);
226 if (GV->hasInitializer()) {
227 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
228 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
229 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
230 DebugInfoDesc *DE = DR.Deserialize(GVE);
231 Field.push_back(DE);
232 }
233 } else if (GV->getInitializer()->isNullValue()) {
234 if (const ArrayType *T =
235 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
236 Field.resize(T->getNumElements());
237 }
238 }
239 }
240 }
241};
242
243//===----------------------------------------------------------------------===//
244/// DISerializeVisitor - This DIVisitor serializes all the fields in
245/// the supplied DebugInfoDesc.
246class DISerializeVisitor : public DIVisitor {
247private:
248 DISerializer &SR; // Active serializer.
249 std::vector<Constant*> &Elements; // Element accumulator.
250
251public:
252 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
Bill Wendling271b7322008-07-02 00:50:02 +0000253 : DIVisitor(), SR(S), Elements(E) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254
255 /// Apply - Set the value of each of the fields.
256 ///
257 virtual void Apply(int &Field) {
258 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
259 }
260 virtual void Apply(unsigned &Field) {
261 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
262 }
263 virtual void Apply(int64_t &Field) {
264 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
265 }
266 virtual void Apply(uint64_t &Field) {
267 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
268 }
269 virtual void Apply(bool &Field) {
270 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
271 }
272 virtual void Apply(std::string &Field) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000273 Elements.push_back(SR.getString(Field));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 }
275 virtual void Apply(DebugInfoDesc *&Field) {
276 GlobalVariable *GV = NULL;
277
278 // If non-NULL then convert to global.
279 if (Field) GV = SR.Serialize(Field);
280
281 // FIXME - At some point should use specific type.
282 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
283
284 if (GV) {
285 // Set to pointer to global.
286 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
287 } else {
288 // Use NULL.
289 Elements.push_back(ConstantPointerNull::get(EmptyTy));
290 }
291 }
292 virtual void Apply(GlobalVariable *&Field) {
293 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
294 if (Field) {
295 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
296 } else {
297 Elements.push_back(ConstantPointerNull::get(EmptyTy));
298 }
299 }
300 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
301 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
302 unsigned N = Field.size();
303 ArrayType *AT = ArrayType::get(EmptyTy, N);
304 std::vector<Constant *> ArrayElements;
305
Bill Wendling266486d2008-06-27 07:13:44 +0000306 for (unsigned i = 0; i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 if (DebugInfoDesc *Element = Field[i]) {
308 GlobalVariable *GVE = SR.Serialize(Element);
309 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
310 ArrayElements.push_back(cast<Constant>(CE));
311 } else {
312 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
313 }
314 }
315
316 Constant *CA = ConstantArray::get(AT, ArrayElements);
317 GlobalVariable *CAGV = new GlobalVariable(AT, true,
318 GlobalValue::InternalLinkage,
319 CA, "llvm.dbg.array",
320 SR.getModule());
321 CAGV->setSection("llvm.metadata");
322 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
323 Elements.push_back(CAE);
324 }
325};
326
327//===----------------------------------------------------------------------===//
328/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
329/// the supplied DebugInfoDesc.
330class DIGetTypesVisitor : public DIVisitor {
331private:
332 DISerializer &SR; // Active serializer.
333 std::vector<const Type*> &Fields; // Type accumulator.
334
335public:
336 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
Bill Wendling271b7322008-07-02 00:50:02 +0000337 : DIVisitor(), SR(S), Fields(F) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 /// Apply - Set the value of each of the fields.
340 ///
341 virtual void Apply(int &Field) {
342 Fields.push_back(Type::Int32Ty);
343 }
344 virtual void Apply(unsigned &Field) {
345 Fields.push_back(Type::Int32Ty);
346 }
347 virtual void Apply(int64_t &Field) {
348 Fields.push_back(Type::Int64Ty);
349 }
350 virtual void Apply(uint64_t &Field) {
351 Fields.push_back(Type::Int64Ty);
352 }
353 virtual void Apply(bool &Field) {
354 Fields.push_back(Type::Int1Ty);
355 }
356 virtual void Apply(std::string &Field) {
357 Fields.push_back(SR.getStrPtrType());
358 }
359 virtual void Apply(DebugInfoDesc *&Field) {
360 // FIXME - At some point should use specific type.
361 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
362 Fields.push_back(EmptyTy);
363 }
364 virtual void Apply(GlobalVariable *&Field) {
365 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
366 Fields.push_back(EmptyTy);
367 }
368 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
369 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
370 Fields.push_back(EmptyTy);
371 }
372};
373
374//===----------------------------------------------------------------------===//
375/// DIVerifyVisitor - This DIVisitor verifies all the field types against
376/// a constant initializer.
377class DIVerifyVisitor : public DIVisitor {
378private:
379 DIVerifier &VR; // Active verifier.
380 bool IsValid; // Validity status.
381 unsigned I; // Current operand index.
382 ConstantStruct *CI; // GlobalVariable constant initializer.
383
384public:
385 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
386 : DIVisitor()
387 , VR(V)
388 , IsValid(true)
389 , I(0)
390 , CI(cast<ConstantStruct>(GV->getInitializer()))
391 {
392 }
393
394 // Accessors.
395 bool isValid() const { return IsValid; }
396
397 /// Apply - Set the value of each of the fields.
398 ///
399 virtual void Apply(int &Field) {
400 Constant *C = CI->getOperand(I++);
401 IsValid = IsValid && isa<ConstantInt>(C);
402 }
403 virtual void Apply(unsigned &Field) {
404 Constant *C = CI->getOperand(I++);
405 IsValid = IsValid && isa<ConstantInt>(C);
406 }
407 virtual void Apply(int64_t &Field) {
408 Constant *C = CI->getOperand(I++);
409 IsValid = IsValid && isa<ConstantInt>(C);
410 }
411 virtual void Apply(uint64_t &Field) {
412 Constant *C = CI->getOperand(I++);
413 IsValid = IsValid && isa<ConstantInt>(C);
414 }
415 virtual void Apply(bool &Field) {
416 Constant *C = CI->getOperand(I++);
417 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
418 }
419 virtual void Apply(std::string &Field) {
420 Constant *C = CI->getOperand(I++);
421 IsValid = IsValid &&
422 (!C || isStringValue(C) || C->isNullValue());
423 }
424 virtual void Apply(DebugInfoDesc *&Field) {
425 // FIXME - Prepare the correct descriptor.
426 Constant *C = CI->getOperand(I++);
427 IsValid = IsValid && isGlobalVariable(C);
428 }
429 virtual void Apply(GlobalVariable *&Field) {
430 Constant *C = CI->getOperand(I++);
431 IsValid = IsValid && isGlobalVariable(C);
432 }
433 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
434 Constant *C = CI->getOperand(I++);
435 IsValid = IsValid && isGlobalVariable(C);
436 if (!IsValid) return;
437
438 GlobalVariable *GV = getGlobalVariable(C);
439 IsValid = IsValid && GV && GV->hasInitializer();
440 if (!IsValid) return;
441
442 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
443 IsValid = IsValid && CA;
444 if (!IsValid) return;
445
446 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
447 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
448 if (!IsValid) return;
449
450 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
451 VR.Verify(GVE);
452 }
453 }
454};
455
Dan Gohman089efff2008-05-13 00:00:25 +0000456}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457
458//===----------------------------------------------------------------------===//
459
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
461 return Deserialize(getGlobalVariable(V));
462}
463DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
464 // Handle NULL.
465 if (!GV) return NULL;
466
467 // Check to see if it has been already deserialized.
468 DebugInfoDesc *&Slot = GlobalDescs[GV];
469 if (Slot) return Slot;
470
471 // Get the Tag from the global.
472 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
473
474 // Create an empty instance of the correct sort.
475 Slot = DebugInfoDesc::DescFactory(Tag);
476
477 // If not a user defined descriptor.
478 if (Slot) {
479 // Deserialize the fields.
480 DIDeserializeVisitor DRAM(*this, GV);
481 DRAM.ApplyToFields(Slot);
482 }
483
484 return Slot;
485}
486
487//===----------------------------------------------------------------------===//
488
489/// getStrPtrType - Return a "sbyte *" type.
490///
491const PointerType *DISerializer::getStrPtrType() {
492 // If not already defined.
493 if (!StrPtrTy) {
494 // Construct the pointer to signed bytes.
Christopher Lambbb2f2222007-12-17 01:12:55 +0000495 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 }
497
498 return StrPtrTy;
499}
500
501/// getEmptyStructPtrType - Return a "{ }*" type.
502///
503const PointerType *DISerializer::getEmptyStructPtrType() {
504 // If not already defined.
Bill Wendling0e679b82008-06-27 01:27:56 +0000505 if (EmptyStructPtrTy) return EmptyStructPtrTy;
Bill Wendlingecc696f2008-06-27 00:56:36 +0000506
Bill Wendling0e679b82008-06-27 01:27:56 +0000507 // Construct the pointer to empty structure type.
Bill Wendling271b7322008-07-02 00:50:02 +0000508 const StructType *EmptyStructTy = StructType::get(NULL, NULL);
Bill Wendlingff424a92008-06-27 01:32:08 +0000509
510 // Construct the pointer to empty structure type.
511 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 return EmptyStructPtrTy;
513}
514
515/// getTagType - Return the type describing the specified descriptor (via tag.)
516///
517const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
518 // Attempt to get the previously defined type.
519 StructType *&Ty = TagTypes[DD->getTag()];
520
521 // If not already defined.
522 if (!Ty) {
523 // Set up fields vector.
524 std::vector<const Type*> Fields;
Bill Wendling271b7322008-07-02 00:50:02 +0000525
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 // Get types of fields.
527 DIGetTypesVisitor GTAM(*this, Fields);
528 GTAM.ApplyToFields(DD);
529
530 // Construct structured type.
531 Ty = StructType::get(Fields);
532
533 // Register type name with module.
534 M->addTypeName(DD->getTypeString(), Ty);
535 }
536
537 return Ty;
538}
539
540/// getString - Construct the string as constant string global.
541///
542Constant *DISerializer::getString(const std::string &String) {
543 // Check string cache for previous edition.
Bill Wendling0e679b82008-06-27 01:27:56 +0000544 Constant *&Slot = StringCache[String.c_str()];
Bill Wendlingecc696f2008-06-27 00:56:36 +0000545
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 // Return Constant if previously defined.
547 if (Slot) return Slot;
Bill Wendlingecc696f2008-06-27 00:56:36 +0000548
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 // If empty string then use a sbyte* null instead.
550 if (String.empty()) {
551 Slot = ConstantPointerNull::get(getStrPtrType());
552 } else {
553 // Construct string as an llvm constant.
554 Constant *ConstStr = ConstantArray::get(String);
Bill Wendlingecc696f2008-06-27 00:56:36 +0000555
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 // Otherwise create and return a new string global.
557 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
558 GlobalVariable::InternalLinkage,
559 ConstStr, ".str", M);
560 StrGV->setSection("llvm.metadata");
Bill Wendlingecc696f2008-06-27 00:56:36 +0000561
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 // Convert to generic string pointer.
563 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
564 }
Bill Wendlingecc696f2008-06-27 00:56:36 +0000565
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 return Slot;
567
568}
569
570/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
571/// so that it can be serialized to a .bc or .ll file.
572GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
573 // Check if the DebugInfoDesc is already in the map.
574 GlobalVariable *&Slot = DescGlobals[DD];
575
576 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
577 if (Slot) return Slot;
578
579 // Get the type associated with the Tag.
580 const StructType *Ty = getTagType(DD);
581
582 // Create the GlobalVariable early to prevent infinite recursion.
Bill Wendling4a41e432008-07-01 22:08:01 +0000583 GlobalVariable *GV =
Bill Wendling9ac67a72008-07-02 00:35:47 +0000584 new GlobalVariable(Ty, true, DD->getLinkage(),
Bill Wendling4a41e432008-07-01 22:08:01 +0000585 NULL, DD->getDescString(), M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 GV->setSection("llvm.metadata");
587
588 // Insert new GlobalVariable in DescGlobals map.
589 Slot = GV;
590
591 // Set up elements vector
592 std::vector<Constant*> Elements;
Bill Wendling271b7322008-07-02 00:50:02 +0000593
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 // Add fields.
595 DISerializeVisitor SRAM(*this, Elements);
596 SRAM.ApplyToFields(DD);
597
598 // Set the globals initializer.
599 GV->setInitializer(ConstantStruct::get(Ty, Elements));
600
601 return GV;
602}
603
Devang Pateld83c2c02007-11-30 00:51:33 +0000604/// addDescriptor - Directly connect DD with existing GV.
605void DISerializer::addDescriptor(DebugInfoDesc *DD,
606 GlobalVariable *GV) {
607 DescGlobals[DD] = GV;
608}
609
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610//===----------------------------------------------------------------------===//
611
612/// Verify - Return true if the GlobalVariable appears to be a valid
613/// serialization of a DebugInfoDesc.
614bool DIVerifier::Verify(Value *V) {
615 return !V || Verify(getGlobalVariable(V));
616}
617bool DIVerifier::Verify(GlobalVariable *GV) {
618 // NULLs are valid.
619 if (!GV) return true;
620
621 // Check prior validity.
622 unsigned &ValiditySlot = Validity[GV];
623
624 // If visited before then use old state.
625 if (ValiditySlot) return ValiditySlot == Valid;
626
627 // Assume validity for the time being (recursion.)
628 ValiditySlot = Valid;
629
630 // Make sure the global is internal or link once (anchor.)
631 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
632 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
633 ValiditySlot = Invalid;
634 return false;
635 }
636
637 // Get the Tag.
638 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
639
640 // Check for user defined descriptors.
641 if (Tag == DW_TAG_invalid) {
642 ValiditySlot = Valid;
643 return true;
644 }
645
646 // Get the Version.
647 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
648
649 // Check for version mismatch.
650 if (Version != LLVMDebugVersion) {
651 ValiditySlot = Invalid;
652 return false;
653 }
654
655 // Construct an empty DebugInfoDesc.
656 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
657
658 // Allow for user defined descriptors.
659 if (!DD) return true;
660
661 // Get the initializer constant.
662 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
663
664 // Get the operand count.
665 unsigned N = CI->getNumOperands();
666
667 // Get the field count.
668 unsigned &CountSlot = Counts[Tag];
669 if (!CountSlot) {
670 // Check the operand count to the field count
671 DICountVisitor CTAM;
672 CTAM.ApplyToFields(DD);
673 CountSlot = CTAM.getCount();
674 }
675
676 // Field count must be at most equal operand count.
677 if (CountSlot > N) {
678 delete DD;
679 ValiditySlot = Invalid;
680 return false;
681 }
682
683 // Check each field for valid type.
684 DIVerifyVisitor VRAM(*this, GV);
685 VRAM.ApplyToFields(DD);
686
687 // Release empty DebugInfoDesc.
688 delete DD;
689
690 // If fields are not valid.
691 if (!VRAM.isValid()) {
692 ValiditySlot = Invalid;
693 return false;
694 }
695
696 return true;
697}
698
Evan Cheng2e28d622008-02-02 04:07:54 +0000699/// isVerified - Return true if the specified GV has already been
700/// verified as a debug information descriptor.
701bool DIVerifier::isVerified(GlobalVariable *GV) {
702 unsigned &ValiditySlot = Validity[GV];
703 if (ValiditySlot) return ValiditySlot == Valid;
704 return false;
705}
706
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707//===----------------------------------------------------------------------===//
708
709DebugScope::~DebugScope() {
Bill Wendling266486d2008-06-27 07:13:44 +0000710 for (unsigned i = 0, e = Scopes.size(); i < e; ++i) delete Scopes[i];
711 for (unsigned i = 0, e = Variables.size(); i < e; ++i) delete Variables[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712}
713
714//===----------------------------------------------------------------------===//
715
716MachineModuleInfo::MachineModuleInfo()
717: ImmutablePass((intptr_t)&ID)
718, DR()
719, VR()
720, CompileUnits()
721, Directories()
722, SourceFiles()
723, Lines()
724, LabelIDList()
725, ScopeMap()
726, RootScope(NULL)
727, FrameMoves()
728, LandingPads()
729, Personalities()
730, CallsEHReturn(0)
731, CallsUnwindInit(0)
732{
733 // Always emit "no personality" info
734 Personalities.push_back(NULL);
735}
736MachineModuleInfo::~MachineModuleInfo() {
737
738}
739
740/// doInitialization - Initialize the state for a new module.
741///
742bool MachineModuleInfo::doInitialization() {
743 return false;
744}
745
746/// doFinalization - Tear down the state after completion of a module.
747///
748bool MachineModuleInfo::doFinalization() {
749 return false;
750}
751
752/// BeginFunction - Begin gathering function meta information.
753///
754void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
755 // Coming soon.
756}
757
758/// EndFunction - Discard function meta information.
759///
760void MachineModuleInfo::EndFunction() {
761 // Clean up scope information.
762 if (RootScope) {
763 delete RootScope;
764 ScopeMap.clear();
765 RootScope = NULL;
766 }
767
768 // Clean up line info.
769 Lines.clear();
770
771 // Clean up frame info.
772 FrameMoves.clear();
773
774 // Clean up exception info.
775 LandingPads.clear();
776 TypeInfos.clear();
777 FilterIds.clear();
778 FilterEnds.clear();
779 CallsEHReturn = 0;
780 CallsUnwindInit = 0;
781}
782
783/// getDescFor - Convert a Value to a debug information descriptor.
784///
785// FIXME - use new Value type when available.
786DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
787 return DR.Deserialize(V);
788}
789
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790/// AnalyzeModule - Scan the module for global debug information.
791///
792void MachineModuleInfo::AnalyzeModule(Module &M) {
793 SetupCompileUnits(M);
Dale Johannesen3dadeb32008-01-16 19:59:28 +0000794
795 // Insert functions in the llvm.used array into UsedFunctions.
796 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
797 if (!GV || !GV->hasInitializer()) return;
798
799 // Should be an array of 'i8*'.
800 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
801 if (InitList == 0) return;
802
803 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
804 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
805 if (CE->getOpcode() == Instruction::BitCast)
806 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
807 UsedFunctions.insert(F);
808 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809}
810
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811/// SetupCompileUnits - Set up the unique vector of compile units.
812///
813void MachineModuleInfo::SetupCompileUnits(Module &M) {
Bill Wendlinge15b1192008-06-27 00:09:40 +0000814 std::vector<void*> CUList;
815 CompileUnitDesc CUD;
816 getAnchoredDescriptors(M, &CUD, CUList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817
Bill Wendling266486d2008-06-27 07:13:44 +0000818 for (unsigned i = 0, e = CUList.size(); i < e; i++)
Bill Wendlinge15b1192008-06-27 00:09:40 +0000819 CompileUnits.insert((CompileUnitDesc*)CUList[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820}
821
822/// getCompileUnits - Return a vector of debug compile units.
823///
824const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
825 return CompileUnits;
826}
827
Bill Wendlinge15b1192008-06-27 00:09:40 +0000828/// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
829///
830void
831MachineModuleInfo::getAnchoredDescriptors(Module &M, const AnchoredDesc *Desc,
832 std::vector<void*> &AnchoredDescs) {
Bill Wendlingff424a92008-06-27 01:32:08 +0000833 std::vector<GlobalVariable*> Globals;
Bill Wendlinge15b1192008-06-27 00:09:40 +0000834 getGlobalVariablesUsing(M, Desc->getAnchorString(), Globals);
835
Bill Wendling266486d2008-06-27 07:13:44 +0000836 for (unsigned i = 0, e = Globals.size(); i < e; ++i) {
Bill Wendlinge15b1192008-06-27 00:09:40 +0000837 GlobalVariable *GV = Globals[i];
838
839 // FIXME - In the short term, changes are too drastic to continue.
840 if (DebugInfoDesc::TagFromGlobal(GV) == Desc->getTag() &&
841 DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion)
842 AnchoredDescs.push_back(DR.Deserialize(GV));
843 }
844}
845
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
847/// named GlobalVariable.
Bill Wendlinge15b1192008-06-27 00:09:40 +0000848void
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849MachineModuleInfo::getGlobalVariablesUsing(Module &M,
Bill Wendlinge15b1192008-06-27 00:09:40 +0000850 const std::string &RootName,
851 std::vector<GlobalVariable*> &Globals) {
852 return ::getGlobalVariablesUsing(M, RootName, Globals);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853}
854
Evan Cheng69eda822008-02-01 02:05:57 +0000855/// RecordSourceLine - Records location information and associates it with a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856/// debug label. Returns a unique label ID used to generate a label and
857/// provide correspondence to the source line list.
Evan Cheng69eda822008-02-01 02:05:57 +0000858unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
859 unsigned Source) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 unsigned ID = NextLabelID();
861 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
862 return ID;
863}
864
865/// RecordSource - Register a source file with debug info. Returns an source
866/// ID.
867unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
868 const std::string &Source) {
869 unsigned DirectoryID = Directories.insert(Directory);
870 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
871}
872unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
873 return RecordSource(CompileUnit->getDirectory(),
874 CompileUnit->getFileName());
875}
876
877/// RecordRegionStart - Indicate the start of a region.
878///
879unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
880 // FIXME - need to be able to handle split scopes because of bb cloning.
881 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
882 DebugScope *Scope = getOrCreateScope(ScopeDesc);
883 unsigned ID = NextLabelID();
884 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
885 return ID;
886}
887
888/// RecordRegionEnd - Indicate the end of a region.
889///
890unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
891 // FIXME - need to be able to handle split scopes because of bb cloning.
892 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
893 DebugScope *Scope = getOrCreateScope(ScopeDesc);
894 unsigned ID = NextLabelID();
895 Scope->setEndLabelID(ID);
896 return ID;
897}
898
899/// RecordVariable - Indicate the declaration of a local variable.
900///
Evan Cheng2e28d622008-02-02 04:07:54 +0000901void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
902 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 DebugScope *Scope = getOrCreateScope(VD->getContext());
904 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
905 Scope->AddVariable(DV);
906}
907
908/// getOrCreateScope - Returns the scope associated with the given descriptor.
909///
910DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
911 DebugScope *&Slot = ScopeMap[ScopeDesc];
912 if (!Slot) {
913 // FIXME - breaks down when the context is an inlined function.
914 DebugInfoDesc *ParentDesc = NULL;
915 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
916 ParentDesc = Block->getContext();
917 }
918 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
919 Slot = new DebugScope(Parent, ScopeDesc);
920 if (Parent) {
921 Parent->AddScope(Slot);
922 } else if (RootScope) {
923 // FIXME - Add inlined function scopes to the root so we can delete
924 // them later. Long term, handle inlined functions properly.
925 RootScope->AddScope(Slot);
926 } else {
927 // First function is top level function.
928 RootScope = Slot;
929 }
930 }
931 return Slot;
932}
933
934//===-EH-------------------------------------------------------------------===//
935
936/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
937/// specified MachineBasicBlock.
Bill Wendling266486d2008-06-27 07:13:44 +0000938LandingPadInfo &
939MachineModuleInfo::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 unsigned N = LandingPads.size();
Bill Wendling266486d2008-06-27 07:13:44 +0000941
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 for (unsigned i = 0; i < N; ++i) {
943 LandingPadInfo &LP = LandingPads[i];
944 if (LP.LandingPadBlock == LandingPad)
945 return LP;
946 }
947
948 LandingPads.push_back(LandingPadInfo(LandingPad));
949 return LandingPads[N];
950}
951
952/// addInvoke - Provide the begin and end labels of an invoke style call and
953/// associate it with a try landing pad block.
954void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
955 unsigned BeginLabel, unsigned EndLabel) {
956 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
957 LP.BeginLabels.push_back(BeginLabel);
958 LP.EndLabels.push_back(EndLabel);
959}
960
961/// addLandingPad - Provide the label of a try LandingPad block.
962///
963unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
964 unsigned LandingPadLabel = NextLabelID();
965 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
966 LP.LandingPadLabel = LandingPadLabel;
967 return LandingPadLabel;
968}
969
970/// addPersonality - Provide the personality function for the exception
971/// information.
972void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
973 Function *Personality) {
974 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
975 LP.Personality = Personality;
976
Bill Wendling266486d2008-06-27 07:13:44 +0000977 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978 if (Personalities[i] == Personality)
979 return;
980
981 Personalities.push_back(Personality);
982}
983
984/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
985///
986void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
987 std::vector<GlobalVariable *> &TyInfo) {
988 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
989 for (unsigned N = TyInfo.size(); N; --N)
990 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
991}
992
993/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
994///
995void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
996 std::vector<GlobalVariable *> &TyInfo) {
997 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling266486d2008-06-27 07:13:44 +0000998 unsigned TyInfoSize = TyInfo.size();
999 std::vector<unsigned> IdsInFilter(TyInfoSize);
1000
1001 for (unsigned I = 0; I != TyInfoSize; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
Bill Wendling266486d2008-06-27 07:13:44 +00001003
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
1005}
1006
Duncan Sands923fdb12007-08-27 15:47:50 +00001007/// addCleanup - Add a cleanup action for a landing pad.
1008///
1009void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1010 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1011 LP.TypeIds.push_back(0);
1012}
1013
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1015/// pads.
1016void MachineModuleInfo::TidyLandingPads() {
1017 for (unsigned i = 0; i != LandingPads.size(); ) {
1018 LandingPadInfo &LandingPad = LandingPads[i];
1019 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1020
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +00001022 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
1024 LandingPads.erase(LandingPads.begin() + i);
1025 continue;
1026 }
Duncan Sands241a0c92007-09-05 11:27:52 +00001027
Bill Wendling266486d2008-06-27 07:13:44 +00001028 for (unsigned j = 0; j != LandingPads[i].BeginLabels.size(); ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1030 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands241a0c92007-09-05 11:27:52 +00001031
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 if (!BeginLabel || !EndLabel) {
1033 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1034 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1035 continue;
1036 }
1037
1038 LandingPad.BeginLabels[j] = BeginLabel;
1039 LandingPad.EndLabels[j] = EndLabel;
1040 ++j;
1041 }
Duncan Sands241a0c92007-09-05 11:27:52 +00001042
1043 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +00001044 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +00001045 LandingPads.erase(LandingPads.begin() + i);
1046 continue;
1047 }
1048
1049 // If there is no landing pad, ensure that the list of typeids is empty.
1050 // If the only typeid is a cleanup, this is the same as having no typeids.
1051 if (!LandingPad.LandingPadBlock ||
1052 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1053 LandingPad.TypeIds.clear();
1054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 ++i;
1056 }
1057}
1058
1059/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1060/// function wide.
1061unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling266486d2008-06-27 07:13:44 +00001062 for (unsigned i = 0, e = TypeInfos.size(); i != e; ++i)
1063 if (TypeInfos[i] == TI)
1064 return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065
1066 TypeInfos.push_back(TI);
1067 return TypeInfos.size();
1068}
1069
1070/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1071/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +00001072int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 // If the new filter coincides with the tail of an existing filter, then
1074 // re-use the existing filter. Folding filters more than this requires
1075 // re-ordering filters and/or their elements - probably not worth it.
1076 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1077 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling0e679b82008-06-27 01:27:56 +00001078 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079
1080 while (i && j)
1081 if (FilterIds[--i] != TyIds[--j])
1082 goto try_next;
1083
1084 if (!j)
1085 // The new filter coincides with range [i, end) of the existing filter.
1086 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +00001087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088try_next:;
1089 }
1090
1091 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +00001092 int FilterID = -(1 + FilterIds.size());
1093 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
Bill Wendling266486d2008-06-27 07:13:44 +00001094
Bill Wendling0e679b82008-06-27 01:27:56 +00001095 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 FilterIds.push_back(TyIds[I]);
Bill Wendling266486d2008-06-27 07:13:44 +00001097
Bill Wendling0e679b82008-06-27 01:27:56 +00001098 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 FilterIds.push_back(0); // terminator
1100 return FilterID;
1101}
1102
1103/// getPersonality - Return the personality function for the current function.
1104Function *MachineModuleInfo::getPersonality() const {
1105 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
1106 // function
1107 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1108}
1109
1110/// getPersonalityIndex - Return unique index for current personality
1111/// function. NULL personality function should always get zero index.
1112unsigned MachineModuleInfo::getPersonalityIndex() const {
1113 const Function* Personality = NULL;
1114
1115 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling266486d2008-06-27 07:13:44 +00001116 for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117 if (LandingPads[i].Personality) {
1118 Personality = LandingPads[i].Personality;
1119 break;
1120 }
1121
Bill Wendling266486d2008-06-27 07:13:44 +00001122 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001123 if (Personalities[i] == Personality)
1124 return i;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125
1126 // This should never happen
1127 assert(0 && "Personality function should be set!");
1128 return 0;
1129}
1130
1131//===----------------------------------------------------------------------===//
1132/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1133/// a info consumer to determine if the range of two labels is empty, by seeing
1134/// if the labels map to the same reduced label.
1135
1136namespace llvm {
1137
1138struct DebugLabelFolder : public MachineFunctionPass {
1139 static char ID;
1140 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1141
1142 virtual bool runOnMachineFunction(MachineFunction &MF);
1143 virtual const char *getPassName() const { return "Label Folder"; }
1144};
1145
1146char DebugLabelFolder::ID = 0;
1147
1148bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1149 // Get machine module info.
1150 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1151 if (!MMI) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152
1153 // Track if change is made.
1154 bool MadeChange = false;
1155 // No prior label to begin.
1156 unsigned PriorLabel = 0;
1157
1158 // Iterate through basic blocks.
1159 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1160 BB != E; ++BB) {
1161 // Iterate through instructions.
1162 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1163 // Is it a label.
Evan Cheng13d1c292008-01-31 09:59:15 +00001164 if (I->isDebugLabel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 // The label ID # is always operand #0, an immediate.
1166 unsigned NextLabel = I->getOperand(0).getImm();
1167
1168 // If there was an immediate prior label.
1169 if (PriorLabel) {
1170 // Remap the current label to prior label.
1171 MMI->RemapLabel(NextLabel, PriorLabel);
1172 // Delete the current label.
1173 I = BB->erase(I);
1174 // Indicate a change has been made.
1175 MadeChange = true;
1176 continue;
1177 } else {
1178 // Start a new round.
1179 PriorLabel = NextLabel;
1180 }
1181 } else {
1182 // No consecutive labels.
1183 PriorLabel = 0;
1184 }
1185
1186 ++I;
1187 }
1188 }
1189
1190 return MadeChange;
1191}
1192
1193FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1194
1195}