WholeProgramDevirt: Create bitcast to i8* at each virtual call site.

We can't reuse the llvm.assume instruction's bitcast because it may not
dominate every user of the vtable pointer.

Differential Revision: https://reviews.llvm.org/D36994

llvm-svn: 311491
This commit is contained in:
Peter Collingbourne 2017-08-22 21:41:19 +00:00
parent b1fa8255db
commit 001052a067
6 changed files with 51 additions and 5 deletions

View File

@ -876,8 +876,9 @@ void DevirtModule::applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
Constant *UniqueMemberAddr) {
for (auto &&Call : CSInfo.CallSites) {
IRBuilder<> B(Call.CS.getInstruction());
Value *Cmp = B.CreateICmp(IsOne ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE,
Call.VTable, UniqueMemberAddr);
Value *Cmp =
B.CreateICmp(IsOne ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE,
B.CreateBitCast(Call.VTable, Int8PtrTy), UniqueMemberAddr);
Cmp = B.CreateZExt(Cmp, Call.CS->getType());
Call.replaceAndErase("unique-ret-val", FnName, RemarksEnabled, OREGetter,
Cmp);
@ -943,7 +944,8 @@ void DevirtModule::applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName,
for (auto Call : CSInfo.CallSites) {
auto *RetType = cast<IntegerType>(Call.CS.getType());
IRBuilder<> B(Call.CS.getInstruction());
Value *Addr = B.CreateGEP(Int8Ty, Call.VTable, Byte);
Value *Addr =
B.CreateGEP(Int8Ty, B.CreateBitCast(Call.VTable, Int8PtrTy), Byte);
if (RetType->getBitWidth() == 1) {
Value *Bits = B.CreateLoad(Addr);
Value *BitsAndBit = B.CreateAnd(Bits, Bit);
@ -1147,8 +1149,7 @@ void DevirtModule::scanTypeTestUsers(Function *TypeTestFunc,
Value *Ptr = CI->getArgOperand(0)->stripPointerCasts();
if (SeenPtrs.insert(Ptr).second) {
for (DevirtCallSite Call : DevirtCalls) {
CallSlots[{TypeId, Call.Offset}].addCallSite(CI->getArgOperand(0),
Call.CS, nullptr);
CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CS, nullptr);
}
}
}

View File

@ -0,0 +1,37 @@
; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-vcp.yaml < %s | FileCheck %s
target datalayout = "e-p:64:64"
target triple = "x86_64-unknown-linux-gnu"
define i32 @call1(i1 %a, i8* %obj) {
%vtableptr = bitcast i8* %obj to [3 x i8*]**
%vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr
br i1 %a, label %bb1, label %bb2
bb1:
; CHECK: {{.*}} = bitcast {{.*}} to i8*
%vtablei8_1 = bitcast [3 x i8*]* %vtable to i8*
%p1 = call i1 @llvm.type.test(i8* %vtablei8_1, metadata !"typeid1")
call void @llvm.assume(i1 %p1)
%fptrptr1 = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0
%fptr1 = load i8*, i8** %fptrptr1
%fptr1_casted = bitcast i8* %fptr1 to i32 (i8*, i32)*
; CHECK: {{.*}} = bitcast {{.*}} to i8*
%result1 = call i32 %fptr1_casted(i8* %obj, i32 1)
br label %bb2
; CHECK: :
bb2:
%vtablei8_2 = bitcast [3 x i8*]* %vtable to i8*
%p = call i1 @llvm.type.test(i8* %vtablei8_2, metadata !"typeid1")
call void @llvm.assume(i1 %p)
%fptrptr2 = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0
%fptr2 = load i8*, i8** %fptrptr2
%fptr2_casted = bitcast i8* %fptr2 to i32 (i8*, i32)*
; CHECK: {{.*}} = bitcast {{.*}} to i8*
%result2 = call i32 %fptr2_casted(i8* %obj, i32 1)
ret i32 %result2
}
declare void @llvm.assume(i1)
declare i1 @llvm.type.test(i8*, metadata)

View File

@ -29,6 +29,7 @@ define i32 @call1(i8* %obj) {
; SINGLE-IMPL: call i32 bitcast (void ()* @singleimpl1 to i32 (i8*, i32)*)
%result = call i32 %fptr_casted(i8* %obj, i32 1)
; UNIFORM-RET-VAL: ret i32 42
; VCP: {{.*}} = bitcast {{.*}} to i8*
; VCP: [[VT1:%.*]] = bitcast {{.*}} to i8*
; VCP: [[GEP1:%.*]] = getelementptr i8, i8* [[VT1]], i32 ptrtoint (i8* @__typeid_typeid1_0_1_byte to i32)
; VCP: [[BC1:%.*]] = bitcast i8* [[GEP1]] to i32*

View File

@ -20,6 +20,7 @@ define i1 @vf1(i8* %this) readnone {
define i1 @call1(i8* %obj) {
%vtableptr = bitcast i8* %obj to [1 x i8*]**
%vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr
; CHECK: {{.*}} = bitcast [1 x i8*]* {{.*}} to i8*
; CHECK: [[VT1:%[^ ]*]] = bitcast [1 x i8*]* {{.*}} to i8*
%vtablei8 = bitcast [1 x i8*]* %vtable to i8*
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1")

View File

@ -71,6 +71,7 @@ define i32 @vf4i32(i8* %this) readnone {
define i1 @call1(i8* %obj) {
%vtableptr = bitcast i8* %obj to [3 x i8*]**
%vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr
; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8*
; CHECK: [[VT1:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*
%vtablei8 = bitcast [3 x i8*]* %vtable to i8*
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")
@ -91,6 +92,7 @@ define i1 @call1(i8* %obj) {
define i1 @call2(i8* %obj) {
%vtableptr = bitcast i8* %obj to [3 x i8*]**
%vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr
; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8*
; CHECK: [[VT2:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*
%vtablei8 = bitcast [3 x i8*]* %vtable to i8*
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")
@ -111,6 +113,7 @@ define i1 @call2(i8* %obj) {
define i32 @call3(i8* %obj) {
%vtableptr = bitcast i8* %obj to [3 x i8*]**
%vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr
; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8*
; CHECK: [[VT3:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*
%vtablei8 = bitcast [3 x i8*]* %vtable to i8*
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")

View File

@ -66,6 +66,7 @@ define i32 @vf4i32(i8* %this) readnone {
define i1 @call1(i8* %obj) {
%vtableptr = bitcast i8* %obj to [3 x i8*]**
%vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr
; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8*
; CHECK: [[VT1:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*
%vtablei8 = bitcast [3 x i8*]* %vtable to i8*
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")
@ -86,6 +87,7 @@ define i1 @call1(i8* %obj) {
define i1 @call2(i8* %obj) {
%vtableptr = bitcast i8* %obj to [3 x i8*]**
%vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr
; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8*
; CHECK: [[VT2:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*
%vtablei8 = bitcast [3 x i8*]* %vtable to i8*
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")
@ -106,6 +108,7 @@ define i1 @call2(i8* %obj) {
define i32 @call3(i8* %obj) {
%vtableptr = bitcast i8* %obj to [3 x i8*]**
%vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr
; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8*
; CHECK: [[VT3:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8*
%vtablei8 = bitcast [3 x i8*]* %vtable to i8*
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")