[llvm] d35fe04 - [LTT] Handle merged llvm.assume when dropping type tests
Teresa Johnson via llvm-commits
llvm-commits at lists.llvm.org
Tue May 25 17:02:30 PDT 2021
Author: Teresa Johnson
Date: 2021-05-25T17:02:13-07:00
New Revision: d35fe04fa35a080cb024ecb56db67dc822c70740
URL: https://github.com/llvm/llvm-project/commit/d35fe04fa35a080cb024ecb56db67dc822c70740
DIFF: https://github.com/llvm/llvm-project/commit/d35fe04fa35a080cb024ecb56db67dc822c70740.diff
LOG: [LTT] Handle merged llvm.assume when dropping type tests
When the lower type test pass is invoked a second time with
DropTypeTests set to true, it expects that all remaining type tests feed
assume instructions, which are removed along with the type tests.
In some cases the llvm.assume might have been merged with another one,
i.e. from a builtin_assume instruction, in which case the type test
would actually feed a phi that in turn feeds the merged assume
instruction. In this case we can simply replace that operand of the phi
with "true" before removing the type test.
Differential Revision: https://reviews.llvm.org/D103073
Added:
llvm/test/Transforms/LowerTypeTests/drop_type_test_phi.ll
Modified:
llvm/lib/Transforms/IPO/LowerTypeTests.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
index 8e72643d9a11..b492b200c6d5 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -117,6 +117,11 @@ static cl::opt<std::string> ClWriteSummary(
cl::desc("Write summary to given YAML file after running pass"),
cl::Hidden);
+static cl::opt<bool>
+ ClDropTypeTests("lowertypetests-drop-type-tests",
+ cl::desc("Simply drop type test assume sequences"),
+ cl::Hidden, cl::init(false));
+
bool BitSetInfo::containsGlobalOffset(uint64_t Offset) const {
if (Offset < ByteOffset)
return false;
@@ -528,7 +533,8 @@ struct LowerTypeTests : public ModulePass {
LowerTypeTests(ModuleSummaryIndex *ExportSummary,
const ModuleSummaryIndex *ImportSummary, bool DropTypeTests)
: ModulePass(ID), ExportSummary(ExportSummary),
- ImportSummary(ImportSummary), DropTypeTests(DropTypeTests) {
+ ImportSummary(ImportSummary),
+ DropTypeTests(DropTypeTests || ClDropTypeTests) {
initializeLowerTypeTestsPass(*PassRegistry::getPassRegistry());
}
@@ -1687,7 +1693,7 @@ LowerTypeTestsModule::LowerTypeTestsModule(
Module &M, ModuleSummaryIndex *ExportSummary,
const ModuleSummaryIndex *ImportSummary, bool DropTypeTests)
: M(M), ExportSummary(ExportSummary), ImportSummary(ImportSummary),
- DropTypeTests(DropTypeTests) {
+ DropTypeTests(DropTypeTests || ClDropTypeTests) {
assert(!(ExportSummary && ImportSummary));
Triple TargetTriple(M.getTargetTriple());
Arch = TargetTriple.getArch();
@@ -1793,6 +1799,14 @@ bool LowerTypeTestsModule::lower() {
for (auto CIU = CI->use_begin(), CIUE = CI->use_end(); CIU != CIUE;)
if (auto *Assume = dyn_cast<AssumeInst>((*CIU++).getUser()))
Assume->eraseFromParent();
+ // If the assume was merged with another assume, we might have a use on a
+ // phi (which will feed the assume). Simply replace the use on the phi
+ // with "true" and leave the merged assume.
+ if (!CI->use_empty()) {
+ assert(all_of(CI->users(),
+ [](User *U) -> bool { return isa<PHINode>(U); }));
+ CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext()));
+ }
CI->eraseFromParent();
}
diff --git a/llvm/test/Transforms/LowerTypeTests/drop_type_test_phi.ll b/llvm/test/Transforms/LowerTypeTests/drop_type_test_phi.ll
new file mode 100644
index 000000000000..9a263157faf5
--- /dev/null
+++ b/llvm/test/Transforms/LowerTypeTests/drop_type_test_phi.ll
@@ -0,0 +1,67 @@
+; Test to ensure dropping of type tests can handle a phi feeding the assume.
+; RUN: opt -S -lowertypetests -lowertypetests-drop-type-tests -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-grtev4-linux-gnu"
+
+%struct.A = type { i32 (...)** }
+%struct.B = type { %struct.A }
+%struct.C = type { %struct.A }
+
+ at _ZTV1B = constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.B*, i32)* @_ZN1B1fEi to i8*), i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1nEi to i8*)] }, !type !0, !type !1
+ at _ZTV1C = constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.C*, i32)* @_ZN1C1fEi to i8*), i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1nEi to i8*)] }, !type !0, !type !2
+
+; CHECK-LABEL: define i32 @test
+define i32 @test(%struct.A* %obj, i32 %a, i32 %b) {
+entry:
+ %tobool.not = icmp eq i32 %a, 0
+ br i1 %tobool.not, label %if.else, label %if.then
+
+if.then:
+ %0 = bitcast %struct.A* %obj to i8***
+ %vtable = load i8**, i8*** %0
+ %1 = bitcast i8** %vtable to i8*
+ %p = call i1 @llvm.type.test(i8* %1, metadata !"_ZTS1A")
+; CHECK-NOT: @llvm.type.test
+ %fptrptr = getelementptr i8*, i8** %vtable, i32 1
+ %2 = bitcast i8** %fptrptr to i32 (%struct.A*, i32)**
+ %fptr1 = load i32 (%struct.A*, i32)*, i32 (%struct.A*, i32)** %2, align 8
+ %call = tail call i32 %fptr1(%struct.A* nonnull %obj, i32 %a)
+ br label %if.end
+
+if.else:
+ %3 = icmp ne i32 %b, 0
+ br label %if.end
+
+if.end:
+ %4 = phi i1 [ %3, %if.else ], [ %p, %if.then ]
+ call void @llvm.assume(i1 %4)
+; Still have the assume, but the type test target replaced with true.
+; CHECK: %4 = phi i1 [ %3, %if.else ], [ true, %if.then ]
+; CHECK: call void @llvm.assume(i1 %4)
+
+ ret i32 0
+}
+; CHECK-LABEL: ret i32
+; CHECK-LABEL: }
+
+declare i1 @llvm.type.test(i8*, metadata)
+declare void @llvm.assume(i1)
+
+define i32 @_ZN1B1fEi(%struct.B* %this, i32 %a) #0 {
+ ret i32 0;
+}
+
+define i32 @_ZN1A1nEi(%struct.A* %this, i32 %a) #0 {
+ ret i32 0;
+}
+
+define i32 @_ZN1C1fEi(%struct.C* %this, i32 %a) #0 {
+ ret i32 0;
+}
+
+attributes #0 = { noinline optnone }
+
+!0 = !{i64 16, !"_ZTS1A"}
+!1 = !{i64 16, !"_ZTS1B"}
+!2 = !{i64 16, !"_ZTS1C"}
More information about the llvm-commits
mailing list