Index: lib/Transforms/IPO/WholeProgramDevirt.cpp =================================================================== --- lib/Transforms/IPO/WholeProgramDevirt.cpp +++ lib/Transforms/IPO/WholeProgramDevirt.cpp @@ -293,6 +293,7 @@ void buildTypeIdentifierMap( std::vector &Bits, DenseMap> &TypeIdMap); + Optional getOpAtOffset(const Constant *I, const uint64_t Offset); bool tryFindVirtualCallTargets(std::vector &TargetsForSlot, const std::set &TypeMemberInfos, @@ -382,6 +383,41 @@ } } +Optional +DevirtModule::getOpAtOffset(const Constant *I, const uint64_t Offset) { + const DataLayout &DL = M.getDataLayout(); + unsigned Op; + + if (auto *C = dyn_cast(I)) { + const StructLayout *SL = DL.getStructLayout(C->getType()); + + if (Offset >= SL->getSizeInBytes()) + return None; + + Op = SL->getElementContainingOffset(Offset); + + if (Offset != SL->getElementOffset(Op)) + return None; + + return Op; + } else if (auto *C = dyn_cast(I)) { + ArrayType *VTableTy = C->getType(); + const uint64_t ElemSize = DL.getTypeAllocSize(VTableTy->getElementType()); + + if (Offset % ElemSize != 0) + return None; + + Op = Offset / ElemSize; + + if (Op >= C->getNumOperands()) + return None; + + return Op; + } + + return None; +} + bool DevirtModule::tryFindVirtualCallTargets( std::vector &TargetsForSlot, const std::set &TypeMemberInfos, uint64_t ByteOffset) { @@ -389,22 +425,14 @@ if (!TM.Bits->GV->isConstant()) return false; - auto Init = dyn_cast(TM.Bits->GV->getInitializer()); - if (!Init) - return false; - ArrayType *VTableTy = Init->getType(); - - uint64_t ElemSize = - M.getDataLayout().getTypeAllocSize(VTableTy->getElementType()); - uint64_t GlobalSlotOffset = TM.Offset + ByteOffset; - if (GlobalSlotOffset % ElemSize != 0) - return false; + const Constant *I = TM.Bits->GV->getInitializer(); + const uint64_t GlobalSlotOffset = TM.Offset + ByteOffset; + Optional Op = getOpAtOffset(I, GlobalSlotOffset); - unsigned Op = GlobalSlotOffset / ElemSize; - if (Op >= Init->getNumOperands()) + if (!Op.hasValue()) return false; - auto Fn = dyn_cast(Init->getOperand(Op)->stripPointerCasts()); + auto Fn = dyn_cast(I->getOperand(*Op)->stripPointerCasts()); if (!Fn) return false; Index: test/Transforms/WholeProgramDevirt/non-aggregate-vtable.ll =================================================================== --- /dev/null +++ test/Transforms/WholeProgramDevirt/non-aggregate-vtable.ll @@ -0,0 +1,30 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt = constant i8* bitcast (void (i8*)* @vf to i8*), !type !0 + +define void @vf(i8* %this) { + ret void +} + +; CHECK: define void @call +define void @call(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 !"typeid") + 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 % + call void %fptr_casted(i8* %obj) + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} Index: test/Transforms/WholeProgramDevirt/non-array-vtable.ll =================================================================== --- test/Transforms/WholeProgramDevirt/non-array-vtable.ll +++ test/Transforms/WholeProgramDevirt/non-array-vtable.ll @@ -3,7 +3,9 @@ target datalayout = "e-p:64:64" target triple = "x86_64-unknown-linux-gnu" -@vt = constant i8* bitcast (void (i8*)* @vf to i8*), !type !0 +%vtTy = type { void (i8*)* } + +@vt = constant %vtTy { void (i8*)* @vf }, !type !0 define void @vf(i8* %this) { ret void @@ -19,6 +21,21 @@ %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 @vf( + call void %fptr_casted(i8* %obj) + ret void +} + +; CHECK: define void @call_oob +define void @call_oob(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 !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 4 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to void (i8*)* ; CHECK: call void % call void %fptr_casted(i8* %obj) ret void