blob: 3d3a9957bb082080f9a1393c7730a70a901ca795 [file] [log] [blame]
Jim Laskey6da18642007-01-26 21:38:26 +00001//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
Jim Laskey6af56812006-01-04 13:36:38 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey6af56812006-01-04 13:36:38 +00007//
8//===----------------------------------------------------------------------===//
Jim Laskey6af56812006-01-04 13:36:38 +00009
Jim Laskey6da18642007-01-26 21:38:26 +000010#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey6af56812006-01-04 13:36:38 +000011
Jim Laskeyb3e789a2006-01-26 20:21:46 +000012#include "llvm/Constants.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000013#include "llvm/Analysis/ValueTracking.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineFunction.h"
Jim Laskey41886992006-04-07 16:34:46 +000016#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000017#include "llvm/Target/TargetInstrInfo.h"
18#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000019#include "llvm/Target/TargetOptions.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000020#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000021#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000022#include "llvm/Intrinsics.h"
23#include "llvm/Instructions.h"
24#include "llvm/Module.h"
25#include "llvm/Support/Dwarf.h"
Bill Wendling832171c2006-12-07 20:04:42 +000026#include "llvm/Support/Streams.h"
Jim Laskey6af56812006-01-04 13:36:38 +000027using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000028using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000029
30// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman844731a2008-05-13 00:00:25 +000031static RegisterPass<MachineModuleInfo>
32X("machinemoduleinfo", "Module Information");
Devang Patel19974732007-05-03 01:11:54 +000033char MachineModuleInfo::ID = 0;
Jim Laskey063e7652006-01-17 17:31:53 +000034
Jim Laskeyb3e789a2006-01-26 20:21:46 +000035//===----------------------------------------------------------------------===//
36
Jim Laskey86cbdba2006-02-06 15:33:21 +000037/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000038/// specified value in their initializer somewhere.
39static void
Bill Wendling0ac1b6d2008-06-27 01:32:08 +000040getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +000041 // Scan though value users.
42 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
43 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000044 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000045 Result.push_back(GV);
46 } else if (Constant *C = dyn_cast<Constant>(*I)) {
47 // If the user is a constant variable then scan its users
48 getGlobalVariablesUsing(C, Result);
49 }
50 }
51}
52
Jim Laskey86cbdba2006-02-06 15:33:21 +000053/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
54/// named GlobalVariable.
Bill Wendlingc04f4652008-07-03 23:13:02 +000055static void
56getGlobalVariablesUsing(Module &M, const std::string &RootName,
57 std::vector<GlobalVariable*> &Result) {
Jim Laskeyce72b172006-02-11 01:01:30 +000058 std::vector<const Type*> FieldTypes;
Reid Spencer47857812006-12-31 05:55:36 +000059 FieldTypes.push_back(Type::Int32Ty);
60 FieldTypes.push_back(Type::Int32Ty);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000061
Jim Laskey86cbdba2006-02-06 15:33:21 +000062 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000063 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000064 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000065
66 // If present and linkonce then scan for users.
Bill Wendlingc04f4652008-07-03 23:13:02 +000067 if (UseRoot && UseRoot->hasLinkOnceLinkage())
Jim Laskeyb3e789a2006-01-26 20:21:46 +000068 getGlobalVariablesUsing(UseRoot, Result);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000069}
70
Jim Laskey86cbdba2006-02-06 15:33:21 +000071/// isStringValue - Return true if the given value can be coerced to a string.
72///
73static bool isStringValue(Value *V) {
74 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 return Init->isString();
78 }
79 } else if (Constant *C = dyn_cast<Constant>(V)) {
80 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
81 return isStringValue(GV);
82 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
83 if (CE->getOpcode() == Instruction::GetElementPtr) {
84 if (CE->getNumOperands() == 3 &&
85 cast<Constant>(CE->getOperand(1))->isNullValue() &&
86 isa<ConstantInt>(CE->getOperand(2))) {
87 return isStringValue(CE->getOperand(0));
88 }
89 }
90 }
91 }
92 return false;
93}
94
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000095/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000096///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000097static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000098 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
99 return GV;
100 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000101 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000102 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000103 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
104 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000105 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000106 return NULL;
107 }
108 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000109 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000110 }
111 return NULL;
112}
113
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000114/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000115/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000116static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000117 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
118 return true;
119 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000120 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000121 return isa<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000122 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
123 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000124 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000125 return false;
126 }
127 return isa<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000128 }
129 }
130 return false;
131}
132
Bill Wendling10fff602008-07-03 22:53:42 +0000133/// getUIntOperand - Return ith operand if it is an unsigned integer.
134///
135static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
136 // Make sure the GlobalVariable has an initializer.
137 if (!GV->hasInitializer()) return NULL;
138
139 // Get the initializer constant.
140 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
141 if (!CI) return NULL;
142
143 // Check if there is at least i + 1 operands.
144 unsigned N = CI->getNumOperands();
145 if (i >= N) return NULL;
146
147 // Check constant.
148 return dyn_cast<ConstantInt>(CI->getOperand(i));
149}
150
Jim Laskey86cbdba2006-02-06 15:33:21 +0000151//===----------------------------------------------------------------------===//
152
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000153/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000154/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000155void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000156 DD->ApplyToFields(this);
157}
158
Dan Gohman844731a2008-05-13 00:00:25 +0000159namespace {
160
Jim Laskey86cbdba2006-02-06 15:33:21 +0000161//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000162/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
163/// the supplied DebugInfoDesc.
164class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000165private:
166 unsigned Count; // Running count of fields.
167
168public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000169 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000170
171 // Accessors.
172 unsigned getCount() const { return Count; }
173
174 /// Apply - Count each of the fields.
175 ///
176 virtual void Apply(int &Field) { ++Count; }
177 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000178 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000179 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000180 virtual void Apply(bool &Field) { ++Count; }
181 virtual void Apply(std::string &Field) { ++Count; }
182 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
183 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000184 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
185 ++Count;
186 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000187};
188
189//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000190/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
191/// supplied DebugInfoDesc.
192class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000193private:
194 DIDeserializer &DR; // Active deserializer.
195 unsigned I; // Current operand index.
196 ConstantStruct *CI; // GlobalVariable constant initializer.
197
198public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000199 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
Bill Wendling914c9702008-06-27 01:27:56 +0000200 : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer()))
Jim Laskey86cbdba2006-02-06 15:33:21 +0000201 {}
202
203 /// Apply - Set the value of each of the fields.
204 ///
205 virtual void Apply(int &Field) {
206 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000207 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000208 }
209 virtual void Apply(unsigned &Field) {
210 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000211 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000212 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000213 virtual void Apply(int64_t &Field) {
214 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000215 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000216 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000217 virtual void Apply(uint64_t &Field) {
218 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000219 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000220 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000221 virtual void Apply(bool &Field) {
222 Constant *C = CI->getOperand(I++);
Reid Spencer579dca12007-01-12 04:24:46 +0000223 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000224 }
225 virtual void Apply(std::string &Field) {
226 Constant *C = CI->getOperand(I++);
Evan Cheng0ff39b32008-06-30 07:31:25 +0000227 // Fills in the string if it succeeds
228 if (!GetConstantStringInfo(C, Field))
229 Field.clear();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000230 }
231 virtual void Apply(DebugInfoDesc *&Field) {
232 Constant *C = CI->getOperand(I++);
233 Field = DR.Deserialize(C);
234 }
235 virtual void Apply(GlobalVariable *&Field) {
236 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000237 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000238 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000239 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000240 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000241 Constant *C = CI->getOperand(I++);
242 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000243 if (GV->hasInitializer()) {
244 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
245 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
246 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
247 DebugInfoDesc *DE = DR.Deserialize(GVE);
248 Field.push_back(DE);
249 }
250 } else if (GV->getInitializer()->isNullValue()) {
251 if (const ArrayType *T =
252 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
253 Field.resize(T->getNumElements());
254 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000255 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000256 }
257 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000258};
259
260//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000261/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000262/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000263class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000264private:
265 DISerializer &SR; // Active serializer.
266 std::vector<Constant*> &Elements; // Element accumulator.
267
268public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000269 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
Bill Wendling10fff602008-07-03 22:53:42 +0000270 : DIVisitor()
271 , SR(S)
272 , Elements(E)
273 {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000274
275 /// Apply - Set the value of each of the fields.
276 ///
277 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000278 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000279 }
280 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000281 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000282 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000283 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000284 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000285 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000286 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000287 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000288 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000289 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000290 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000291 }
292 virtual void Apply(std::string &Field) {
Bill Wendling914c9702008-06-27 01:27:56 +0000293 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000294 }
295 virtual void Apply(DebugInfoDesc *&Field) {
296 GlobalVariable *GV = NULL;
297
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000298 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000299 if (Field) GV = SR.Serialize(Field);
300
301 // FIXME - At some point should use specific type.
302 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
303
304 if (GV) {
305 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000306 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000307 } else {
308 // Use NULL.
309 Elements.push_back(ConstantPointerNull::get(EmptyTy));
310 }
311 }
312 virtual void Apply(GlobalVariable *&Field) {
313 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000314 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000315 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000316 } else {
317 Elements.push_back(ConstantPointerNull::get(EmptyTy));
318 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000319 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000320 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
321 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
322 unsigned N = Field.size();
323 ArrayType *AT = ArrayType::get(EmptyTy, N);
324 std::vector<Constant *> ArrayElements;
325
Bill Wendling10fff602008-07-03 22:53:42 +0000326 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000327 if (DebugInfoDesc *Element = Field[i]) {
328 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000329 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000330 ArrayElements.push_back(cast<Constant>(CE));
331 } else {
332 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
333 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000334 }
335
336 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000337 GlobalVariable *CAGV = new GlobalVariable(AT, true,
338 GlobalValue::InternalLinkage,
339 CA, "llvm.dbg.array",
340 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000341 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000342 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000343 Elements.push_back(CAE);
344 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000345};
346
347//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000348/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000349/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000350class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000351private:
352 DISerializer &SR; // Active serializer.
353 std::vector<const Type*> &Fields; // Type accumulator.
354
355public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000356 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
Bill Wendling10fff602008-07-03 22:53:42 +0000357 : DIVisitor()
358 , SR(S)
359 , Fields(F)
360 {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000361
362 /// Apply - Set the value of each of the fields.
363 ///
364 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000365 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000366 }
367 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000368 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000369 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000370 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000371 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000372 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000373 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000374 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000375 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000376 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000377 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000378 }
379 virtual void Apply(std::string &Field) {
380 Fields.push_back(SR.getStrPtrType());
381 }
382 virtual void Apply(DebugInfoDesc *&Field) {
383 // FIXME - At some point should use specific type.
384 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
385 Fields.push_back(EmptyTy);
386 }
387 virtual void Apply(GlobalVariable *&Field) {
388 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
389 Fields.push_back(EmptyTy);
390 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000391 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
392 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
393 Fields.push_back(EmptyTy);
394 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000395};
396
397//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000398/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000399/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000400class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000401private:
402 DIVerifier &VR; // Active verifier.
403 bool IsValid; // Validity status.
404 unsigned I; // Current operand index.
405 ConstantStruct *CI; // GlobalVariable constant initializer.
406
407public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000408 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
409 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000410 , VR(V)
411 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000412 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000413 , CI(cast<ConstantStruct>(GV->getInitializer()))
414 {
415 }
416
417 // Accessors.
418 bool isValid() const { return IsValid; }
419
420 /// Apply - Set the value of each of the fields.
421 ///
422 virtual void Apply(int &Field) {
423 Constant *C = CI->getOperand(I++);
424 IsValid = IsValid && isa<ConstantInt>(C);
425 }
426 virtual void Apply(unsigned &Field) {
427 Constant *C = CI->getOperand(I++);
428 IsValid = IsValid && isa<ConstantInt>(C);
429 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000430 virtual void Apply(int64_t &Field) {
431 Constant *C = CI->getOperand(I++);
432 IsValid = IsValid && isa<ConstantInt>(C);
433 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000434 virtual void Apply(uint64_t &Field) {
435 Constant *C = CI->getOperand(I++);
436 IsValid = IsValid && isa<ConstantInt>(C);
437 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000438 virtual void Apply(bool &Field) {
439 Constant *C = CI->getOperand(I++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000440 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000441 }
442 virtual void Apply(std::string &Field) {
443 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000444 IsValid = IsValid &&
445 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000446 }
447 virtual void Apply(DebugInfoDesc *&Field) {
448 // FIXME - Prepare the correct descriptor.
449 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000450 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000451 }
452 virtual void Apply(GlobalVariable *&Field) {
453 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000454 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000455 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000456 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
457 Constant *C = CI->getOperand(I++);
458 IsValid = IsValid && isGlobalVariable(C);
459 if (!IsValid) return;
460
461 GlobalVariable *GV = getGlobalVariable(C);
462 IsValid = IsValid && GV && GV->hasInitializer();
463 if (!IsValid) return;
464
465 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
466 IsValid = IsValid && CA;
467 if (!IsValid) return;
468
469 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
470 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
471 if (!IsValid) return;
472
473 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
474 VR.Verify(GVE);
475 }
476 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000477};
478
Dan Gohman844731a2008-05-13 00:00:25 +0000479}
Jim Laskeyce72b172006-02-11 01:01:30 +0000480
Jim Laskey86cbdba2006-02-06 15:33:21 +0000481//===----------------------------------------------------------------------===//
482
Bill Wendling10fff602008-07-03 22:53:42 +0000483/// TagFromGlobal - Returns the tag number from a debug info descriptor
484/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
485unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
486 ConstantInt *C = getUIntOperand(GV, 0);
487 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
488 (unsigned)DW_TAG_invalid;
489}
490
491/// VersionFromGlobal - Returns the version number from a debug info
492/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
493/// int.
494unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
495 ConstantInt *C = getUIntOperand(GV, 0);
496 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
497 (unsigned)DW_TAG_invalid;
498}
499
500/// DescFactory - Create an instance of debug info descriptor based on Tag.
501/// Return NULL if not a recognized Tag.
502DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
503 switch (Tag) {
504 case DW_TAG_anchor: return new AnchorDesc();
505 case DW_TAG_compile_unit: return new CompileUnitDesc();
506 case DW_TAG_variable: return new GlobalVariableDesc();
507 case DW_TAG_subprogram: return new SubprogramDesc();
508 case DW_TAG_lexical_block: return new BlockDesc();
509 case DW_TAG_base_type: return new BasicTypeDesc();
510 case DW_TAG_typedef:
511 case DW_TAG_pointer_type:
512 case DW_TAG_reference_type:
513 case DW_TAG_const_type:
514 case DW_TAG_volatile_type:
515 case DW_TAG_restrict_type:
516 case DW_TAG_member:
517 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
518 case DW_TAG_array_type:
519 case DW_TAG_structure_type:
520 case DW_TAG_union_type:
521 case DW_TAG_enumeration_type:
522 case DW_TAG_vector_type:
523 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
524 case DW_TAG_subrange_type: return new SubrangeDesc();
525 case DW_TAG_enumerator: return new EnumeratorDesc();
526 case DW_TAG_return_variable:
527 case DW_TAG_arg_variable:
528 case DW_TAG_auto_variable: return new VariableDesc(Tag);
529 default: break;
530 }
531 return NULL;
532}
533
534/// getLinkage - get linkage appropriate for this type of descriptor.
535///
536GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
537 return GlobalValue::InternalLinkage;
538}
539
540/// ApplyToFields - Target the vistor to the fields of the descriptor.
541///
542void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
543 Visitor->Apply(Tag);
544}
545
546//===----------------------------------------------------------------------===//
547
548AnchorDesc::AnchorDesc()
549: DebugInfoDesc(DW_TAG_anchor)
550, AnchorTag(0)
551{}
552AnchorDesc::AnchorDesc(AnchoredDesc *D)
553: DebugInfoDesc(DW_TAG_anchor)
554, AnchorTag(D->getTag())
555{}
556
557// Implement isa/cast/dyncast.
558bool AnchorDesc::classof(const DebugInfoDesc *D) {
559 return D->getTag() == DW_TAG_anchor;
560}
561
562/// getLinkage - get linkage appropriate for this type of descriptor.
563///
564GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
565 return GlobalValue::LinkOnceLinkage;
566}
567
568/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
569///
570void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
571 DebugInfoDesc::ApplyToFields(Visitor);
572
573 Visitor->Apply(AnchorTag);
574}
575
576/// getDescString - Return a string used to compose global names and labels. A
577/// A global variable name needs to be defined for each debug descriptor that is
578/// anchored. NOTE: that each global variable named here also needs to be added
579/// to the list of names left external in the internalizer.
580/// ExternalNames.insert("llvm.dbg.compile_units");
581/// ExternalNames.insert("llvm.dbg.global_variables");
582/// ExternalNames.insert("llvm.dbg.subprograms");
583const char *AnchorDesc::getDescString() const {
584 switch (AnchorTag) {
585 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
586 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
587 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
588 default: break;
589 }
590
591 assert(0 && "Tag does not have a case for anchor string");
592 return "";
593}
594
595/// getTypeString - Return a string used to label this descriptors type.
596///
597const char *AnchorDesc::getTypeString() const {
598 return "llvm.dbg.anchor.type";
599}
600
601#ifndef NDEBUG
602void AnchorDesc::dump() {
603 cerr << getDescString() << " "
604 << "Version(" << getVersion() << "), "
605 << "Tag(" << getTag() << "), "
606 << "AnchorTag(" << AnchorTag << ")\n";
607}
608#endif
609
610//===----------------------------------------------------------------------===//
611
612AnchoredDesc::AnchoredDesc(unsigned T)
613: DebugInfoDesc(T)
614, Anchor(NULL)
615{}
616
617/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
618///
619void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
620 DebugInfoDesc::ApplyToFields(Visitor);
621
622 Visitor->Apply(Anchor);
623}
624
625//===----------------------------------------------------------------------===//
626
627CompileUnitDesc::CompileUnitDesc()
628: AnchoredDesc(DW_TAG_compile_unit)
629, Language(0)
630, FileName("")
631, Directory("")
632, Producer("")
633{}
634
635// Implement isa/cast/dyncast.
636bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
637 return D->getTag() == DW_TAG_compile_unit;
638}
639
640/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
641///
642void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
643 AnchoredDesc::ApplyToFields(Visitor);
644
645 // Handle cases out of sync with compiler.
646 if (getVersion() == 0) {
647 unsigned DebugVersion;
648 Visitor->Apply(DebugVersion);
649 }
650
651 Visitor->Apply(Language);
652 Visitor->Apply(FileName);
653 Visitor->Apply(Directory);
654 Visitor->Apply(Producer);
655}
656
657/// getDescString - Return a string used to compose global names and labels.
658///
659const char *CompileUnitDesc::getDescString() const {
660 return "llvm.dbg.compile_unit";
661}
662
663/// getTypeString - Return a string used to label this descriptors type.
664///
665const char *CompileUnitDesc::getTypeString() const {
666 return "llvm.dbg.compile_unit.type";
667}
668
669/// getAnchorString - Return a string used to label this descriptor's anchor.
670///
671const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
672const char *CompileUnitDesc::getAnchorString() const {
673 return AnchorString;
674}
675
676#ifndef NDEBUG
677void CompileUnitDesc::dump() {
678 cerr << getDescString() << " "
679 << "Version(" << getVersion() << "), "
680 << "Tag(" << getTag() << "), "
681 << "Anchor(" << getAnchor() << "), "
682 << "Language(" << Language << "), "
683 << "FileName(\"" << FileName << "\"), "
684 << "Directory(\"" << Directory << "\"), "
685 << "Producer(\"" << Producer << "\")\n";
686}
687#endif
688
689//===----------------------------------------------------------------------===//
690
691TypeDesc::TypeDesc(unsigned T)
692: DebugInfoDesc(T)
693, Context(NULL)
694, Name("")
695, File(NULL)
696, Line(0)
697, Size(0)
698, Align(0)
699, Offset(0)
700, Flags(0)
701{}
702
703/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
704///
705void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
706 DebugInfoDesc::ApplyToFields(Visitor);
707
708 Visitor->Apply(Context);
709 Visitor->Apply(Name);
710 Visitor->Apply(File);
711 Visitor->Apply(Line);
712 Visitor->Apply(Size);
713 Visitor->Apply(Align);
714 Visitor->Apply(Offset);
715 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
716}
717
718/// getDescString - Return a string used to compose global names and labels.
719///
720const char *TypeDesc::getDescString() const {
721 return "llvm.dbg.type";
722}
723
724/// getTypeString - Return a string used to label this descriptor's type.
725///
726const char *TypeDesc::getTypeString() const {
727 return "llvm.dbg.type.type";
728}
729
730#ifndef NDEBUG
731void TypeDesc::dump() {
732 cerr << getDescString() << " "
733 << "Version(" << getVersion() << "), "
734 << "Tag(" << getTag() << "), "
735 << "Context(" << Context << "), "
736 << "Name(\"" << Name << "\"), "
737 << "File(" << File << "), "
738 << "Line(" << Line << "), "
739 << "Size(" << Size << "), "
740 << "Align(" << Align << "), "
741 << "Offset(" << Offset << "), "
742 << "Flags(" << Flags << ")\n";
743}
744#endif
745
746//===----------------------------------------------------------------------===//
747
748BasicTypeDesc::BasicTypeDesc()
749: TypeDesc(DW_TAG_base_type)
750, Encoding(0)
751{}
752
753// Implement isa/cast/dyncast.
754bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
755 return D->getTag() == DW_TAG_base_type;
756}
757
758/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
759///
760void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
761 TypeDesc::ApplyToFields(Visitor);
762
763 Visitor->Apply(Encoding);
764}
765
766/// getDescString - Return a string used to compose global names and labels.
767///
768const char *BasicTypeDesc::getDescString() const {
769 return "llvm.dbg.basictype";
770}
771
772/// getTypeString - Return a string used to label this descriptor's type.
773///
774const char *BasicTypeDesc::getTypeString() const {
775 return "llvm.dbg.basictype.type";
776}
777
778#ifndef NDEBUG
779void BasicTypeDesc::dump() {
780 cerr << getDescString() << " "
781 << "Version(" << getVersion() << "), "
782 << "Tag(" << getTag() << "), "
783 << "Context(" << getContext() << "), "
784 << "Name(\"" << getName() << "\"), "
785 << "Size(" << getSize() << "), "
786 << "Encoding(" << Encoding << ")\n";
787}
788#endif
789
790//===----------------------------------------------------------------------===//
791
792DerivedTypeDesc::DerivedTypeDesc(unsigned T)
793: TypeDesc(T)
794, FromType(NULL)
795{}
796
797// Implement isa/cast/dyncast.
798bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
799 unsigned T = D->getTag();
800 switch (T) {
801 case DW_TAG_typedef:
802 case DW_TAG_pointer_type:
803 case DW_TAG_reference_type:
804 case DW_TAG_const_type:
805 case DW_TAG_volatile_type:
806 case DW_TAG_restrict_type:
807 case DW_TAG_member:
808 case DW_TAG_inheritance:
809 return true;
810 default: break;
811 }
812 return false;
813}
814
815/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
816///
817void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
818 TypeDesc::ApplyToFields(Visitor);
819
820 Visitor->Apply(FromType);
821}
822
823/// getDescString - Return a string used to compose global names and labels.
824///
825const char *DerivedTypeDesc::getDescString() const {
826 return "llvm.dbg.derivedtype";
827}
828
829/// getTypeString - Return a string used to label this descriptor's type.
830///
831const char *DerivedTypeDesc::getTypeString() const {
832 return "llvm.dbg.derivedtype.type";
833}
834
835#ifndef NDEBUG
836void DerivedTypeDesc::dump() {
837 cerr << getDescString() << " "
838 << "Version(" << getVersion() << "), "
839 << "Tag(" << getTag() << "), "
840 << "Context(" << getContext() << "), "
841 << "Name(\"" << getName() << "\"), "
842 << "Size(" << getSize() << "), "
843 << "File(" << getFile() << "), "
844 << "Line(" << getLine() << "), "
845 << "FromType(" << FromType << ")\n";
846}
847#endif
848
849//===----------------------------------------------------------------------===//
850
851CompositeTypeDesc::CompositeTypeDesc(unsigned T)
852: DerivedTypeDesc(T)
853, Elements()
854{}
855
856// Implement isa/cast/dyncast.
857bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
858 unsigned T = D->getTag();
859 switch (T) {
860 case DW_TAG_array_type:
861 case DW_TAG_structure_type:
862 case DW_TAG_union_type:
863 case DW_TAG_enumeration_type:
864 case DW_TAG_vector_type:
865 case DW_TAG_subroutine_type:
866 return true;
867 default: break;
868 }
869 return false;
870}
871
872/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
873///
874void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
875 DerivedTypeDesc::ApplyToFields(Visitor);
876
877 Visitor->Apply(Elements);
878}
879
880/// getDescString - Return a string used to compose global names and labels.
881///
882const char *CompositeTypeDesc::getDescString() const {
883 return "llvm.dbg.compositetype";
884}
885
886/// getTypeString - Return a string used to label this descriptor's type.
887///
888const char *CompositeTypeDesc::getTypeString() const {
889 return "llvm.dbg.compositetype.type";
890}
891
892#ifndef NDEBUG
893void CompositeTypeDesc::dump() {
894 cerr << getDescString() << " "
895 << "Version(" << getVersion() << "), "
896 << "Tag(" << getTag() << "), "
897 << "Context(" << getContext() << "), "
898 << "Name(\"" << getName() << "\"), "
899 << "Size(" << getSize() << "), "
900 << "File(" << getFile() << "), "
901 << "Line(" << getLine() << "), "
902 << "FromType(" << getFromType() << "), "
903 << "Elements.size(" << Elements.size() << ")\n";
904}
905#endif
906
907//===----------------------------------------------------------------------===//
908
909SubrangeDesc::SubrangeDesc()
910: DebugInfoDesc(DW_TAG_subrange_type)
911, Lo(0)
912, Hi(0)
913{}
914
915// Implement isa/cast/dyncast.
916bool SubrangeDesc::classof(const DebugInfoDesc *D) {
917 return D->getTag() == DW_TAG_subrange_type;
918}
919
920/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
921///
922void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
923 DebugInfoDesc::ApplyToFields(Visitor);
924
925 Visitor->Apply(Lo);
926 Visitor->Apply(Hi);
927}
928
929/// getDescString - Return a string used to compose global names and labels.
930///
931const char *SubrangeDesc::getDescString() const {
932 return "llvm.dbg.subrange";
933}
934
935/// getTypeString - Return a string used to label this descriptor's type.
936///
937const char *SubrangeDesc::getTypeString() const {
938 return "llvm.dbg.subrange.type";
939}
940
941#ifndef NDEBUG
942void SubrangeDesc::dump() {
943 cerr << getDescString() << " "
944 << "Version(" << getVersion() << "), "
945 << "Tag(" << getTag() << "), "
946 << "Lo(" << Lo << "), "
947 << "Hi(" << Hi << ")\n";
948}
949#endif
950
951//===----------------------------------------------------------------------===//
952
953EnumeratorDesc::EnumeratorDesc()
954: DebugInfoDesc(DW_TAG_enumerator)
955, Name("")
956, Value(0)
957{}
958
959// Implement isa/cast/dyncast.
960bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
961 return D->getTag() == DW_TAG_enumerator;
962}
963
964/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
965///
966void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
967 DebugInfoDesc::ApplyToFields(Visitor);
968
969 Visitor->Apply(Name);
970 Visitor->Apply(Value);
971}
972
973/// getDescString - Return a string used to compose global names and labels.
974///
975const char *EnumeratorDesc::getDescString() const {
976 return "llvm.dbg.enumerator";
977}
978
979/// getTypeString - Return a string used to label this descriptor's type.
980///
981const char *EnumeratorDesc::getTypeString() const {
982 return "llvm.dbg.enumerator.type";
983}
984
985#ifndef NDEBUG
986void EnumeratorDesc::dump() {
987 cerr << getDescString() << " "
988 << "Version(" << getVersion() << "), "
989 << "Tag(" << getTag() << "), "
990 << "Name(" << Name << "), "
991 << "Value(" << Value << ")\n";
992}
993#endif
994
995//===----------------------------------------------------------------------===//
996
997VariableDesc::VariableDesc(unsigned T)
998: DebugInfoDesc(T)
999, Context(NULL)
1000, Name("")
1001, File(NULL)
1002, Line(0)
1003, TyDesc(0)
1004{}
1005
1006// Implement isa/cast/dyncast.
1007bool VariableDesc::classof(const DebugInfoDesc *D) {
1008 unsigned T = D->getTag();
1009 switch (T) {
1010 case DW_TAG_auto_variable:
1011 case DW_TAG_arg_variable:
1012 case DW_TAG_return_variable:
1013 return true;
1014 default: break;
1015 }
1016 return false;
1017}
1018
1019/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1020///
1021void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1022 DebugInfoDesc::ApplyToFields(Visitor);
1023
1024 Visitor->Apply(Context);
1025 Visitor->Apply(Name);
1026 Visitor->Apply(File);
1027 Visitor->Apply(Line);
1028 Visitor->Apply(TyDesc);
1029}
1030
1031/// getDescString - Return a string used to compose global names and labels.
1032///
1033const char *VariableDesc::getDescString() const {
1034 return "llvm.dbg.variable";
1035}
1036
1037/// getTypeString - Return a string used to label this descriptor's type.
1038///
1039const char *VariableDesc::getTypeString() const {
1040 return "llvm.dbg.variable.type";
1041}
1042
1043#ifndef NDEBUG
1044void VariableDesc::dump() {
1045 cerr << getDescString() << " "
1046 << "Version(" << getVersion() << "), "
1047 << "Tag(" << getTag() << "), "
1048 << "Context(" << Context << "), "
1049 << "Name(\"" << Name << "\"), "
1050 << "File(" << File << "), "
1051 << "Line(" << Line << "), "
1052 << "TyDesc(" << TyDesc << ")\n";
1053}
1054#endif
1055
1056//===----------------------------------------------------------------------===//
1057
1058GlobalDesc::GlobalDesc(unsigned T)
1059: AnchoredDesc(T)
1060, Context(0)
1061, Name("")
1062, FullName("")
1063, LinkageName("")
1064, File(NULL)
1065, Line(0)
1066, TyDesc(NULL)
1067, IsStatic(false)
1068, IsDefinition(false)
1069{}
1070
1071/// ApplyToFields - Target the visitor to the fields of the global.
1072///
1073void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1074 AnchoredDesc::ApplyToFields(Visitor);
1075
1076 Visitor->Apply(Context);
1077 Visitor->Apply(Name);
1078 Visitor->Apply(FullName);
1079 Visitor->Apply(LinkageName);
1080 Visitor->Apply(File);
1081 Visitor->Apply(Line);
1082 Visitor->Apply(TyDesc);
1083 Visitor->Apply(IsStatic);
1084 Visitor->Apply(IsDefinition);
1085}
1086
1087//===----------------------------------------------------------------------===//
1088
1089GlobalVariableDesc::GlobalVariableDesc()
1090: GlobalDesc(DW_TAG_variable)
1091, Global(NULL)
1092{}
1093
1094// Implement isa/cast/dyncast.
1095bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1096 return D->getTag() == DW_TAG_variable;
1097}
1098
1099/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1100///
1101void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1102 GlobalDesc::ApplyToFields(Visitor);
1103
1104 Visitor->Apply(Global);
1105}
1106
1107/// getDescString - Return a string used to compose global names and labels.
1108///
1109const char *GlobalVariableDesc::getDescString() const {
1110 return "llvm.dbg.global_variable";
1111}
1112
1113/// getTypeString - Return a string used to label this descriptors type.
1114///
1115const char *GlobalVariableDesc::getTypeString() const {
1116 return "llvm.dbg.global_variable.type";
1117}
1118
1119/// getAnchorString - Return a string used to label this descriptor's anchor.
1120///
1121const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1122const char *GlobalVariableDesc::getAnchorString() const {
1123 return AnchorString;
1124}
1125
1126#ifndef NDEBUG
1127void GlobalVariableDesc::dump() {
1128 cerr << getDescString() << " "
1129 << "Version(" << getVersion() << "), "
1130 << "Tag(" << getTag() << "), "
1131 << "Anchor(" << getAnchor() << "), "
1132 << "Name(\"" << getName() << "\"), "
1133 << "FullName(\"" << getFullName() << "\"), "
1134 << "LinkageName(\"" << getLinkageName() << "\"), "
1135 << "File(" << getFile() << "),"
1136 << "Line(" << getLine() << "),"
1137 << "Type(" << getType() << "), "
1138 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1139 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1140 << "Global(" << Global << ")\n";
1141}
1142#endif
1143
1144//===----------------------------------------------------------------------===//
1145
1146SubprogramDesc::SubprogramDesc()
1147: GlobalDesc(DW_TAG_subprogram)
1148{}
1149
1150// Implement isa/cast/dyncast.
1151bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1152 return D->getTag() == DW_TAG_subprogram;
1153}
1154
1155/// ApplyToFields - Target the visitor to the fields of the
1156/// SubprogramDesc.
1157void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1158 GlobalDesc::ApplyToFields(Visitor);
1159}
1160
1161/// getDescString - Return a string used to compose global names and labels.
1162///
1163const char *SubprogramDesc::getDescString() const {
1164 return "llvm.dbg.subprogram";
1165}
1166
1167/// getTypeString - Return a string used to label this descriptors type.
1168///
1169const char *SubprogramDesc::getTypeString() const {
1170 return "llvm.dbg.subprogram.type";
1171}
1172
1173/// getAnchorString - Return a string used to label this descriptor's anchor.
1174///
1175const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1176const char *SubprogramDesc::getAnchorString() const {
1177 return AnchorString;
1178}
1179
1180#ifndef NDEBUG
1181void SubprogramDesc::dump() {
1182 cerr << getDescString() << " "
1183 << "Version(" << getVersion() << "), "
1184 << "Tag(" << getTag() << "), "
1185 << "Anchor(" << getAnchor() << "), "
1186 << "Name(\"" << getName() << "\"), "
1187 << "FullName(\"" << getFullName() << "\"), "
1188 << "LinkageName(\"" << getLinkageName() << "\"), "
1189 << "File(" << getFile() << "),"
1190 << "Line(" << getLine() << "),"
1191 << "Type(" << getType() << "), "
1192 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1193 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1194}
1195#endif
1196
1197//===----------------------------------------------------------------------===//
1198
1199BlockDesc::BlockDesc()
1200: DebugInfoDesc(DW_TAG_lexical_block)
1201, Context(NULL)
1202{}
1203
1204// Implement isa/cast/dyncast.
1205bool BlockDesc::classof(const DebugInfoDesc *D) {
1206 return D->getTag() == DW_TAG_lexical_block;
1207}
1208
1209/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1210///
1211void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1212 DebugInfoDesc::ApplyToFields(Visitor);
1213
1214 Visitor->Apply(Context);
1215}
1216
1217/// getDescString - Return a string used to compose global names and labels.
1218///
1219const char *BlockDesc::getDescString() const {
1220 return "llvm.dbg.block";
1221}
1222
1223/// getTypeString - Return a string used to label this descriptors type.
1224///
1225const char *BlockDesc::getTypeString() const {
1226 return "llvm.dbg.block.type";
1227}
1228
1229#ifndef NDEBUG
1230void BlockDesc::dump() {
1231 cerr << getDescString() << " "
1232 << "Version(" << getVersion() << "), "
1233 << "Tag(" << getTag() << "),"
1234 << "Context(" << Context << ")\n";
1235}
1236#endif
1237
1238//===----------------------------------------------------------------------===//
1239
Jim Laskey86cbdba2006-02-06 15:33:21 +00001240DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001241 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001242}
1243DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001244 // Handle NULL.
1245 if (!GV) return NULL;
1246
Jim Laskey86cbdba2006-02-06 15:33:21 +00001247 // Check to see if it has been already deserialized.
1248 DebugInfoDesc *&Slot = GlobalDescs[GV];
1249 if (Slot) return Slot;
1250
1251 // Get the Tag from the global.
1252 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1253
Jim Laskey86cbdba2006-02-06 15:33:21 +00001254 // Create an empty instance of the correct sort.
1255 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001256
Jim Laskey21407982006-03-14 18:37:57 +00001257 // If not a user defined descriptor.
1258 if (Slot) {
1259 // Deserialize the fields.
1260 DIDeserializeVisitor DRAM(*this, GV);
1261 DRAM.ApplyToFields(Slot);
1262 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001263
1264 return Slot;
1265}
1266
1267//===----------------------------------------------------------------------===//
1268
1269/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001270///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001271const PointerType *DISerializer::getStrPtrType() {
1272 // If not already defined.
1273 if (!StrPtrTy) {
1274 // Construct the pointer to signed bytes.
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001275 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001276 }
1277
1278 return StrPtrTy;
1279}
1280
1281/// getEmptyStructPtrType - Return a "{ }*" type.
1282///
1283const PointerType *DISerializer::getEmptyStructPtrType() {
1284 // If not already defined.
Bill Wendling914c9702008-06-27 01:27:56 +00001285 if (EmptyStructPtrTy) return EmptyStructPtrTy;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001286
Bill Wendling914c9702008-06-27 01:27:56 +00001287 // Construct the pointer to empty structure type.
Bill Wendling10fff602008-07-03 22:53:42 +00001288 const StructType *EmptyStructTy =
1289 StructType::get(std::vector<const Type*>());
Bill Wendling0ac1b6d2008-06-27 01:32:08 +00001290
1291 // Construct the pointer to empty structure type.
1292 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001293 return EmptyStructPtrTy;
1294}
1295
1296/// getTagType - Return the type describing the specified descriptor (via tag.)
1297///
1298const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1299 // Attempt to get the previously defined type.
1300 StructType *&Ty = TagTypes[DD->getTag()];
1301
1302 // If not already defined.
1303 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001304 // Set up fields vector.
1305 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001306 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001307 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001308 GTAM.ApplyToFields(DD);
1309
1310 // Construct structured type.
1311 Ty = StructType::get(Fields);
1312
Jim Laskey86cbdba2006-02-06 15:33:21 +00001313 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001314 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001315 }
1316
1317 return Ty;
1318}
1319
1320/// getString - Construct the string as constant string global.
1321///
Jim Laskeyce72b172006-02-11 01:01:30 +00001322Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001323 // Check string cache for previous edition.
Bill Wendling914c9702008-06-27 01:27:56 +00001324 Constant *&Slot = StringCache[String.c_str()];
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001325
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001326 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001327 if (Slot) return Slot;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001328
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001329 // If empty string then use a sbyte* null instead.
1330 if (String.empty()) {
1331 Slot = ConstantPointerNull::get(getStrPtrType());
1332 } else {
1333 // Construct string as an llvm constant.
1334 Constant *ConstStr = ConstantArray::get(String);
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001335
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001336 // Otherwise create and return a new string global.
1337 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1338 GlobalVariable::InternalLinkage,
Devang Patel1e4c23a2007-05-11 23:14:43 +00001339 ConstStr, ".str", M);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001340 StrGV->setSection("llvm.metadata");
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001341
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001342 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001343 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001344 }
Bill Wendlinge6b6bae2008-06-27 00:56:36 +00001345
Jim Laskeyce72b172006-02-11 01:01:30 +00001346 return Slot;
1347
Jim Laskey86cbdba2006-02-06 15:33:21 +00001348}
1349
1350/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1351/// so that it can be serialized to a .bc or .ll file.
1352GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1353 // Check if the DebugInfoDesc is already in the map.
1354 GlobalVariable *&Slot = DescGlobals[DD];
1355
1356 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1357 if (Slot) return Slot;
1358
Jim Laskey86cbdba2006-02-06 15:33:21 +00001359 // Get the type associated with the Tag.
1360 const StructType *Ty = getTagType(DD);
1361
1362 // Create the GlobalVariable early to prevent infinite recursion.
Bill Wendling10fff602008-07-03 22:53:42 +00001363 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1364 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001365 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001366
1367 // Insert new GlobalVariable in DescGlobals map.
1368 Slot = GV;
1369
1370 // Set up elements vector
1371 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001372 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001373 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001374 SRAM.ApplyToFields(DD);
Bill Wendling10fff602008-07-03 22:53:42 +00001375
Jim Laskey86cbdba2006-02-06 15:33:21 +00001376 // Set the globals initializer.
1377 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1378
1379 return GV;
1380}
1381
Devang Patel962e0752007-11-30 00:51:33 +00001382/// addDescriptor - Directly connect DD with existing GV.
1383void DISerializer::addDescriptor(DebugInfoDesc *DD,
1384 GlobalVariable *GV) {
1385 DescGlobals[DD] = GV;
1386}
1387
Jim Laskey86cbdba2006-02-06 15:33:21 +00001388//===----------------------------------------------------------------------===//
1389
Jim Laskey86cbdba2006-02-06 15:33:21 +00001390/// Verify - Return true if the GlobalVariable appears to be a valid
1391/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001392bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001393 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001394}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001395bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001396 // NULLs are valid.
1397 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001398
Jim Laskey98e04102006-03-26 22:45:20 +00001399 // Check prior validity.
1400 unsigned &ValiditySlot = Validity[GV];
1401
1402 // If visited before then use old state.
1403 if (ValiditySlot) return ValiditySlot == Valid;
1404
1405 // Assume validity for the time being (recursion.)
1406 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001407
1408 // Make sure the global is internal or link once (anchor.)
1409 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1410 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1411 ValiditySlot = Invalid;
1412 return false;
1413 }
Jim Laskey98e04102006-03-26 22:45:20 +00001414
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001415 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001416 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001417
1418 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001419 if (Tag == DW_TAG_invalid) {
1420 ValiditySlot = Valid;
1421 return true;
1422 }
1423
1424 // Get the Version.
1425 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1426
1427 // Check for version mismatch.
1428 if (Version != LLVMDebugVersion) {
1429 ValiditySlot = Invalid;
1430 return false;
1431 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001432
Jim Laskey86cbdba2006-02-06 15:33:21 +00001433 // Construct an empty DebugInfoDesc.
1434 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001435
1436 // Allow for user defined descriptors.
1437 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001438
1439 // Get the initializer constant.
1440 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1441
1442 // Get the operand count.
1443 unsigned N = CI->getNumOperands();
1444
1445 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001446 unsigned &CountSlot = Counts[Tag];
1447 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001448 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001449 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001450 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001451 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001452 }
1453
Jim Laskey21407982006-03-14 18:37:57 +00001454 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001455 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001456 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001457 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001458 return false;
1459 }
1460
1461 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001462 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001463 VRAM.ApplyToFields(DD);
1464
1465 // Release empty DebugInfoDesc.
1466 delete DD;
1467
Jim Laskey98e04102006-03-26 22:45:20 +00001468 // If fields are not valid.
1469 if (!VRAM.isValid()) {
1470 ValiditySlot = Invalid;
1471 return false;
1472 }
1473
1474 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001475}
1476
Evan Chenga844bde2008-02-02 04:07:54 +00001477/// isVerified - Return true if the specified GV has already been
1478/// verified as a debug information descriptor.
1479bool DIVerifier::isVerified(GlobalVariable *GV) {
1480 unsigned &ValiditySlot = Validity[GV];
1481 if (ValiditySlot) return ValiditySlot == Valid;
1482 return false;
1483}
1484
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001485//===----------------------------------------------------------------------===//
1486
Jim Laskeyb8509c52006-03-23 18:07:55 +00001487DebugScope::~DebugScope() {
Bill Wendling10fff602008-07-03 22:53:42 +00001488 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1489 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
Jim Laskeyb8509c52006-03-23 18:07:55 +00001490}
1491
1492//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001493
Jim Laskey6da18642007-01-26 21:38:26 +00001494MachineModuleInfo::MachineModuleInfo()
Devang Patel794fd752007-05-01 21:15:47 +00001495: ImmutablePass((intptr_t)&ID)
1496, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001497, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001498, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001499, Directories()
1500, SourceFiles()
1501, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001502, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001503, ScopeMap()
1504, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001505, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001506, LandingPads()
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001507, Personalities()
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001508, CallsEHReturn(0)
1509, CallsUnwindInit(0)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001510{
1511 // Always emit "no personality" info
1512 Personalities.push_back(NULL);
1513}
Jim Laskey6da18642007-01-26 21:38:26 +00001514MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001515
1516}
1517
Jim Laskey6da18642007-01-26 21:38:26 +00001518/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001519///
Jim Laskey6da18642007-01-26 21:38:26 +00001520bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001521 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001522}
1523
Jim Laskey6da18642007-01-26 21:38:26 +00001524/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001525///
Jim Laskey6da18642007-01-26 21:38:26 +00001526bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001527 return false;
1528}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001529
Jim Laskey6da18642007-01-26 21:38:26 +00001530/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001531///
Jim Laskey6da18642007-01-26 21:38:26 +00001532void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001533 // Coming soon.
1534}
1535
Jim Laskey6da18642007-01-26 21:38:26 +00001536/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001537///
Jim Laskey6da18642007-01-26 21:38:26 +00001538void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001539 // Clean up scope information.
1540 if (RootScope) {
1541 delete RootScope;
1542 ScopeMap.clear();
1543 RootScope = NULL;
1544 }
1545
Jim Laskeyb82313f2007-02-01 16:31:34 +00001546 // Clean up line info.
1547 Lines.clear();
1548
Jim Laskey41886992006-04-07 16:34:46 +00001549 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001550 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001551
1552 // Clean up exception info.
1553 LandingPads.clear();
1554 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +00001555 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +00001556 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001557 CallsEHReturn = 0;
1558 CallsUnwindInit = 0;
Jim Laskey41886992006-04-07 16:34:46 +00001559}
1560
Jim Laskeyd96185a2006-02-13 12:50:39 +00001561/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001562///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001563// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001564DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001565 return DR.Deserialize(V);
1566}
1567
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001568/// AnalyzeModule - Scan the module for global debug information.
1569///
Jim Laskey6da18642007-01-26 21:38:26 +00001570void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001571 SetupCompileUnits(M);
Dale Johannesen48ae02f2008-01-16 19:59:28 +00001572
1573 // Insert functions in the llvm.used array into UsedFunctions.
1574 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1575 if (!GV || !GV->hasInitializer()) return;
1576
1577 // Should be an array of 'i8*'.
1578 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1579 if (InitList == 0) return;
1580
1581 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1582 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1583 if (CE->getOpcode() == Instruction::BitCast)
1584 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1585 UsedFunctions.insert(F);
1586 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001587}
1588
1589/// SetupCompileUnits - Set up the unique vector of compile units.
1590///
Jim Laskey6da18642007-01-26 21:38:26 +00001591void MachineModuleInfo::SetupCompileUnits(Module &M) {
Bill Wendlingc04f4652008-07-03 23:13:02 +00001592 std::vector<CompileUnitDesc *> CU;
1593 getAnchoredDescriptors<CompileUnitDesc>(M, CU);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001594
Bill Wendling10fff602008-07-03 22:53:42 +00001595 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1596 CompileUnits.insert(CU[i]);
1597 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001598}
1599
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001600/// getCompileUnits - Return a vector of debug compile units.
1601///
Jim Laskey6da18642007-01-26 21:38:26 +00001602const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001603 return CompileUnits;
1604}
1605
Jim Laskey0420f2a2006-02-22 19:02:11 +00001606/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1607/// named GlobalVariable.
Bill Wendlingc04f4652008-07-03 23:13:02 +00001608void
Jim Laskey6da18642007-01-26 21:38:26 +00001609MachineModuleInfo::getGlobalVariablesUsing(Module &M,
Bill Wendlingc04f4652008-07-03 23:13:02 +00001610 const std::string &RootName,
1611 std::vector<GlobalVariable*>&Result){
1612 return ::getGlobalVariablesUsing(M, RootName, Result);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001613}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001614
Evan Chenga647c922008-02-01 02:05:57 +00001615/// RecordSourceLine - Records location information and associates it with a
Jim Laskeyb8509c52006-03-23 18:07:55 +00001616/// debug label. Returns a unique label ID used to generate a label and
1617/// provide correspondence to the source line list.
Evan Chenga647c922008-02-01 02:05:57 +00001618unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
1619 unsigned Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001620 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001621 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001622 return ID;
1623}
1624
1625/// RecordSource - Register a source file with debug info. Returns an source
1626/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001627unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1628 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001629 unsigned DirectoryID = Directories.insert(Directory);
1630 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1631}
Jim Laskey6da18642007-01-26 21:38:26 +00001632unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001633 return RecordSource(CompileUnit->getDirectory(),
1634 CompileUnit->getFileName());
1635}
1636
1637/// RecordRegionStart - Indicate the start of a region.
1638///
Jim Laskey6da18642007-01-26 21:38:26 +00001639unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001640 // FIXME - need to be able to handle split scopes because of bb cloning.
1641 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1642 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1643 unsigned ID = NextLabelID();
1644 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1645 return ID;
1646}
1647
1648/// RecordRegionEnd - Indicate the end of a region.
1649///
Jim Laskey6da18642007-01-26 21:38:26 +00001650unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001651 // FIXME - need to be able to handle split scopes because of bb cloning.
1652 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1653 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1654 unsigned ID = NextLabelID();
1655 Scope->setEndLabelID(ID);
1656 return ID;
1657}
1658
1659/// RecordVariable - Indicate the declaration of a local variable.
1660///
Evan Chenga844bde2008-02-02 04:07:54 +00001661void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
1662 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001663 DebugScope *Scope = getOrCreateScope(VD->getContext());
1664 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1665 Scope->AddVariable(DV);
1666}
1667
1668/// getOrCreateScope - Returns the scope associated with the given descriptor.
1669///
Jim Laskey6da18642007-01-26 21:38:26 +00001670DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001671 DebugScope *&Slot = ScopeMap[ScopeDesc];
1672 if (!Slot) {
1673 // FIXME - breaks down when the context is an inlined function.
1674 DebugInfoDesc *ParentDesc = NULL;
1675 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1676 ParentDesc = Block->getContext();
1677 }
1678 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1679 Slot = new DebugScope(Parent, ScopeDesc);
1680 if (Parent) {
1681 Parent->AddScope(Slot);
1682 } else if (RootScope) {
1683 // FIXME - Add inlined function scopes to the root so we can delete
1684 // them later. Long term, handle inlined functions properly.
1685 RootScope->AddScope(Slot);
1686 } else {
1687 // First function is top level function.
1688 RootScope = Slot;
1689 }
1690 }
1691 return Slot;
1692}
1693
Jim Laskey59667fe2007-02-21 22:38:31 +00001694//===-EH-------------------------------------------------------------------===//
1695
1696/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1697/// specified MachineBasicBlock.
Bill Wendling10fff602008-07-03 22:53:42 +00001698LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1699 (MachineBasicBlock *LandingPad) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001700 unsigned N = LandingPads.size();
1701 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001702 LandingPadInfo &LP = LandingPads[i];
1703 if (LP.LandingPadBlock == LandingPad)
1704 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001705 }
1706
1707 LandingPads.push_back(LandingPadInfo(LandingPad));
1708 return LandingPads[N];
1709}
1710
1711/// addInvoke - Provide the begin and end labels of an invoke style call and
1712/// associate it with a try landing pad block.
1713void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1714 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001715 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001716 LP.BeginLabels.push_back(BeginLabel);
1717 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +00001718}
1719
1720/// addLandingPad - Provide the label of a try LandingPad block.
1721///
1722unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1723 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001724 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1725 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001726 return LandingPadLabel;
1727}
1728
1729/// addPersonality - Provide the personality function for the exception
1730/// information.
1731void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001732 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001733 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001734 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001735
Bill Wendling10fff602008-07-03 22:53:42 +00001736 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001737 if (Personalities[i] == Personality)
1738 return;
1739
1740 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +00001741}
1742
1743/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1744///
1745void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1746 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001747 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001748 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001749 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001750}
Duncan Sands73ef58a2007-06-02 16:53:42 +00001751
1752/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +00001753///
Duncan Sands73ef58a2007-06-02 16:53:42 +00001754void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1755 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001756 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling10fff602008-07-03 22:53:42 +00001757 std::vector<unsigned> IdsInFilter (TyInfo.size());
1758 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001759 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
1760 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +00001761}
1762
Duncan Sands6590b042007-08-27 15:47:50 +00001763/// addCleanup - Add a cleanup action for a landing pad.
1764///
1765void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1766 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1767 LP.TypeIds.push_back(0);
1768}
1769
Jim Laskey59667fe2007-02-21 22:38:31 +00001770/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1771/// pads.
1772void MachineModuleInfo::TidyLandingPads() {
1773 for (unsigned i = 0; i != LandingPads.size(); ) {
1774 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001775 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001776
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001777 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +00001778 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001779 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001780 LandingPads.erase(LandingPads.begin() + i);
1781 continue;
1782 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001783
Bill Wendling10fff602008-07-03 22:53:42 +00001784 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001785 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1786 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands57810cd2007-09-05 11:27:52 +00001787
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001788 if (!BeginLabel || !EndLabel) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001789 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1790 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1791 continue;
1792 }
1793
1794 LandingPad.BeginLabels[j] = BeginLabel;
1795 LandingPad.EndLabels[j] = EndLabel;
1796 ++j;
1797 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001798
1799 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +00001800 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +00001801 LandingPads.erase(LandingPads.begin() + i);
1802 continue;
1803 }
1804
1805 // If there is no landing pad, ensure that the list of typeids is empty.
1806 // If the only typeid is a cleanup, this is the same as having no typeids.
1807 if (!LandingPad.LandingPadBlock ||
1808 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1809 LandingPad.TypeIds.clear();
1810
Jim Laskey59667fe2007-02-21 22:38:31 +00001811 ++i;
1812 }
1813}
1814
1815/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1816/// function wide.
1817unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling10fff602008-07-03 22:53:42 +00001818 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1819 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey59667fe2007-02-21 22:38:31 +00001820
1821 TypeInfos.push_back(TI);
1822 return TypeInfos.size();
1823}
1824
Duncan Sands73ef58a2007-06-02 16:53:42 +00001825/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1826/// function wide.
Bill Wendling914c9702008-06-27 01:27:56 +00001827int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +00001828 // If the new filter coincides with the tail of an existing filter, then
1829 // re-use the existing filter. Folding filters more than this requires
1830 // re-ordering filters and/or their elements - probably not worth it.
1831 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1832 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling914c9702008-06-27 01:27:56 +00001833 unsigned i = *I, j = TyIds.size();
Duncan Sands14da32a2007-07-05 15:15:01 +00001834
1835 while (i && j)
1836 if (FilterIds[--i] != TyIds[--j])
1837 goto try_next;
1838
1839 if (!j)
1840 // The new filter coincides with range [i, end) of the existing filter.
1841 return -(1 + i);
Bill Wendling914c9702008-06-27 01:27:56 +00001842
Duncan Sands14da32a2007-07-05 15:15:01 +00001843try_next:;
1844 }
1845
1846 // Add the new filter.
Bill Wendling914c9702008-06-27 01:27:56 +00001847 int FilterID = -(1 + FilterIds.size());
1848 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
1849 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001850 FilterIds.push_back(TyIds[I]);
Bill Wendling914c9702008-06-27 01:27:56 +00001851 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +00001852 FilterIds.push_back(0); // terminator
1853 return FilterID;
1854}
1855
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001856/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +00001857Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001858 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001859 // function
1860 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +00001861}
1862
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001863/// getPersonalityIndex - Return unique index for current personality
1864/// function. NULL personality function should always get zero index.
1865unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001866 const Function* Personality = NULL;
1867
1868 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling10fff602008-07-03 22:53:42 +00001869 for (unsigned i = 0; i != LandingPads.size(); ++i)
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001870 if (LandingPads[i].Personality) {
1871 Personality = LandingPads[i].Personality;
1872 break;
1873 }
1874
Bill Wendling10fff602008-07-03 22:53:42 +00001875 for (unsigned i = 0; i < Personalities.size(); ++i) {
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001876 if (Personalities[i] == Personality)
1877 return i;
Bill Wendling10fff602008-07-03 22:53:42 +00001878 }
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001879
1880 // This should never happen
1881 assert(0 && "Personality function should be set!");
1882 return 0;
1883}
Jim Laskey59667fe2007-02-21 22:38:31 +00001884
Jim Laskey9d4209f2006-11-07 19:33:46 +00001885//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00001886/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1887/// a info consumer to determine if the range of two labels is empty, by seeing
1888/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00001889
1890namespace llvm {
1891
1892struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00001893 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +00001894 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1895
Jim Laskey9d4209f2006-11-07 19:33:46 +00001896 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00001897 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00001898};
1899
Devang Patel19974732007-05-03 01:11:54 +00001900char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00001901
Jim Laskey9d4209f2006-11-07 19:33:46 +00001902bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00001903 // Get machine module info.
1904 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1905 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001906
1907 // Track if change is made.
1908 bool MadeChange = false;
1909 // No prior label to begin.
1910 unsigned PriorLabel = 0;
1911
1912 // Iterate through basic blocks.
1913 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1914 BB != E; ++BB) {
1915 // Iterate through instructions.
1916 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00001917 // Is it a label.
Evan Chengbb81d972008-01-31 09:59:15 +00001918 if (I->isDebugLabel()) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00001919 // The label ID # is always operand #0, an immediate.
1920 unsigned NextLabel = I->getOperand(0).getImm();
1921
1922 // If there was an immediate prior label.
1923 if (PriorLabel) {
1924 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00001925 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00001926 // Delete the current label.
1927 I = BB->erase(I);
1928 // Indicate a change has been made.
1929 MadeChange = true;
1930 continue;
1931 } else {
1932 // Start a new round.
1933 PriorLabel = NextLabel;
1934 }
1935 } else {
1936 // No consecutive labels.
1937 PriorLabel = 0;
1938 }
1939
1940 ++I;
1941 }
1942 }
1943
1944 return MadeChange;
1945}
1946
1947FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1948
1949}
Bill Wendling10fff602008-07-03 22:53:42 +00001950