From e846dd89c173d462d197046b76d37588896623f3 Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Tue, 11 Mar 2008 22:28:56 +0000 Subject: [PATCH] Add helper for ultimate aliasee resoltion git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48255 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/GlobalAlias.h | 7 ++++++- lib/VMCore/Globals.cpp | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/llvm/GlobalAlias.h b/include/llvm/GlobalAlias.h index 572de670371..1dee0414ebc 100644 --- a/include/llvm/GlobalAlias.h +++ b/include/llvm/GlobalAlias.h @@ -78,7 +78,12 @@ public: /// getAliasedGlobal() - Aliasee can be either global or bitcast of /// global. This method retrives the global for both aliasee flavours. const GlobalValue* getAliasedGlobal() const; - + + /// resolveAliasedGlobal() - This method tries to ultimately resolve alias by + /// going through aliasing chain and trying to find the very last + /// global. Return NULL is cycle was found. + const GlobalValue* resolveAliasedGlobal() const; + // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const GlobalAlias *) { return true; } static inline bool classof(const Value *V) { diff --git a/lib/VMCore/Globals.cpp b/lib/VMCore/Globals.cpp index dae22e48aea..d66d351bc23 100644 --- a/lib/VMCore/Globals.cpp +++ b/lib/VMCore/Globals.cpp @@ -17,6 +17,7 @@ #include "llvm/GlobalAlias.h" #include "llvm/DerivedTypes.h" #include "llvm/Module.h" +#include "llvm/ADT/SmallPtrSet.h" #include "llvm/Support/LeakDetector.h" using namespace llvm; @@ -230,3 +231,18 @@ const GlobalValue *GlobalAlias::getAliasedGlobal() const { return 0; } +const GlobalValue *GlobalAlias::resolveAliasedGlobal() const { + SmallPtrSet Visited; + + const GlobalValue *GV = getAliasedGlobal(); + Visited.insert(GV); + + while (const GlobalAlias *GA = dyn_cast(GV)) { + GV = GA->getAliasedGlobal(); + + if (!Visited.insert(GV)) + return NULL; + } + + return GV; +}