From c79e1182470ed12f1f3d0d35c1725366519a9af7 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Fri, 6 Mar 2009 00:19:37 +0000 Subject: [PATCH 1/1] Add "check/remove dbg var" helper routines. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66223 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/Utils/Local.h | 9 ++++++ lib/Transforms/Utils/Local.cpp | 44 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/llvm/Transforms/Utils/Local.h b/include/llvm/Transforms/Utils/Local.h index 36c13f4c981..c3088a2c7c8 100644 --- a/include/llvm/Transforms/Utils/Local.h +++ b/include/llvm/Transforms/Utils/Local.h @@ -17,6 +17,7 @@ namespace llvm { +class User; class BasicBlock; class Instruction; class Value; @@ -100,6 +101,14 @@ AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0); /// with DbgInfoIntrinsic that use the instruction I. bool OnlyUsedByDbgInfoIntrinsics(Instruction *I, SmallVectorImpl *DbgInUses = 0); + +/// UserIsDebugInfo - Return true if U is a constant expr used by +/// llvm.dbg.variable or llvm.dbg.global_variable +bool UserIsDebugInfo(User *U); + +/// RemoveDbgInfoUser - Remove an User which is representing debug info. +void RemoveDbgInfoUser(User *U); + } // End llvm namespace #endif diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index d2cbec0226e..4be1b8717d2 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -14,11 +14,13 @@ #include "llvm/Transforms/Utils/Local.h" #include "llvm/Constants.h" +#include "llvm/GlobalVariable.h" #include "llvm/DerivedTypes.h" #include "llvm/Instructions.h" #include "llvm/Intrinsics.h" #include "llvm/IntrinsicInst.h" #include "llvm/Analysis/ConstantFolding.h" +#include "llvm/Analysis/DebugInfo.h" #include "llvm/Target/TargetData.h" #include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/MathExtras.h" @@ -273,3 +275,45 @@ bool llvm::OnlyUsedByDbgInfoIntrinsics(Instruction *I, } return true; } + +/// UserIsDebugInfo - Return true if U is a constant expr used by +/// llvm.dbg.variable or llvm.dbg.global_variable +bool llvm::UserIsDebugInfo(User *U) { + ConstantExpr *CE = dyn_cast(U); + + if (!CE || CE->getNumUses() != 1) + return false; + + Constant *Init = dyn_cast(CE->use_back()); + if (!Init || Init->getNumUses() != 1) + return false; + + GlobalVariable *GV = dyn_cast(Init->use_back()); + if (!GV || !GV->hasInitializer() || GV->getInitializer() != Init) + return false; + + DIVariable DV(GV); + if (!DV.isNull()) + return true; // User is llvm.dbg.variable + + DIGlobalVariable DGV(GV); + if (!DGV.isNull()) + return true; // User is llvm.dbg.global_variable + + return false; +} + +/// RemoveDbgInfoUser - Remove an User which is representing debug info. +void llvm::RemoveDbgInfoUser(User *U) { + assert (UserIsDebugInfo(U) && "Unexpected User!"); + ConstantExpr *CE = cast(U); + while (!CE->use_empty()) { + Constant *C = cast(CE->use_back()); + while (!C->use_empty()) { + GlobalVariable *GV = cast(C->use_back()); + GV->eraseFromParent(); + } + C->destroyConstant(); + } + CE->destroyConstant(); +} -- 2.34.1