From: John Brawn Date: Mon, 23 Jul 2018 12:14:45 +0000 (+0000) Subject: [GVN] Don't use the eliminated load as an available value in phi construction X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=576662c3d6b01d195a908a313559804f2fe45306;p=llvm [GVN] Don't use the eliminated load as an available value in phi construction In ConstructSSAForLoadSet if an available value is actually the load that we're doing SSA construction to eliminate, then we can omit it as SSAUpdate will add in the value for the phi that will be replacing it anyway. This can result in simpler IR which can allow further optimisation. Differential Revision: https://reviews.llvm.org/D44160 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337686 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index a8f52f0cb8d..1e0a22cb14b 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -766,6 +766,15 @@ static Value *ConstructSSAForLoadSet(LoadInst *LI, if (SSAUpdate.HasValueForBlock(BB)) continue; + // If the value is the load that we will be eliminating, and the block it's + // available in is the block that the load is in, then don't add it as + // SSAUpdater will resolve the value to the relevant phi which may let it + // avoid phi construction entirely if there's actually only one value. + if (BB == LI->getParent() && + ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == LI) || + (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == LI))) + continue; + SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(LI, gvn)); } diff --git a/test/Transforms/GVN/PRE/phi-translate-2.ll b/test/Transforms/GVN/PRE/phi-translate-2.ll index 7911b525625..b40c745ea13 100644 --- a/test/Transforms/GVN/PRE/phi-translate-2.ll +++ b/test/Transforms/GVN/PRE/phi-translate-2.ll @@ -177,5 +177,44 @@ if.end3: ; preds = %if.then2, %if.else, ret i64 %sub.ptr.div7 } +; Here the load from arrayidx1 is partially redundant, but its value is +; available in if.then. Check that we correctly phi-translate to the phi that +; the load has been replaced with. +; CHECK-LABEL: @test6 +define void @test6(i32* %ptr) { +; CHECK: entry: +; CHECK: %[[PREGEP:.*]] = getelementptr inbounds i32, i32* %ptr, i64 1 +; CHECK: %[[PRE:.*]] = load i32, i32* %[[PREGEP]] +entry: + br label %while + +; CHECK: while: +; CHECK: %[[PHI1:.*]] = phi i32 [ %[[PRE]], %entry ], [ %[[PHI2:.*]], %if.end ] +; CHECK-NOT: load i32, i32* %arrayidx1 +; CHECK: %[[LOAD:.*]] = load i32, i32* %arrayidx2 +while: + %i = phi i64 [ 1, %entry ], [ %i.next, %if.end ] + %arrayidx1 = getelementptr inbounds i32, i32* %ptr, i64 %i + %0 = load i32, i32* %arrayidx1, align 4 + %i.next = add nuw nsw i64 %i, 1 + %arrayidx2 = getelementptr inbounds i32, i32* %ptr, i64 %i.next + %1 = load i32, i32* %arrayidx2, align 4 + %cmp = icmp sgt i32 %0, %1 + br i1 %cmp, label %if.then, label %if.end + +if.then: + store i32 %1, i32* %arrayidx1, align 4 + store i32 %0, i32* %arrayidx2, align 4 + br label %if.end + +; CHECK: if.then: +; CHECK: %[[PHI2]] = phi i32 [ %[[PHI1]], %if.then ], [ %[[LOAD]], %while ] +if.end: + br i1 undef, label %while.end, label %while + +while.end: + ret void +} + ; CHECK: [[var_p0]] = !DILocalVariable ; CHECK: [[var_sub_ptr]] = !DILocalVariable diff --git a/test/Transforms/GVN/PRE/pre-after-rle.ll b/test/Transforms/GVN/PRE/pre-after-rle.ll new file mode 100644 index 00000000000..ff35e56a676 --- /dev/null +++ b/test/Transforms/GVN/PRE/pre-after-rle.ll @@ -0,0 +1,82 @@ +; RUN: opt -gvn -S < %s | FileCheck %s + +declare noalias i8* @malloc(i64) + +; Detecting that %s is fully redundant should let us detect that %w is partially +; redundant. +define void @fn1(i32** noalias %start, i32* %width, i32 %h) { +; CHECK-LABEL: @fn1 +entry: + %call = tail call noalias i8* @malloc(i64 1024) + %call.cast = bitcast i8* %call to i32* + store i32* %call.cast, i32** %start, align 8 + br label %preheader + +preheader: + %cmp = icmp slt i32 1, %h + br i1 %cmp, label %body, label %exit + +; CHECK-LABEL: preheader.body_crit_edge: +; CHECK: load i32, i32* %width, align 8 + +; CHECK-LABEL: body: +; CHECK-NOT: load i32*, i32** %start, align 8 +; CHECK-NOT: load i32, i32* %width, align 8 +body: + %j = phi i32 [ 0, %preheader ], [ %j.next, %body ] + %s = load i32*, i32** %start, align 8 + %idx = getelementptr inbounds i32, i32* %s, i64 0 + store i32 0, i32* %idx, align 4 + %j.next = add nuw nsw i32 %j, 1 + %w = load i32, i32* %width, align 8 + %cmp3 = icmp slt i32 %j.next, %w + br i1 %cmp3, label %body, label %preheader + +exit: + ret void +} + +; %s is fully redundant but has more than one available value. Detecting that +; %w is partially redundant requires alias analysis that can analyze those +; values. +define void @fn2(i32** noalias %start, i32* %width, i32 %h, i32 %arg) { +; CHECK-LABEL: @fn2 +entry: + %call = tail call noalias i8* @malloc(i64 1024) + %call.cast = bitcast i8* %call to i32* + %cmp1 = icmp slt i32 %arg, 0 + br i1 %cmp1, label %if, label %else + +if: + store i32* %call.cast, i32** %start, align 8 + br label %preheader + +else: + %gep = getelementptr inbounds i32, i32* %call.cast, i32 %arg + store i32* %gep, i32** %start, align 8 + br label %preheader + +; CHECK-LABEL: preheader: +; CHECK: %s = phi i32* [ %s, %body ], [ %gep, %else ], [ %call.cast, %if ] + +preheader: + %cmp = icmp slt i32 1, %h + br i1 %cmp, label %body, label %exit + +; Alias analysis currently can't figure out %width doesn't alias %s, so just +; check that the redundant load has been removed. +; CHECK-LABEL: body: +; CHECK-NOT: load i32*, i32** %start, align 8 +body: + %j = phi i32 [ 0, %preheader ], [ %j.next, %body ] + %s = load i32*, i32** %start, align 8 + %idx = getelementptr inbounds i32, i32* %s, i64 0 + store i32 0, i32* %idx, align 4 + %j.next = add nuw nsw i32 %j, 1 + %w = load i32, i32* %width, align 8 + %cmp3 = icmp slt i32 %j.next, %w + br i1 %cmp3, label %body, label %preheader + +exit: + ret void +}