forked from OSchip/llvm-project
Fix another pointer-to-member function miscompile, this time when trying to call a virtual member function.
llvm-svn: 95307
This commit is contained in:
parent
674cf89301
commit
c4325486b9
|
@ -159,8 +159,7 @@ CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
|
|||
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
|
||||
FPT->isVariadic());
|
||||
|
||||
const llvm::Type *Int8PtrTy =
|
||||
llvm::Type::getInt8Ty(VMContext)->getPointerTo();
|
||||
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
|
||||
|
||||
// Get the member function pointer.
|
||||
llvm::Value *MemFnPtr =
|
||||
|
@ -206,19 +205,20 @@ CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
|
|||
Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
|
||||
EmitBlock(FnVirtual);
|
||||
|
||||
const llvm::Type *VTableTy =
|
||||
FTy->getPointerTo()->getPointerTo()->getPointerTo();
|
||||
const llvm::Type *VtableTy =
|
||||
FTy->getPointerTo()->getPointerTo();
|
||||
|
||||
llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
|
||||
VTable = Builder.CreateLoad(VTable);
|
||||
llvm::Value *Vtable = Builder.CreateBitCast(This, VtableTy->getPointerTo());
|
||||
Vtable = Builder.CreateLoad(Vtable);
|
||||
|
||||
VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
|
||||
Vtable = Builder.CreateBitCast(Vtable, Int8PtrTy);
|
||||
llvm::Value *VtableOffset =
|
||||
Builder.CreateSub(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1));
|
||||
|
||||
// Since the function pointer is 1 plus the virtual table offset, we
|
||||
// subtract 1 by using a GEP.
|
||||
VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
|
||||
Vtable = Builder.CreateGEP(Vtable, VtableOffset, "fn");
|
||||
Vtable = Builder.CreateBitCast(Vtable, VtableTy);
|
||||
|
||||
llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
|
||||
llvm::Value *VirtualFn = Builder.CreateLoad(Vtable, "virtualfn");
|
||||
|
||||
EmitBranch(FnEnd);
|
||||
EmitBlock(FnNonVirtual);
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -O3 -o - | FileCheck %s
|
||||
struct A {
|
||||
virtual int vf1() { return 1; }
|
||||
virtual int vf2() { return 2; }
|
||||
};
|
||||
|
||||
int f(A* a, int (A::*fp)()) {
|
||||
return (a->*fp)();
|
||||
}
|
||||
|
||||
// CHECK: define i32 @_Z2g1v()
|
||||
int g1() {
|
||||
A a;
|
||||
|
||||
// CHECK: call i32 @_ZN1A3vf1Ev
|
||||
// CHECK-NEXT: ret i32
|
||||
return f(&a, &A::vf1);
|
||||
}
|
||||
|
||||
int g2() {
|
||||
A a;
|
||||
|
||||
// CHECK: call i32 @_ZN1A3vf2Ev
|
||||
// CHECK-NEXT: ret i32
|
||||
return f(&a, &A::vf2);
|
||||
}
|
Loading…
Reference in New Issue