blob: 44e4ee36ecd8417820daded98fe43692b8febf44 [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"
Bill Wendling305635a2008-06-27 00:09:40 +000017#include "llvm/CodeGen/MachineDebugInfoDesc.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000018#include "llvm/Target/TargetInstrInfo.h"
19#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000020#include "llvm/Target/TargetOptions.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000021#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000022#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000023#include "llvm/Intrinsics.h"
24#include "llvm/Instructions.h"
25#include "llvm/Module.h"
26#include "llvm/Support/Dwarf.h"
Bill Wendling832171c2006-12-07 20:04:42 +000027#include "llvm/Support/Streams.h"
Jim Laskey6af56812006-01-04 13:36:38 +000028using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000029using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000030
31// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman844731a2008-05-13 00:00:25 +000032static RegisterPass<MachineModuleInfo>
33X("machinemoduleinfo", "Module Information");
Devang Patel19974732007-05-03 01:11:54 +000034char MachineModuleInfo::ID = 0;
Jim Laskey063e7652006-01-17 17:31:53 +000035
Jim Laskeyb3e789a2006-01-26 20:21:46 +000036//===----------------------------------------------------------------------===//
37
Jim Laskey86cbdba2006-02-06 15:33:21 +000038/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000039/// specified value in their initializer somewhere.
40static void
Bill Wendling0ac1b6d2008-06-27 01:32:08 +000041getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +000042 // Scan though value users.
43 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
44 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000045 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000046 Result.push_back(GV);
47 } else if (Constant *C = dyn_cast<Constant>(*I)) {
48 // If the user is a constant variable then scan its users
49 getGlobalVariablesUsing(C, Result);
50 }
51 }
52}
53
Jim Laskey86cbdba2006-02-06 15:33:21 +000054/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
55/// named GlobalVariable.
Bill Wendling305635a2008-06-27 00:09:40 +000056static void
57getGlobalVariablesUsing(Module &M, const std::string &RootName,
Bill Wendling0ac1b6d2008-06-27 01:32:08 +000058 std::vector<GlobalVariable*> &Result) {
Jim Laskeyce72b172006-02-11 01:01:30 +000059 std::vector<const Type*> FieldTypes;
Reid Spencer47857812006-12-31 05:55:36 +000060 FieldTypes.push_back(Type::Int32Ty);
61 FieldTypes.push_back(Type::Int32Ty);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000062
Jim Laskey86cbdba2006-02-06 15:33:21 +000063 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000064 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000065 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000066
67 // If present and linkonce then scan for users.
Bill Wendling305635a2008-06-27 00:09:40 +000068 if (UseRoot && UseRoot->hasLinkOnceLinkage())
Jim Laskeyb3e789a2006-01-26 20:21:46 +000069 getGlobalVariablesUsing(UseRoot, Result);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000070}
71
Jim Laskey86cbdba2006-02-06 15:33:21 +000072/// isStringValue - Return true if the given value can be coerced to a string.
73///
74static bool isStringValue(Value *V) {
75 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
76 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
77 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
78 return Init->isString();
79 }
80 } else if (Constant *C = dyn_cast<Constant>(V)) {
81 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
82 return isStringValue(GV);
83 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
84 if (CE->getOpcode() == Instruction::GetElementPtr) {
85 if (CE->getNumOperands() == 3 &&
86 cast<Constant>(CE->getOperand(1))->isNullValue() &&
87 isa<ConstantInt>(CE->getOperand(2))) {
88 return isStringValue(CE->getOperand(0));
89 }
90 }
91 }
92 }
93 return false;
94}
95
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000096/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000097///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000098static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000099 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
100 return GV;
101 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000102 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000103 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000104 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
105 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000106 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000107 return NULL;
108 }
109 return dyn_cast<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000110 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000111 }
112 return NULL;
113}
114
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000115/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000116/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000117static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000118 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
119 return true;
120 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000121 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000122 return isa<GlobalVariable>(CE->getOperand(0));
Dale Johannesen7757fff2008-01-30 19:00:21 +0000123 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
124 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
Dale Johannesen43b8f3b2008-01-30 19:44:39 +0000125 if (!CE->getOperand(i)->isNullValue())
Dale Johannesen7757fff2008-01-30 19:00:21 +0000126 return false;
127 }
128 return isa<GlobalVariable>(CE->getOperand(0));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000129 }
130 }
131 return false;
132}
133
Jim Laskey86cbdba2006-02-06 15:33:21 +0000134//===----------------------------------------------------------------------===//
135
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000136/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000137/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000138void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000139 DD->ApplyToFields(this);
140}
141
Dan Gohman844731a2008-05-13 00:00:25 +0000142namespace {
143
Jim Laskey86cbdba2006-02-06 15:33:21 +0000144//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000145/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
146/// the supplied DebugInfoDesc.
147class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000148private:
149 unsigned Count; // Running count of fields.
150
151public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000152 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000153
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; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000161 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000162 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000163 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; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000167 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
168 ++Count;
169 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000170};
171
172//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000173/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
174/// supplied DebugInfoDesc.
175class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000176private:
177 DIDeserializer &DR; // Active deserializer.
178 unsigned I; // Current operand index.
179 ConstantStruct *CI; // GlobalVariable constant initializer.
180
181public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000182 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
Bill Wendling914c9702008-06-27 01:27:56 +0000183 : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer()))
Jim Laskey86cbdba2006-02-06 15:33:21 +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++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000190 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000191 }
192 virtual void Apply(unsigned &Field) {
193 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000194 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000195 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000196 virtual void Apply(int64_t &Field) {
197 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000198 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000199 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000200 virtual void Apply(uint64_t &Field) {
201 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000202 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000203 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000204 virtual void Apply(bool &Field) {
205 Constant *C = CI->getOperand(I++);
Reid Spencer579dca12007-01-12 04:24:46 +0000206 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000207 }
208 virtual void Apply(std::string &Field) {
209 Constant *C = CI->getOperand(I++);
Evan Cheng0ff39b32008-06-30 07:31:25 +0000210 // Fills in the string if it succeeds
211 if (!GetConstantStringInfo(C, Field))
212 Field.clear();
Jim Laskey86cbdba2006-02-06 15:33:21 +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++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000220 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000221 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000222 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000223 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000224 Constant *C = CI->getOperand(I++);
225 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000226 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 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000238 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000239 }
240 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000241};
242
243//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000244/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000245/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000246class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000247private:
248 DISerializer &SR; // Active serializer.
249 std::vector<Constant*> &Elements; // Element accumulator.
250
251public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000252 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
253 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000254 , SR(S)
255 , Elements(E)
256 {}
257
258 /// Apply - Set the value of each of the fields.
259 ///
260 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000261 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000262 }
263 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000264 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000265 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000266 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000267 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000268 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000269 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000270 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000271 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000272 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000273 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000274 }
275 virtual void Apply(std::string &Field) {
Bill Wendling914c9702008-06-27 01:27:56 +0000276 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000277 }
278 virtual void Apply(DebugInfoDesc *&Field) {
279 GlobalVariable *GV = NULL;
280
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000281 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000282 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.
Reid Spencer15f46d62006-12-12 01:17:41 +0000289 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000290 } 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();
Jim Laskeyce72b172006-02-11 01:01:30 +0000297 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000298 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000299 } else {
300 Elements.push_back(ConstantPointerNull::get(EmptyTy));
301 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000302 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000303 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 Wendlingf0e9c562008-06-27 07:13:44 +0000309 for (unsigned i = 0; i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000310 if (DebugInfoDesc *Element = Field[i]) {
311 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000312 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000313 ArrayElements.push_back(cast<Constant>(CE));
314 } else {
315 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
316 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000317 }
318
319 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000320 GlobalVariable *CAGV = new GlobalVariable(AT, true,
321 GlobalValue::InternalLinkage,
322 CA, "llvm.dbg.array",
323 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000324 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000325 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000326 Elements.push_back(CAE);
327 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000328};
329
330//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000331/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000332/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000333class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000334private:
335 DISerializer &SR; // Active serializer.
336 std::vector<const Type*> &Fields; // Type accumulator.
337
338public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000339 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
340 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000341 , SR(S)
342 , Fields(F)
343 {}
344
345 /// Apply - Set the value of each of the fields.
346 ///
347 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000348 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000349 }
350 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000351 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000352 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000353 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000354 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000355 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000356 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000357 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000358 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000359 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000360 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000361 }
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 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000374 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
375 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
376 Fields.push_back(EmptyTy);
377 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000378};
379
380//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000381/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000382/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000383class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000384private:
385 DIVerifier &VR; // Active verifier.
386 bool IsValid; // Validity status.
387 unsigned I; // Current operand index.
388 ConstantStruct *CI; // GlobalVariable constant initializer.
389
390public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000391 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
392 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000393 , VR(V)
394 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000395 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000396 , 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 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000413 virtual void Apply(int64_t &Field) {
414 Constant *C = CI->getOperand(I++);
415 IsValid = IsValid && isa<ConstantInt>(C);
416 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000417 virtual void Apply(uint64_t &Field) {
418 Constant *C = CI->getOperand(I++);
419 IsValid = IsValid && isa<ConstantInt>(C);
420 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000421 virtual void Apply(bool &Field) {
422 Constant *C = CI->getOperand(I++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000423 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000424 }
425 virtual void Apply(std::string &Field) {
426 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000427 IsValid = IsValid &&
428 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000429 }
430 virtual void Apply(DebugInfoDesc *&Field) {
431 // FIXME - Prepare the correct descriptor.
432 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000433 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000434 }
435 virtual void Apply(GlobalVariable *&Field) {
436 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000437 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000438 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000439 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 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000460};
461
Dan Gohman844731a2008-05-13 00:00:25 +0000462}
Jim Laskeyce72b172006-02-11 01:01:30 +0000463
Jim Laskey86cbdba2006-02-06 15:33:21 +0000464//===----------------------------------------------------------------------===//
465
Jim Laskey86cbdba2006-02-06 15:33:21 +0000466DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000467 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000468}
469DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000470 // Handle NULL.
471 if (!GV) return NULL;
472
Jim Laskey86cbdba2006-02-06 15:33:21 +0000473 // 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
Jim Laskey86cbdba2006-02-06 15:33:21 +0000480 // Create an empty instance of the correct sort.
481 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000482
Jim Laskey21407982006-03-14 18:37:57 +0000483 // If not a user defined descriptor.
484 if (Slot) {
485 // Deserialize the fields.
486 DIDeserializeVisitor DRAM(*this, GV);
487 DRAM.ApplyToFields(Slot);
488 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000489
490 return Slot;
491}
492
493//===----------------------------------------------------------------------===//
494
495/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000496///
Jim Laskey86cbdba2006-02-06 15:33:21 +0000497const PointerType *DISerializer::getStrPtrType() {
498 // If not already defined.
499 if (!StrPtrTy) {
500 // Construct the pointer to signed bytes.
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000501 StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000502 }
503
504 return StrPtrTy;
505}
506
507/// getEmptyStructPtrType - Return a "{ }*" type.
508///
509const PointerType *DISerializer::getEmptyStructPtrType() {
510 // If not already defined.
Bill Wendling914c9702008-06-27 01:27:56 +0000511 if (EmptyStructPtrTy) return EmptyStructPtrTy;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +0000512
Bill Wendling914c9702008-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 Wendling0ac1b6d2008-06-27 01:32:08 +0000516
517 // Construct the pointer to empty structure type.
518 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
Jim Laskey86cbdba2006-02-06 15:33:21 +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) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000530 // Set up fields vector.
531 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +0000532 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000533 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000534 GTAM.ApplyToFields(DD);
535
536 // Construct structured type.
537 Ty = StructType::get(Fields);
538
Jim Laskey86cbdba2006-02-06 15:33:21 +0000539 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +0000540 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000541 }
542
543 return Ty;
544}
545
546/// getString - Construct the string as constant string global.
547///
Jim Laskeyce72b172006-02-11 01:01:30 +0000548Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000549 // Check string cache for previous edition.
Bill Wendling914c9702008-06-27 01:27:56 +0000550 Constant *&Slot = StringCache[String.c_str()];
Bill Wendlinge6b6bae2008-06-27 00:56:36 +0000551
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000552 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000553 if (Slot) return Slot;
Bill Wendlinge6b6bae2008-06-27 00:56:36 +0000554
Jim Laskey9d0ff8e2006-03-15 19:09:58 +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 Wendlinge6b6bae2008-06-27 00:56:36 +0000561
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000562 // Otherwise create and return a new string global.
563 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
564 GlobalVariable::InternalLinkage,
Devang Patel1e4c23a2007-05-11 23:14:43 +0000565 ConstStr, ".str", M);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000566 StrGV->setSection("llvm.metadata");
Bill Wendlinge6b6bae2008-06-27 00:56:36 +0000567
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000568 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +0000569 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000570 }
Bill Wendlinge6b6bae2008-06-27 00:56:36 +0000571
Jim Laskeyce72b172006-02-11 01:01:30 +0000572 return Slot;
573
Jim Laskey86cbdba2006-02-06 15:33:21 +0000574}
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
Jim Laskey86cbdba2006-02-06 15:33:21 +0000585 // Get the type associated with the Tag.
586 const StructType *Ty = getTagType(DD);
587
588 // Create the GlobalVariable early to prevent infinite recursion.
Bill Wendlinga28cd122008-07-01 22:08:01 +0000589 GlobalVariable *GV =
590 new GlobalVariable(Ty, true,
591 (GlobalValue::LinkageTypes)DD->getLinkage(),
592 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +0000593 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +0000594
595 // Insert new GlobalVariable in DescGlobals map.
596 Slot = GV;
597
598 // Set up elements vector
599 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +0000600 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000601 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000602 SRAM.ApplyToFields(DD);
603
604 // Set the globals initializer.
605 GV->setInitializer(ConstantStruct::get(Ty, Elements));
606
607 return GV;
608}
609
Devang Patel962e0752007-11-30 00:51:33 +0000610/// addDescriptor - Directly connect DD with existing GV.
611void DISerializer::addDescriptor(DebugInfoDesc *DD,
612 GlobalVariable *GV) {
613 DescGlobals[DD] = GV;
614}
615
Jim Laskey86cbdba2006-02-06 15:33:21 +0000616//===----------------------------------------------------------------------===//
617
Jim Laskey86cbdba2006-02-06 15:33:21 +0000618/// Verify - Return true if the GlobalVariable appears to be a valid
619/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +0000620bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +0000621 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +0000622}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000623bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +0000624 // NULLs are valid.
625 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000626
Jim Laskey98e04102006-03-26 22:45:20 +0000627 // Check prior validity.
628 unsigned &ValiditySlot = Validity[GV];
629
630 // If visited before then use old state.
631 if (ValiditySlot) return ValiditySlot == Valid;
632
633 // Assume validity for the time being (recursion.)
634 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +0000635
636 // Make sure the global is internal or link once (anchor.)
637 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
638 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
639 ValiditySlot = Invalid;
640 return false;
641 }
Jim Laskey98e04102006-03-26 22:45:20 +0000642
Jim Laskey2bbff6d2006-11-30 18:29:23 +0000643 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000644 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000645
646 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +0000647 if (Tag == DW_TAG_invalid) {
648 ValiditySlot = Valid;
649 return true;
650 }
651
652 // Get the Version.
653 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
654
655 // Check for version mismatch.
656 if (Version != LLVMDebugVersion) {
657 ValiditySlot = Invalid;
658 return false;
659 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000660
Jim Laskey86cbdba2006-02-06 15:33:21 +0000661 // Construct an empty DebugInfoDesc.
662 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +0000663
664 // Allow for user defined descriptors.
665 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000666
667 // Get the initializer constant.
668 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
669
670 // Get the operand count.
671 unsigned N = CI->getNumOperands();
672
673 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +0000674 unsigned &CountSlot = Counts[Tag];
675 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000676 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000677 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000678 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +0000679 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000680 }
681
Jim Laskey21407982006-03-14 18:37:57 +0000682 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +0000683 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000684 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +0000685 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000686 return false;
687 }
688
689 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000690 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000691 VRAM.ApplyToFields(DD);
692
693 // Release empty DebugInfoDesc.
694 delete DD;
695
Jim Laskey98e04102006-03-26 22:45:20 +0000696 // If fields are not valid.
697 if (!VRAM.isValid()) {
698 ValiditySlot = Invalid;
699 return false;
700 }
701
702 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000703}
704
Evan Chenga844bde2008-02-02 04:07:54 +0000705/// isVerified - Return true if the specified GV has already been
706/// verified as a debug information descriptor.
707bool DIVerifier::isVerified(GlobalVariable *GV) {
708 unsigned &ValiditySlot = Validity[GV];
709 if (ValiditySlot) return ValiditySlot == Valid;
710 return false;
711}
712
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000713//===----------------------------------------------------------------------===//
714
Jim Laskeyb8509c52006-03-23 18:07:55 +0000715DebugScope::~DebugScope() {
Bill Wendlingf0e9c562008-06-27 07:13:44 +0000716 for (unsigned i = 0, e = Scopes.size(); i < e; ++i) delete Scopes[i];
717 for (unsigned i = 0, e = Variables.size(); i < e; ++i) delete Variables[i];
Jim Laskeyb8509c52006-03-23 18:07:55 +0000718}
719
720//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000721
Jim Laskey6da18642007-01-26 21:38:26 +0000722MachineModuleInfo::MachineModuleInfo()
Devang Patel794fd752007-05-01 21:15:47 +0000723: ImmutablePass((intptr_t)&ID)
724, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +0000725, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000726, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000727, Directories()
728, SourceFiles()
729, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +0000730, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +0000731, ScopeMap()
732, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +0000733, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +0000734, LandingPads()
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000735, Personalities()
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000736, CallsEHReturn(0)
737, CallsUnwindInit(0)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000738{
739 // Always emit "no personality" info
740 Personalities.push_back(NULL);
741}
Jim Laskey6da18642007-01-26 21:38:26 +0000742MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000743
744}
745
Jim Laskey6da18642007-01-26 21:38:26 +0000746/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000747///
Jim Laskey6da18642007-01-26 21:38:26 +0000748bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000749 return false;
Jim Laskey6af56812006-01-04 13:36:38 +0000750}
751
Jim Laskey6da18642007-01-26 21:38:26 +0000752/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000753///
Jim Laskey6da18642007-01-26 21:38:26 +0000754bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000755 return false;
756}
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000757
Jim Laskey6da18642007-01-26 21:38:26 +0000758/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +0000759///
Jim Laskey6da18642007-01-26 21:38:26 +0000760void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +0000761 // Coming soon.
762}
763
Jim Laskey6da18642007-01-26 21:38:26 +0000764/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +0000765///
Jim Laskey6da18642007-01-26 21:38:26 +0000766void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +0000767 // Clean up scope information.
768 if (RootScope) {
769 delete RootScope;
770 ScopeMap.clear();
771 RootScope = NULL;
772 }
773
Jim Laskeyb82313f2007-02-01 16:31:34 +0000774 // Clean up line info.
775 Lines.clear();
776
Jim Laskey41886992006-04-07 16:34:46 +0000777 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +0000778 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +0000779
780 // Clean up exception info.
781 LandingPads.clear();
782 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +0000783 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +0000784 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000785 CallsEHReturn = 0;
786 CallsUnwindInit = 0;
Jim Laskey41886992006-04-07 16:34:46 +0000787}
788
Jim Laskeyd96185a2006-02-13 12:50:39 +0000789/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +0000790///
Jim Laskeyd96185a2006-02-13 12:50:39 +0000791// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +0000792DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000793 return DR.Deserialize(V);
794}
795
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000796/// AnalyzeModule - Scan the module for global debug information.
797///
Jim Laskey6da18642007-01-26 21:38:26 +0000798void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000799 SetupCompileUnits(M);
Dale Johannesen48ae02f2008-01-16 19:59:28 +0000800
801 // Insert functions in the llvm.used array into UsedFunctions.
802 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
803 if (!GV || !GV->hasInitializer()) return;
804
805 // Should be an array of 'i8*'.
806 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
807 if (InitList == 0) return;
808
809 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
810 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
811 if (CE->getOpcode() == Instruction::BitCast)
812 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
813 UsedFunctions.insert(F);
814 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000815}
816
817/// SetupCompileUnits - Set up the unique vector of compile units.
818///
Jim Laskey6da18642007-01-26 21:38:26 +0000819void MachineModuleInfo::SetupCompileUnits(Module &M) {
Bill Wendling305635a2008-06-27 00:09:40 +0000820 std::vector<void*> CUList;
821 CompileUnitDesc CUD;
822 getAnchoredDescriptors(M, &CUD, CUList);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000823
Bill Wendlingf0e9c562008-06-27 07:13:44 +0000824 for (unsigned i = 0, e = CUList.size(); i < e; i++)
Bill Wendling305635a2008-06-27 00:09:40 +0000825 CompileUnits.insert((CompileUnitDesc*)CUList[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000826}
827
Jim Laskey6e87c0e2006-01-26 21:22:49 +0000828/// getCompileUnits - Return a vector of debug compile units.
829///
Jim Laskey6da18642007-01-26 21:38:26 +0000830const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +0000831 return CompileUnits;
832}
833
Bill Wendling305635a2008-06-27 00:09:40 +0000834/// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
835///
836void
837MachineModuleInfo::getAnchoredDescriptors(Module &M, const AnchoredDesc *Desc,
838 std::vector<void*> &AnchoredDescs) {
Bill Wendling0ac1b6d2008-06-27 01:32:08 +0000839 std::vector<GlobalVariable*> Globals;
Bill Wendling305635a2008-06-27 00:09:40 +0000840 getGlobalVariablesUsing(M, Desc->getAnchorString(), Globals);
841
Bill Wendlingf0e9c562008-06-27 07:13:44 +0000842 for (unsigned i = 0, e = Globals.size(); i < e; ++i) {
Bill Wendling305635a2008-06-27 00:09:40 +0000843 GlobalVariable *GV = Globals[i];
844
845 // FIXME - In the short term, changes are too drastic to continue.
846 if (DebugInfoDesc::TagFromGlobal(GV) == Desc->getTag() &&
847 DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion)
848 AnchoredDescs.push_back(DR.Deserialize(GV));
849 }
850}
851
Jim Laskey0420f2a2006-02-22 19:02:11 +0000852/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
853/// named GlobalVariable.
Bill Wendling305635a2008-06-27 00:09:40 +0000854void
Jim Laskey6da18642007-01-26 21:38:26 +0000855MachineModuleInfo::getGlobalVariablesUsing(Module &M,
Bill Wendling305635a2008-06-27 00:09:40 +0000856 const std::string &RootName,
857 std::vector<GlobalVariable*> &Globals) {
858 return ::getGlobalVariablesUsing(M, RootName, Globals);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000859}
Jim Laskeyb8509c52006-03-23 18:07:55 +0000860
Evan Chenga647c922008-02-01 02:05:57 +0000861/// RecordSourceLine - Records location information and associates it with a
Jim Laskeyb8509c52006-03-23 18:07:55 +0000862/// debug label. Returns a unique label ID used to generate a label and
863/// provide correspondence to the source line list.
Evan Chenga647c922008-02-01 02:05:57 +0000864unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
865 unsigned Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +0000866 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +0000867 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +0000868 return ID;
869}
870
871/// RecordSource - Register a source file with debug info. Returns an source
872/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +0000873unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
874 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +0000875 unsigned DirectoryID = Directories.insert(Directory);
876 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
877}
Jim Laskey6da18642007-01-26 21:38:26 +0000878unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +0000879 return RecordSource(CompileUnit->getDirectory(),
880 CompileUnit->getFileName());
881}
882
883/// RecordRegionStart - Indicate the start of a region.
884///
Jim Laskey6da18642007-01-26 21:38:26 +0000885unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +0000886 // FIXME - need to be able to handle split scopes because of bb cloning.
887 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
888 DebugScope *Scope = getOrCreateScope(ScopeDesc);
889 unsigned ID = NextLabelID();
890 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
891 return ID;
892}
893
894/// RecordRegionEnd - Indicate the end of a region.
895///
Jim Laskey6da18642007-01-26 21:38:26 +0000896unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +0000897 // FIXME - need to be able to handle split scopes because of bb cloning.
898 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
899 DebugScope *Scope = getOrCreateScope(ScopeDesc);
900 unsigned ID = NextLabelID();
901 Scope->setEndLabelID(ID);
902 return ID;
903}
904
905/// RecordVariable - Indicate the declaration of a local variable.
906///
Evan Chenga844bde2008-02-02 04:07:54 +0000907void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
908 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
Jim Laskeyb8509c52006-03-23 18:07:55 +0000909 DebugScope *Scope = getOrCreateScope(VD->getContext());
910 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
911 Scope->AddVariable(DV);
912}
913
914/// getOrCreateScope - Returns the scope associated with the given descriptor.
915///
Jim Laskey6da18642007-01-26 21:38:26 +0000916DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +0000917 DebugScope *&Slot = ScopeMap[ScopeDesc];
918 if (!Slot) {
919 // FIXME - breaks down when the context is an inlined function.
920 DebugInfoDesc *ParentDesc = NULL;
921 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
922 ParentDesc = Block->getContext();
923 }
924 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
925 Slot = new DebugScope(Parent, ScopeDesc);
926 if (Parent) {
927 Parent->AddScope(Slot);
928 } else if (RootScope) {
929 // FIXME - Add inlined function scopes to the root so we can delete
930 // them later. Long term, handle inlined functions properly.
931 RootScope->AddScope(Slot);
932 } else {
933 // First function is top level function.
934 RootScope = Slot;
935 }
936 }
937 return Slot;
938}
939
Jim Laskey59667fe2007-02-21 22:38:31 +0000940//===-EH-------------------------------------------------------------------===//
941
942/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
943/// specified MachineBasicBlock.
Bill Wendlingf0e9c562008-06-27 07:13:44 +0000944LandingPadInfo &
945MachineModuleInfo::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
Jim Laskey59667fe2007-02-21 22:38:31 +0000946 unsigned N = LandingPads.size();
Bill Wendlingf0e9c562008-06-27 07:13:44 +0000947
Jim Laskey59667fe2007-02-21 22:38:31 +0000948 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +0000949 LandingPadInfo &LP = LandingPads[i];
950 if (LP.LandingPadBlock == LandingPad)
951 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +0000952 }
953
954 LandingPads.push_back(LandingPadInfo(LandingPad));
955 return LandingPads[N];
956}
957
958/// addInvoke - Provide the begin and end labels of an invoke style call and
959/// associate it with a try landing pad block.
960void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
961 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +0000962 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +0000963 LP.BeginLabels.push_back(BeginLabel);
964 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +0000965}
966
967/// addLandingPad - Provide the label of a try LandingPad block.
968///
969unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
970 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +0000971 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
972 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +0000973 return LandingPadLabel;
974}
975
976/// addPersonality - Provide the personality function for the exception
977/// information.
978void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000979 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +0000980 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000981 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000982
Bill Wendlingf0e9c562008-06-27 07:13:44 +0000983 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000984 if (Personalities[i] == Personality)
985 return;
986
987 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +0000988}
989
990/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
991///
992void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
993 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +0000994 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +0000995 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +0000996 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +0000997}
Duncan Sands73ef58a2007-06-02 16:53:42 +0000998
999/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +00001000///
Duncan Sands73ef58a2007-06-02 16:53:42 +00001001void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
1002 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001003 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendlingf0e9c562008-06-27 07:13:44 +00001004 unsigned TyInfoSize = TyInfo.size();
1005 std::vector<unsigned> IdsInFilter(TyInfoSize);
1006
1007 for (unsigned I = 0; I != TyInfoSize; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001008 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
Bill Wendlingf0e9c562008-06-27 07:13:44 +00001009
Duncan Sands73ef58a2007-06-02 16:53:42 +00001010 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +00001011}
1012
Duncan Sands6590b042007-08-27 15:47:50 +00001013/// addCleanup - Add a cleanup action for a landing pad.
1014///
1015void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
1016 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1017 LP.TypeIds.push_back(0);
1018}
1019
Jim Laskey59667fe2007-02-21 22:38:31 +00001020/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1021/// pads.
1022void MachineModuleInfo::TidyLandingPads() {
1023 for (unsigned i = 0; i != LandingPads.size(); ) {
1024 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001025 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001026
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001027 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +00001028 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001029 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001030 LandingPads.erase(LandingPads.begin() + i);
1031 continue;
1032 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001033
Bill Wendlingf0e9c562008-06-27 07:13:44 +00001034 for (unsigned j = 0; j != LandingPads[i].BeginLabels.size(); ) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001035 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1036 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands57810cd2007-09-05 11:27:52 +00001037
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001038 if (!BeginLabel || !EndLabel) {
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001039 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1040 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1041 continue;
1042 }
1043
1044 LandingPad.BeginLabels[j] = BeginLabel;
1045 LandingPad.EndLabels[j] = EndLabel;
1046 ++j;
1047 }
Duncan Sands57810cd2007-09-05 11:27:52 +00001048
1049 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +00001050 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +00001051 LandingPads.erase(LandingPads.begin() + i);
1052 continue;
1053 }
1054
1055 // If there is no landing pad, ensure that the list of typeids is empty.
1056 // If the only typeid is a cleanup, this is the same as having no typeids.
1057 if (!LandingPad.LandingPadBlock ||
1058 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
1059 LandingPad.TypeIds.clear();
1060
Jim Laskey59667fe2007-02-21 22:38:31 +00001061 ++i;
1062 }
1063}
1064
1065/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1066/// function wide.
1067unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendlingf0e9c562008-06-27 07:13:44 +00001068 for (unsigned i = 0, e = TypeInfos.size(); i != e; ++i)
1069 if (TypeInfos[i] == TI)
1070 return i + 1;
Jim Laskey59667fe2007-02-21 22:38:31 +00001071
1072 TypeInfos.push_back(TI);
1073 return TypeInfos.size();
1074}
1075
Duncan Sands73ef58a2007-06-02 16:53:42 +00001076/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
1077/// function wide.
Bill Wendling914c9702008-06-27 01:27:56 +00001078int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +00001079 // If the new filter coincides with the tail of an existing filter, then
1080 // re-use the existing filter. Folding filters more than this requires
1081 // re-ordering filters and/or their elements - probably not worth it.
1082 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
1083 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling914c9702008-06-27 01:27:56 +00001084 unsigned i = *I, j = TyIds.size();
Duncan Sands14da32a2007-07-05 15:15:01 +00001085
1086 while (i && j)
1087 if (FilterIds[--i] != TyIds[--j])
1088 goto try_next;
1089
1090 if (!j)
1091 // The new filter coincides with range [i, end) of the existing filter.
1092 return -(1 + i);
Bill Wendling914c9702008-06-27 01:27:56 +00001093
Duncan Sands14da32a2007-07-05 15:15:01 +00001094try_next:;
1095 }
1096
1097 // Add the new filter.
Bill Wendling914c9702008-06-27 01:27:56 +00001098 int FilterID = -(1 + FilterIds.size());
1099 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
Bill Wendlingf0e9c562008-06-27 07:13:44 +00001100
Bill Wendling914c9702008-06-27 01:27:56 +00001101 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +00001102 FilterIds.push_back(TyIds[I]);
Bill Wendlingf0e9c562008-06-27 07:13:44 +00001103
Bill Wendling914c9702008-06-27 01:27:56 +00001104 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +00001105 FilterIds.push_back(0); // terminator
1106 return FilterID;
1107}
1108
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001109/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +00001110Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001111 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001112 // function
1113 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +00001114}
1115
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001116/// getPersonalityIndex - Return unique index for current personality
1117/// function. NULL personality function should always get zero index.
1118unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001119 const Function* Personality = NULL;
1120
1121 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendlingf0e9c562008-06-27 07:13:44 +00001122 for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
Anton Korobeynikov070280e2007-05-23 11:08:31 +00001123 if (LandingPads[i].Personality) {
1124 Personality = LandingPads[i].Personality;
1125 break;
1126 }
1127
Bill Wendlingf0e9c562008-06-27 07:13:44 +00001128 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001129 if (Personalities[i] == Personality)
1130 return i;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00001131
1132 // This should never happen
1133 assert(0 && "Personality function should be set!");
1134 return 0;
1135}
Jim Laskey59667fe2007-02-21 22:38:31 +00001136
Jim Laskey9d4209f2006-11-07 19:33:46 +00001137//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00001138/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1139/// a info consumer to determine if the range of two labels is empty, by seeing
1140/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00001141
1142namespace llvm {
1143
1144struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00001145 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +00001146 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1147
Jim Laskey9d4209f2006-11-07 19:33:46 +00001148 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00001149 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00001150};
1151
Devang Patel19974732007-05-03 01:11:54 +00001152char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00001153
Jim Laskey9d4209f2006-11-07 19:33:46 +00001154bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00001155 // Get machine module info.
1156 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1157 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001158
1159 // Track if change is made.
1160 bool MadeChange = false;
1161 // No prior label to begin.
1162 unsigned PriorLabel = 0;
1163
1164 // Iterate through basic blocks.
1165 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1166 BB != E; ++BB) {
1167 // Iterate through instructions.
1168 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00001169 // Is it a label.
Evan Chengbb81d972008-01-31 09:59:15 +00001170 if (I->isDebugLabel()) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00001171 // The label ID # is always operand #0, an immediate.
1172 unsigned NextLabel = I->getOperand(0).getImm();
1173
1174 // If there was an immediate prior label.
1175 if (PriorLabel) {
1176 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00001177 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00001178 // Delete the current label.
1179 I = BB->erase(I);
1180 // Indicate a change has been made.
1181 MadeChange = true;
1182 continue;
1183 } else {
1184 // Start a new round.
1185 PriorLabel = NextLabel;
1186 }
1187 } else {
1188 // No consecutive labels.
1189 PriorLabel = 0;
1190 }
1191
1192 ++I;
1193 }
1194 }
1195
1196 return MadeChange;
1197}
1198
1199FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1200
1201}