blob: 473b60554b641844b03baeaf561a2398794194dc [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)
253 : DIVisitor()
254 , SR(S)
255 , Elements(E)
256 {}
257
258 /// Apply - Set the value of each of the fields.
259 ///
260 virtual void Apply(int &Field) {
261 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
262 }
263 virtual void Apply(unsigned &Field) {
264 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
265 }
266 virtual void Apply(int64_t &Field) {
267 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
268 }
269 virtual void Apply(uint64_t &Field) {
270 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
271 }
272 virtual void Apply(bool &Field) {
273 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
274 }
275 virtual void Apply(std::string &Field) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000276 Elements.push_back(SR.getString(Field));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 }
278 virtual void Apply(DebugInfoDesc *&Field) {
279 GlobalVariable *GV = NULL;
280
281 // If non-NULL then convert to global.
282 if (Field) GV = SR.Serialize(Field);
283
284 // FIXME - At some point should use specific type.
285 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
286
287 if (GV) {
288 // Set to pointer to global.
289 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
290 } else {
291 // Use NULL.
292 Elements.push_back(ConstantPointerNull::get(EmptyTy));
293 }
294 }
295 virtual void Apply(GlobalVariable *&Field) {
296 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
297 if (Field) {
298 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
299 } else {
300 Elements.push_back(ConstantPointerNull::get(EmptyTy));
301 }
302 }
303 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
304 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
305 unsigned N = Field.size();
306 ArrayType *AT = ArrayType::get(EmptyTy, N);
307 std::vector<Constant *> ArrayElements;
308
Bill Wendling266486d2008-06-27 07:13:44 +0000309 for (unsigned i = 0; i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 if (DebugInfoDesc *Element = Field[i]) {
311 GlobalVariable *GVE = SR.Serialize(Element);
312 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
313 ArrayElements.push_back(cast<Constant>(CE));
314 } else {
315 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
316 }
317 }
318
319 Constant *CA = ConstantArray::get(AT, ArrayElements);
320 GlobalVariable *CAGV = new GlobalVariable(AT, true,
321 GlobalValue::InternalLinkage,
322 CA, "llvm.dbg.array",
323 SR.getModule());
324 CAGV->setSection("llvm.metadata");
325 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
326 Elements.push_back(CAE);
327 }
328};
329
330//===----------------------------------------------------------------------===//
331/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
332/// the supplied DebugInfoDesc.
333class DIGetTypesVisitor : public DIVisitor {
334private:
335 DISerializer &SR; // Active serializer.
336 std::vector<const Type*> &Fields; // Type accumulator.
337
338public:
339 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
340 : DIVisitor()
341 , SR(S)
342 , Fields(F)
343 {}
344
345 /// Apply - Set the value of each of the fields.
346 ///
347 virtual void Apply(int &Field) {
348 Fields.push_back(Type::Int32Ty);
349 }
350 virtual void Apply(unsigned &Field) {
351 Fields.push_back(Type::Int32Ty);
352 }
353 virtual void Apply(int64_t &Field) {
354 Fields.push_back(Type::Int64Ty);
355 }
356 virtual void Apply(uint64_t &Field) {
357 Fields.push_back(Type::Int64Ty);
358 }
359 virtual void Apply(bool &Field) {
360 Fields.push_back(Type::Int1Ty);
361 }
362 virtual void Apply(std::string &Field) {
363 Fields.push_back(SR.getStrPtrType());
364 }
365 virtual void Apply(DebugInfoDesc *&Field) {
366 // FIXME - At some point should use specific type.
367 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
368 Fields.push_back(EmptyTy);
369 }
370 virtual void Apply(GlobalVariable *&Field) {
371 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
372 Fields.push_back(EmptyTy);
373 }
374 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
375 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
376 Fields.push_back(EmptyTy);
377 }
378};
379
380//===----------------------------------------------------------------------===//
381/// DIVerifyVisitor - This DIVisitor verifies all the field types against
382/// a constant initializer.
383class DIVerifyVisitor : public DIVisitor {
384private:
385 DIVerifier &VR; // Active verifier.
386 bool IsValid; // Validity status.
387 unsigned I; // Current operand index.
388 ConstantStruct *CI; // GlobalVariable constant initializer.
389
390public:
391 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
392 : DIVisitor()
393 , VR(V)
394 , IsValid(true)
395 , I(0)
396 , CI(cast<ConstantStruct>(GV->getInitializer()))
397 {
398 }
399
400 // Accessors.
401 bool isValid() const { return IsValid; }
402
403 /// Apply - Set the value of each of the fields.
404 ///
405 virtual void Apply(int &Field) {
406 Constant *C = CI->getOperand(I++);
407 IsValid = IsValid && isa<ConstantInt>(C);
408 }
409 virtual void Apply(unsigned &Field) {
410 Constant *C = CI->getOperand(I++);
411 IsValid = IsValid && isa<ConstantInt>(C);
412 }
413 virtual void Apply(int64_t &Field) {
414 Constant *C = CI->getOperand(I++);
415 IsValid = IsValid && isa<ConstantInt>(C);
416 }
417 virtual void Apply(uint64_t &Field) {
418 Constant *C = CI->getOperand(I++);
419 IsValid = IsValid && isa<ConstantInt>(C);
420 }
421 virtual void Apply(bool &Field) {
422 Constant *C = CI->getOperand(I++);
423 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
424 }
425 virtual void Apply(std::string &Field) {
426 Constant *C = CI->getOperand(I++);
427 IsValid = IsValid &&
428 (!C || isStringValue(C) || C->isNullValue());
429 }
430 virtual void Apply(DebugInfoDesc *&Field) {
431 // FIXME - Prepare the correct descriptor.
432 Constant *C = CI->getOperand(I++);
433 IsValid = IsValid && isGlobalVariable(C);
434 }
435 virtual void Apply(GlobalVariable *&Field) {
436 Constant *C = CI->getOperand(I++);
437 IsValid = IsValid && isGlobalVariable(C);
438 }
439 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
440 Constant *C = CI->getOperand(I++);
441 IsValid = IsValid && isGlobalVariable(C);
442 if (!IsValid) return;
443
444 GlobalVariable *GV = getGlobalVariable(C);
445 IsValid = IsValid && GV && GV->hasInitializer();
446 if (!IsValid) return;
447
448 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
449 IsValid = IsValid && CA;
450 if (!IsValid) return;
451
452 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
453 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
454 if (!IsValid) return;
455
456 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
457 VR.Verify(GVE);
458 }
459 }
460};
461
Dan Gohman089efff2008-05-13 00:00:25 +0000462}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463
464//===----------------------------------------------------------------------===//
465
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
467 return Deserialize(getGlobalVariable(V));
468}
469DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
470 // Handle NULL.
471 if (!GV) return NULL;
472
473 // Check to see if it has been already deserialized.
474 DebugInfoDesc *&Slot = GlobalDescs[GV];
475 if (Slot) return Slot;
476
477 // Get the Tag from the global.
478 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
479
480 // Create an empty instance of the correct sort.
481 Slot = DebugInfoDesc::DescFactory(Tag);
482
483 // If not a user defined descriptor.
484 if (Slot) {
485 // Deserialize the fields.
486 DIDeserializeVisitor DRAM(*this, GV);
487 DRAM.ApplyToFields(Slot);
488 }
489
490 return Slot;
491}
492
493//===----------------------------------------------------------------------===//
494
495/// getStrPtrType - Return a "sbyte *" type.
496///
497const PointerType *DISerializer::getStrPtrType() {
498 // If not already defined.
499 if (!StrPtrTy) {
500 // Construct the pointer to signed bytes.
Christopher Lambbb2f2222007-12-17 01:12:55 +0000501 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 }
503
504 return StrPtrTy;
505}
506
507/// getEmptyStructPtrType - Return a "{ }*" type.
508///
509const PointerType *DISerializer::getEmptyStructPtrType() {
510 // If not already defined.
Bill Wendling0e679b82008-06-27 01:27:56 +0000511 if (EmptyStructPtrTy) return EmptyStructPtrTy;
Bill Wendlingecc696f2008-06-27 00:56:36 +0000512
Bill Wendling0e679b82008-06-27 01:27:56 +0000513 // Construct the pointer to empty structure type.
514 const StructType *EmptyStructTy =
515 StructType::get(std::vector<const Type*>());
Bill Wendlingff424a92008-06-27 01:32:08 +0000516
517 // Construct the pointer to empty structure type.
518 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 return EmptyStructPtrTy;
520}
521
522/// getTagType - Return the type describing the specified descriptor (via tag.)
523///
524const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
525 // Attempt to get the previously defined type.
526 StructType *&Ty = TagTypes[DD->getTag()];
527
528 // If not already defined.
529 if (!Ty) {
530 // Set up fields vector.
531 std::vector<const Type*> Fields;
532 // Get types of fields.
533 DIGetTypesVisitor GTAM(*this, Fields);
534 GTAM.ApplyToFields(DD);
535
536 // Construct structured type.
537 Ty = StructType::get(Fields);
538
539 // Register type name with module.
540 M->addTypeName(DD->getTypeString(), Ty);
541 }
542
543 return Ty;
544}
545
546/// getString - Construct the string as constant string global.
547///
548Constant *DISerializer::getString(const std::string &String) {
549 // Check string cache for previous edition.
Bill Wendling0e679b82008-06-27 01:27:56 +0000550 Constant *&Slot = StringCache[String.c_str()];
Bill Wendlingecc696f2008-06-27 00:56:36 +0000551
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 // Return Constant if previously defined.
553 if (Slot) return Slot;
Bill Wendlingecc696f2008-06-27 00:56:36 +0000554
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 // If empty string then use a sbyte* null instead.
556 if (String.empty()) {
557 Slot = ConstantPointerNull::get(getStrPtrType());
558 } else {
559 // Construct string as an llvm constant.
560 Constant *ConstStr = ConstantArray::get(String);
Bill Wendlingecc696f2008-06-27 00:56:36 +0000561
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 // Otherwise create and return a new string global.
563 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
564 GlobalVariable::InternalLinkage,
565 ConstStr, ".str", M);
566 StrGV->setSection("llvm.metadata");
Bill Wendlingecc696f2008-06-27 00:56:36 +0000567
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 // Convert to generic string pointer.
569 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
570 }
Bill Wendlingecc696f2008-06-27 00:56:36 +0000571
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 return Slot;
573
574}
575
576/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
577/// so that it can be serialized to a .bc or .ll file.
578GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
579 // Check if the DebugInfoDesc is already in the map.
580 GlobalVariable *&Slot = DescGlobals[DD];
581
582 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
583 if (Slot) return Slot;
584
585 // Get the type associated with the Tag.
586 const StructType *Ty = getTagType(DD);
587
588 // Create the GlobalVariable early to prevent infinite recursion.
Bill Wendling4a41e432008-07-01 22:08:01 +0000589 GlobalVariable *GV =
Bill Wendling9ac67a72008-07-02 00:35:47 +0000590 new GlobalVariable(Ty, true, DD->getLinkage(),
Bill Wendling4a41e432008-07-01 22:08:01 +0000591 NULL, DD->getDescString(), M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 GV->setSection("llvm.metadata");
593
594 // Insert new GlobalVariable in DescGlobals map.
595 Slot = GV;
596
597 // Set up elements vector
598 std::vector<Constant*> Elements;
599 // Add fields.
600 DISerializeVisitor SRAM(*this, Elements);
601 SRAM.ApplyToFields(DD);
602
603 // Set the globals initializer.
604 GV->setInitializer(ConstantStruct::get(Ty, Elements));
605
606 return GV;
607}
608
Devang Pateld83c2c02007-11-30 00:51:33 +0000609/// addDescriptor - Directly connect DD with existing GV.
610void DISerializer::addDescriptor(DebugInfoDesc *DD,
611 GlobalVariable *GV) {
612 DescGlobals[DD] = GV;
613}
614
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615//===----------------------------------------------------------------------===//
616
617/// Verify - Return true if the GlobalVariable appears to be a valid
618/// serialization of a DebugInfoDesc.
619bool DIVerifier::Verify(Value *V) {
620 return !V || Verify(getGlobalVariable(V));
621}
622bool DIVerifier::Verify(GlobalVariable *GV) {
623 // NULLs are valid.
624 if (!GV) return true;
625
626 // Check prior validity.
627 unsigned &ValiditySlot = Validity[GV];
628
629 // If visited before then use old state.
630 if (ValiditySlot) return ValiditySlot == Valid;
631
632 // Assume validity for the time being (recursion.)
633 ValiditySlot = Valid;
634
635 // Make sure the global is internal or link once (anchor.)
636 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
637 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
638 ValiditySlot = Invalid;
639 return false;
640 }
641
642 // Get the Tag.
643 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
644
645 // Check for user defined descriptors.
646 if (Tag == DW_TAG_invalid) {
647 ValiditySlot = Valid;
648 return true;
649 }
650
651 // Get the Version.
652 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
653
654 // Check for version mismatch.
655 if (Version != LLVMDebugVersion) {
656 ValiditySlot = Invalid;
657 return false;
658 }
659
660 // Construct an empty DebugInfoDesc.
661 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
662
663 // Allow for user defined descriptors.
664 if (!DD) return true;
665
666 // Get the initializer constant.
667 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
668
669 // Get the operand count.
670 unsigned N = CI->getNumOperands();
671
672 // Get the field count.
673 unsigned &CountSlot = Counts[Tag];
674 if (!CountSlot) {
675 // Check the operand count to the field count
676 DICountVisitor CTAM;
677 CTAM.ApplyToFields(DD);
678 CountSlot = CTAM.getCount();
679 }
680
681 // Field count must be at most equal operand count.
682 if (CountSlot > N) {
683 delete DD;
684 ValiditySlot = Invalid;
685 return false;
686 }
687
688 // Check each field for valid type.
689 DIVerifyVisitor VRAM(*this, GV);
690 VRAM.ApplyToFields(DD);
691
692 // Release empty DebugInfoDesc.
693 delete DD;
694
695 // If fields are not valid.
696 if (!VRAM.isValid()) {
697 ValiditySlot = Invalid;
698 return false;
699 }
700
701 return true;
702}
703
Evan Cheng2e28d622008-02-02 04:07:54 +0000704/// isVerified - Return true if the specified GV has already been
705/// verified as a debug information descriptor.
706bool DIVerifier::isVerified(GlobalVariable *GV) {
707 unsigned &ValiditySlot = Validity[GV];
708 if (ValiditySlot) return ValiditySlot == Valid;
709 return false;
710}
711
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712//===----------------------------------------------------------------------===//
713
714DebugScope::~DebugScope() {
Bill Wendling266486d2008-06-27 07:13:44 +0000715 for (unsigned i = 0, e = Scopes.size(); i < e; ++i) delete Scopes[i];
716 for (unsigned i = 0, e = Variables.size(); i < e; ++i) delete Variables[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717}
718
719//===----------------------------------------------------------------------===//
720
721MachineModuleInfo::MachineModuleInfo()
722: ImmutablePass((intptr_t)&ID)
723, DR()
724, VR()
725, CompileUnits()
726, Directories()
727, SourceFiles()
728, Lines()
729, LabelIDList()
730, ScopeMap()
731, RootScope(NULL)
732, FrameMoves()
733, LandingPads()
734, Personalities()
735, CallsEHReturn(0)
736, CallsUnwindInit(0)
737{
738 // Always emit "no personality" info
739 Personalities.push_back(NULL);
740}
741MachineModuleInfo::~MachineModuleInfo() {
742
743}
744
745/// doInitialization - Initialize the state for a new module.
746///
747bool MachineModuleInfo::doInitialization() {
748 return false;
749}
750
751/// doFinalization - Tear down the state after completion of a module.
752///
753bool MachineModuleInfo::doFinalization() {
754 return false;
755}
756
757/// BeginFunction - Begin gathering function meta information.
758///
759void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
760 // Coming soon.
761}
762
763/// EndFunction - Discard function meta information.
764///
765void MachineModuleInfo::EndFunction() {
766 // Clean up scope information.
767 if (RootScope) {
768 delete RootScope;
769 ScopeMap.clear();
770 RootScope = NULL;
771 }
772
773 // Clean up line info.
774 Lines.clear();
775
776 // Clean up frame info.
777 FrameMoves.clear();
778
779 // Clean up exception info.
780 LandingPads.clear();
781 TypeInfos.clear();
782 FilterIds.clear();
783 FilterEnds.clear();
784 CallsEHReturn = 0;
785 CallsUnwindInit = 0;
786}
787
788/// getDescFor - Convert a Value to a debug information descriptor.
789///
790// FIXME - use new Value type when available.
791DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
792 return DR.Deserialize(V);
793}
794
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795/// AnalyzeModule - Scan the module for global debug information.
796///
797void MachineModuleInfo::AnalyzeModule(Module &M) {
798 SetupCompileUnits(M);
Dale Johannesen3dadeb32008-01-16 19:59:28 +0000799
800 // Insert functions in the llvm.used array into UsedFunctions.
801 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
802 if (!GV || !GV->hasInitializer()) return;
803
804 // Should be an array of 'i8*'.
805 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
806 if (InitList == 0) return;
807
808 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
809 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
810 if (CE->getOpcode() == Instruction::BitCast)
811 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
812 UsedFunctions.insert(F);
813 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814}
815
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816/// SetupCompileUnits - Set up the unique vector of compile units.
817///
818void MachineModuleInfo::SetupCompileUnits(Module &M) {
Bill Wendlinge15b1192008-06-27 00:09:40 +0000819 std::vector<void*> CUList;
820 CompileUnitDesc CUD;
821 getAnchoredDescriptors(M, &CUD, CUList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822
Bill Wendling266486d2008-06-27 07:13:44 +0000823 for (unsigned i = 0, e = CUList.size(); i < e; i++)
Bill Wendlinge15b1192008-06-27 00:09:40 +0000824 CompileUnits.insert((CompileUnitDesc*)CUList[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825}
826
827/// getCompileUnits - Return a vector of debug compile units.
828///
829const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
830 return CompileUnits;
831}
832
Bill Wendlinge15b1192008-06-27 00:09:40 +0000833/// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
834///
835void
836MachineModuleInfo::getAnchoredDescriptors(Module &M, const AnchoredDesc *Desc,
837 std::vector<void*> &AnchoredDescs) {
Bill Wendlingff424a92008-06-27 01:32:08 +0000838 std::vector<GlobalVariable*> Globals;
Bill Wendlinge15b1192008-06-27 00:09:40 +0000839 getGlobalVariablesUsing(M, Desc->getAnchorString(), Globals);
840
Bill Wendling266486d2008-06-27 07:13:44 +0000841 for (unsigned i = 0, e = Globals.size(); i < e; ++i) {
Bill Wendlinge15b1192008-06-27 00:09:40 +0000842 GlobalVariable *GV = Globals[i];
843
844 // FIXME - In the short term, changes are too drastic to continue.
845 if (DebugInfoDesc::TagFromGlobal(GV) == Desc->getTag() &&
846 DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion)
847 AnchoredDescs.push_back(DR.Deserialize(GV));
848 }
849}
850
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
852/// named GlobalVariable.
Bill Wendlinge15b1192008-06-27 00:09:40 +0000853void
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854MachineModuleInfo::getGlobalVariablesUsing(Module &M,
Bill Wendlinge15b1192008-06-27 00:09:40 +0000855 const std::string &RootName,
856 std::vector<GlobalVariable*> &Globals) {
857 return ::getGlobalVariablesUsing(M, RootName, Globals);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858}
859
Evan Cheng69eda822008-02-01 02:05:57 +0000860/// RecordSourceLine - Records location information and associates it with a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861/// debug label. Returns a unique label ID used to generate a label and
862/// provide correspondence to the source line list.
Evan Cheng69eda822008-02-01 02:05:57 +0000863unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
864 unsigned Source) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 unsigned ID = NextLabelID();
866 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
867 return ID;
868}
869
870/// RecordSource - Register a source file with debug info. Returns an source
871/// ID.
872unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
873 const std::string &Source) {
874 unsigned DirectoryID = Directories.insert(Directory);
875 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
876}
877unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
878 return RecordSource(CompileUnit->getDirectory(),
879 CompileUnit->getFileName());
880}
881
882/// RecordRegionStart - Indicate the start of a region.
883///
884unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
885 // FIXME - need to be able to handle split scopes because of bb cloning.
886 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
887 DebugScope *Scope = getOrCreateScope(ScopeDesc);
888 unsigned ID = NextLabelID();
889 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
890 return ID;
891}
892
893/// RecordRegionEnd - Indicate the end of a region.
894///
895unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
896 // FIXME - need to be able to handle split scopes because of bb cloning.
897 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
898 DebugScope *Scope = getOrCreateScope(ScopeDesc);
899 unsigned ID = NextLabelID();
900 Scope->setEndLabelID(ID);
901 return ID;
902}
903
904/// RecordVariable - Indicate the declaration of a local variable.
905///
Evan Cheng2e28d622008-02-02 04:07:54 +0000906void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
907 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 DebugScope *Scope = getOrCreateScope(VD->getContext());
909 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
910 Scope->AddVariable(DV);
911}
912
913/// getOrCreateScope - Returns the scope associated with the given descriptor.
914///
915DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
916 DebugScope *&Slot = ScopeMap[ScopeDesc];
917 if (!Slot) {
918 // FIXME - breaks down when the context is an inlined function.
919 DebugInfoDesc *ParentDesc = NULL;
920 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
921 ParentDesc = Block->getContext();
922 }
923 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
924 Slot = new DebugScope(Parent, ScopeDesc);
925 if (Parent) {
926 Parent->AddScope(Slot);
927 } else if (RootScope) {
928 // FIXME - Add inlined function scopes to the root so we can delete
929 // them later. Long term, handle inlined functions properly.
930 RootScope->AddScope(Slot);
931 } else {
932 // First function is top level function.
933 RootScope = Slot;
934 }
935 }
936 return Slot;
937}
938
939//===-EH-------------------------------------------------------------------===//
940
941/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
942/// specified MachineBasicBlock.
Bill Wendling266486d2008-06-27 07:13:44 +0000943LandingPadInfo &
944MachineModuleInfo::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 unsigned N = LandingPads.size();
Bill Wendling266486d2008-06-27 07:13:44 +0000946
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 for (unsigned i = 0; i < N; ++i) {
948 LandingPadInfo &LP = LandingPads[i];
949 if (LP.LandingPadBlock == LandingPad)
950 return LP;
951 }
952
953 LandingPads.push_back(LandingPadInfo(LandingPad));
954 return LandingPads[N];
955}
956
957/// addInvoke - Provide the begin and end labels of an invoke style call and
958/// associate it with a try landing pad block.
959void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
960 unsigned BeginLabel, unsigned EndLabel) {
961 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
962 LP.BeginLabels.push_back(BeginLabel);
963 LP.EndLabels.push_back(EndLabel);
964}
965
966/// addLandingPad - Provide the label of a try LandingPad block.
967///
968unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
969 unsigned LandingPadLabel = NextLabelID();
970 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
971 LP.LandingPadLabel = LandingPadLabel;
972 return LandingPadLabel;
973}
974
975/// addPersonality - Provide the personality function for the exception
976/// information.
977void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
978 Function *Personality) {
979 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
980 LP.Personality = Personality;
981
Bill Wendling266486d2008-06-27 07:13:44 +0000982 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 if (Personalities[i] == Personality)
984 return;
985
986 Personalities.push_back(Personality);
987}
988
989/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
990///
991void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
992 std::vector<GlobalVariable *> &TyInfo) {
993 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
994 for (unsigned N = TyInfo.size(); N; --N)
995 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
996}
997
998/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
999///
1000void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1001 std::vector<GlobalVariable *> &TyInfo) {
1002 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling266486d2008-06-27 07:13:44 +00001003 unsigned TyInfoSize = TyInfo.size();
1004 std::vector<unsigned> IdsInFilter(TyInfoSize);
1005
1006 for (unsigned I = 0; I != TyInfoSize; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
Bill Wendling266486d2008-06-27 07:13:44 +00001008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
1010}
1011
Duncan Sands923fdb12007-08-27 15:47:50 +00001012/// addCleanup - Add a cleanup action for a landing pad.
1013///
1014void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1015 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1016 LP.TypeIds.push_back(0);
1017}
1018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1020/// pads.
1021void MachineModuleInfo::TidyLandingPads() {
1022 for (unsigned i = 0; i != LandingPads.size(); ) {
1023 LandingPadInfo &LandingPad = LandingPads[i];
1024 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
1025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +00001027 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
1029 LandingPads.erase(LandingPads.begin() + i);
1030 continue;
1031 }
Duncan Sands241a0c92007-09-05 11:27:52 +00001032
Bill Wendling266486d2008-06-27 07:13:44 +00001033 for (unsigned j = 0; j != LandingPads[i].BeginLabels.size(); ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1035 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands241a0c92007-09-05 11:27:52 +00001036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 if (!BeginLabel || !EndLabel) {
1038 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1039 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1040 continue;
1041 }
1042
1043 LandingPad.BeginLabels[j] = BeginLabel;
1044 LandingPad.EndLabels[j] = EndLabel;
1045 ++j;
1046 }
Duncan Sands241a0c92007-09-05 11:27:52 +00001047
1048 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +00001049 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +00001050 LandingPads.erase(LandingPads.begin() + i);
1051 continue;
1052 }
1053
1054 // If there is no landing pad, ensure that the list of typeids is empty.
1055 // If the only typeid is a cleanup, this is the same as having no typeids.
1056 if (!LandingPad.LandingPadBlock ||
1057 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1058 LandingPad.TypeIds.clear();
1059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 ++i;
1061 }
1062}
1063
1064/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1065/// function wide.
1066unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling266486d2008-06-27 07:13:44 +00001067 for (unsigned i = 0, e = TypeInfos.size(); i != e; ++i)
1068 if (TypeInfos[i] == TI)
1069 return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070
1071 TypeInfos.push_back(TI);
1072 return TypeInfos.size();
1073}
1074
1075/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1076/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +00001077int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 // If the new filter coincides with the tail of an existing filter, then
1079 // re-use the existing filter. Folding filters more than this requires
1080 // re-ordering filters and/or their elements - probably not worth it.
1081 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1082 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling0e679b82008-06-27 01:27:56 +00001083 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084
1085 while (i && j)
1086 if (FilterIds[--i] != TyIds[--j])
1087 goto try_next;
1088
1089 if (!j)
1090 // The new filter coincides with range [i, end) of the existing filter.
1091 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +00001092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093try_next:;
1094 }
1095
1096 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +00001097 int FilterID = -(1 + FilterIds.size());
1098 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
Bill Wendling266486d2008-06-27 07:13:44 +00001099
Bill Wendling0e679b82008-06-27 01:27:56 +00001100 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 FilterIds.push_back(TyIds[I]);
Bill Wendling266486d2008-06-27 07:13:44 +00001102
Bill Wendling0e679b82008-06-27 01:27:56 +00001103 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104 FilterIds.push_back(0); // terminator
1105 return FilterID;
1106}
1107
1108/// getPersonality - Return the personality function for the current function.
1109Function *MachineModuleInfo::getPersonality() const {
1110 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
1111 // function
1112 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1113}
1114
1115/// getPersonalityIndex - Return unique index for current personality
1116/// function. NULL personality function should always get zero index.
1117unsigned MachineModuleInfo::getPersonalityIndex() const {
1118 const Function* Personality = NULL;
1119
1120 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling266486d2008-06-27 07:13:44 +00001121 for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122 if (LandingPads[i].Personality) {
1123 Personality = LandingPads[i].Personality;
1124 break;
1125 }
1126
Bill Wendling266486d2008-06-27 07:13:44 +00001127 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 if (Personalities[i] == Personality)
1129 return i;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130
1131 // This should never happen
1132 assert(0 && "Personality function should be set!");
1133 return 0;
1134}
1135
1136//===----------------------------------------------------------------------===//
1137/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1138/// a info consumer to determine if the range of two labels is empty, by seeing
1139/// if the labels map to the same reduced label.
1140
1141namespace llvm {
1142
1143struct DebugLabelFolder : public MachineFunctionPass {
1144 static char ID;
1145 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1146
1147 virtual bool runOnMachineFunction(MachineFunction &MF);
1148 virtual const char *getPassName() const { return "Label Folder"; }
1149};
1150
1151char DebugLabelFolder::ID = 0;
1152
1153bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1154 // Get machine module info.
1155 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1156 if (!MMI) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157
1158 // Track if change is made.
1159 bool MadeChange = false;
1160 // No prior label to begin.
1161 unsigned PriorLabel = 0;
1162
1163 // Iterate through basic blocks.
1164 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1165 BB != E; ++BB) {
1166 // Iterate through instructions.
1167 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1168 // Is it a label.
Evan Cheng13d1c292008-01-31 09:59:15 +00001169 if (I->isDebugLabel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170 // The label ID # is always operand #0, an immediate.
1171 unsigned NextLabel = I->getOperand(0).getImm();
1172
1173 // If there was an immediate prior label.
1174 if (PriorLabel) {
1175 // Remap the current label to prior label.
1176 MMI->RemapLabel(NextLabel, PriorLabel);
1177 // Delete the current label.
1178 I = BB->erase(I);
1179 // Indicate a change has been made.
1180 MadeChange = true;
1181 continue;
1182 } else {
1183 // Start a new round.
1184 PriorLabel = NextLabel;
1185 }
1186 } else {
1187 // No consecutive labels.
1188 PriorLabel = 0;
1189 }
1190
1191 ++I;
1192 }
1193 }
1194
1195 return MadeChange;
1196}
1197
1198FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1199
1200}