From 2d7cb069fe101da3971a07900ff583380bcac184 Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Sun, 15 Apr 2012 18:04:54 +0000 Subject: Use MDBuilder to help with metadata creation. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154767 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 13 +++---------- lib/CodeGen/CodeGenTBAA.cpp | 36 +++++++----------------------------- lib/CodeGen/CodeGenTBAA.h | 8 ++++---- test/CodeGen/may-alias.c | 2 +- test/CodeGen/tbaa-for-vptr.cpp | 2 +- 5 files changed, 16 insertions(+), 45 deletions(-) diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index e0bf9768ab..ad9daebb84 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -24,6 +24,7 @@ #include "clang/Frontend/CodeGenOptions.h" #include "llvm/Intrinsics.h" #include "llvm/LLVMContext.h" +#include "llvm/Support/MDBuilder.h" #include "llvm/Target/TargetData.h" using namespace clang; using namespace CodeGen; @@ -908,16 +909,8 @@ llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) { } } - if (End == Min) - return NULL; - - llvm::Value *LowAndHigh[2]; - LowAndHigh[0] = llvm::ConstantInt::get(LTy, Min); - LowAndHigh[1] = llvm::ConstantInt::get(LTy, End); - - llvm::LLVMContext &C = getLLVMContext(); - llvm::MDNode *Range = llvm::MDNode::get(C, LowAndHigh); - return Range; + llvm::MDBuilder MDHelper(getLLVMContext()); + return MDHelper.CreateRange(Min, End); } llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, diff --git a/lib/CodeGen/CodeGenTBAA.cpp b/lib/CodeGen/CodeGenTBAA.cpp index 9ee3f1d2e6..34f91e1621 100644 --- a/lib/CodeGen/CodeGenTBAA.cpp +++ b/lib/CodeGen/CodeGenTBAA.cpp @@ -28,7 +28,7 @@ using namespace CodeGen; CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, const LangOptions &Features, MangleContext &MContext) : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext), - Root(0), Char(0) { + MDHelper(VMContext), Root(0), Char(0) { } CodeGenTBAA::~CodeGenTBAA() { @@ -40,7 +40,7 @@ llvm::MDNode *CodeGenTBAA::getRoot() { // (or a different version of this front-end), their TBAA trees will // remain distinct, and the optimizer will treat them conservatively. if (!Root) - Root = getTBAAInfoForNamedType("Simple C/C++ TBAA", 0); + Root = MDHelper.CreateTBAARoot("Simple C/C++ TBAA"); return Root; } @@ -51,33 +51,11 @@ llvm::MDNode *CodeGenTBAA::getChar() { // these special powers only cover user-accessible memory, and doesn't // include things like vtables. if (!Char) - Char = getTBAAInfoForNamedType("omnipotent char", getRoot()); + Char = MDHelper.CreateTBAANode("omnipotent char", getRoot()); return Char; } -/// getTBAAInfoForNamedType - Create a TBAA tree node with the given string -/// as its identifier, and the given Parent node as its tree parent. -llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(StringRef NameStr, - llvm::MDNode *Parent, - bool Readonly) { - // Currently there is only one flag defined - the readonly flag. - llvm::Value *Flags = 0; - if (Readonly) - Flags = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), true); - - // Set up the mdnode operand list. - llvm::Value *Ops[] = { - llvm::MDString::get(VMContext, NameStr), - Parent, - Flags - }; - - // Create the mdnode. - unsigned Len = llvm::array_lengthof(Ops) - !Flags; - return llvm::MDNode::get(VMContext, llvm::makeArrayRef(Ops, Len)); -} - static bool TypeHasMayAlias(QualType QTy) { // Tagged types have declarations, and therefore may have attributes. if (const TagType *TTy = dyn_cast(QTy)) @@ -137,7 +115,7 @@ CodeGenTBAA::getTBAAInfo(QualType QTy) { // "underlying types". default: return MetadataCache[Ty] = - getTBAAInfoForNamedType(BTy->getName(Features), getChar()); + MDHelper.CreateTBAANode(BTy->getName(Features), getChar()); } } @@ -145,7 +123,7 @@ CodeGenTBAA::getTBAAInfo(QualType QTy) { // TODO: Implement C++'s type "similarity" and consider dis-"similar" // pointers distinct. if (Ty->isPointerType()) - return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer", + return MetadataCache[Ty] = MDHelper.CreateTBAANode("any pointer", getChar()); // Enum types are distinct types. In C++ they have "underlying types", @@ -173,7 +151,7 @@ CodeGenTBAA::getTBAAInfo(QualType QTy) { llvm::raw_svector_ostream Out(OutName); MContext.mangleCXXRTTIName(QualType(ETy, 0), Out); Out.flush(); - return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, getChar()); + return MetadataCache[Ty] = MDHelper.CreateTBAANode(OutName, getChar()); } // For now, handle any other kind of type conservatively. @@ -181,5 +159,5 @@ CodeGenTBAA::getTBAAInfo(QualType QTy) { } llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { - return getTBAAInfoForNamedType("vtable pointer", getRoot()); + return MDHelper.CreateTBAANode("vtable pointer", getRoot()); } diff --git a/lib/CodeGen/CodeGenTBAA.h b/lib/CodeGen/CodeGenTBAA.h index 8e08498b7e..4a9785287d 100644 --- a/lib/CodeGen/CodeGenTBAA.h +++ b/lib/CodeGen/CodeGenTBAA.h @@ -17,6 +17,7 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/Support/MDBuilder.h" namespace llvm { class LLVMContext; @@ -41,6 +42,9 @@ class CodeGenTBAA { const LangOptions &Features; MangleContext &MContext; + // MDHelper - Helper for creating metadata. + llvm::MDBuilder MDHelper; + /// MetadataCache - This maps clang::Types to llvm::MDNodes describing them. llvm::DenseMap MetadataCache; @@ -55,10 +59,6 @@ class CodeGenTBAA { /// considered to be equivalent to it. llvm::MDNode *getChar(); - llvm::MDNode *getTBAAInfoForNamedType(StringRef NameStr, - llvm::MDNode *Parent, - bool Readonly = false); - public: CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext, const LangOptions &Features, diff --git a/test/CodeGen/may-alias.c b/test/CodeGen/may-alias.c index 6171be763d..b73ee15f87 100644 --- a/test/CodeGen/may-alias.c +++ b/test/CodeGen/may-alias.c @@ -26,5 +26,5 @@ void test1(struct Test1MA *p1, struct Test1 *p2) { // CHECK: !0 = metadata !{metadata !"any pointer", metadata !1} // CHECK: !1 = metadata !{metadata !"omnipotent char", metadata !2} -// CHECK: !2 = metadata !{metadata !"Simple C/C++ TBAA", null} +// CHECK: !2 = metadata !{metadata !"Simple C/C++ TBAA"} // CHECK: !3 = metadata !{metadata !"int", metadata !1} diff --git a/test/CodeGen/tbaa-for-vptr.cpp b/test/CodeGen/tbaa-for-vptr.cpp index f4885f805d..5ce6bf32ed 100644 --- a/test/CodeGen/tbaa-for-vptr.cpp +++ b/test/CodeGen/tbaa-for-vptr.cpp @@ -16,4 +16,4 @@ void CallFoo(A *a) { // CHECK: %{{.*}} = load {{.*}} !tbaa !0 // CHECK: store {{.*}} !tbaa !0 // CHECK: !0 = metadata !{metadata !"vtable pointer", metadata !1} -// CHECK: !1 = metadata !{metadata !"Simple C/C++ TBAA", null} +// CHECK: !1 = metadata !{metadata !"Simple C/C++ TBAA"} -- cgit v1.2.3