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