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