Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===// |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 9 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 11 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 12 | #include "llvm/Constants.h" |
Eric Christopher | 0d2b0ab | 2008-06-26 00:31:12 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/ValueTracking.h" |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 15 | #include "llvm/CodeGen/MachineFunction.h" |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineLocation.h" |
Bill Wendling | 305635a | 2008-06-27 00:09:40 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineDebugInfoDesc.h" |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetInstrInfo.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
Jim Laskey | c1c47c3 | 2007-01-29 23:40:33 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetOptions.h" |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 22 | #include "llvm/GlobalVariable.h" |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
| 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/Module.h" |
| 26 | #include "llvm/Support/Dwarf.h" |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Jim Laskey | 9c4447a | 2006-03-01 20:39:36 +0000 | [diff] [blame] | 29 | using namespace llvm::dwarf; |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 30 | |
| 31 | // Handle the Pass registration stuff necessary to use TargetData's. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 32 | static RegisterPass<MachineModuleInfo> |
| 33 | X("machinemoduleinfo", "Module Information"); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 34 | char MachineModuleInfo::ID = 0; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 35 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 38 | /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 39 | /// specified value in their initializer somewhere. |
| 40 | static void |
| 41 | getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) { |
| 42 | // 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 Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 45 | // If the user is a GlobalVariable then add to result. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 46 | Result.push_back(GV); |
| 47 | } else if (Constant *C = dyn_cast<Constant>(*I)) { |
| 48 | // If the user is a constant variable then scan its users |
| 49 | getGlobalVariablesUsing(C, Result); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 54 | /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the |
| 55 | /// named GlobalVariable. |
Bill Wendling | 305635a | 2008-06-27 00:09:40 +0000 | [diff] [blame] | 56 | static void |
| 57 | getGlobalVariablesUsing(Module &M, const std::string &RootName, |
| 58 | std::vector<GlobalVariable*> &Result) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 59 | std::vector<const Type*> FieldTypes; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 60 | FieldTypes.push_back(Type::Int32Ty); |
| 61 | FieldTypes.push_back(Type::Int32Ty); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 62 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 63 | // Get the GlobalVariable root. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 64 | GlobalVariable *UseRoot = M.getGlobalVariable(RootName, |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 65 | StructType::get(FieldTypes)); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 66 | |
| 67 | // If present and linkonce then scan for users. |
Bill Wendling | 305635a | 2008-06-27 00:09:40 +0000 | [diff] [blame] | 68 | if (UseRoot && UseRoot->hasLinkOnceLinkage()) |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 69 | getGlobalVariablesUsing(UseRoot, Result); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 72 | /// isStringValue - Return true if the given value can be coerced to a string. |
| 73 | /// |
| 74 | static 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 Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 96 | /// getGlobalVariable - Return either a direct or cast Global value. |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 97 | /// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 98 | static GlobalVariable *getGlobalVariable(Value *V) { |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 99 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 100 | return GV; |
| 101 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 102 | if (CE->getOpcode() == Instruction::BitCast) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 103 | return dyn_cast<GlobalVariable>(CE->getOperand(0)); |
Dale Johannesen | 7757fff | 2008-01-30 19:00:21 +0000 | [diff] [blame] | 104 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 105 | for (unsigned int i=1; i<CE->getNumOperands(); i++) { |
Dale Johannesen | 43b8f3b | 2008-01-30 19:44:39 +0000 | [diff] [blame] | 106 | if (!CE->getOperand(i)->isNullValue()) |
Dale Johannesen | 7757fff | 2008-01-30 19:00:21 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | return dyn_cast<GlobalVariable>(CE->getOperand(0)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 110 | } |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 115 | /// isGlobalVariable - Return true if the given value can be coerced to a |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 116 | /// GlobalVariable. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 117 | static bool isGlobalVariable(Value *V) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 118 | if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) { |
| 119 | return true; |
| 120 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 121 | if (CE->getOpcode() == Instruction::BitCast) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 122 | return isa<GlobalVariable>(CE->getOperand(0)); |
Dale Johannesen | 7757fff | 2008-01-30 19:00:21 +0000 | [diff] [blame] | 123 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 124 | for (unsigned int i=1; i<CE->getNumOperands(); i++) { |
Dale Johannesen | 43b8f3b | 2008-01-30 19:44:39 +0000 | [diff] [blame] | 125 | if (!CE->getOperand(i)->isNullValue()) |
Dale Johannesen | 7757fff | 2008-01-30 19:00:21 +0000 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | return isa<GlobalVariable>(CE->getOperand(0)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 134 | //===----------------------------------------------------------------------===// |
| 135 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 136 | /// ApplyToFields - Target the visitor to each field of the debug information |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 137 | /// descriptor. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 138 | void DIVisitor::ApplyToFields(DebugInfoDesc *DD) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 139 | DD->ApplyToFields(this); |
| 140 | } |
| 141 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 142 | namespace { |
| 143 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 144 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 145 | /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug |
| 146 | /// the supplied DebugInfoDesc. |
| 147 | class DICountVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 148 | private: |
| 149 | unsigned Count; // Running count of fields. |
| 150 | |
| 151 | public: |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 152 | DICountVisitor() : DIVisitor(), Count(0) {} |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 153 | |
| 154 | // Accessors. |
| 155 | unsigned getCount() const { return Count; } |
| 156 | |
| 157 | /// Apply - Count each of the fields. |
| 158 | /// |
| 159 | virtual void Apply(int &Field) { ++Count; } |
| 160 | virtual void Apply(unsigned &Field) { ++Count; } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 161 | virtual void Apply(int64_t &Field) { ++Count; } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 162 | virtual void Apply(uint64_t &Field) { ++Count; } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 163 | virtual void Apply(bool &Field) { ++Count; } |
| 164 | virtual void Apply(std::string &Field) { ++Count; } |
| 165 | virtual void Apply(DebugInfoDesc *&Field) { ++Count; } |
| 166 | virtual void Apply(GlobalVariable *&Field) { ++Count; } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 167 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 168 | ++Count; |
| 169 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 173 | /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the |
| 174 | /// supplied DebugInfoDesc. |
| 175 | class DIDeserializeVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 176 | private: |
| 177 | DIDeserializer &DR; // Active deserializer. |
| 178 | unsigned I; // Current operand index. |
| 179 | ConstantStruct *CI; // GlobalVariable constant initializer. |
| 180 | |
| 181 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 182 | DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV) |
| 183 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 184 | , DR(D) |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 185 | , I(0) |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 186 | , CI(cast<ConstantStruct>(GV->getInitializer())) |
| 187 | {} |
| 188 | |
| 189 | /// Apply - Set the value of each of the fields. |
| 190 | /// |
| 191 | virtual void Apply(int &Field) { |
| 192 | Constant *C = CI->getOperand(I++); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 193 | Field = cast<ConstantInt>(C)->getSExtValue(); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 194 | } |
| 195 | virtual void Apply(unsigned &Field) { |
| 196 | Constant *C = CI->getOperand(I++); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 197 | Field = cast<ConstantInt>(C)->getZExtValue(); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 198 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 199 | virtual void Apply(int64_t &Field) { |
| 200 | Constant *C = CI->getOperand(I++); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 201 | Field = cast<ConstantInt>(C)->getSExtValue(); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 202 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 203 | virtual void Apply(uint64_t &Field) { |
| 204 | Constant *C = CI->getOperand(I++); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 205 | Field = cast<ConstantInt>(C)->getZExtValue(); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 206 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 207 | virtual void Apply(bool &Field) { |
| 208 | Constant *C = CI->getOperand(I++); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 209 | Field = cast<ConstantInt>(C)->getZExtValue(); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 210 | } |
| 211 | virtual void Apply(std::string &Field) { |
| 212 | Constant *C = CI->getOperand(I++); |
Owen Anderson | 8342cff | 2008-06-26 17:06:02 +0000 | [diff] [blame] | 213 | // Fills in the string if it succeeds |
| 214 | if (!GetConstantStringInfo(C, Field)) |
| 215 | Field.clear(); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 216 | } |
| 217 | virtual void Apply(DebugInfoDesc *&Field) { |
| 218 | Constant *C = CI->getOperand(I++); |
| 219 | Field = DR.Deserialize(C); |
| 220 | } |
| 221 | virtual void Apply(GlobalVariable *&Field) { |
| 222 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 223 | Field = getGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 224 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 225 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
Jim Laskey | d04c159 | 2006-07-13 15:27:42 +0000 | [diff] [blame] | 226 | Field.resize(0); |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 227 | Constant *C = CI->getOperand(I++); |
| 228 | GlobalVariable *GV = getGlobalVariable(C); |
Jim Laskey | d04c159 | 2006-07-13 15:27:42 +0000 | [diff] [blame] | 229 | if (GV->hasInitializer()) { |
| 230 | if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) { |
| 231 | for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) { |
| 232 | GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i)); |
| 233 | DebugInfoDesc *DE = DR.Deserialize(GVE); |
| 234 | Field.push_back(DE); |
| 235 | } |
| 236 | } else if (GV->getInitializer()->isNullValue()) { |
| 237 | if (const ArrayType *T = |
| 238 | dyn_cast<ArrayType>(GV->getType()->getElementType())) { |
| 239 | Field.resize(T->getNumElements()); |
| 240 | } |
Jim Laskey | 2b0e309 | 2006-03-08 02:07:02 +0000 | [diff] [blame] | 241 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 247 | /// DISerializeVisitor - This DIVisitor serializes all the fields in |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 248 | /// the supplied DebugInfoDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 249 | class DISerializeVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 250 | private: |
| 251 | DISerializer &SR; // Active serializer. |
| 252 | std::vector<Constant*> &Elements; // Element accumulator. |
| 253 | |
| 254 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 255 | DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E) |
| 256 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 257 | , SR(S) |
| 258 | , Elements(E) |
| 259 | {} |
| 260 | |
| 261 | /// Apply - Set the value of each of the fields. |
| 262 | /// |
| 263 | virtual void Apply(int &Field) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 264 | Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field))); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 265 | } |
| 266 | virtual void Apply(unsigned &Field) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 267 | Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field))); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 268 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 269 | virtual void Apply(int64_t &Field) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 270 | Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field))); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 271 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 272 | virtual void Apply(uint64_t &Field) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 273 | Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field))); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 274 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 275 | virtual void Apply(bool &Field) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 276 | Elements.push_back(ConstantInt::get(Type::Int1Ty, Field)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 277 | } |
| 278 | virtual void Apply(std::string &Field) { |
Jim Laskey | 2140798 | 2006-03-14 18:37:57 +0000 | [diff] [blame] | 279 | Elements.push_back(SR.getString(Field)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 280 | } |
| 281 | virtual void Apply(DebugInfoDesc *&Field) { |
| 282 | GlobalVariable *GV = NULL; |
| 283 | |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 284 | // If non-NULL then convert to global. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 285 | if (Field) GV = SR.Serialize(Field); |
| 286 | |
| 287 | // FIXME - At some point should use specific type. |
| 288 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 289 | |
| 290 | if (GV) { |
| 291 | // Set to pointer to global. |
Reid Spencer | 15f46d6 | 2006-12-12 01:17:41 +0000 | [diff] [blame] | 292 | Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 293 | } else { |
| 294 | // Use NULL. |
| 295 | Elements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 296 | } |
| 297 | } |
| 298 | virtual void Apply(GlobalVariable *&Field) { |
| 299 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 300 | if (Field) { |
Reid Spencer | 15f46d6 | 2006-12-12 01:17:41 +0000 | [diff] [blame] | 301 | Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy)); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 302 | } else { |
| 303 | Elements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 304 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 305 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 306 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 307 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 308 | unsigned N = Field.size(); |
| 309 | ArrayType *AT = ArrayType::get(EmptyTy, N); |
| 310 | std::vector<Constant *> ArrayElements; |
| 311 | |
| 312 | for (unsigned i = 0, N = Field.size(); i < N; ++i) { |
Jim Laskey | d04c159 | 2006-07-13 15:27:42 +0000 | [diff] [blame] | 313 | if (DebugInfoDesc *Element = Field[i]) { |
| 314 | GlobalVariable *GVE = SR.Serialize(Element); |
Reid Spencer | 15f46d6 | 2006-12-12 01:17:41 +0000 | [diff] [blame] | 315 | Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy); |
Jim Laskey | d04c159 | 2006-07-13 15:27:42 +0000 | [diff] [blame] | 316 | ArrayElements.push_back(cast<Constant>(CE)); |
| 317 | } else { |
| 318 | ArrayElements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 319 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | Constant *CA = ConstantArray::get(AT, ArrayElements); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 323 | GlobalVariable *CAGV = new GlobalVariable(AT, true, |
| 324 | GlobalValue::InternalLinkage, |
| 325 | CA, "llvm.dbg.array", |
| 326 | SR.getModule()); |
Jim Laskey | 7809811 | 2006-03-07 22:00:35 +0000 | [diff] [blame] | 327 | CAGV->setSection("llvm.metadata"); |
Reid Spencer | 15f46d6 | 2006-12-12 01:17:41 +0000 | [diff] [blame] | 328 | Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy); |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 329 | Elements.push_back(CAE); |
| 330 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 334 | /// DIGetTypesVisitor - This DIVisitor gathers all the field types in |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 335 | /// the supplied DebugInfoDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 336 | class DIGetTypesVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 337 | private: |
| 338 | DISerializer &SR; // Active serializer. |
| 339 | std::vector<const Type*> &Fields; // Type accumulator. |
| 340 | |
| 341 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 342 | DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F) |
| 343 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 344 | , SR(S) |
| 345 | , Fields(F) |
| 346 | {} |
| 347 | |
| 348 | /// Apply - Set the value of each of the fields. |
| 349 | /// |
| 350 | virtual void Apply(int &Field) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 351 | Fields.push_back(Type::Int32Ty); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 352 | } |
| 353 | virtual void Apply(unsigned &Field) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 354 | Fields.push_back(Type::Int32Ty); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 355 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 356 | virtual void Apply(int64_t &Field) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 357 | Fields.push_back(Type::Int64Ty); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 358 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 359 | virtual void Apply(uint64_t &Field) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 360 | Fields.push_back(Type::Int64Ty); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 361 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 362 | virtual void Apply(bool &Field) { |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 363 | Fields.push_back(Type::Int1Ty); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 364 | } |
| 365 | virtual void Apply(std::string &Field) { |
| 366 | Fields.push_back(SR.getStrPtrType()); |
| 367 | } |
| 368 | virtual void Apply(DebugInfoDesc *&Field) { |
| 369 | // FIXME - At some point should use specific type. |
| 370 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 371 | Fields.push_back(EmptyTy); |
| 372 | } |
| 373 | virtual void Apply(GlobalVariable *&Field) { |
| 374 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 375 | Fields.push_back(EmptyTy); |
| 376 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 377 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 378 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 379 | Fields.push_back(EmptyTy); |
| 380 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 381 | }; |
| 382 | |
| 383 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 384 | /// DIVerifyVisitor - This DIVisitor verifies all the field types against |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 385 | /// a constant initializer. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 386 | class DIVerifyVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 387 | private: |
| 388 | DIVerifier &VR; // Active verifier. |
| 389 | bool IsValid; // Validity status. |
| 390 | unsigned I; // Current operand index. |
| 391 | ConstantStruct *CI; // GlobalVariable constant initializer. |
| 392 | |
| 393 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 394 | DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV) |
| 395 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 396 | , VR(V) |
| 397 | , IsValid(true) |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 398 | , I(0) |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 399 | , CI(cast<ConstantStruct>(GV->getInitializer())) |
| 400 | { |
| 401 | } |
| 402 | |
| 403 | // Accessors. |
| 404 | bool isValid() const { return IsValid; } |
| 405 | |
| 406 | /// Apply - Set the value of each of the fields. |
| 407 | /// |
| 408 | virtual void Apply(int &Field) { |
| 409 | Constant *C = CI->getOperand(I++); |
| 410 | IsValid = IsValid && isa<ConstantInt>(C); |
| 411 | } |
| 412 | virtual void Apply(unsigned &Field) { |
| 413 | Constant *C = CI->getOperand(I++); |
| 414 | IsValid = IsValid && isa<ConstantInt>(C); |
| 415 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 416 | virtual void Apply(int64_t &Field) { |
| 417 | Constant *C = CI->getOperand(I++); |
| 418 | IsValid = IsValid && isa<ConstantInt>(C); |
| 419 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 420 | virtual void Apply(uint64_t &Field) { |
| 421 | Constant *C = CI->getOperand(I++); |
| 422 | IsValid = IsValid && isa<ConstantInt>(C); |
| 423 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 424 | virtual void Apply(bool &Field) { |
| 425 | Constant *C = CI->getOperand(I++); |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 426 | IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 427 | } |
| 428 | virtual void Apply(std::string &Field) { |
| 429 | Constant *C = CI->getOperand(I++); |
Jim Laskey | 26a3687 | 2007-01-03 13:46:20 +0000 | [diff] [blame] | 430 | IsValid = IsValid && |
| 431 | (!C || isStringValue(C) || C->isNullValue()); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 432 | } |
| 433 | virtual void Apply(DebugInfoDesc *&Field) { |
| 434 | // FIXME - Prepare the correct descriptor. |
| 435 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 436 | IsValid = IsValid && isGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 437 | } |
| 438 | virtual void Apply(GlobalVariable *&Field) { |
| 439 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 440 | IsValid = IsValid && isGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 441 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 442 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 443 | Constant *C = CI->getOperand(I++); |
| 444 | IsValid = IsValid && isGlobalVariable(C); |
| 445 | if (!IsValid) return; |
| 446 | |
| 447 | GlobalVariable *GV = getGlobalVariable(C); |
| 448 | IsValid = IsValid && GV && GV->hasInitializer(); |
| 449 | if (!IsValid) return; |
| 450 | |
| 451 | ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 452 | IsValid = IsValid && CA; |
| 453 | if (!IsValid) return; |
| 454 | |
| 455 | for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) { |
| 456 | IsValid = IsValid && isGlobalVariable(CA->getOperand(i)); |
| 457 | if (!IsValid) return; |
| 458 | |
| 459 | GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i)); |
| 460 | VR.Verify(GVE); |
| 461 | } |
| 462 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 465 | } |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 466 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 467 | //===----------------------------------------------------------------------===// |
| 468 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 469 | DebugInfoDesc *DIDeserializer::Deserialize(Value *V) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 470 | return Deserialize(getGlobalVariable(V)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 471 | } |
| 472 | DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 473 | // Handle NULL. |
| 474 | if (!GV) return NULL; |
| 475 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 476 | // Check to see if it has been already deserialized. |
| 477 | DebugInfoDesc *&Slot = GlobalDescs[GV]; |
| 478 | if (Slot) return Slot; |
| 479 | |
| 480 | // Get the Tag from the global. |
| 481 | unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); |
| 482 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 483 | // Create an empty instance of the correct sort. |
| 484 | Slot = DebugInfoDesc::DescFactory(Tag); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 485 | |
Jim Laskey | 2140798 | 2006-03-14 18:37:57 +0000 | [diff] [blame] | 486 | // If not a user defined descriptor. |
| 487 | if (Slot) { |
| 488 | // Deserialize the fields. |
| 489 | DIDeserializeVisitor DRAM(*this, GV); |
| 490 | DRAM.ApplyToFields(Slot); |
| 491 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 492 | |
| 493 | return Slot; |
| 494 | } |
| 495 | |
| 496 | //===----------------------------------------------------------------------===// |
| 497 | |
| 498 | /// getStrPtrType - Return a "sbyte *" type. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 499 | /// |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 500 | const PointerType *DISerializer::getStrPtrType() { |
| 501 | // If not already defined. |
| 502 | if (!StrPtrTy) { |
| 503 | // Construct the pointer to signed bytes. |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 504 | StrPtrTy = PointerType::getUnqual(Type::Int8Ty); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | return StrPtrTy; |
| 508 | } |
| 509 | |
| 510 | /// getEmptyStructPtrType - Return a "{ }*" type. |
| 511 | /// |
| 512 | const PointerType *DISerializer::getEmptyStructPtrType() { |
| 513 | // If not already defined. |
| 514 | if (!EmptyStructPtrTy) { |
| 515 | // Construct the empty structure type. |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 516 | const StructType *EmptyStructTy = StructType::get(NULL, NULL); |
| 517 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 518 | // Construct the pointer to empty structure type. |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 519 | EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | return EmptyStructPtrTy; |
| 523 | } |
| 524 | |
| 525 | /// getTagType - Return the type describing the specified descriptor (via tag.) |
| 526 | /// |
| 527 | const StructType *DISerializer::getTagType(DebugInfoDesc *DD) { |
| 528 | // Attempt to get the previously defined type. |
| 529 | StructType *&Ty = TagTypes[DD->getTag()]; |
| 530 | |
| 531 | // If not already defined. |
| 532 | if (!Ty) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 533 | // Set up fields vector. |
| 534 | std::vector<const Type*> Fields; |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 535 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 536 | // Get types of fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 537 | DIGetTypesVisitor GTAM(*this, Fields); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 538 | GTAM.ApplyToFields(DD); |
| 539 | |
| 540 | // Construct structured type. |
| 541 | Ty = StructType::get(Fields); |
| 542 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 543 | // Register type name with module. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 544 | M->addTypeName(DD->getTypeString(), Ty); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return Ty; |
| 548 | } |
| 549 | |
| 550 | /// getString - Construct the string as constant string global. |
| 551 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 552 | Constant *DISerializer::getString(const std::string &String) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 553 | // Check string cache for previous edition. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 554 | Constant *&Slot = StringCache[String]; |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 555 | |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 556 | // Return Constant if previously defined. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 557 | if (Slot) return Slot; |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 558 | |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 559 | // If empty string then use a sbyte* null instead. |
| 560 | if (String.empty()) { |
| 561 | Slot = ConstantPointerNull::get(getStrPtrType()); |
| 562 | } else { |
| 563 | // Construct string as an llvm constant. |
| 564 | Constant *ConstStr = ConstantArray::get(String); |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 565 | |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 566 | // Otherwise create and return a new string global. |
| 567 | GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, |
| 568 | GlobalVariable::InternalLinkage, |
Devang Patel | 1e4c23a | 2007-05-11 23:14:43 +0000 | [diff] [blame] | 569 | ConstStr, ".str", M); |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 570 | StrGV->setSection("llvm.metadata"); |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 571 | |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 572 | // Convert to generic string pointer. |
Reid Spencer | 15f46d6 | 2006-12-12 01:17:41 +0000 | [diff] [blame] | 573 | Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType()); |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 574 | } |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 575 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 576 | return Slot; |
| 577 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /// Serialize - Recursively cast the specified descriptor into a GlobalVariable |
| 581 | /// so that it can be serialized to a .bc or .ll file. |
| 582 | GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) { |
| 583 | // Check if the DebugInfoDesc is already in the map. |
| 584 | GlobalVariable *&Slot = DescGlobals[DD]; |
| 585 | |
| 586 | // See if DebugInfoDesc exists, if so return prior GlobalVariable. |
| 587 | if (Slot) return Slot; |
| 588 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 589 | // Get the type associated with the Tag. |
| 590 | const StructType *Ty = getTagType(DD); |
| 591 | |
| 592 | // Create the GlobalVariable early to prevent infinite recursion. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 593 | GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(), |
| 594 | NULL, DD->getDescString(), M); |
Jim Laskey | 7809811 | 2006-03-07 22:00:35 +0000 | [diff] [blame] | 595 | GV->setSection("llvm.metadata"); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 596 | |
| 597 | // Insert new GlobalVariable in DescGlobals map. |
| 598 | Slot = GV; |
| 599 | |
| 600 | // Set up elements vector |
| 601 | std::vector<Constant*> Elements; |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 602 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 603 | // Add fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 604 | DISerializeVisitor SRAM(*this, Elements); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 605 | SRAM.ApplyToFields(DD); |
| 606 | |
| 607 | // Set the globals initializer. |
| 608 | GV->setInitializer(ConstantStruct::get(Ty, Elements)); |
| 609 | |
| 610 | return GV; |
| 611 | } |
| 612 | |
Devang Patel | 962e075 | 2007-11-30 00:51:33 +0000 | [diff] [blame] | 613 | /// addDescriptor - Directly connect DD with existing GV. |
| 614 | void DISerializer::addDescriptor(DebugInfoDesc *DD, |
| 615 | GlobalVariable *GV) { |
| 616 | DescGlobals[DD] = GV; |
| 617 | } |
| 618 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 619 | //===----------------------------------------------------------------------===// |
| 620 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 621 | /// Verify - Return true if the GlobalVariable appears to be a valid |
| 622 | /// serialization of a DebugInfoDesc. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 623 | bool DIVerifier::Verify(Value *V) { |
Jim Laskey | aaa80eb | 2006-03-28 01:30:18 +0000 | [diff] [blame] | 624 | return !V || Verify(getGlobalVariable(V)); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 625 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 626 | bool DIVerifier::Verify(GlobalVariable *GV) { |
Jim Laskey | 98e0410 | 2006-03-26 22:45:20 +0000 | [diff] [blame] | 627 | // NULLs are valid. |
| 628 | if (!GV) return true; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 629 | |
Jim Laskey | 98e0410 | 2006-03-26 22:45:20 +0000 | [diff] [blame] | 630 | // Check prior validity. |
| 631 | unsigned &ValiditySlot = Validity[GV]; |
| 632 | |
| 633 | // If visited before then use old state. |
| 634 | if (ValiditySlot) return ValiditySlot == Valid; |
| 635 | |
| 636 | // Assume validity for the time being (recursion.) |
| 637 | ValiditySlot = Valid; |
Jim Laskey | a8299de | 2006-03-27 01:51:47 +0000 | [diff] [blame] | 638 | |
| 639 | // Make sure the global is internal or link once (anchor.) |
| 640 | if (GV->getLinkage() != GlobalValue::InternalLinkage && |
| 641 | GV->getLinkage() != GlobalValue::LinkOnceLinkage) { |
| 642 | ValiditySlot = Invalid; |
| 643 | return false; |
| 644 | } |
Jim Laskey | 98e0410 | 2006-03-26 22:45:20 +0000 | [diff] [blame] | 645 | |
Jim Laskey | 2bbff6d | 2006-11-30 18:29:23 +0000 | [diff] [blame] | 646 | // Get the Tag. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 647 | unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 648 | |
| 649 | // Check for user defined descriptors. |
Jim Laskey | 2bbff6d | 2006-11-30 18:29:23 +0000 | [diff] [blame] | 650 | if (Tag == DW_TAG_invalid) { |
| 651 | ValiditySlot = Valid; |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | // Get the Version. |
| 656 | unsigned Version = DebugInfoDesc::VersionFromGlobal(GV); |
| 657 | |
| 658 | // Check for version mismatch. |
| 659 | if (Version != LLVMDebugVersion) { |
| 660 | ValiditySlot = Invalid; |
| 661 | return false; |
| 662 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 663 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 664 | // Construct an empty DebugInfoDesc. |
| 665 | DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag); |
Jim Laskey | 2140798 | 2006-03-14 18:37:57 +0000 | [diff] [blame] | 666 | |
| 667 | // Allow for user defined descriptors. |
| 668 | if (!DD) return true; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 669 | |
| 670 | // Get the initializer constant. |
| 671 | ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer()); |
| 672 | |
| 673 | // Get the operand count. |
| 674 | unsigned N = CI->getNumOperands(); |
| 675 | |
| 676 | // Get the field count. |
Jim Laskey | 98e0410 | 2006-03-26 22:45:20 +0000 | [diff] [blame] | 677 | unsigned &CountSlot = Counts[Tag]; |
| 678 | if (!CountSlot) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 679 | // Check the operand count to the field count |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 680 | DICountVisitor CTAM; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 681 | CTAM.ApplyToFields(DD); |
Jim Laskey | 98e0410 | 2006-03-26 22:45:20 +0000 | [diff] [blame] | 682 | CountSlot = CTAM.getCount(); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Jim Laskey | 2140798 | 2006-03-14 18:37:57 +0000 | [diff] [blame] | 685 | // Field count must be at most equal operand count. |
Jim Laskey | 98e0410 | 2006-03-26 22:45:20 +0000 | [diff] [blame] | 686 | if (CountSlot > N) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 687 | delete DD; |
Jim Laskey | 98e0410 | 2006-03-26 22:45:20 +0000 | [diff] [blame] | 688 | ValiditySlot = Invalid; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 689 | return false; |
| 690 | } |
| 691 | |
| 692 | // Check each field for valid type. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 693 | DIVerifyVisitor VRAM(*this, GV); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 694 | VRAM.ApplyToFields(DD); |
| 695 | |
| 696 | // Release empty DebugInfoDesc. |
| 697 | delete DD; |
| 698 | |
Jim Laskey | 98e0410 | 2006-03-26 22:45:20 +0000 | [diff] [blame] | 699 | // If fields are not valid. |
| 700 | if (!VRAM.isValid()) { |
| 701 | ValiditySlot = Invalid; |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | return true; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 708 | /// isVerified - Return true if the specified GV has already been |
| 709 | /// verified as a debug information descriptor. |
| 710 | bool DIVerifier::isVerified(GlobalVariable *GV) { |
| 711 | unsigned &ValiditySlot = Validity[GV]; |
| 712 | if (ValiditySlot) return ValiditySlot == Valid; |
| 713 | return false; |
| 714 | } |
| 715 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 716 | //===----------------------------------------------------------------------===// |
| 717 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 718 | DebugScope::~DebugScope() { |
| 719 | for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i]; |
| 720 | for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j]; |
| 721 | } |
| 722 | |
| 723 | //===----------------------------------------------------------------------===// |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 724 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 725 | MachineModuleInfo::MachineModuleInfo() |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 726 | : ImmutablePass((intptr_t)&ID) |
| 727 | , DR() |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 728 | , VR() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 729 | , CompileUnits() |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 730 | , Directories() |
| 731 | , SourceFiles() |
| 732 | , Lines() |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 733 | , LabelIDList() |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 734 | , ScopeMap() |
| 735 | , RootScope(NULL) |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 736 | , FrameMoves() |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 737 | , LandingPads() |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 738 | , Personalities() |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 739 | , CallsEHReturn(0) |
| 740 | , CallsUnwindInit(0) |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 741 | { |
| 742 | // Always emit "no personality" info |
| 743 | Personalities.push_back(NULL); |
| 744 | } |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 745 | MachineModuleInfo::~MachineModuleInfo() { |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 746 | |
| 747 | } |
| 748 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 749 | /// doInitialization - Initialize the state for a new module. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 750 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 751 | bool MachineModuleInfo::doInitialization() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 752 | return false; |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 755 | /// doFinalization - Tear down the state after completion of a module. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 756 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 757 | bool MachineModuleInfo::doFinalization() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 758 | return false; |
| 759 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 760 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 761 | /// BeginFunction - Begin gathering function meta information. |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 762 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 763 | void MachineModuleInfo::BeginFunction(MachineFunction *MF) { |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 764 | // Coming soon. |
| 765 | } |
| 766 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 767 | /// EndFunction - Discard function meta information. |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 768 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 769 | void MachineModuleInfo::EndFunction() { |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 770 | // Clean up scope information. |
| 771 | if (RootScope) { |
| 772 | delete RootScope; |
| 773 | ScopeMap.clear(); |
| 774 | RootScope = NULL; |
| 775 | } |
| 776 | |
Jim Laskey | b82313f | 2007-02-01 16:31:34 +0000 | [diff] [blame] | 777 | // Clean up line info. |
| 778 | Lines.clear(); |
| 779 | |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 780 | // Clean up frame info. |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 781 | FrameMoves.clear(); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 782 | |
| 783 | // Clean up exception info. |
| 784 | LandingPads.clear(); |
| 785 | TypeInfos.clear(); |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 786 | FilterIds.clear(); |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 787 | FilterEnds.clear(); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 788 | CallsEHReturn = 0; |
| 789 | CallsUnwindInit = 0; |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Jim Laskey | d96185a | 2006-02-13 12:50:39 +0000 | [diff] [blame] | 792 | /// getDescFor - Convert a Value to a debug information descriptor. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 793 | /// |
Jim Laskey | d96185a | 2006-02-13 12:50:39 +0000 | [diff] [blame] | 794 | // FIXME - use new Value type when available. |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 795 | DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 796 | return DR.Deserialize(V); |
| 797 | } |
| 798 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 799 | /// AnalyzeModule - Scan the module for global debug information. |
| 800 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 801 | void MachineModuleInfo::AnalyzeModule(Module &M) { |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 802 | SetupCompileUnits(M); |
Dale Johannesen | 48ae02f | 2008-01-16 19:59:28 +0000 | [diff] [blame] | 803 | |
| 804 | // Insert functions in the llvm.used array into UsedFunctions. |
| 805 | GlobalVariable *GV = M.getGlobalVariable("llvm.used"); |
| 806 | if (!GV || !GV->hasInitializer()) return; |
| 807 | |
| 808 | // Should be an array of 'i8*'. |
| 809 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 810 | if (InitList == 0) return; |
| 811 | |
| 812 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
| 813 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i))) |
| 814 | if (CE->getOpcode() == Instruction::BitCast) |
| 815 | if (Function *F = dyn_cast<Function>(CE->getOperand(0))) |
| 816 | UsedFunctions.insert(F); |
| 817 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /// SetupCompileUnits - Set up the unique vector of compile units. |
| 821 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 822 | void MachineModuleInfo::SetupCompileUnits(Module &M) { |
Bill Wendling | 305635a | 2008-06-27 00:09:40 +0000 | [diff] [blame] | 823 | std::vector<void*> CUList; |
| 824 | CompileUnitDesc CUD; |
| 825 | getAnchoredDescriptors(M, &CUD, CUList); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 826 | |
Bill Wendling | 305635a | 2008-06-27 00:09:40 +0000 | [diff] [blame] | 827 | for (unsigned i = 0, N = CUList.size(); i < N; i++) |
| 828 | CompileUnits.insert((CompileUnitDesc*)CUList[i]); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 831 | /// getCompileUnits - Return a vector of debug compile units. |
| 832 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 833 | const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{ |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 834 | return CompileUnits; |
| 835 | } |
| 836 | |
Bill Wendling | 305635a | 2008-06-27 00:09:40 +0000 | [diff] [blame] | 837 | /// getAnchoredDescriptors - Return a vector of anchored debug descriptors. |
| 838 | /// |
| 839 | void |
| 840 | MachineModuleInfo::getAnchoredDescriptors(Module &M, const AnchoredDesc *Desc, |
| 841 | std::vector<void*> &AnchoredDescs) { |
| 842 | std::vector<GlobalVariable*> Globals; |
| 843 | getGlobalVariablesUsing(M, Desc->getAnchorString(), Globals); |
| 844 | |
| 845 | for (unsigned i = 0, N = Globals.size(); i < N; ++i) { |
| 846 | GlobalVariable *GV = Globals[i]; |
| 847 | |
| 848 | // FIXME - In the short term, changes are too drastic to continue. |
| 849 | if (DebugInfoDesc::TagFromGlobal(GV) == Desc->getTag() && |
| 850 | DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion) |
| 851 | AnchoredDescs.push_back(DR.Deserialize(GV)); |
| 852 | } |
| 853 | } |
| 854 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 855 | /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the |
| 856 | /// named GlobalVariable. |
Bill Wendling | 305635a | 2008-06-27 00:09:40 +0000 | [diff] [blame] | 857 | void |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 858 | MachineModuleInfo::getGlobalVariablesUsing(Module &M, |
Bill Wendling | 305635a | 2008-06-27 00:09:40 +0000 | [diff] [blame] | 859 | const std::string &RootName, |
| 860 | std::vector<GlobalVariable*> &Globals) { |
| 861 | return ::getGlobalVariablesUsing(M, RootName, Globals); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 862 | } |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 863 | |
Evan Cheng | a647c92 | 2008-02-01 02:05:57 +0000 | [diff] [blame] | 864 | /// RecordSourceLine - Records location information and associates it with a |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 865 | /// debug label. Returns a unique label ID used to generate a label and |
| 866 | /// provide correspondence to the source line list. |
Evan Cheng | a647c92 | 2008-02-01 02:05:57 +0000 | [diff] [blame] | 867 | unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column, |
| 868 | unsigned Source) { |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 869 | unsigned ID = NextLabelID(); |
Chris Lattner | 8466b21 | 2006-10-17 22:06:46 +0000 | [diff] [blame] | 870 | Lines.push_back(SourceLineInfo(Line, Column, Source, ID)); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 871 | return ID; |
| 872 | } |
| 873 | |
| 874 | /// RecordSource - Register a source file with debug info. Returns an source |
| 875 | /// ID. |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 876 | unsigned MachineModuleInfo::RecordSource(const std::string &Directory, |
| 877 | const std::string &Source) { |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 878 | unsigned DirectoryID = Directories.insert(Directory); |
| 879 | return SourceFiles.insert(SourceFileInfo(DirectoryID, Source)); |
| 880 | } |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 881 | unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) { |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 882 | return RecordSource(CompileUnit->getDirectory(), |
| 883 | CompileUnit->getFileName()); |
| 884 | } |
| 885 | |
| 886 | /// RecordRegionStart - Indicate the start of a region. |
| 887 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 888 | unsigned MachineModuleInfo::RecordRegionStart(Value *V) { |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 889 | // FIXME - need to be able to handle split scopes because of bb cloning. |
| 890 | DebugInfoDesc *ScopeDesc = DR.Deserialize(V); |
| 891 | DebugScope *Scope = getOrCreateScope(ScopeDesc); |
| 892 | unsigned ID = NextLabelID(); |
| 893 | if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID); |
| 894 | return ID; |
| 895 | } |
| 896 | |
| 897 | /// RecordRegionEnd - Indicate the end of a region. |
| 898 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 899 | unsigned MachineModuleInfo::RecordRegionEnd(Value *V) { |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 900 | // FIXME - need to be able to handle split scopes because of bb cloning. |
| 901 | DebugInfoDesc *ScopeDesc = DR.Deserialize(V); |
| 902 | DebugScope *Scope = getOrCreateScope(ScopeDesc); |
| 903 | unsigned ID = NextLabelID(); |
| 904 | Scope->setEndLabelID(ID); |
| 905 | return ID; |
| 906 | } |
| 907 | |
| 908 | /// RecordVariable - Indicate the declaration of a local variable. |
| 909 | /// |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 910 | void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) { |
| 911 | VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV)); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 912 | DebugScope *Scope = getOrCreateScope(VD->getContext()); |
| 913 | DebugVariable *DV = new DebugVariable(VD, FrameIndex); |
| 914 | Scope->AddVariable(DV); |
| 915 | } |
| 916 | |
| 917 | /// getOrCreateScope - Returns the scope associated with the given descriptor. |
| 918 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 919 | DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) { |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame] | 920 | DebugScope *&Slot = ScopeMap[ScopeDesc]; |
| 921 | if (!Slot) { |
| 922 | // FIXME - breaks down when the context is an inlined function. |
| 923 | DebugInfoDesc *ParentDesc = NULL; |
| 924 | if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) { |
| 925 | ParentDesc = Block->getContext(); |
| 926 | } |
| 927 | DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL; |
| 928 | Slot = new DebugScope(Parent, ScopeDesc); |
| 929 | if (Parent) { |
| 930 | Parent->AddScope(Slot); |
| 931 | } else if (RootScope) { |
| 932 | // FIXME - Add inlined function scopes to the root so we can delete |
| 933 | // them later. Long term, handle inlined functions properly. |
| 934 | RootScope->AddScope(Slot); |
| 935 | } else { |
| 936 | // First function is top level function. |
| 937 | RootScope = Slot; |
| 938 | } |
| 939 | } |
| 940 | return Slot; |
| 941 | } |
| 942 | |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 943 | //===-EH-------------------------------------------------------------------===// |
| 944 | |
| 945 | /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the |
| 946 | /// specified MachineBasicBlock. |
| 947 | LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo |
| 948 | (MachineBasicBlock *LandingPad) { |
| 949 | unsigned N = LandingPads.size(); |
| 950 | for (unsigned i = 0; i < N; ++i) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 951 | LandingPadInfo &LP = LandingPads[i]; |
| 952 | if (LP.LandingPadBlock == LandingPad) |
| 953 | return LP; |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | LandingPads.push_back(LandingPadInfo(LandingPad)); |
| 957 | return LandingPads[N]; |
| 958 | } |
| 959 | |
| 960 | /// addInvoke - Provide the begin and end labels of an invoke style call and |
| 961 | /// associate it with a try landing pad block. |
| 962 | void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad, |
| 963 | unsigned BeginLabel, unsigned EndLabel) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 964 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Anton Korobeynikov | eeb37e0 | 2007-05-10 22:34:59 +0000 | [diff] [blame] | 965 | LP.BeginLabels.push_back(BeginLabel); |
| 966 | LP.EndLabels.push_back(EndLabel); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /// addLandingPad - Provide the label of a try LandingPad block. |
| 970 | /// |
| 971 | unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) { |
| 972 | unsigned LandingPadLabel = NextLabelID(); |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 973 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 974 | LP.LandingPadLabel = LandingPadLabel; |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 975 | return LandingPadLabel; |
| 976 | } |
| 977 | |
| 978 | /// addPersonality - Provide the personality function for the exception |
| 979 | /// information. |
| 980 | void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad, |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 981 | Function *Personality) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 982 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 983 | LP.Personality = Personality; |
Anton Korobeynikov | 0ff3ca4 | 2007-05-12 22:36:25 +0000 | [diff] [blame] | 984 | |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 985 | for (unsigned i = 0; i < Personalities.size(); ++i) |
| 986 | if (Personalities[i] == Personality) |
| 987 | return; |
| 988 | |
| 989 | Personalities.push_back(Personality); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. |
| 993 | /// |
| 994 | void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad, |
| 995 | std::vector<GlobalVariable *> &TyInfo) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 996 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 997 | for (unsigned N = TyInfo.size(); N; --N) |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 998 | LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 999 | } |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 1000 | |
| 1001 | /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 1002 | /// |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 1003 | void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad, |
| 1004 | std::vector<GlobalVariable *> &TyInfo) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 1005 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1006 | SmallVector<unsigned, 32> IdsInFilter(TyInfo.size()); |
| 1007 | |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 1008 | for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) |
| 1009 | IdsInFilter[I] = getTypeIDFor(TyInfo[I]); |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1010 | |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 1011 | LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Duncan Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 1014 | /// addCleanup - Add a cleanup action for a landing pad. |
| 1015 | /// |
| 1016 | void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) { |
| 1017 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 1018 | LP.TypeIds.push_back(0); |
| 1019 | } |
| 1020 | |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 1021 | /// TidyLandingPads - Remap landing pad labels and remove any deleted landing |
| 1022 | /// pads. |
| 1023 | void MachineModuleInfo::TidyLandingPads() { |
| 1024 | for (unsigned i = 0; i != LandingPads.size(); ) { |
| 1025 | LandingPadInfo &LandingPad = LandingPads[i]; |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 1026 | LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel); |
Anton Korobeynikov | eeb37e0 | 2007-05-10 22:34:59 +0000 | [diff] [blame] | 1027 | |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 1028 | // Special case: we *should* emit LPs with null LP MBB. This indicates |
Duncan Sands | 481dc72 | 2007-12-19 07:36:31 +0000 | [diff] [blame] | 1029 | // "nounwind" case. |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 1030 | if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 1031 | LandingPads.erase(LandingPads.begin() + i); |
| 1032 | continue; |
| 1033 | } |
Duncan Sands | 57810cd | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 1034 | |
Anton Korobeynikov | eeb37e0 | 2007-05-10 22:34:59 +0000 | [diff] [blame] | 1035 | for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) { |
| 1036 | unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]); |
| 1037 | unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]); |
Duncan Sands | 57810cd | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 1038 | |
Anton Korobeynikov | eeb37e0 | 2007-05-10 22:34:59 +0000 | [diff] [blame] | 1039 | if (!BeginLabel || !EndLabel) { |
Anton Korobeynikov | eeb37e0 | 2007-05-10 22:34:59 +0000 | [diff] [blame] | 1040 | LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); |
| 1041 | LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | LandingPad.BeginLabels[j] = BeginLabel; |
| 1046 | LandingPad.EndLabels[j] = EndLabel; |
| 1047 | ++j; |
| 1048 | } |
Duncan Sands | 57810cd | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Remove landing pads with no try-ranges. |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 1051 | if (LandingPads[i].BeginLabels.empty()) { |
Duncan Sands | 57810cd | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 1052 | LandingPads.erase(LandingPads.begin() + i); |
| 1053 | continue; |
| 1054 | } |
| 1055 | |
| 1056 | // If there is no landing pad, ensure that the list of typeids is empty. |
| 1057 | // If the only typeid is a cleanup, this is the same as having no typeids. |
| 1058 | if (!LandingPad.LandingPadBlock || |
| 1059 | (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) |
| 1060 | LandingPad.TypeIds.clear(); |
| 1061 | |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 1062 | ++i; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | /// getTypeIDFor - Return the type id for the specified typeinfo. This is |
| 1067 | /// function wide. |
| 1068 | unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) { |
| 1069 | for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) |
| 1070 | if (TypeInfos[i] == TI) return i + 1; |
| 1071 | |
| 1072 | TypeInfos.push_back(TI); |
| 1073 | return TypeInfos.size(); |
| 1074 | } |
| 1075 | |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 1076 | /// getFilterIDFor - Return the filter id for the specified typeinfos. This is |
| 1077 | /// function wide. |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1078 | int MachineModuleInfo::getFilterIDFor(SmallVectorImpl<unsigned> &TyIds) { |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 1079 | // 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. |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1082 | unsigned TyIDSize = TyIds.size(); |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 1083 | for (std::vector<unsigned>::iterator I = FilterEnds.begin(), |
| 1084 | E = FilterEnds.end(); I != E; ++I) { |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1085 | unsigned i = *I, j = TyIDSize; |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 1086 | |
| 1087 | while (i && j) |
| 1088 | if (FilterIds[--i] != TyIds[--j]) |
| 1089 | goto try_next; |
| 1090 | |
| 1091 | if (!j) |
| 1092 | // The new filter coincides with range [i, end) of the existing filter. |
| 1093 | return -(1 + i); |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 1094 | try_next:; |
| 1095 | } |
| 1096 | |
| 1097 | // Add the new filter. |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1098 | unsigned FilterIDSize = FilterIds.size(); |
| 1099 | int FilterID = -(1 + FilterIDSize); |
| 1100 | FilterIds.reserve(FilterIDSize + TyIDSize + 1); |
| 1101 | |
| 1102 | for (unsigned I = 0, N = TyIDSize; I != N; ++I) |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 1103 | FilterIds.push_back(TyIds[I]); |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1104 | |
| 1105 | FilterEnds.push_back(FilterIDSize); |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 1106 | FilterIds.push_back(0); // terminator |
| 1107 | return FilterID; |
| 1108 | } |
| 1109 | |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 1110 | /// getPersonality - Return the personality function for the current function. |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 1111 | Function *MachineModuleInfo::getPersonality() const { |
Anton Korobeynikov | 0ff3ca4 | 2007-05-12 22:36:25 +0000 | [diff] [blame] | 1112 | // FIXME: Until PR1414 will be fixed, we're using 1 personality function per |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 1113 | // function |
| 1114 | return !LandingPads.empty() ? LandingPads[0].Personality : NULL; |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 1117 | /// getPersonalityIndex - Return unique index for current personality |
| 1118 | /// function. NULL personality function should always get zero index. |
| 1119 | unsigned MachineModuleInfo::getPersonalityIndex() const { |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 1120 | const Function* Personality = NULL; |
| 1121 | |
| 1122 | // Scan landing pads. If there is at least one non-NULL personality - use it. |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1123 | for (unsigned i = 0, e = LandingPads.size(); i != e; ++i) |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 1124 | if (LandingPads[i].Personality) { |
| 1125 | Personality = LandingPads[i].Personality; |
| 1126 | break; |
| 1127 | } |
| 1128 | |
Bill Wendling | e6b6bae | 2008-06-27 00:56:36 +0000 | [diff] [blame] | 1129 | for (unsigned i = 0, e = Personalities.size(); i < e; ++i) { |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 1130 | if (Personalities[i] == Personality) |
| 1131 | return i; |
| 1132 | } |
| 1133 | |
| 1134 | // This should never happen |
| 1135 | assert(0 && "Personality function should be set!"); |
| 1136 | return 0; |
| 1137 | } |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 1138 | |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 1139 | //===----------------------------------------------------------------------===// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 1140 | /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows |
| 1141 | /// a info consumer to determine if the range of two labels is empty, by seeing |
| 1142 | /// if the labels map to the same reduced label. |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 1143 | |
| 1144 | namespace llvm { |
| 1145 | |
| 1146 | struct DebugLabelFolder : public MachineFunctionPass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1147 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 1148 | DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {} |
| 1149 | |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 1150 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 1151 | virtual const char *getPassName() const { return "Label Folder"; } |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 1152 | }; |
| 1153 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1154 | char DebugLabelFolder::ID = 0; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 1155 | |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 1156 | bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) { |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 1157 | // Get machine module info. |
| 1158 | MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>(); |
| 1159 | if (!MMI) return false; |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Track if change is made. |
| 1162 | bool MadeChange = false; |
| 1163 | // No prior label to begin. |
| 1164 | unsigned PriorLabel = 0; |
| 1165 | |
| 1166 | // Iterate through basic blocks. |
| 1167 | for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); |
| 1168 | BB != E; ++BB) { |
| 1169 | // Iterate through instructions. |
| 1170 | for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 1171 | // Is it a label. |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 1172 | if (I->isDebugLabel()) { |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 1173 | // The label ID # is always operand #0, an immediate. |
| 1174 | unsigned NextLabel = I->getOperand(0).getImm(); |
| 1175 | |
| 1176 | // If there was an immediate prior label. |
| 1177 | if (PriorLabel) { |
| 1178 | // Remap the current label to prior label. |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 1179 | MMI->RemapLabel(NextLabel, PriorLabel); |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 1180 | // Delete the current label. |
| 1181 | I = BB->erase(I); |
| 1182 | // Indicate a change has been made. |
| 1183 | MadeChange = true; |
| 1184 | continue; |
| 1185 | } else { |
| 1186 | // Start a new round. |
| 1187 | PriorLabel = NextLabel; |
| 1188 | } |
| 1189 | } else { |
| 1190 | // No consecutive labels. |
| 1191 | PriorLabel = 0; |
| 1192 | } |
| 1193 | |
| 1194 | ++I; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | return MadeChange; |
| 1199 | } |
| 1200 | |
| 1201 | FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); } |
| 1202 | |
| 1203 | } |