Skip to content

Commit 947c9af

Browse files
author
Dan Gohman
committed
Experimental TBAA support.
This enables metadata generation by default, however the TBAA pass in the optimizer is still disabled for now. llvm-svn: 116536
1 parent 0f58561 commit 947c9af

File tree

7 files changed

+191
-10
lines changed

7 files changed

+191
-10
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "CodeGenFunction.h"
1515
#include "CodeGenModule.h"
16+
#include "CodeGenTBAA.h"
1617
#include "CGCall.h"
1718
#include "CGCXXABI.h"
1819
#include "CGRecordLayout.h"
@@ -580,12 +581,15 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) {
580581
}
581582

582583
llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
583-
unsigned Alignment, QualType Ty) {
584+
unsigned Alignment, QualType Ty,
585+
llvm::MDNode *TBAAInfo) {
584586
llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
585587
if (Volatile)
586588
Load->setVolatile(true);
587589
if (Alignment)
588590
Load->setAlignment(Alignment);
591+
if (TBAAInfo)
592+
CGM.DecorateInstruction(Load, TBAAInfo);
589593

590594
// Bool can have different representation in memory than in registers.
591595
llvm::Value *V = Load;
@@ -604,7 +608,8 @@ static bool isBooleanUnderlyingType(QualType Ty) {
604608

605609
void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
606610
bool Volatile, unsigned Alignment,
607-
QualType Ty) {
611+
QualType Ty,
612+
llvm::MDNode *TBAAInfo) {
608613

609614
if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
610615
// Bool can have different representation in memory than in registers.
@@ -615,6 +620,8 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
615620
llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
616621
if (Alignment)
617622
Store->setAlignment(Alignment);
623+
if (TBAAInfo)
624+
CGM.DecorateInstruction(Store, TBAAInfo);
618625
}
619626

620627
/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
@@ -637,7 +644,8 @@ RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
637644

638645
// Everything needs a load.
639646
return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
640-
LV.getAlignment(), ExprType));
647+
LV.getAlignment(), ExprType,
648+
LV.getTBAAInfo()));
641649

642650
}
643651

@@ -846,7 +854,8 @@ void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
846854

847855
assert(Src.isScalar() && "Can't emit an agg store with this method");
848856
EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
849-
Dst.isVolatileQualified(), Dst.getAlignment(), Ty);
857+
Dst.isVolatileQualified(), Dst.getAlignment(), Ty,
858+
Dst.getTBAAInfo());
850859
}
851860

852861
void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,

