MachineModuleInfo.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/CodeGen/MachineModuleInfo.h"
  10. #include "llvm/Constants.h"
  11. #include "llvm/Analysis/ValueTracking.h"
  12. #include "llvm/CodeGen/MachineFunctionPass.h"
  13. #include "llvm/CodeGen/MachineFunction.h"
  14. #include "llvm/CodeGen/MachineLocation.h"
  15. #include "llvm/Target/TargetInstrInfo.h"
  16. #include "llvm/Target/TargetMachine.h"
  17. #include "llvm/Target/TargetOptions.h"
  18. #include "llvm/DerivedTypes.h"
  19. #include "llvm/GlobalVariable.h"
  20. #include "llvm/Intrinsics.h"
  21. #include "llvm/Instructions.h"
  22. #include "llvm/Module.h"
  23. #include "llvm/Support/Dwarf.h"
  24. #include "llvm/Support/Streams.h"
  25. using namespace llvm;
  26. using namespace llvm::dwarf;
  27. // Handle the Pass registration stuff necessary to use TargetData's.
  28. static RegisterPass<MachineModuleInfo>
  29. X("machinemoduleinfo", "Module Information");
  30. char MachineModuleInfo::ID = 0;
  31. //===----------------------------------------------------------------------===//
  32. /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
  33. /// specified value in their initializer somewhere.
  34. static void
  35. getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
  36. // Scan though value users.
  37. for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
  38. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
  39. // If the user is a GlobalVariable then add to result.
  40. Result.push_back(GV);
  41. } else if (Constant *C = dyn_cast<Constant>(*I)) {
  42. // If the user is a constant variable then scan its users
  43. getGlobalVariablesUsing(C, Result);
  44. }
  45. }
  46. }
  47. /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
  48. /// named GlobalVariable.
  49. static void
  50. getGlobalVariablesUsing(Module &M, const std::string &RootName,
  51. std::vector<GlobalVariable*> &Result) {
  52. std::vector<const Type*> FieldTypes;
  53. FieldTypes.push_back(Type::Int32Ty);
  54. FieldTypes.push_back(Type::Int32Ty);
  55. // Get the GlobalVariable root.
  56. GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
  57. StructType::get(FieldTypes));
  58. // If present and linkonce then scan for users.
  59. if (UseRoot && UseRoot->hasLinkOnceLinkage())
  60. getGlobalVariablesUsing(UseRoot, Result);
  61. }
  62. /// isStringValue - Return true if the given value can be coerced to a string.
  63. ///
  64. static bool isStringValue(Value *V) {
  65. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
  66. if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
  67. ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
  68. return Init->isString();
  69. }
  70. } else if (Constant *C = dyn_cast<Constant>(V)) {
  71. if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
  72. return isStringValue(GV);
  73. else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
  74. if (CE->getOpcode() == Instruction::GetElementPtr) {
  75. if (CE->getNumOperands() == 3 &&
  76. cast<Constant>(CE->getOperand(1))->isNullValue() &&
  77. isa<ConstantInt>(CE->getOperand(2))) {
  78. return isStringValue(CE->getOperand(0));
  79. }
  80. }
  81. }
  82. }
  83. return false;
  84. }
  85. /// getGlobalVariable - Return either a direct or cast Global value.
  86. ///
  87. static GlobalVariable *getGlobalVariable(Value *V) {
  88. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
  89. return GV;
  90. } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  91. if (CE->getOpcode() == Instruction::BitCast) {
  92. return dyn_cast<GlobalVariable>(CE->getOperand(0));
  93. } else if (CE->getOpcode() == Instruction::GetElementPtr) {
  94. for (unsigned int i=1; i<CE->getNumOperands(); i++) {
  95. if (!CE->getOperand(i)->isNullValue())
  96. return NULL;
  97. }
  98. return dyn_cast<GlobalVariable>(CE->getOperand(0));
  99. }
  100. }
  101. return NULL;
  102. }
  103. /// isGlobalVariable - Return true if the given value can be coerced to a
  104. /// GlobalVariable.
  105. static bool isGlobalVariable(Value *V) {
  106. if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
  107. return true;
  108. } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  109. if (CE->getOpcode() == Instruction::BitCast) {
  110. return isa<GlobalVariable>(CE->getOperand(0));
  111. } else if (CE->getOpcode() == Instruction::GetElementPtr) {
  112. for (unsigned int i=1; i<CE->getNumOperands(); i++) {
  113. if (!CE->getOperand(i)->isNullValue())
  114. return false;
  115. }
  116. return isa<GlobalVariable>(CE->getOperand(0));
  117. }
  118. }
  119. return false;
  120. }
  121. /// getUIntOperand - Return ith operand if it is an unsigned integer.
  122. ///
  123. static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
  124. // Make sure the GlobalVariable has an initializer.
  125. if (!GV->hasInitializer()) return NULL;
  126. // Get the initializer constant.
  127. ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
  128. if (!CI) return NULL;
  129. // Check if there is at least i + 1 operands.
  130. unsigned N = CI->getNumOperands();
  131. if (i >= N) return NULL;
  132. // Check constant.
  133. return dyn_cast<ConstantInt>(CI->getOperand(i));
  134. }
  135. //===----------------------------------------------------------------------===//
  136. static unsigned CountFields(DebugInfoDesc *DD) {
  137. unsigned Count = 0;
  138. switch (DD->getTag()) {
  139. case DW_TAG_anchor: // AnchorDesc
  140. // Tag
  141. // AnchorTag
  142. Count = 2;
  143. break;
  144. case DW_TAG_compile_unit: // CompileUnitDesc
  145. // [DW_TAG_anchor]
  146. // if (Version == 0) DebugVersion
  147. // Language
  148. // FileName
  149. // Directory
  150. // Producer
  151. Count = 6;
  152. // Handle cases out of sync with compiler.
  153. if (DD->getVersion() == 0)
  154. ++Count;
  155. break;
  156. case DW_TAG_variable: // GlobalVariableDesc
  157. // [DW_TAG_anchor]
  158. // Context
  159. // Name
  160. // FullName
  161. // LinkageName
  162. // File
  163. // Line
  164. // TyDesc
  165. // IsStatic
  166. // IsDefinition
  167. // Global
  168. Count = 12;
  169. break;
  170. case DW_TAG_subprogram: // SubprogramDesc
  171. // [DW_TAG_anchor]
  172. // Context
  173. // Name
  174. // FullName
  175. // LinkageName
  176. // File
  177. // Line
  178. // TyDesc
  179. // IsStatic
  180. // IsDefinition
  181. Count = 11;
  182. break;
  183. case DW_TAG_lexical_block: // BlockDesc
  184. // Tag
  185. // Context
  186. Count = 2;
  187. break;
  188. case DW_TAG_base_type: // BasicTypeDesc
  189. // Tag
  190. // Context
  191. // Name
  192. // File
  193. // Line
  194. // Size
  195. // Align
  196. // Offset
  197. // if (Version > LLVMDebugVersion4) Flags
  198. // Encoding
  199. Count = 9;
  200. if (DD->getVersion() > LLVMDebugVersion4)
  201. ++Count;
  202. break;
  203. case DW_TAG_typedef:
  204. case DW_TAG_pointer_type:
  205. case DW_TAG_reference_type:
  206. case DW_TAG_const_type:
  207. case DW_TAG_volatile_type:
  208. case DW_TAG_restrict_type:
  209. case DW_TAG_member:
  210. case DW_TAG_inheritance: // DerivedTypeDesc
  211. // Tag
  212. // Context
  213. // Name
  214. // File
  215. // Line
  216. // Size
  217. // Align
  218. // Offset
  219. // if (Version > LLVMDebugVersion4) Flags
  220. // FromType
  221. Count = 9;
  222. if (DD->getVersion() > LLVMDebugVersion4)
  223. ++Count;
  224. break;
  225. case DW_TAG_array_type:
  226. case DW_TAG_structure_type:
  227. case DW_TAG_union_type:
  228. case DW_TAG_enumeration_type:
  229. case DW_TAG_vector_type:
  230. case DW_TAG_subroutine_type: // CompositeTypeDesc
  231. // Tag
  232. // Context
  233. // Name
  234. // File
  235. // Line
  236. // Size
  237. // Align
  238. // Offset
  239. // if (Version > LLVMDebugVersion4) Flags
  240. // FromType
  241. // Elements
  242. Count = 10;
  243. if (DD->getVersion() > LLVMDebugVersion4)
  244. ++Count;
  245. break;
  246. case DW_TAG_subrange_type: // SubrangeDesc
  247. // Tag
  248. // Lo
  249. // Hi
  250. Count = 3;
  251. break;
  252. case DW_TAG_enumerator: // EnumeratorDesc
  253. // Tag
  254. // Name
  255. // Value
  256. Count = 3;
  257. break;
  258. case DW_TAG_return_variable:
  259. case DW_TAG_arg_variable:
  260. case DW_TAG_auto_variable: // VariableDesc
  261. // Tag
  262. // Context
  263. // Name
  264. // File
  265. // Line
  266. // TyDesc
  267. Count = 6;
  268. break;
  269. default:
  270. break;
  271. }
  272. return Count;
  273. }
  274. //===----------------------------------------------------------------------===//
  275. /// ApplyToFields - Target the visitor to each field of the debug information
  276. /// descriptor.
  277. void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
  278. DD->ApplyToFields(this);
  279. }
  280. namespace {
  281. //===----------------------------------------------------------------------===//
  282. /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
  283. /// supplied DebugInfoDesc.
  284. class DIDeserializeVisitor : public DIVisitor {
  285. private:
  286. DIDeserializer &DR; // Active deserializer.
  287. unsigned I; // Current operand index.
  288. ConstantStruct *CI; // GlobalVariable constant initializer.
  289. public:
  290. DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
  291. : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer()))
  292. {}
  293. /// Apply - Set the value of each of the fields.
  294. ///
  295. virtual void Apply(int &Field) {
  296. Constant *C = CI->getOperand(I++);
  297. Field = cast<ConstantInt>(C)->getSExtValue();
  298. }
  299. virtual void Apply(unsigned &Field) {
  300. Constant *C = CI->getOperand(I++);
  301. Field = cast<ConstantInt>(C)->getZExtValue();
  302. }
  303. virtual void Apply(int64_t &Field) {
  304. Constant *C = CI->getOperand(I++);
  305. Field = cast<ConstantInt>(C)->getSExtValue();
  306. }
  307. virtual void Apply(uint64_t &Field) {
  308. Constant *C = CI->getOperand(I++);
  309. Field = cast<ConstantInt>(C)->getZExtValue();
  310. }
  311. virtual void Apply(bool &Field) {
  312. Constant *C = CI->getOperand(I++);
  313. Field = cast<ConstantInt>(C)->getZExtValue();
  314. }
  315. virtual void Apply(std::string &Field) {
  316. Constant *C = CI->getOperand(I++);
  317. // Fills in the string if it succeeds
  318. if (!GetConstantStringInfo(C, Field))
  319. Field.clear();
  320. }
  321. virtual void Apply(DebugInfoDesc *&Field) {
  322. Constant *C = CI->getOperand(I++);
  323. Field = DR.Deserialize(C);
  324. }
  325. virtual void Apply(GlobalVariable *&Field) {
  326. Constant *C = CI->getOperand(I++);
  327. Field = getGlobalVariable(C);
  328. }
  329. virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
  330. Field.resize(0);
  331. Constant *C = CI->getOperand(I++);
  332. GlobalVariable *GV = getGlobalVariable(C);
  333. if (GV->hasInitializer()) {
  334. if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
  335. for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
  336. GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
  337. DebugInfoDesc *DE = DR.Deserialize(GVE);
  338. Field.push_back(DE);
  339. }
  340. } else if (GV->getInitializer()->isNullValue()) {
  341. if (const ArrayType *T =
  342. dyn_cast<ArrayType>(GV->getType()->getElementType())) {
  343. Field.resize(T->getNumElements());
  344. }
  345. }
  346. }
  347. }
  348. };
  349. //===----------------------------------------------------------------------===//
  350. /// DISerializeVisitor - This DIVisitor serializes all the fields in
  351. /// the supplied DebugInfoDesc.
  352. class DISerializeVisitor : public DIVisitor {
  353. private:
  354. DISerializer &SR; // Active serializer.
  355. std::vector<Constant*> &Elements; // Element accumulator.
  356. public:
  357. DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
  358. : DIVisitor()
  359. , SR(S)
  360. , Elements(E)
  361. {}
  362. /// Apply - Set the value of each of the fields.
  363. ///
  364. virtual void Apply(int &Field) {
  365. Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
  366. }
  367. virtual void Apply(unsigned &Field) {
  368. Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
  369. }
  370. virtual void Apply(int64_t &Field) {
  371. Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
  372. }
  373. virtual void Apply(uint64_t &Field) {
  374. Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
  375. }
  376. virtual void Apply(bool &Field) {
  377. Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
  378. }
  379. virtual void Apply(std::string &Field) {
  380. Elements.push_back(SR.getString(Field));
  381. }
  382. virtual void Apply(DebugInfoDesc *&Field) {
  383. GlobalVariable *GV = NULL;
  384. // If non-NULL then convert to global.
  385. if (Field) GV = SR.Serialize(Field);
  386. // FIXME - At some point should use specific type.
  387. const PointerType *EmptyTy = SR.getEmptyStructPtrType();
  388. if (GV) {
  389. // Set to pointer to global.
  390. Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
  391. } else {
  392. // Use NULL.
  393. Elements.push_back(ConstantPointerNull::get(EmptyTy));
  394. }
  395. }
  396. virtual void Apply(GlobalVariable *&Field) {
  397. const PointerType *EmptyTy = SR.getEmptyStructPtrType();
  398. if (Field) {
  399. Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
  400. } else {
  401. Elements.push_back(ConstantPointerNull::get(EmptyTy));
  402. }
  403. }
  404. virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
  405. const PointerType *EmptyTy = SR.getEmptyStructPtrType();
  406. unsigned N = Field.size();
  407. ArrayType *AT = ArrayType::get(EmptyTy, N);
  408. std::vector<Constant *> ArrayElements;
  409. for (unsigned i = 0, N = Field.size(); i < N; ++i) {
  410. if (DebugInfoDesc *Element = Field[i]) {
  411. GlobalVariable *GVE = SR.Serialize(Element);
  412. Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
  413. ArrayElements.push_back(cast<Constant>(CE));
  414. } else {
  415. ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
  416. }
  417. }
  418. Constant *CA = ConstantArray::get(AT, ArrayElements);
  419. GlobalVariable *CAGV = new GlobalVariable(AT, true,
  420. GlobalValue::InternalLinkage,
  421. CA, "llvm.dbg.array",
  422. SR.getModule());
  423. CAGV->setSection("llvm.metadata");
  424. Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
  425. Elements.push_back(CAE);
  426. }
  427. };
  428. //===----------------------------------------------------------------------===//
  429. /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
  430. /// the supplied DebugInfoDesc.
  431. class DIGetTypesVisitor : public DIVisitor {
  432. private:
  433. DISerializer &SR; // Active serializer.
  434. std::vector<const Type*> &Fields; // Type accumulator.
  435. public:
  436. DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
  437. : DIVisitor()
  438. , SR(S)
  439. , Fields(F)
  440. {}
  441. /// Apply - Set the value of each of the fields.
  442. ///
  443. virtual void Apply(int &Field) {
  444. Fields.push_back(Type::Int32Ty);
  445. }
  446. virtual void Apply(unsigned &Field) {
  447. Fields.push_back(Type::Int32Ty);
  448. }
  449. virtual void Apply(int64_t &Field) {
  450. Fields.push_back(Type::Int64Ty);
  451. }
  452. virtual void Apply(uint64_t &Field) {
  453. Fields.push_back(Type::Int64Ty);
  454. }
  455. virtual void Apply(bool &Field) {
  456. Fields.push_back(Type::Int1Ty);
  457. }
  458. virtual void Apply(std::string &Field) {
  459. Fields.push_back(SR.getStrPtrType());
  460. }
  461. virtual void Apply(DebugInfoDesc *&Field) {
  462. // FIXME - At some point should use specific type.
  463. const PointerType *EmptyTy = SR.getEmptyStructPtrType();
  464. Fields.push_back(EmptyTy);
  465. }
  466. virtual void Apply(GlobalVariable *&Field) {
  467. const PointerType *EmptyTy = SR.getEmptyStructPtrType();
  468. Fields.push_back(EmptyTy);
  469. }
  470. virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
  471. const PointerType *EmptyTy = SR.getEmptyStructPtrType();
  472. Fields.push_back(EmptyTy);
  473. }
  474. };
  475. //===----------------------------------------------------------------------===//
  476. /// DIVerifyVisitor - This DIVisitor verifies all the field types against
  477. /// a constant initializer.
  478. class DIVerifyVisitor : public DIVisitor {
  479. private:
  480. DIVerifier &VR; // Active verifier.
  481. bool IsValid; // Validity status.
  482. unsigned I; // Current operand index.
  483. ConstantStruct *CI; // GlobalVariable constant initializer.
  484. public:
  485. DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
  486. : DIVisitor()
  487. , VR(V)
  488. , IsValid(true)
  489. , I(0)
  490. , CI(cast<ConstantStruct>(GV->getInitializer()))
  491. {
  492. }
  493. // Accessors.
  494. bool isValid() const { return IsValid; }
  495. /// Apply - Set the value of each of the fields.
  496. ///
  497. virtual void Apply(int &Field) {
  498. Constant *C = CI->getOperand(I++);
  499. IsValid = IsValid && isa<ConstantInt>(C);
  500. }
  501. virtual void Apply(unsigned &Field) {
  502. Constant *C = CI->getOperand(I++);
  503. IsValid = IsValid && isa<ConstantInt>(C);
  504. }
  505. virtual void Apply(int64_t &Field) {
  506. Constant *C = CI->getOperand(I++);
  507. IsValid = IsValid && isa<ConstantInt>(C);
  508. }
  509. virtual void Apply(uint64_t &Field) {
  510. Constant *C = CI->getOperand(I++);
  511. IsValid = IsValid && isa<ConstantInt>(C);
  512. }
  513. virtual void Apply(bool &Field) {
  514. Constant *C = CI->getOperand(I++);
  515. IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
  516. }
  517. virtual void Apply(std::string &Field) {
  518. Constant *C = CI->getOperand(I++);
  519. IsValid = IsValid &&
  520. (!C || isStringValue(C) || C->isNullValue());
  521. }
  522. virtual void Apply(DebugInfoDesc *&Field) {
  523. // FIXME - Prepare the correct descriptor.
  524. Constant *C = CI->getOperand(I++);
  525. IsValid = IsValid && isGlobalVariable(C);
  526. }
  527. virtual void Apply(GlobalVariable *&Field) {
  528. Constant *C = CI->getOperand(I++);
  529. IsValid = IsValid && isGlobalVariable(C);
  530. }
  531. virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
  532. Constant *C = CI->getOperand(I++);
  533. IsValid = IsValid && isGlobalVariable(C);
  534. if (!IsValid) return;
  535. GlobalVariable *GV = getGlobalVariable(C);
  536. IsValid = IsValid && GV && GV->hasInitializer();
  537. if (!IsValid) return;
  538. ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
  539. IsValid = IsValid && CA;
  540. if (!IsValid) return;
  541. for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
  542. IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
  543. if (!IsValid) return;
  544. GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
  545. VR.Verify(GVE);
  546. }
  547. }
  548. };
  549. }
  550. //===----------------------------------------------------------------------===//
  551. /// TagFromGlobal - Returns the tag number from a debug info descriptor
  552. /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
  553. unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
  554. ConstantInt *C = getUIntOperand(GV, 0);
  555. return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
  556. (unsigned)DW_TAG_invalid;
  557. }
  558. /// VersionFromGlobal - Returns the version number from a debug info
  559. /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
  560. /// int.
  561. unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
  562. ConstantInt *C = getUIntOperand(GV, 0);
  563. return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
  564. (unsigned)DW_TAG_invalid;
  565. }
  566. /// DescFactory - Create an instance of debug info descriptor based on Tag.
  567. /// Return NULL if not a recognized Tag.
  568. DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
  569. switch (Tag) {
  570. case DW_TAG_anchor: return new AnchorDesc();
  571. case DW_TAG_compile_unit: return new CompileUnitDesc();
  572. case DW_TAG_variable: return new GlobalVariableDesc();
  573. case DW_TAG_subprogram: return new SubprogramDesc();
  574. case DW_TAG_lexical_block: return new BlockDesc();
  575. case DW_TAG_base_type: return new BasicTypeDesc();
  576. case DW_TAG_typedef:
  577. case DW_TAG_pointer_type:
  578. case DW_TAG_reference_type:
  579. case DW_TAG_const_type:
  580. case DW_TAG_volatile_type:
  581. case DW_TAG_restrict_type:
  582. case DW_TAG_member:
  583. case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
  584. case DW_TAG_array_type:
  585. case DW_TAG_structure_type:
  586. case DW_TAG_union_type:
  587. case DW_TAG_enumeration_type:
  588. case DW_TAG_vector_type:
  589. case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
  590. case DW_TAG_subrange_type: return new SubrangeDesc();
  591. case DW_TAG_enumerator: return new EnumeratorDesc();
  592. case DW_TAG_return_variable:
  593. case DW_TAG_arg_variable:
  594. case DW_TAG_auto_variable: return new VariableDesc(Tag);
  595. default: break;
  596. }
  597. return NULL;
  598. }
  599. /// getLinkage - get linkage appropriate for this type of descriptor.
  600. ///
  601. GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
  602. return GlobalValue::InternalLinkage;
  603. }
  604. /// ApplyToFields - Target the vistor to the fields of the descriptor.
  605. ///
  606. void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
  607. Visitor->Apply(Tag);
  608. }
  609. //===----------------------------------------------------------------------===//
  610. AnchorDesc::AnchorDesc()
  611. : DebugInfoDesc(DW_TAG_anchor)
  612. , AnchorTag(0)
  613. {}
  614. AnchorDesc::AnchorDesc(AnchoredDesc *D)
  615. : DebugInfoDesc(DW_TAG_anchor)
  616. , AnchorTag(D->getTag())
  617. {}
  618. // Implement isa/cast/dyncast.
  619. bool AnchorDesc::classof(const DebugInfoDesc *D) {
  620. return D->getTag() == DW_TAG_anchor;
  621. }
  622. /// getLinkage - get linkage appropriate for this type of descriptor.
  623. ///
  624. GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
  625. return GlobalValue::LinkOnceLinkage;
  626. }
  627. /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
  628. ///
  629. void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
  630. DebugInfoDesc::ApplyToFields(Visitor);
  631. Visitor->Apply(AnchorTag);
  632. }
  633. /// getDescString - Return a string used to compose global names and labels. A
  634. /// A global variable name needs to be defined for each debug descriptor that is
  635. /// anchored. NOTE: that each global variable named here also needs to be added
  636. /// to the list of names left external in the internalizer.
  637. /// ExternalNames.insert("llvm.dbg.compile_units");
  638. /// ExternalNames.insert("llvm.dbg.global_variables");
  639. /// ExternalNames.insert("llvm.dbg.subprograms");
  640. const char *AnchorDesc::getDescString() const {
  641. switch (AnchorTag) {
  642. case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
  643. case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
  644. case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
  645. default: break;
  646. }
  647. assert(0 && "Tag does not have a case for anchor string");
  648. return "";
  649. }
  650. /// getTypeString - Return a string used to label this descriptors type.
  651. ///
  652. const char *AnchorDesc::getTypeString() const {
  653. return "llvm.dbg.anchor.type";
  654. }
  655. #ifndef NDEBUG
  656. void AnchorDesc::dump() {
  657. cerr << getDescString() << " "
  658. << "Version(" << getVersion() << "), "
  659. << "Tag(" << getTag() << "), "
  660. << "AnchorTag(" << AnchorTag << ")\n";
  661. }
  662. #endif
  663. //===----------------------------------------------------------------------===//
  664. AnchoredDesc::AnchoredDesc(unsigned T)
  665. : DebugInfoDesc(T)
  666. , Anchor(NULL)
  667. {}
  668. /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
  669. ///
  670. void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
  671. DebugInfoDesc::ApplyToFields(Visitor);
  672. Visitor->Apply(Anchor);
  673. }
  674. //===----------------------------------------------------------------------===//
  675. CompileUnitDesc::CompileUnitDesc()
  676. : AnchoredDesc(DW_TAG_compile_unit)
  677. , Language(0)
  678. , FileName("")
  679. , Directory("")
  680. , Producer("")
  681. {}
  682. // Implement isa/cast/dyncast.
  683. bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
  684. return D->getTag() == DW_TAG_compile_unit;
  685. }
  686. /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
  687. ///
  688. void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
  689. AnchoredDesc::ApplyToFields(Visitor);
  690. // Handle cases out of sync with compiler.
  691. if (getVersion() == 0) {
  692. unsigned DebugVersion;
  693. Visitor->Apply(DebugVersion);
  694. }
  695. Visitor->Apply(Language);
  696. Visitor->Apply(FileName);
  697. Visitor->Apply(Directory);
  698. Visitor->Apply(Producer);
  699. }
  700. /// getDescString - Return a string used to compose global names and labels.
  701. ///
  702. const char *CompileUnitDesc::getDescString() const {
  703. return "llvm.dbg.compile_unit";
  704. }
  705. /// getTypeString - Return a string used to label this descriptors type.
  706. ///
  707. const char *CompileUnitDesc::getTypeString() const {
  708. return "llvm.dbg.compile_unit.type";
  709. }
  710. /// getAnchorString - Return a string used to label this descriptor's anchor.
  711. ///
  712. const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
  713. const char *CompileUnitDesc::getAnchorString() const {
  714. return AnchorString;
  715. }
  716. #ifndef NDEBUG
  717. void CompileUnitDesc::dump() {
  718. cerr << getDescString() << " "
  719. << "Version(" << getVersion() << "), "
  720. << "Tag(" << getTag() << "), "
  721. << "Anchor(" << getAnchor() << "), "
  722. << "Language(" << Language << "), "
  723. << "FileName(\"" << FileName << "\"), "
  724. << "Directory(\"" << Directory << "\"), "
  725. << "Producer(\"" << Producer << "\")\n";
  726. }
  727. #endif
  728. //===----------------------------------------------------------------------===//
  729. TypeDesc::TypeDesc(unsigned T)
  730. : DebugInfoDesc(T)
  731. , Context(NULL)
  732. , Name("")
  733. , File(NULL)
  734. , Line(0)
  735. , Size(0)
  736. , Align(0)
  737. , Offset(0)
  738. , Flags(0)
  739. {}
  740. /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
  741. ///
  742. void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
  743. DebugInfoDesc::ApplyToFields(Visitor);
  744. Visitor->Apply(Context);
  745. Visitor->Apply(Name);
  746. Visitor->Apply(File);
  747. Visitor->Apply(Line);
  748. Visitor->Apply(Size);
  749. Visitor->Apply(Align);
  750. Visitor->Apply(Offset);
  751. if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
  752. }
  753. /// getDescString - Return a string used to compose global names and labels.
  754. ///
  755. const char *TypeDesc::getDescString() const {
  756. return "llvm.dbg.type";
  757. }
  758. /// getTypeString - Return a string used to label this descriptor's type.
  759. ///
  760. const char *TypeDesc::getTypeString() const {
  761. return "llvm.dbg.type.type";
  762. }
  763. #ifndef NDEBUG
  764. void TypeDesc::dump() {
  765. cerr << getDescString() << " "
  766. << "Version(" << getVersion() << "), "
  767. << "Tag(" << getTag() << "), "
  768. << "Context(" << Context << "), "
  769. << "Name(\"" << Name << "\"), "
  770. << "File(" << File << "), "
  771. << "Line(" << Line << "), "
  772. << "Size(" << Size << "), "
  773. << "Align(" << Align << "), "
  774. << "Offset(" << Offset << "), "
  775. << "Flags(" << Flags << ")\n";
  776. }
  777. #endif
  778. //===----------------------------------------------------------------------===//
  779. BasicTypeDesc::BasicTypeDesc()
  780. : TypeDesc(DW_TAG_base_type)
  781. , Encoding(0)
  782. {}
  783. // Implement isa/cast/dyncast.
  784. bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
  785. return D->getTag() == DW_TAG_base_type;
  786. }
  787. /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
  788. ///
  789. void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
  790. TypeDesc::ApplyToFields(Visitor);
  791. Visitor->Apply(Encoding);
  792. }
  793. /// getDescString - Return a string used to compose global names and labels.
  794. ///
  795. const char *BasicTypeDesc::getDescString() const {
  796. return "llvm.dbg.basictype";
  797. }
  798. /// getTypeString - Return a string used to label this descriptor's type.
  799. ///
  800. const char *BasicTypeDesc::getTypeString() const {
  801. return "llvm.dbg.basictype.type";
  802. }
  803. #ifndef NDEBUG
  804. void BasicTypeDesc::dump() {
  805. cerr << getDescString() << " "
  806. << "Version(" << getVersion() << "), "
  807. << "Tag(" << getTag() << "), "
  808. << "Context(" << getContext() << "), "
  809. << "Name(\"" << getName() << "\"), "
  810. << "Size(" << getSize() << "), "
  811. << "Encoding(" << Encoding << ")\n";
  812. }
  813. #endif
  814. //===----------------------------------------------------------------------===//
  815. DerivedTypeDesc::DerivedTypeDesc(unsigned T)
  816. : TypeDesc(T)
  817. , FromType(NULL)
  818. {}
  819. // Implement isa/cast/dyncast.
  820. bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
  821. unsigned T = D->getTag();
  822. switch (T) {
  823. case DW_TAG_typedef:
  824. case DW_TAG_pointer_type:
  825. case DW_TAG_reference_type:
  826. case DW_TAG_const_type:
  827. case DW_TAG_volatile_type:
  828. case DW_TAG_restrict_type:
  829. case DW_TAG_member:
  830. case DW_TAG_inheritance:
  831. return true;
  832. default: break;
  833. }
  834. return false;
  835. }
  836. /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
  837. ///
  838. void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
  839. TypeDesc::ApplyToFields(Visitor);
  840. Visitor->Apply(FromType);
  841. }
  842. /// getDescString - Return a string used to compose global names and labels.
  843. ///
  844. const char *DerivedTypeDesc::getDescString() const {
  845. return "llvm.dbg.derivedtype";
  846. }
  847. /// getTypeString - Return a string used to label this descriptor's type.
  848. ///
  849. const char *DerivedTypeDesc::getTypeString() const {
  850. return "llvm.dbg.derivedtype.type";
  851. }
  852. #ifndef NDEBUG
  853. void DerivedTypeDesc::dump() {
  854. cerr << getDescString() << " "
  855. << "Version(" << getVersion() << "), "
  856. << "Tag(" << getTag() << "), "
  857. << "Context(" << getContext() << "), "
  858. << "Name(\"" << getName() << "\"), "
  859. << "Size(" << getSize() << "), "
  860. << "File(" << getFile() << "), "
  861. << "Line(" << getLine() << "), "
  862. << "FromType(" << FromType << ")\n";
  863. }
  864. #endif
  865. //===----------------------------------------------------------------------===//
  866. CompositeTypeDesc::CompositeTypeDesc(unsigned T)
  867. : DerivedTypeDesc(T)
  868. , Elements()
  869. {}
  870. // Implement isa/cast/dyncast.
  871. bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
  872. unsigned T = D->getTag();
  873. switch (T) {
  874. case DW_TAG_array_type:
  875. case DW_TAG_structure_type:
  876. case DW_TAG_union_type:
  877. case DW_TAG_enumeration_type:
  878. case DW_TAG_vector_type:
  879. case DW_TAG_subroutine_type:
  880. return true;
  881. default: break;
  882. }
  883. return false;
  884. }
  885. /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
  886. ///
  887. void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
  888. DerivedTypeDesc::ApplyToFields(Visitor);
  889. Visitor->Apply(Elements);
  890. }
  891. /// getDescString - Return a string used to compose global names and labels.
  892. ///
  893. const char *CompositeTypeDesc::getDescString() const {
  894. return "llvm.dbg.compositetype";
  895. }
  896. /// getTypeString - Return a string used to label this descriptor's type.
  897. ///
  898. const char *CompositeTypeDesc::getTypeString() const {
  899. return "llvm.dbg.compositetype.type";
  900. }
  901. #ifndef NDEBUG
  902. void CompositeTypeDesc::dump() {
  903. cerr << getDescString() << " "
  904. << "Version(" << getVersion() << "), "
  905. << "Tag(" << getTag() << "), "
  906. << "Context(" << getContext() << "), "
  907. << "Name(\"" << getName() << "\"), "
  908. << "Size(" << getSize() << "), "
  909. << "File(" << getFile() << "), "
  910. << "Line(" << getLine() << "), "
  911. << "FromType(" << getFromType() << "), "
  912. << "Elements.size(" << Elements.size() << ")\n";
  913. }
  914. #endif
  915. //===----------------------------------------------------------------------===//
  916. SubrangeDesc::SubrangeDesc()
  917. : DebugInfoDesc(DW_TAG_subrange_type)
  918. , Lo(0)
  919. , Hi(0)
  920. {}
  921. // Implement isa/cast/dyncast.
  922. bool SubrangeDesc::classof(const DebugInfoDesc *D) {
  923. return D->getTag() == DW_TAG_subrange_type;
  924. }
  925. /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
  926. ///
  927. void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
  928. DebugInfoDesc::ApplyToFields(Visitor);
  929. Visitor->Apply(Lo);
  930. Visitor->Apply(Hi);
  931. }
  932. /// getDescString - Return a string used to compose global names and labels.
  933. ///
  934. const char *SubrangeDesc::getDescString() const {
  935. return "llvm.dbg.subrange";
  936. }
  937. /// getTypeString - Return a string used to label this descriptor's type.
  938. ///
  939. const char *SubrangeDesc::getTypeString() const {
  940. return "llvm.dbg.subrange.type";
  941. }
  942. #ifndef NDEBUG
  943. void SubrangeDesc::dump() {
  944. cerr << getDescString() << " "
  945. << "Version(" << getVersion() << "), "
  946. << "Tag(" << getTag() << "), "
  947. << "Lo(" << Lo << "), "
  948. << "Hi(" << Hi << ")\n";
  949. }
  950. #endif
  951. //===----------------------------------------------------------------------===//
  952. EnumeratorDesc::EnumeratorDesc()
  953. : DebugInfoDesc(DW_TAG_enumerator)
  954. , Name("")
  955. , Value(0)
  956. {}
  957. // Implement isa/cast/dyncast.
  958. bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
  959. return D->getTag() == DW_TAG_enumerator;
  960. }
  961. /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
  962. ///
  963. void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
  964. DebugInfoDesc::ApplyToFields(Visitor);
  965. Visitor->Apply(Name);
  966. Visitor->Apply(Value);
  967. }
  968. /// getDescString - Return a string used to compose global names and labels.
  969. ///
  970. const char *EnumeratorDesc::getDescString() const {
  971. return "llvm.dbg.enumerator";
  972. }
  973. /// getTypeString - Return a string used to label this descriptor's type.
  974. ///
  975. const char *EnumeratorDesc::getTypeString() const {
  976. return "llvm.dbg.enumerator.type";
  977. }
  978. #ifndef NDEBUG
  979. void EnumeratorDesc::dump() {
  980. cerr << getDescString() << " "
  981. << "Version(" << getVersion() << "), "
  982. << "Tag(" << getTag() << "), "
  983. << "Name(" << Name << "), "
  984. << "Value(" << Value << ")\n";
  985. }
  986. #endif
  987. //===----------------------------------------------------------------------===//
  988. VariableDesc::VariableDesc(unsigned T)
  989. : DebugInfoDesc(T)
  990. , Context(NULL)
  991. , Name("")
  992. , File(NULL)
  993. , Line(0)
  994. , TyDesc(0)
  995. {}
  996. // Implement isa/cast/dyncast.
  997. bool VariableDesc::classof(const DebugInfoDesc *D) {
  998. unsigned T = D->getTag();
  999. switch (T) {
  1000. case DW_TAG_auto_variable:
  1001. case DW_TAG_arg_variable:
  1002. case DW_TAG_return_variable:
  1003. return true;
  1004. default: break;
  1005. }
  1006. return false;
  1007. }
  1008. /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
  1009. ///
  1010. void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
  1011. DebugInfoDesc::ApplyToFields(Visitor);
  1012. Visitor->Apply(Context);
  1013. Visitor->Apply(Name);
  1014. Visitor->Apply(File);
  1015. Visitor->Apply(Line);
  1016. Visitor->Apply(TyDesc);
  1017. }
  1018. /// getDescString - Return a string used to compose global names and labels.
  1019. ///
  1020. const char *VariableDesc::getDescString() const {
  1021. return "llvm.dbg.variable";
  1022. }
  1023. /// getTypeString - Return a string used to label this descriptor's type.
  1024. ///
  1025. const char *VariableDesc::getTypeString() const {
  1026. return "llvm.dbg.variable.type";
  1027. }
  1028. #ifndef NDEBUG
  1029. void VariableDesc::dump() {
  1030. cerr << getDescString() << " "
  1031. << "Version(" << getVersion() << "), "
  1032. << "Tag(" << getTag() << "), "
  1033. << "Context(" << Context << "), "
  1034. << "Name(\"" << Name << "\"), "
  1035. << "File(" << File << "), "
  1036. << "Line(" << Line << "), "
  1037. << "TyDesc(" << TyDesc << ")\n";
  1038. }
  1039. #endif
  1040. //===----------------------------------------------------------------------===//
  1041. GlobalDesc::GlobalDesc(unsigned T)
  1042. : AnchoredDesc(T)
  1043. , Context(0)
  1044. , Name("")
  1045. , FullName("")
  1046. , LinkageName("")
  1047. , File(NULL)
  1048. , Line(0)
  1049. , TyDesc(NULL)
  1050. , IsStatic(false)
  1051. , IsDefinition(false)
  1052. {}
  1053. /// ApplyToFields - Target the visitor to the fields of the global.
  1054. ///
  1055. void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
  1056. AnchoredDesc::ApplyToFields(Visitor);
  1057. Visitor->Apply(Context);
  1058. Visitor->Apply(Name);
  1059. Visitor->Apply(FullName);
  1060. Visitor->Apply(LinkageName);
  1061. Visitor->Apply(File);
  1062. Visitor->Apply(Line);
  1063. Visitor->Apply(TyDesc);
  1064. Visitor->Apply(IsStatic);
  1065. Visitor->Apply(IsDefinition);
  1066. }
  1067. //===----------------------------------------------------------------------===//
  1068. GlobalVariableDesc::GlobalVariableDesc()
  1069. : GlobalDesc(DW_TAG_variable)
  1070. , Global(NULL)
  1071. {}
  1072. // Implement isa/cast/dyncast.
  1073. bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
  1074. return D->getTag() == DW_TAG_variable;
  1075. }
  1076. /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
  1077. ///
  1078. void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
  1079. GlobalDesc::ApplyToFields(Visitor);
  1080. Visitor->Apply(Global);
  1081. }
  1082. /// getDescString - Return a string used to compose global names and labels.
  1083. ///
  1084. const char *GlobalVariableDesc::getDescString() const {
  1085. return "llvm.dbg.global_variable";
  1086. }
  1087. /// getTypeString - Return a string used to label this descriptors type.
  1088. ///
  1089. const char *GlobalVariableDesc::getTypeString() const {
  1090. return "llvm.dbg.global_variable.type";
  1091. }
  1092. /// getAnchorString - Return a string used to label this descriptor's anchor.
  1093. ///
  1094. const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
  1095. const char *GlobalVariableDesc::getAnchorString() const {
  1096. return AnchorString;
  1097. }
  1098. #ifndef NDEBUG
  1099. void GlobalVariableDesc::dump() {
  1100. cerr << getDescString() << " "
  1101. << "Version(" << getVersion() << "), "
  1102. << "Tag(" << getTag() << "), "
  1103. << "Anchor(" << getAnchor() << "), "
  1104. << "Name(\"" << getName() << "\"), "
  1105. << "FullName(\"" << getFullName() << "\"), "
  1106. << "LinkageName(\"" << getLinkageName() << "\"), "
  1107. << "File(" << getFile() << "),"
  1108. << "Line(" << getLine() << "),"
  1109. << "Type(" << getType() << "), "
  1110. << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
  1111. << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
  1112. << "Global(" << Global << ")\n";
  1113. }
  1114. #endif
  1115. //===----------------------------------------------------------------------===//
  1116. SubprogramDesc::SubprogramDesc()
  1117. : GlobalDesc(DW_TAG_subprogram)
  1118. {}
  1119. // Implement isa/cast/dyncast.
  1120. bool SubprogramDesc::classof(const DebugInfoDesc *D) {
  1121. return D->getTag() == DW_TAG_subprogram;
  1122. }
  1123. /// ApplyToFields - Target the visitor to the fields of the
  1124. /// SubprogramDesc.
  1125. void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
  1126. GlobalDesc::ApplyToFields(Visitor);
  1127. }
  1128. /// getDescString - Return a string used to compose global names and labels.
  1129. ///
  1130. const char *SubprogramDesc::getDescString() const {
  1131. return "llvm.dbg.subprogram";
  1132. }
  1133. /// getTypeString - Return a string used to label this descriptors type.
  1134. ///
  1135. const char *SubprogramDesc::getTypeString() const {
  1136. return "llvm.dbg.subprogram.type";
  1137. }
  1138. /// getAnchorString - Return a string used to label this descriptor's anchor.
  1139. ///
  1140. const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
  1141. const char *SubprogramDesc::getAnchorString() const {
  1142. return AnchorString;
  1143. }
  1144. #ifndef NDEBUG
  1145. void SubprogramDesc::dump() {
  1146. cerr << getDescString() << " "
  1147. << "Version(" << getVersion() << "), "
  1148. << "Tag(" << getTag() << "), "
  1149. << "Anchor(" << getAnchor() << "), "
  1150. << "Name(\"" << getName() << "\"), "
  1151. << "FullName(\"" << getFullName() << "\"), "
  1152. << "LinkageName(\"" << getLinkageName() << "\"), "
  1153. << "File(" << getFile() << "),"
  1154. << "Line(" << getLine() << "),"
  1155. << "Type(" << getType() << "), "
  1156. << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
  1157. << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
  1158. }
  1159. #endif
  1160. //===----------------------------------------------------------------------===//
  1161. BlockDesc::BlockDesc()
  1162. : DebugInfoDesc(DW_TAG_lexical_block)
  1163. , Context(NULL)
  1164. {}
  1165. // Implement isa/cast/dyncast.
  1166. bool BlockDesc::classof(const DebugInfoDesc *D) {
  1167. return D->getTag() == DW_TAG_lexical_block;
  1168. }
  1169. /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
  1170. ///
  1171. void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
  1172. DebugInfoDesc::ApplyToFields(Visitor);
  1173. Visitor->Apply(Context);
  1174. }
  1175. /// getDescString - Return a string used to compose global names and labels.
  1176. ///
  1177. const char *BlockDesc::getDescString() const {
  1178. return "llvm.dbg.block";
  1179. }
  1180. /// getTypeString - Return a string used to label this descriptors type.
  1181. ///
  1182. const char *BlockDesc::getTypeString() const {
  1183. return "llvm.dbg.block.type";
  1184. }
  1185. #ifndef NDEBUG
  1186. void BlockDesc::dump() {
  1187. cerr << getDescString() << " "
  1188. << "Version(" << getVersion() << "), "
  1189. << "Tag(" << getTag() << "),"
  1190. << "Context(" << Context << ")\n";
  1191. }
  1192. #endif
  1193. //===----------------------------------------------------------------------===//
  1194. DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
  1195. return Deserialize(getGlobalVariable(V));
  1196. }
  1197. DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
  1198. // Handle NULL.
  1199. if (!GV) return NULL;
  1200. // Check to see if it has been already deserialized.
  1201. DebugInfoDesc *&Slot = GlobalDescs[GV];
  1202. if (Slot) return Slot;
  1203. // Get the Tag from the global.
  1204. unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
  1205. // Create an empty instance of the correct sort.
  1206. Slot = DebugInfoDesc::DescFactory(Tag);
  1207. // If not a user defined descriptor.
  1208. if (Slot) {
  1209. // Deserialize the fields.
  1210. DIDeserializeVisitor DRAM(*this, GV);
  1211. DRAM.ApplyToFields(Slot);
  1212. }
  1213. return Slot;
  1214. }
  1215. //===----------------------------------------------------------------------===//
  1216. /// getStrPtrType - Return a "sbyte *" type.
  1217. ///
  1218. const PointerType *DISerializer::getStrPtrType() {
  1219. // If not already defined.
  1220. if (!StrPtrTy) {
  1221. // Construct the pointer to signed bytes.
  1222. StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
  1223. }
  1224. return StrPtrTy;
  1225. }
  1226. /// getEmptyStructPtrType - Return a "{ }*" type.
  1227. ///
  1228. const PointerType *DISerializer::getEmptyStructPtrType() {
  1229. // If not already defined.
  1230. if (EmptyStructPtrTy) return EmptyStructPtrTy;
  1231. // Construct the pointer to empty structure type.
  1232. const StructType *EmptyStructTy = StructType::get(NULL, NULL);
  1233. // Construct the pointer to empty structure type.
  1234. EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
  1235. return EmptyStructPtrTy;
  1236. }
  1237. /// getTagType - Return the type describing the specified descriptor (via tag.)
  1238. ///
  1239. const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
  1240. // Attempt to get the previously defined type.
  1241. StructType *&Ty = TagTypes[DD->getTag()];
  1242. // If not already defined.
  1243. if (!Ty) {
  1244. // Set up fields vector.
  1245. std::vector<const Type*> Fields;
  1246. // Get types of fields.
  1247. DIGetTypesVisitor GTAM(*this, Fields);
  1248. GTAM.ApplyToFields(DD);
  1249. // Construct structured type.
  1250. Ty = StructType::get(Fields);
  1251. // Register type name with module.
  1252. M->addTypeName(DD->getTypeString(), Ty);
  1253. }
  1254. return Ty;
  1255. }
  1256. /// getString - Construct the string as constant string global.
  1257. ///
  1258. Constant *DISerializer::getString(const std::string &String) {
  1259. // Check string cache for previous edition.
  1260. Constant *&Slot = StringCache[String];
  1261. // Return Constant if previously defined.
  1262. if (Slot) return Slot;
  1263. // If empty string then use a sbyte* null instead.
  1264. if (String.empty()) {
  1265. Slot = ConstantPointerNull::get(getStrPtrType());
  1266. } else {
  1267. // Construct string as an llvm constant.
  1268. Constant *ConstStr = ConstantArray::get(String);
  1269. // Otherwise create and return a new string global.
  1270. GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
  1271. GlobalVariable::InternalLinkage,
  1272. ConstStr, ".str", M);
  1273. StrGV->setSection("llvm.metadata");
  1274. // Convert to generic string pointer.
  1275. Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
  1276. }
  1277. return Slot;
  1278. }
  1279. /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
  1280. /// so that it can be serialized to a .bc or .ll file.
  1281. GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
  1282. // Check if the DebugInfoDesc is already in the map.
  1283. GlobalVariable *&Slot = DescGlobals[DD];
  1284. // See if DebugInfoDesc exists, if so return prior GlobalVariable.
  1285. if (Slot) return Slot;
  1286. // Get the type associated with the Tag.
  1287. const StructType *Ty = getTagType(DD);
  1288. // Create the GlobalVariable early to prevent infinite recursion.
  1289. GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
  1290. NULL, DD->getDescString(), M);
  1291. GV->setSection("llvm.metadata");
  1292. // Insert new GlobalVariable in DescGlobals map.
  1293. Slot = GV;
  1294. // Set up elements vector
  1295. std::vector<Constant*> Elements;
  1296. // Add fields.
  1297. DISerializeVisitor SRAM(*this, Elements);
  1298. SRAM.ApplyToFields(DD);
  1299. // Set the globals initializer.
  1300. GV->setInitializer(ConstantStruct::get(Ty, Elements));
  1301. return GV;
  1302. }
  1303. /// addDescriptor - Directly connect DD with existing GV.
  1304. void DISerializer::addDescriptor(DebugInfoDesc *DD,
  1305. GlobalVariable *GV) {
  1306. DescGlobals[DD] = GV;
  1307. }
  1308. //===----------------------------------------------------------------------===//
  1309. /// Verify - Return true if the GlobalVariable appears to be a valid
  1310. /// serialization of a DebugInfoDesc.
  1311. bool DIVerifier::Verify(Value *V) {
  1312. return !V || Verify(getGlobalVariable(V));
  1313. }
  1314. bool DIVerifier::Verify(GlobalVariable *GV) {
  1315. // NULLs are valid.
  1316. if (!GV) return true;
  1317. // Check prior validity.
  1318. unsigned &ValiditySlot = Validity[GV];
  1319. // If visited before then use old state.
  1320. if (ValiditySlot) return ValiditySlot == Valid;
  1321. // Assume validity for the time being (recursion.)
  1322. ValiditySlot = Valid;
  1323. // Make sure the global is internal or link once (anchor.)
  1324. if (GV->getLinkage() != GlobalValue::InternalLinkage &&
  1325. GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
  1326. ValiditySlot = Invalid;
  1327. return false;
  1328. }
  1329. // Get the Tag.
  1330. unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
  1331. // Check for user defined descriptors.
  1332. if (Tag == DW_TAG_invalid) {
  1333. ValiditySlot = Valid;
  1334. return true;
  1335. }
  1336. // Get the Version.
  1337. unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
  1338. // Check for version mismatch.
  1339. if (Version != LLVMDebugVersion) {
  1340. ValiditySlot = Invalid;
  1341. return false;
  1342. }
  1343. // Construct an empty DebugInfoDesc.
  1344. DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
  1345. // Allow for user defined descriptors.
  1346. if (!DD) return true;
  1347. // Get the initializer constant.
  1348. ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
  1349. // Get the operand count.
  1350. unsigned N = CI->getNumOperands();
  1351. // Get the field count.
  1352. unsigned &CountSlot = Counts[Tag];
  1353. if (!CountSlot)
  1354. // Check the operand count to the field count
  1355. CountSlot = CountFields(DD);
  1356. // Field count must be at most equal operand count.
  1357. if (CountSlot > N) {
  1358. delete DD;
  1359. ValiditySlot = Invalid;
  1360. return false;
  1361. }
  1362. // Check each field for valid type.
  1363. DIVerifyVisitor VRAM(*this, GV);
  1364. VRAM.ApplyToFields(DD);
  1365. // Release empty DebugInfoDesc.
  1366. delete DD;
  1367. // If fields are not valid.
  1368. if (!VRAM.isValid()) {
  1369. ValiditySlot = Invalid;
  1370. return false;
  1371. }
  1372. return true;
  1373. }
  1374. /// isVerified - Return true if the specified GV has already been
  1375. /// verified as a debug information descriptor.
  1376. bool DIVerifier::isVerified(GlobalVariable *GV) {
  1377. unsigned &ValiditySlot = Validity[GV];
  1378. if (ValiditySlot) return ValiditySlot == Valid;
  1379. return false;
  1380. }
  1381. //===----------------------------------------------------------------------===//
  1382. DebugScope::~DebugScope() {
  1383. for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
  1384. for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
  1385. }
  1386. //===----------------------------------------------------------------------===//
  1387. MachineModuleInfo::MachineModuleInfo()
  1388. : ImmutablePass(&ID)
  1389. , DR()
  1390. , VR()
  1391. , CompileUnits()
  1392. , Directories()
  1393. , SourceFiles()
  1394. , Lines()
  1395. , LabelIDList()
  1396. , ScopeMap()
  1397. , RootScope(NULL)
  1398. , FrameMoves()
  1399. , LandingPads()
  1400. , Personalities()
  1401. , CallsEHReturn(0)
  1402. , CallsUnwindInit(0)
  1403. {
  1404. // Always emit "no personality" info
  1405. Personalities.push_back(NULL);
  1406. }
  1407. MachineModuleInfo::~MachineModuleInfo() {
  1408. }
  1409. /// doInitialization - Initialize the state for a new module.
  1410. ///
  1411. bool MachineModuleInfo::doInitialization() {
  1412. return false;
  1413. }
  1414. /// doFinalization - Tear down the state after completion of a module.
  1415. ///
  1416. bool MachineModuleInfo::doFinalization() {
  1417. return false;
  1418. }
  1419. /// BeginFunction - Begin gathering function meta information.
  1420. ///
  1421. void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
  1422. // Coming soon.
  1423. }
  1424. /// EndFunction - Discard function meta information.
  1425. ///
  1426. void MachineModuleInfo::EndFunction() {
  1427. // Clean up scope information.
  1428. if (RootScope) {
  1429. delete RootScope;
  1430. ScopeMap.clear();
  1431. RootScope = NULL;
  1432. }
  1433. // Clean up line info.
  1434. Lines.clear();
  1435. // Clean up frame info.
  1436. FrameMoves.clear();
  1437. // Clean up exception info.
  1438. LandingPads.clear();
  1439. TypeInfos.clear();
  1440. FilterIds.clear();
  1441. FilterEnds.clear();
  1442. CallsEHReturn = 0;
  1443. CallsUnwindInit = 0;
  1444. }
  1445. /// getDescFor - Convert a Value to a debug information descriptor.
  1446. ///
  1447. // FIXME - use new Value type when available.
  1448. DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
  1449. return DR.Deserialize(V);
  1450. }
  1451. /// AnalyzeModule - Scan the module for global debug information.
  1452. ///
  1453. void MachineModuleInfo::AnalyzeModule(Module &M) {
  1454. SetupCompileUnits(M);
  1455. // Insert functions in the llvm.used array into UsedFunctions.
  1456. GlobalVariable *GV = M.getGlobalVariable("llvm.used");
  1457. if (!GV || !GV->hasInitializer()) return;
  1458. // Should be an array of 'i8*'.
  1459. ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
  1460. if (InitList == 0) return;
  1461. for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
  1462. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
  1463. if (CE->getOpcode() == Instruction::BitCast)
  1464. if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
  1465. UsedFunctions.insert(F);
  1466. }
  1467. }
  1468. /// SetupCompileUnits - Set up the unique vector of compile units.
  1469. ///
  1470. void MachineModuleInfo::SetupCompileUnits(Module &M) {
  1471. std::vector<CompileUnitDesc *> CU;
  1472. getAnchoredDescriptors<CompileUnitDesc>(M, CU);
  1473. for (unsigned i = 0, N = CU.size(); i < N; i++) {
  1474. CompileUnits.insert(CU[i]);
  1475. }
  1476. }
  1477. /// getCompileUnits - Return a vector of debug compile units.
  1478. ///
  1479. const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
  1480. return CompileUnits;
  1481. }
  1482. /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
  1483. /// named GlobalVariable.
  1484. void
  1485. MachineModuleInfo::getGlobalVariablesUsing(Module &M,
  1486. const std::string &RootName,
  1487. std::vector<GlobalVariable*>&Result){
  1488. return ::getGlobalVariablesUsing(M, RootName, Result);
  1489. }
  1490. /// RecordSourceLine - Records location information and associates it with a
  1491. /// debug label. Returns a unique label ID used to generate a label and
  1492. /// provide correspondence to the source line list.
  1493. unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
  1494. unsigned Source) {
  1495. unsigned ID = NextLabelID();
  1496. Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
  1497. return ID;
  1498. }
  1499. /// RecordSource - Register a source file with debug info. Returns an source
  1500. /// ID.
  1501. unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
  1502. const std::string &Source) {
  1503. unsigned DirectoryID = Directories.insert(Directory);
  1504. return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
  1505. }
  1506. unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
  1507. return RecordSource(CompileUnit->getDirectory(),
  1508. CompileUnit->getFileName());
  1509. }
  1510. /// RecordRegionStart - Indicate the start of a region.
  1511. ///
  1512. unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
  1513. // FIXME - need to be able to handle split scopes because of bb cloning.
  1514. DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
  1515. DebugScope *Scope = getOrCreateScope(ScopeDesc);
  1516. unsigned ID = NextLabelID();
  1517. if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
  1518. return ID;
  1519. }
  1520. /// RecordRegionEnd - Indicate the end of a region.
  1521. ///
  1522. unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
  1523. // FIXME - need to be able to handle split scopes because of bb cloning.
  1524. DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
  1525. DebugScope *Scope = getOrCreateScope(ScopeDesc);
  1526. unsigned ID = NextLabelID();
  1527. Scope->setEndLabelID(ID);
  1528. return ID;
  1529. }
  1530. /// RecordVariable - Indicate the declaration of a local variable.
  1531. ///
  1532. void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
  1533. VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
  1534. DebugScope *Scope = getOrCreateScope(VD->getContext());
  1535. DebugVariable *DV = new DebugVariable(VD, FrameIndex);
  1536. Scope->AddVariable(DV);
  1537. }
  1538. /// getOrCreateScope - Returns the scope associated with the given descriptor.
  1539. ///
  1540. DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
  1541. DebugScope *&Slot = ScopeMap[ScopeDesc];
  1542. if (!Slot) {
  1543. // FIXME - breaks down when the context is an inlined function.
  1544. DebugInfoDesc *ParentDesc = NULL;
  1545. if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
  1546. ParentDesc = Block->getContext();
  1547. }
  1548. DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
  1549. Slot = new DebugScope(Parent, ScopeDesc);
  1550. if (Parent) {
  1551. Parent->AddScope(Slot);
  1552. } else if (RootScope) {
  1553. // FIXME - Add inlined function scopes to the root so we can delete
  1554. // them later. Long term, handle inlined functions properly.
  1555. RootScope->AddScope(Slot);
  1556. } else {
  1557. // First function is top level function.
  1558. RootScope = Slot;
  1559. }
  1560. }
  1561. return Slot;
  1562. }
  1563. //===-EH-------------------------------------------------------------------===//
  1564. /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
  1565. /// specified MachineBasicBlock.
  1566. LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
  1567. (MachineBasicBlock *LandingPad) {
  1568. unsigned N = LandingPads.size();
  1569. for (unsigned i = 0; i < N; ++i) {
  1570. LandingPadInfo &LP = LandingPads[i];
  1571. if (LP.LandingPadBlock == LandingPad)
  1572. return LP;
  1573. }
  1574. LandingPads.push_back(LandingPadInfo(LandingPad));
  1575. return LandingPads[N];
  1576. }
  1577. /// addInvoke - Provide the begin and end labels of an invoke style call and
  1578. /// associate it with a try landing pad block.
  1579. void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
  1580. unsigned BeginLabel, unsigned EndLabel) {
  1581. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  1582. LP.BeginLabels.push_back(BeginLabel);
  1583. LP.EndLabels.push_back(EndLabel);
  1584. }
  1585. /// addLandingPad - Provide the label of a try LandingPad block.
  1586. ///
  1587. unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
  1588. unsigned LandingPadLabel = NextLabelID();
  1589. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  1590. LP.LandingPadLabel = LandingPadLabel;
  1591. return LandingPadLabel;
  1592. }
  1593. /// addPersonality - Provide the personality function for the exception
  1594. /// information.
  1595. void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
  1596. Function *Personality) {
  1597. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  1598. LP.Personality = Personality;
  1599. for (unsigned i = 0; i < Personalities.size(); ++i)
  1600. if (Personalities[i] == Personality)
  1601. return;
  1602. Personalities.push_back(Personality);
  1603. }
  1604. /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
  1605. ///
  1606. void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
  1607. std::vector<GlobalVariable *> &TyInfo) {
  1608. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  1609. for (unsigned N = TyInfo.size(); N; --N)
  1610. LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
  1611. }
  1612. /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
  1613. ///
  1614. void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
  1615. std::vector<GlobalVariable *> &TyInfo) {
  1616. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  1617. std::vector<unsigned> IdsInFilter(TyInfo.size());
  1618. for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
  1619. IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
  1620. LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
  1621. }
  1622. /// addCleanup - Add a cleanup action for a landing pad.
  1623. ///
  1624. void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
  1625. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  1626. LP.TypeIds.push_back(0);
  1627. }
  1628. /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
  1629. /// pads.
  1630. void MachineModuleInfo::TidyLandingPads() {
  1631. for (unsigned i = 0; i != LandingPads.size(); ) {
  1632. LandingPadInfo &LandingPad = LandingPads[i];
  1633. LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
  1634. // Special case: we *should* emit LPs with null LP MBB. This indicates
  1635. // "nounwind" case.
  1636. if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
  1637. LandingPads.erase(LandingPads.begin() + i);
  1638. continue;
  1639. }
  1640. for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
  1641. unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
  1642. unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
  1643. if (!BeginLabel || !EndLabel) {
  1644. LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
  1645. LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
  1646. continue;
  1647. }
  1648. LandingPad.BeginLabels[j] = BeginLabel;
  1649. LandingPad.EndLabels[j] = EndLabel;
  1650. ++j;
  1651. }
  1652. // Remove landing pads with no try-ranges.
  1653. if (LandingPads[i].BeginLabels.empty()) {
  1654. LandingPads.erase(LandingPads.begin() + i);
  1655. continue;
  1656. }
  1657. // If there is no landing pad, ensure that the list of typeids is empty.
  1658. // If the only typeid is a cleanup, this is the same as having no typeids.
  1659. if (!LandingPad.LandingPadBlock ||
  1660. (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
  1661. LandingPad.TypeIds.clear();
  1662. ++i;
  1663. }
  1664. }
  1665. /// getTypeIDFor - Return the type id for the specified typeinfo. This is
  1666. /// function wide.
  1667. unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
  1668. for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
  1669. if (TypeInfos[i] == TI) return i + 1;
  1670. TypeInfos.push_back(TI);
  1671. return TypeInfos.size();
  1672. }
  1673. /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
  1674. /// function wide.
  1675. int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
  1676. // If the new filter coincides with the tail of an existing filter, then
  1677. // re-use the existing filter. Folding filters more than this requires
  1678. // re-ordering filters and/or their elements - probably not worth it.
  1679. for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
  1680. E = FilterEnds.end(); I != E; ++I) {
  1681. unsigned i = *I, j = TyIds.size();
  1682. while (i && j)
  1683. if (FilterIds[--i] != TyIds[--j])
  1684. goto try_next;
  1685. if (!j)
  1686. // The new filter coincides with range [i, end) of the existing filter.
  1687. return -(1 + i);
  1688. try_next:;
  1689. }
  1690. // Add the new filter.
  1691. int FilterID = -(1 + FilterIds.size());
  1692. FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
  1693. for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
  1694. FilterIds.push_back(TyIds[I]);
  1695. FilterEnds.push_back(FilterIds.size());
  1696. FilterIds.push_back(0); // terminator
  1697. return FilterID;
  1698. }
  1699. /// getPersonality - Return the personality function for the current function.
  1700. Function *MachineModuleInfo::getPersonality() const {
  1701. // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
  1702. // function
  1703. return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
  1704. }
  1705. /// getPersonalityIndex - Return unique index for current personality
  1706. /// function. NULL personality function should always get zero index.
  1707. unsigned MachineModuleInfo::getPersonalityIndex() const {
  1708. const Function* Personality = NULL;
  1709. // Scan landing pads. If there is at least one non-NULL personality - use it.
  1710. for (unsigned i = 0; i != LandingPads.size(); ++i)
  1711. if (LandingPads[i].Personality) {
  1712. Personality = LandingPads[i].Personality;
  1713. break;
  1714. }
  1715. for (unsigned i = 0; i < Personalities.size(); ++i) {
  1716. if (Personalities[i] == Personality)
  1717. return i;
  1718. }
  1719. // This should never happen
  1720. assert(0 && "Personality function should be set!");
  1721. return 0;
  1722. }
  1723. //===----------------------------------------------------------------------===//
  1724. /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
  1725. /// a info consumer to determine if the range of two labels is empty, by seeing
  1726. /// if the labels map to the same reduced label.
  1727. namespace llvm {
  1728. struct DebugLabelFolder : public MachineFunctionPass {
  1729. static char ID;
  1730. DebugLabelFolder() : MachineFunctionPass(&ID) {}
  1731. virtual bool runOnMachineFunction(MachineFunction &MF);
  1732. virtual const char *getPassName() const { return "Label Folder"; }
  1733. };
  1734. char DebugLabelFolder::ID = 0;
  1735. bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
  1736. // Get machine module info.
  1737. MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
  1738. if (!MMI) return false;
  1739. // Track if change is made.
  1740. bool MadeChange = false;
  1741. // No prior label to begin.
  1742. unsigned PriorLabel = 0;
  1743. // Iterate through basic blocks.
  1744. for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
  1745. BB != E; ++BB) {
  1746. // Iterate through instructions.
  1747. for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
  1748. // Is it a label.
  1749. if (I->isDebugLabel()) {
  1750. // The label ID # is always operand #0, an immediate.
  1751. unsigned NextLabel = I->getOperand(0).getImm();
  1752. // If there was an immediate prior label.
  1753. if (PriorLabel) {
  1754. // Remap the current label to prior label.
  1755. MMI->RemapLabel(NextLabel, PriorLabel);
  1756. // Delete the current label.
  1757. I = BB->erase(I);
  1758. // Indicate a change has been made.
  1759. MadeChange = true;
  1760. continue;
  1761. } else {
  1762. // Start a new round.
  1763. PriorLabel = NextLabel;
  1764. }
  1765. } else {
  1766. // No consecutive labels.
  1767. PriorLabel = 0;
  1768. }
  1769. ++I;
  1770. }
  1771. }
  1772. return MadeChange;
  1773. }
  1774. FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
  1775. }