Index: lib/CodeGen/CGCXXABI.cpp =================================================================== --- lib/CodeGen/CGCXXABI.cpp +++ lib/CodeGen/CGCXXABI.cpp @@ -152,9 +152,12 @@ void CGCXXABI::EmitThisParam(CodeGenFunction &CGF) { /// Initialize the 'this' slot. assert(getThisDecl(CGF) && "no 'this' variable for function"); - CGF.CXXABIThisValue - = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)), - "this"); + ImplicitParamDecl *ThisPtrDecl = getThisDecl(CGF); + auto *Load = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(ThisPtrDecl), + "this"); + QualType ThisPtrType = ThisPtrDecl->getType(); + CGM.DecorateInstructionWithTBAA(Load, CGM.getTBAAAccessInfo(ThisPtrType)); + CGF.CXXABIThisValue = Load; } void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, Index: test/CodeGen/tbaa-this.cpp =================================================================== --- test/CodeGen/tbaa-this.cpp +++ test/CodeGen/tbaa-this.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s \ +// RUN: -emit-llvm -o - | FileCheck %s +// +// Check that we generate correct TBAA information for 'this' pointers. + +struct C { + C *self(); +}; + +C *C::self() { +// CHECK-LABEL: _ZN1C4selfEv +// CHECK: load {{.*}}, !tbaa [[TAG_pointer:!.*]] + return this; +} + +C* foo(C *p) { + return p->self(); +} + +// CHECK-DAG: [[TAG_pointer]] = !{[[TYPE_pointer:!.*]], [[TYPE_pointer]], i64 0} +// CHECK-DAG: [[TYPE_pointer]] = !{!"any pointer", !{{.*}}, i64 0}