clang/lib/CodeGen/CGValue.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,13 @@ class LValue {
157157
bool ThreadLocalRef : 1;
158158

159159
Expr *BaseIvarExp;
160+
161+
/// TBAAInfo - TBAA information to attach to dereferences of this LValue.
162+
llvm::MDNode *TBAAInfo;
163+
160164
private:
161-
void Initialize(Qualifiers Quals, unsigned Alignment = 0) {
165+
void Initialize(Qualifiers Quals, unsigned Alignment = 0,
166+
llvm::MDNode *TBAAInfo = 0) {
162167
this->Quals = Quals;
163168
this->Alignment = Alignment;
164169
assert(this->Alignment == Alignment && "Alignment exceeds allowed max!");
@@ -167,6 +172,7 @@ class LValue {
167172
this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
168173
this->ThreadLocalRef = false;
169174
this->BaseIvarExp = 0;
175+
this->TBAAInfo = TBAAInfo;
170176
}
171177

172178
public:
@@ -208,6 +214,9 @@ class LValue {
208214
Expr *getBaseIvarExp() const { return BaseIvarExp; }
209215
void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
210216

217+
llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
218+
void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
219+
211220
const Qualifiers &getQuals() const { return Quals; }
212221
Qualifiers &getQuals() { return Quals; }
213222

@@ -252,14 +261,15 @@ class LValue {
252261
}
253262

254263
static LValue MakeAddr(llvm::Value *V, QualType T, unsigned Alignment,
255-
ASTContext &Context) {
264+
ASTContext &Context,
265+
llvm::MDNode *TBAAInfo = 0) {
256266
Qualifiers Quals = Context.getCanonicalType(T).getQualifiers();
257267
Quals.setObjCGCAttr(Context.getObjCGCAttrKind(T));
258268

259269
LValue R;
260270
R.LVType = Simple;
261271
R.V = V;
262-
R.Initialize(Quals, Alignment);
272+
R.Initialize(Quals, Alignment, TBAAInfo);
263273
return R;
264274
}
265275

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,8 @@ class CodeGenFunction : public BlockFunction {
10111011
//===--------------------------------------------------------------------===//
10121012

10131013
LValue MakeAddrLValue(llvm::Value *V, QualType T, unsigned Alignment = 0) {
1014-
return LValue::MakeAddr(V, T, Alignment, getContext());
1014+
return LValue::MakeAddr(V, T, Alignment, getContext(),
1015+
CGM.getTBAAInfo(T));
10151016
}
10161017

10171018
/// CreateTempAlloca - This creates a alloca and inserts it into the entry
@@ -1349,13 +1350,15 @@ class CodeGenFunction : public BlockFunction {
13491350
/// care to appropriately convert from the memory representation to
13501351
/// the LLVM value representation.
13511352
llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
1352-
unsigned Alignment, QualType Ty);
1353+
unsigned Alignment, QualType Ty,
1354+
llvm::MDNode *TBAAInfo = 0);
13531355

13541356
/// EmitStoreOfScalar - Store a scalar value to an address, taking
13551357
/// care to appropriately convert from the memory representation to
13561358
/// the LLVM value representation.
13571359
void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
1358-
bool Volatile, unsigned Alignment, QualType Ty);
1360+
bool Volatile, unsigned Alignment, QualType Ty,
1361+
llvm::MDNode *TBAAInfo = 0);
13591362

13601363
/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
13611364
/// this method emits the address of the lvalue, then loads the result as an

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CodeGenModule.h"
1515
#include "CGDebugInfo.h"
1616
#include "CodeGenFunction.h"
17+
#include "CodeGenTBAA.h"
1718
#include "CGCall.h"
1819
#include "CGCXXABI.h"
1920
#include "CGObjCRuntime.h"
@@ -62,6 +63,7 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
6263
TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags),
6364
ABI(createCXXABI(*this)),
6465
Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI),
66+
TBAA(0),
6567
VTables(*this), Runtime(0),
6668
CFConstantStringClassRef(0), NSConstantStringClassRef(0),
6769
VMContext(M.getContext()),
@@ -79,6 +81,10 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
7981
else
8082
Runtime = CreateMacObjCRuntime(*this);
8183

84+
// Enable TBAA unless it's suppressed.
85+
if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)
86+
TBAA = new CodeGenTBAA(Context, VMContext, getLangOptions());
87+
8288
// If debug info generation is enabled, create the CGDebugInfo object.
8389
DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0;
8490
}
@@ -116,6 +122,17 @@ void CodeGenModule::Release() {
116122
EmitDeclMetadata();
117123
}
118124

