DebugInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
  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. //
  10. // This file implements the helper classes used to build and interpret debug
  11. // information in LLVM IR form.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/DebugInfo.h"
  15. #include "LLVMContextImpl.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/Analysis/ValueTracking.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/DIBuilder.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/Instructions.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Intrinsics.h"
  26. #include "llvm/IR/GVMaterializer.h"
  27. #include "llvm/IR/Module.h"
  28. #include "llvm/IR/ValueHandle.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/Dwarf.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. using namespace llvm;
  33. using namespace llvm::dwarf;
  34. DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
  35. if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
  36. return LocalScope->getSubprogram();
  37. return nullptr;
  38. }
  39. DISubprogram *llvm::getDISubprogram(const Function *F) {
  40. // We look for the first instr that has a debug annotation leading back to F.
  41. for (auto &BB : *F) {
  42. auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
  43. return Inst.getDebugLoc();
  44. });
  45. if (Inst == BB.end())
  46. continue;
  47. DebugLoc DLoc = Inst->getDebugLoc();
  48. const MDNode *Scope = DLoc.getInlinedAtScope();
  49. auto *Subprogram = getDISubprogram(Scope);
  50. return Subprogram->describes(F) ? Subprogram : nullptr;
  51. }
  52. return nullptr;
  53. }
  54. DICompositeTypeBase *llvm::getDICompositeType(DIType *T) {
  55. if (auto *C = dyn_cast_or_null<DICompositeTypeBase>(T))
  56. return C;
  57. if (auto *D = dyn_cast_or_null<DIDerivedTypeBase>(T)) {
  58. // This function is currently used by dragonegg and dragonegg does
  59. // not generate identifier for types, so using an empty map to resolve
  60. // DerivedFrom should be fine.
  61. DITypeIdentifierMap EmptyMap;
  62. return getDICompositeType(D->getBaseType().resolve(EmptyMap));
  63. }
  64. return nullptr;
  65. }
  66. DITypeIdentifierMap
  67. llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
  68. DITypeIdentifierMap Map;
  69. for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
  70. auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi));
  71. DINodeArray Retain = CU->getRetainedTypes();
  72. for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
  73. if (!isa<DICompositeType>(Retain[Ti]))
  74. continue;
  75. auto *Ty = cast<DICompositeType>(Retain[Ti]);
  76. if (MDString *TypeId = Ty->getRawIdentifier()) {
  77. // Definition has priority over declaration.
  78. // Try to insert (TypeId, Ty) to Map.
  79. std::pair<DITypeIdentifierMap::iterator, bool> P =
  80. Map.insert(std::make_pair(TypeId, Ty));
  81. // If TypeId already exists in Map and this is a definition, replace
  82. // whatever we had (declaration or definition) with the definition.
  83. if (!P.second && !Ty->isForwardDecl())
  84. P.first->second = Ty;
  85. }
  86. }
  87. }
  88. return Map;
  89. }
  90. //===----------------------------------------------------------------------===//
  91. // DebugInfoFinder implementations.
  92. //===----------------------------------------------------------------------===//
  93. void DebugInfoFinder::reset() {
  94. CUs.clear();
  95. SPs.clear();
  96. GVs.clear();
  97. TYs.clear();
  98. Scopes.clear();
  99. NodesSeen.clear();
  100. TypeIdentifierMap.clear();
  101. TypeMapInitialized = false;
  102. }
  103. void DebugInfoFinder::InitializeTypeMap(const Module &M) {
  104. if (!TypeMapInitialized)
  105. if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
  106. TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
  107. TypeMapInitialized = true;
  108. }
  109. }
  110. void DebugInfoFinder::processModule(const Module &M) {
  111. InitializeTypeMap(M);
  112. if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
  113. for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
  114. auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
  115. addCompileUnit(CU);
  116. for (auto *DIG : CU->getGlobalVariables()) {
  117. if (addGlobalVariable(DIG)) {
  118. processScope(DIG->getScope());
  119. processType(DIG->getType().resolve(TypeIdentifierMap));
  120. }
  121. }
  122. for (auto *SP : CU->getSubprograms())
  123. processSubprogram(SP);
  124. for (auto *ET : CU->getEnumTypes())
  125. processType(ET);
  126. for (auto *RT : CU->getRetainedTypes())
  127. processType(RT);
  128. for (auto *Import : CU->getImportedEntities()) {
  129. auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
  130. if (auto *T = dyn_cast<DIType>(Entity))
  131. processType(T);
  132. else if (auto *SP = dyn_cast<DISubprogram>(Entity))
  133. processSubprogram(SP);
  134. else if (auto *NS = dyn_cast<DINamespace>(Entity))
  135. processScope(NS->getScope());
  136. else if (auto *M = dyn_cast<DIModule>(Entity))
  137. processScope(M->getScope());
  138. }
  139. }
  140. }
  141. }
  142. void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
  143. if (!Loc)
  144. return;
  145. InitializeTypeMap(M);
  146. processScope(Loc->getScope());
  147. processLocation(M, Loc->getInlinedAt());
  148. }
  149. void DebugInfoFinder::processType(DIType *DT) {
  150. if (!addType(DT))
  151. return;
  152. processScope(DT->getScope().resolve(TypeIdentifierMap));
  153. if (auto *DCT = dyn_cast<DICompositeTypeBase>(DT)) {
  154. processType(DCT->getBaseType().resolve(TypeIdentifierMap));
  155. if (auto *ST = dyn_cast<DISubroutineType>(DCT)) {
  156. for (DITypeRef Ref : ST->getTypeArray())
  157. processType(Ref.resolve(TypeIdentifierMap));
  158. return;
  159. }
  160. for (Metadata *D : DCT->getElements()) {
  161. if (auto *T = dyn_cast<DIType>(D))
  162. processType(T);
  163. else if (auto *SP = dyn_cast<DISubprogram>(D))
  164. processSubprogram(SP);
  165. }
  166. } else if (auto *DDT = dyn_cast<DIDerivedTypeBase>(DT)) {
  167. processType(DDT->getBaseType().resolve(TypeIdentifierMap));
  168. }
  169. }
  170. void DebugInfoFinder::processScope(DIScope *Scope) {
  171. if (!Scope)
  172. return;
  173. if (auto *Ty = dyn_cast<DIType>(Scope)) {
  174. processType(Ty);
  175. return;
  176. }
  177. if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
  178. addCompileUnit(CU);
  179. return;
  180. }
  181. if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
  182. processSubprogram(SP);
  183. return;
  184. }
  185. if (!addScope(Scope))
  186. return;
  187. if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
  188. processScope(LB->getScope());
  189. } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
  190. processScope(NS->getScope());
  191. } else if (auto *M = dyn_cast<DIModule>(Scope)) {
  192. processScope(M->getScope());
  193. }
  194. }
  195. void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
  196. if (!addSubprogram(SP))
  197. return;
  198. processScope(SP->getScope().resolve(TypeIdentifierMap));
  199. processType(SP->getType());
  200. for (auto *Element : SP->getTemplateParams()) {
  201. if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
  202. processType(TType->getType().resolve(TypeIdentifierMap));
  203. } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
  204. processType(TVal->getType().resolve(TypeIdentifierMap));
  205. }
  206. }
  207. }
  208. void DebugInfoFinder::processDeclare(const Module &M,
  209. const DbgDeclareInst *DDI) {
  210. auto *N = dyn_cast<MDNode>(DDI->getVariable());
  211. if (!N)
  212. return;
  213. InitializeTypeMap(M);
  214. auto *DV = dyn_cast<DILocalVariable>(N);
  215. if (!DV)
  216. return;
  217. if (!NodesSeen.insert(DV).second)
  218. return;
  219. processScope(DV->getScope());
  220. processType(DV->getType().resolve(TypeIdentifierMap));
  221. }
  222. void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
  223. auto *N = dyn_cast<MDNode>(DVI->getVariable());
  224. if (!N)
  225. return;
  226. InitializeTypeMap(M);
  227. auto *DV = dyn_cast<DILocalVariable>(N);
  228. if (!DV)
  229. return;
  230. if (!NodesSeen.insert(DV).second)
  231. return;
  232. processScope(DV->getScope());
  233. processType(DV->getType().resolve(TypeIdentifierMap));
  234. }
  235. bool DebugInfoFinder::addType(DIType *DT) {
  236. if (!DT)
  237. return false;
  238. if (!NodesSeen.insert(DT).second)
  239. return false;
  240. TYs.push_back(const_cast<DIType *>(DT));
  241. return true;
  242. }
  243. bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
  244. if (!CU)
  245. return false;
  246. if (!NodesSeen.insert(CU).second)
  247. return false;
  248. CUs.push_back(CU);
  249. return true;
  250. }
  251. bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
  252. if (!DIG)
  253. return false;
  254. if (!NodesSeen.insert(DIG).second)
  255. return false;
  256. GVs.push_back(DIG);
  257. return true;
  258. }
  259. // HLSL Change Begins.
  260. bool DebugInfoFinder::appendGlobalVariable(DIGlobalVariable *DIG) {
  261. return addGlobalVariable(DIG);
  262. }
  263. // HLSL Change Ends.
  264. bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
  265. if (!SP)
  266. return false;
  267. if (!NodesSeen.insert(SP).second)
  268. return false;
  269. SPs.push_back(SP);
  270. return true;
  271. }
  272. bool DebugInfoFinder::addScope(DIScope *Scope) {
  273. if (!Scope)
  274. return false;
  275. // FIXME: Ocaml binding generates a scope with no content, we treat it
  276. // as null for now.
  277. if (Scope->getNumOperands() == 0)
  278. return false;
  279. if (!NodesSeen.insert(Scope).second)
  280. return false;
  281. Scopes.push_back(Scope);
  282. return true;
  283. }
  284. bool llvm::stripDebugInfo(Function &F) {
  285. bool Changed = false;
  286. for (BasicBlock &BB : F) {
  287. for (Instruction &I : BB) {
  288. if (I.getDebugLoc()) {
  289. Changed = true;
  290. I.setDebugLoc(DebugLoc());
  291. }
  292. }
  293. }
  294. return Changed;
  295. }
  296. bool llvm::StripDebugInfo(Module &M) {
  297. bool Changed = false;
  298. // Remove all of the calls to the debugger intrinsics, and remove them from
  299. // the module.
  300. if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
  301. while (!Declare->use_empty()) {
  302. CallInst *CI = cast<CallInst>(Declare->user_back());
  303. CI->eraseFromParent();
  304. }
  305. Declare->eraseFromParent();
  306. Changed = true;
  307. }
  308. if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
  309. while (!DbgVal->use_empty()) {
  310. CallInst *CI = cast<CallInst>(DbgVal->user_back());
  311. CI->eraseFromParent();
  312. }
  313. DbgVal->eraseFromParent();
  314. Changed = true;
  315. }
  316. for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
  317. NME = M.named_metadata_end(); NMI != NME;) {
  318. NamedMDNode *NMD = NMI;
  319. ++NMI;
  320. if (NMD->getName().startswith("llvm.dbg.")) {
  321. NMD->eraseFromParent();
  322. Changed = true;
  323. }
  324. }
  325. for (Function &F : M)
  326. Changed |= stripDebugInfo(F);
  327. if (GVMaterializer *Materializer = M.getMaterializer())
  328. Materializer->setStripDebugInfo();
  329. return Changed;
  330. }
  331. unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
  332. if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
  333. M.getModuleFlag("Debug Info Version")))
  334. return Val->getZExtValue();
  335. return 0;
  336. }
  337. // HLSL Change - begin
  338. bool llvm::hasDebugInfo(const Module &M) {
  339. // We might just get away with checking if there's "llvm.dbg.cu",
  340. // but this is more robust.
  341. for (Module::const_named_metadata_iterator NMI = M.named_metadata_begin(),
  342. NME = M.named_metadata_end();
  343. NMI != NME; ++NMI) {
  344. if (NMI->getName().startswith("llvm.dbg.")) {
  345. return true;
  346. }
  347. }
  348. return false;
  349. }
  350. // HLSL Change - end
  351. DenseMap<const llvm::Function *, DISubprogram *>
  352. llvm::makeSubprogramMap(const Module &M) {
  353. DenseMap<const Function *, DISubprogram *> R;
  354. NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
  355. if (!CU_Nodes)
  356. return R;
  357. for (MDNode *N : CU_Nodes->operands()) {
  358. auto *CUNode = cast<DICompileUnit>(N);
  359. for (auto *SP : CUNode->getSubprograms()) {
  360. if (Function *F = SP->getFunction())
  361. R.insert(std::make_pair(F, SP));
  362. }
  363. }
  364. return R;
  365. }