forked from OSchip/llvm-project
WholeProgramDevirt: Teach the pass to handle structs of arrays.
This will become necessary in some cases once D22296 lands. llvm-svn: 289165
This commit is contained in:
parent
14f6b2c035
commit
8786754cc3
|
@ -293,7 +293,7 @@ struct DevirtModule {
|
|||
void buildTypeIdentifierMap(
|
||||
std::vector<VTableBits> &Bits,
|
||||
DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap);
|
||||
Constant *getValueAtOffset(Constant *I, uint64_t Offset);
|
||||
Constant *getPointerAtOffset(Constant *I, uint64_t Offset);
|
||||
bool
|
||||
tryFindVirtualCallTargets(std::vector<VirtualCallTarget> &TargetsForSlot,
|
||||
const std::set<TypeMemberInfo> &TypeMemberInfos,
|
||||
|
@ -383,36 +383,36 @@ void DevirtModule::buildTypeIdentifierMap(
|
|||
}
|
||||
}
|
||||
|
||||
Constant *DevirtModule::getValueAtOffset(Constant *I, uint64_t Offset) {
|
||||
Constant *DevirtModule::getPointerAtOffset(Constant *I, uint64_t Offset) {
|
||||
if (I->getType()->isPointerTy()) {
|
||||
if (Offset == 0)
|
||||
return I;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const DataLayout &DL = M.getDataLayout();
|
||||
unsigned Op;
|
||||
|
||||
if (auto *C = dyn_cast<ConstantStruct>(I)) {
|
||||
const StructLayout *SL = DL.getStructLayout(C->getType());
|
||||
|
||||
if (Offset >= SL->getSizeInBytes())
|
||||
return nullptr;
|
||||
|
||||
Op = SL->getElementContainingOffset(Offset);
|
||||
|
||||
if (Offset != SL->getElementOffset(Op))
|
||||
return nullptr;
|
||||
|
||||
} else if (auto *C = dyn_cast<ConstantArray>(I)) {
|
||||
unsigned Op = SL->getElementContainingOffset(Offset);
|
||||
return getPointerAtOffset(cast<Constant>(I->getOperand(Op)),
|
||||
Offset - SL->getElementOffset(Op));
|
||||
}
|
||||
if (auto *C = dyn_cast<ConstantArray>(I)) {
|
||||
ArrayType *VTableTy = C->getType();
|
||||
uint64_t ElemSize = DL.getTypeAllocSize(VTableTy->getElementType());
|
||||
|
||||
if (Offset % ElemSize != 0)
|
||||
return nullptr;
|
||||
|
||||
Op = Offset / ElemSize;
|
||||
|
||||
unsigned Op = Offset / ElemSize;
|
||||
if (Op >= C->getNumOperands())
|
||||
return nullptr;
|
||||
} else
|
||||
return nullptr;
|
||||
|
||||
return cast<Constant>(I->getOperand(Op));
|
||||
return getPointerAtOffset(cast<Constant>(I->getOperand(Op)),
|
||||
Offset % ElemSize);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool DevirtModule::tryFindVirtualCallTargets(
|
||||
|
@ -422,13 +422,12 @@ bool DevirtModule::tryFindVirtualCallTargets(
|
|||
if (!TM.Bits->GV->isConstant())
|
||||
return false;
|
||||
|
||||
Constant *I = TM.Bits->GV->getInitializer();
|
||||
Value *V = getValueAtOffset(I, TM.Offset + ByteOffset);
|
||||
|
||||
if (!V)
|
||||
Constant *Ptr = getPointerAtOffset(TM.Bits->GV->getInitializer(),
|
||||
TM.Offset + ByteOffset);
|
||||
if (!Ptr)
|
||||
return false;
|
||||
|
||||
auto Fn = dyn_cast<Function>(V->stripPointerCasts());
|
||||
auto Fn = dyn_cast<Function>(Ptr->stripPointerCasts());
|
||||
if (!Fn)
|
||||
return false;
|
||||
|
||||
|
|
|
@ -3,14 +3,15 @@
|
|||
target datalayout = "e-p:64:64"
|
||||
target triple = "x86_64-unknown-linux-gnu"
|
||||
|
||||
@vt = global [2 x i8*] [i8* zeroinitializer, i8* bitcast (void (i8*)* @vf to i8*)], !type !0
|
||||
@vt1 = global [2 x i8*] [i8* zeroinitializer, i8* bitcast (void (i8*)* @vf to i8*)], !type !0
|
||||
@vt2 = global i8* bitcast (void (i8*)* @vf to i8*), !type !1
|
||||
|
||||
define void @vf(i8* %this) {
|
||||
ret void
|
||||
}
|
||||
|
||||
; CHECK: define void @unaligned
|
||||
define void @unaligned(i8* %obj) {
|
||||
; CHECK: define void @unaligned1
|
||||
define void @unaligned1(i8* %obj) {
|
||||
%vtableptr = bitcast i8* %obj to [1 x i8*]**
|
||||
%vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr
|
||||
%vtablei8 = bitcast [1 x i8*]* %vtable to i8*
|
||||
|
@ -25,6 +26,22 @@ define void @unaligned(i8* %obj) {
|
|||
ret void
|
||||
}
|
||||
|
||||
; CHECK: define void @unaligned2
|
||||
define void @unaligned2(i8* %obj) {
|
||||
%vtableptr = bitcast i8* %obj to [1 x i8*]**
|
||||
%vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr
|
||||
%vtablei8 = bitcast [1 x i8*]* %vtable to i8*
|
||||
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid2")
|
||||
call void @llvm.assume(i1 %p)
|
||||
%fptrptr = getelementptr i8, i8* %vtablei8, i32 1
|
||||
%fptrptr_casted = bitcast i8* %fptrptr to i8**
|
||||
%fptr = load i8*, i8** %fptrptr_casted
|
||||
%fptr_casted = bitcast i8* %fptr to void (i8*)*
|
||||
; CHECK: call void %
|
||||
call void %fptr_casted(i8* %obj)
|
||||
ret void
|
||||
}
|
||||
|
||||
; CHECK: define void @outofbounds
|
||||
define void @outofbounds(i8* %obj) {
|
||||
%vtableptr = bitcast i8* %obj to [1 x i8*]**
|
||||
|
@ -61,3 +78,4 @@ declare i1 @llvm.type.test(i8*, metadata)
|
|||
declare void @llvm.assume(i1)
|
||||
|
||||
!0 = !{i32 0, !"typeid"}
|
||||
!1 = !{i32 0, !"typeid2"}
|
||||
|
|
|
@ -19,7 +19,7 @@ define void @call(i8* %obj) {
|
|||
%fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0
|
||||
%fptr = load i8*, i8** %fptrptr
|
||||
%fptr_casted = bitcast i8* %fptr to void (i8*)*
|
||||
; CHECK: call void %
|
||||
; CHECK: call void @vf(
|
||||
call void %fptr_casted(i8* %obj)
|
||||
ret void
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s
|
||||
|
||||
target datalayout = "e-p:64:64"
|
||||
target triple = "x86_64-unknown-linux-gnu"
|
||||
|
||||
%vtTy = type { [2 x void (i8*)*], [2 x void (i8*)*] }
|
||||
|
||||
@vt = constant %vtTy { [2 x void (i8*)*] [void (i8*)* null, void (i8*)* @vf1], [2 x void (i8*)*] [void (i8*)* null, void (i8*)* @vf2] }, !type !0, !type !1
|
||||
|
||||
define void @vf1(i8* %this) {
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @vf2(i8* %this) {
|
||||
ret void
|
||||
}
|
||||
|
||||
; CHECK: define void @call1
|
||||
define void @call1(i8* %obj) {
|
||||
%vtableptr = bitcast i8* %obj to [1 x i8*]**
|
||||
%vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr
|
||||
%vtablei8 = bitcast [1 x i8*]* %vtable to i8*
|
||||
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1")
|
||||
call void @llvm.assume(i1 %p)
|
||||
%fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0
|
||||
%fptr = load i8*, i8** %fptrptr
|
||||
%fptr_casted = bitcast i8* %fptr to void (i8*)*
|
||||
; CHECK: call void @vf1(
|
||||
call void %fptr_casted(i8* %obj)
|
||||
ret void
|
||||
}
|
||||
|
||||
; CHECK: define void @call2
|
||||
define void @call2(i8* %obj) {
|
||||
%vtableptr = bitcast i8* %obj to [1 x i8*]**
|
||||
%vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr
|
||||
%vtablei8 = bitcast [1 x i8*]* %vtable to i8*
|
||||
%p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid2")
|
||||
call void @llvm.assume(i1 %p)
|
||||
%fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0
|
||||
%fptr = load i8*, i8** %fptrptr
|
||||
%fptr_casted = bitcast i8* %fptr to void (i8*)*
|
||||
; CHECK: call void @vf2(
|
||||
call void %fptr_casted(i8* %obj)
|
||||
ret void
|
||||
}
|
||||
|
||||
declare i1 @llvm.type.test(i8*, metadata)
|
||||
declare void @llvm.assume(i1)
|
||||
|
||||
!0 = !{i32 8, !"typeid1"}
|
||||
!1 = !{i32 24, !"typeid2"}
|
Loading…
Reference in New Issue