125+
llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
126+
if (!TBAA)
127+
return 0;
128+
return TBAA->getTBAAInfo(QTy);
129+
}
130+
131+
void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
132+
llvm::MDNode *TBAAInfo) {
133+
Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
134+
}
135+
119136
bool CodeGenModule::isTargetDarwin() const {
120137
return getContext().Target.getTriple().getOS() == llvm::Triple::Darwin;
121138
}

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ namespace clang {
7070
namespace CodeGen {
7171

7272
class CodeGenFunction;
73+
class CodeGenTBAA;
7374
class CGCXXABI;
7475
class CGDebugInfo;
7576
class CGObjCRuntime;
@@ -111,6 +112,7 @@ class CodeGenModule : public BlockModule {
111112
Diagnostic &Diags;
112113
CGCXXABI &ABI;
113114
CodeGenTypes Types;
115+
CodeGenTBAA *TBAA;
114116

115117
/// VTables - Holds information about C++ vtables.
116118
CodeGenVTables VTables;
@@ -250,6 +252,11 @@ class CodeGenModule : public BlockModule {
250252
const TargetCodeGenInfo &getTargetCodeGenInfo();
251253
bool isTargetDarwin() const;
252254

255+
llvm::MDNode *getTBAAInfo(QualType QTy);
256+
257+
static void DecorateInstruction(llvm::Instruction *Inst,
258+
llvm::MDNode *TBAAInfo);
259+
253260
/// getDeclVisibilityMode - Compute the visibility of the decl \arg D.
254261
LangOptions::VisibilityMode getDeclVisibilityMode(const Decl *D) const;
255262

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===--- CodeGenTypes.cpp - TBAA information for LLVM CodeGen -------------===//
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 is the code that manages TBAA information.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "CodeGenTBAA.h"
15+
#include "clang/AST/ASTContext.h"
16+
#include "llvm/LLVMContext.h"
17+
#include "llvm/Metadata.h"
18+
using namespace clang;
19+
using namespace CodeGen;
20+
21+
CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
22+
const LangOptions &Features)
23+
: Context(Ctx), VMContext(VMContext), Features(Features), Root(0), Char(0) {
24+
}
25+
26+
CodeGenTBAA::~CodeGenTBAA() {
27+
}
28+
29+
llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(const char *NameStr,
30+
llvm::MDNode *Parent) {
31+
llvm::Value *Ops[] = {
32+
llvm::MDString::get(VMContext, NameStr),
33+
Parent
34+
};
35+
36+
return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops));
37+
}
38+
39+
llvm::MDNode *
40+
CodeGenTBAA::getTBAAInfo(QualType QTy) {
41+
Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
42+
43+
if (llvm::MDNode *N = MetadataCache[Ty])
44+
return N;
45+
46+
if (!Root) {
47+
Root = getTBAAInfoForNamedType("Experimental TBAA", 0);
48+
Char = getTBAAInfoForNamedType("omnipotent char", Root);
49+
}
50+
51+
// For now, just emit a very minimal tree.
52+
const Type *CanonicalTy = Context.getCanonicalType(Ty);
53+
if (const BuiltinType *BTy = dyn_cast<BuiltinType>(CanonicalTy)) {
54+
switch (BTy->getKind()) {
55+
case BuiltinType::Char_U:
56+
case BuiltinType::Char_S:
57+
case BuiltinType::UChar:
58+
case BuiltinType::SChar:
59+
// Charactar types are special.
60+
return Char;
61+
default:
62+
return MetadataCache[Ty] =
63+
getTBAAInfoForNamedType(BTy->getName(Features), Char);
64+
}
65+
}
66+
67+
return MetadataCache[Ty] = getTBAAInfoForNamedType("TBAA.other", Char);
68+
}

clang/lib/CodeGen/CodeGenTBAA.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
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 is the code that manages TBAA information.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef CLANG_CODEGEN_CODEGENTBAA_H
15+
#define CLANG_CODEGEN_CODEGENTBAA_H
16+
17+
#include "llvm/LLVMContext.h"
18+
#include "llvm/ADT/DenseMap.h"
19+
20+
namespace llvm {
21+
class LLVMContext;
22+
class MDNode;
23+
}
24+
25+
namespace clang {
26+
class ASTContext;
27+
class LangOptions;
28+
class QualType;
29+
class Type;
30+
31+
namespace CodeGen {
32+
class CGCXXABI;
33+
class CGRecordLayout;
34+
35+
/// CodeGenTBAA - This class organizes the cross-module state that is used
36+
/// while lowering AST types to LLVM types.
37+
class CodeGenTBAA {
38+
ASTContext &Context;
39+
llvm::LLVMContext& VMContext;
40+
const LangOptions &Features;
41+
42+
/// MetadataCache - This maps clang::Types to llvm::MDNodes describing them.
43+
llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
44+
45+
/// Root - This is the mdnode for the root of the metadata type graph
46+
/// for this translation unit.
47+
llvm::MDNode *Root;
48+
49+
/// Char - This is the mdnode for "char", which is special, and any types
50+
/// considered to be equivalent to it.
51+
llvm::MDNode *Char;
52+
53+
llvm::MDNode *getTBAAInfoForNamedType(const char *NameStr,
54+
llvm::MDNode *Parent);
55+
56+
public:
57+
CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
58+
const LangOptions &Features);
59+
~CodeGenTBAA();
60+
61+
llvm::MDNode *getTBAAInfo(QualType QTy);
62+
};
63+
64+
} // end namespace CodeGen
65+
} // end namespace clang
66+
67+
#endif

0 commit comments

Comments
 (0)