[llvm] r297333 - WholeProgramDevirt: Implement importing for single-impl devirtualization.
Peter Collingbourne via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 8 16:21:25 PST 2017
Author: pcc
Date: Wed Mar 8 18:21:25 2017
New Revision: 297333
URL: http://llvm.org/viewvc/llvm-project?rev=297333&view=rev
Log:
WholeProgramDevirt: Implement importing for single-impl devirtualization.
Differential Revision: https://reviews.llvm.org/D29844
Added:
llvm/trunk/test/Transforms/WholeProgramDevirt/Inputs/import-single-impl.yaml
llvm/trunk/test/Transforms/WholeProgramDevirt/import.ll
Modified:
llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp
Modified: llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp?rev=297333&r1=297332&r2=297333&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/WholeProgramDevirt.cpp Wed Mar 8 18:21:25 2017
@@ -445,6 +445,13 @@ struct DevirtModule {
void rebuildGlobal(VTableBits &B);
+ // Apply the summary resolution for Slot to all virtual calls in SlotInfo.
+ void importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo);
+
+ // If we were able to eliminate all unsafe uses for a type checked load,
+ // eliminate the associated type tests by replacing them with true.
+ void removeRedundantTypeTests();
+
bool run();
// Lower the module using the action and summary passed as command line
@@ -1103,6 +1110,34 @@ void DevirtModule::scanTypeCheckedLoadUs
}
}
+void DevirtModule::importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo) {
+ const WholeProgramDevirtResolution &Res =
+ Summary->getTypeIdSummary(cast<MDString>(Slot.TypeID)->getString())
+ .WPDRes[Slot.ByteOffset];
+
+ if (Res.TheKind == WholeProgramDevirtResolution::SingleImpl) {
+ // The type of the function in the declaration is irrelevant because every
+ // call site will cast it to the correct type.
+ auto *SingleImpl = M.getOrInsertFunction(
+ Res.SingleImplName, Type::getVoidTy(M.getContext()), nullptr);
+
+ // This is the import phase so we should not be exporting anything.
+ bool IsExported = false;
+ applySingleImplDevirt(SlotInfo, SingleImpl, IsExported);
+ assert(!IsExported);
+ }
+}
+
+void DevirtModule::removeRedundantTypeTests() {
+ auto True = ConstantInt::getTrue(M.getContext());
+ for (auto &&U : NumUnsafeUsesForTypeTest) {
+ if (U.second == 0) {
+ U.first->replaceAllUsesWith(True);
+ U.first->eraseFromParent();
+ }
+ }
+}
+
bool DevirtModule::run() {
Function *TypeTestFunc =
M.getFunction(Intrinsic::getName(Intrinsic::type_test));
@@ -1125,6 +1160,17 @@ bool DevirtModule::run() {
if (TypeCheckedLoadFunc)
scanTypeCheckedLoadUsers(TypeCheckedLoadFunc);
+ if (Action == PassSummaryAction::Import) {
+ for (auto &S : CallSlots)
+ importResolution(S.first, S.second);
+
+ removeRedundantTypeTests();
+
+ // The rest of the code is only necessary when exporting or during regular
+ // LTO, so we are done.
+ return true;
+ }
+
// Rebuild type metadata into a map for easy lookup.
std::vector<VTableBits> Bits;
DenseMap<Metadata *, std::set<TypeMemberInfo>> TypeIdMap;
@@ -1223,17 +1269,7 @@ bool DevirtModule::run() {
}
}
- // If we were able to eliminate all unsafe uses for a type checked load,
- // eliminate the type test by replacing it with true.
- if (TypeCheckedLoadFunc) {
- auto True = ConstantInt::getTrue(M.getContext());
- for (auto &&U : NumUnsafeUsesForTypeTest) {
- if (U.second == 0) {
- U.first->replaceAllUsesWith(True);
- U.first->eraseFromParent();
- }
- }
- }
+ removeRedundantTypeTests();
// Rebuild each global we touched as part of virtual constant propagation to
// include the before and after bytes.
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/Inputs/import-single-impl.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/Inputs/import-single-impl.yaml?rev=297333&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/WholeProgramDevirt/Inputs/import-single-impl.yaml (added)
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/Inputs/import-single-impl.yaml Wed Mar 8 18:21:25 2017
@@ -0,0 +1,13 @@
+---
+TypeIdMap:
+ typeid1:
+ WPDRes:
+ 0:
+ Kind: SingleImpl
+ SingleImplName: singleimpl1
+ typeid2:
+ WPDRes:
+ 8:
+ Kind: SingleImpl
+ SingleImplName: singleimpl2
+...
Added: llvm/trunk/test/Transforms/WholeProgramDevirt/import.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/WholeProgramDevirt/import.ll?rev=297333&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/WholeProgramDevirt/import.ll (added)
+++ llvm/trunk/test/Transforms/WholeProgramDevirt/import.ll Wed Mar 8 18:21:25 2017
@@ -0,0 +1,49 @@
+; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-single-impl.yaml < %s | FileCheck --check-prefixes=CHECK,SINGLE-IMPL %s
+
+target datalayout = "e-p:64:64"
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK: define i32 @call1
+define i32 @call1(i8* %obj) {
+ %vtableptr = bitcast i8* %obj to [3 x i8*]**
+ %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr
+ %vtablei8 = bitcast [3 x i8*]* %vtable to i8*
+ %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1")
+ call void @llvm.assume(i1 %p)
+ %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0
+ %fptr = load i8*, i8** %fptrptr
+ %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)*
+ ; SINGLE-IMPL: call i32 bitcast (void ()* @singleimpl1 to i32 (i8*, i32)*)
+ %result = call i32 %fptr_casted(i8* %obj, i32 1)
+ ret i32 %result
+}
+
+; CHECK: define i1 @call2
+define i1 @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*
+ %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 8, metadata !"typeid2")
+ %fptr = extractvalue {i8*, i1} %pair, 0
+ %p = extractvalue {i8*, i1} %pair, 1
+ ; SINGLE-IMPL: br i1 true,
+ br i1 %p, label %cont, label %trap
+
+cont:
+ %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)*
+ ; SINGLE-IMPL: call i1 bitcast (void ()* @singleimpl2 to i1 (i8*, i32)*)
+ %result = call i1 %fptr_casted(i8* %obj, i32 undef)
+ ret i1 %result
+
+trap:
+ call void @llvm.trap()
+ unreachable
+}
+
+; SINGLE-IMPL-DAG: declare void @singleimpl1()
+; SINGLE-IMPL-DAG: declare void @singleimpl2()
+
+declare void @llvm.assume(i1)
+declare void @llvm.trap()
+declare {i8*, i1} @llvm.type.checked.load(i8*, i32, metadata)
+declare i1 @llvm.type.test(i8*, metadata)
More information about the llvm-commits
mailing list