Use MDBuilder to help with metadata creation.

llvm-svn: 154767
This commit is contained in:
Duncan Sands 2012-04-15 18:04:54 +00:00
parent 62d5f6f247
commit c720e78ebc
5 changed files with 16 additions and 45 deletions

View File

@ -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,

View File

@ -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<TagType>(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());
}

View File

@ -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<const Type *, llvm::MDNode *> 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,

View File

@ -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}

View File

@ -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"}