From bb516bcc22131b6d8539a3e89f9c221f25983f99 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 22 Aug 2017 21:41:19 +0000 Subject: [PATCH] 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311491 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/IPO/WholeProgramDevirt.cpp | 11 +++--- .../import-no-dominating-assume.ll | 37 +++++++++++++++++++ test/Transforms/WholeProgramDevirt/import.ll | 1 + .../WholeProgramDevirt/unique-retval.ll | 1 + .../virtual-const-prop-begin.ll | 3 ++ .../virtual-const-prop-end.ll | 3 ++ 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 test/Transforms/WholeProgramDevirt/import-no-dominating-assume.ll diff --git a/lib/Transforms/IPO/WholeProgramDevirt.cpp b/lib/Transforms/IPO/WholeProgramDevirt.cpp index 3731ac91265..c4e6e3957d0 100644 --- a/lib/Transforms/IPO/WholeProgramDevirt.cpp +++ b/lib/Transforms/IPO/WholeProgramDevirt.cpp @@ -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(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); } } } diff --git a/test/Transforms/WholeProgramDevirt/import-no-dominating-assume.ll b/test/Transforms/WholeProgramDevirt/import-no-dominating-assume.ll new file mode 100644 index 00000000000..b652ea4afe7 --- /dev/null +++ b/test/Transforms/WholeProgramDevirt/import-no-dominating-assume.ll @@ -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) diff --git a/test/Transforms/WholeProgramDevirt/import.ll b/test/Transforms/WholeProgramDevirt/import.ll index 7f34b04ce11..18f048fd06c 100644 --- a/test/Transforms/WholeProgramDevirt/import.ll +++ b/test/Transforms/WholeProgramDevirt/import.ll @@ -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* diff --git a/test/Transforms/WholeProgramDevirt/unique-retval.ll b/test/Transforms/WholeProgramDevirt/unique-retval.ll index e9ae176fe8a..4452bb75d03 100644 --- a/test/Transforms/WholeProgramDevirt/unique-retval.ll +++ b/test/Transforms/WholeProgramDevirt/unique-retval.ll @@ -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") diff --git a/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll b/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll index 080ed6caac5..63549e8540c 100644 --- a/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll +++ b/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll @@ -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") diff --git a/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll b/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll index 14360c78d95..3d61e1e2082 100644 --- a/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll +++ b/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll @@ -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") -- 2.50.1