From 0a9e3d613bbbd8c47f4edf6841f566fa22775932 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 28 Apr 2011 18:15:47 +0000 Subject: [PATCH] final step needed to resolve PR6627, which allows us to flatten the code down to a nice and tidy: %x1 = load i32* %0, align 4 %1 = icmp eq i32 %x1, 1179403647 br i1 %1, label %if.then, label %if.end instead of doing lots of loads and branches. May the FreeBSD bootloader long fit in its allocated space. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@130416 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/GVN.cpp | 10 ++++-- test/Transforms/GVN/rle.ll | 29 ++++++++++++++-- test/Transforms/PhaseOrdering/basic.ll | 46 +++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index 7480ed80943..abd76898cd3 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -432,14 +432,14 @@ namespace { /// addToLeaderTable - Push a new Value to the LeaderTable onto the list for /// its value number. void addToLeaderTable(uint32_t N, Value *V, BasicBlock *BB) { - LeaderTableEntry& Curr = LeaderTable[N]; + LeaderTableEntry &Curr = LeaderTable[N]; if (!Curr.Val) { Curr.Val = V; Curr.BB = BB; return; } - LeaderTableEntry* Node = TableAllocator.Allocate(); + LeaderTableEntry *Node = TableAllocator.Allocate(); Node->Val = V; Node->BB = BB; Node->Next = Curr.Next; @@ -944,7 +944,10 @@ static Value *GetLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Value *PtrVal = SrcVal->getPointerOperand(); - IRBuilder<> Builder(SrcVal->getParent(), SrcVal); + // Insert the new load after the old load. This ensures that subsequent + // memdep queries will find the new load. We can't easily remove the old + // load completely because it is already in the value numbering table. + IRBuilder<> Builder(SrcVal->getParent(), ++BasicBlock::iterator(SrcVal)); const Type *DestPTy = IntegerType::get(LoadTy->getContext(), NewLoadSize*8); DestPTy = PointerType::get(DestPTy, @@ -967,6 +970,7 @@ static Value *GetLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, RV = Builder.CreateTrunc(RV, SrcVal->getType()); SrcVal->replaceAllUsesWith(RV); gvn.getMemDep().removeInstruction(SrcVal); + //gvn.markInstructionForDeletion(SrcVal); SrcVal = NewLoad; } diff --git a/test/Transforms/GVN/rle.ll b/test/Transforms/GVN/rle.ll index 8f6e3b0e70e..4ff5becb206 100644 --- a/test/Transforms/GVN/rle.ll +++ b/test/Transforms/GVN/rle.ll @@ -1,4 +1,4 @@ -; RUN: opt < %s -basicaa -gvn -S | FileCheck %s +; RUN: opt < %s -basicaa -gvn -S -die | FileCheck %s ; 32-bit little endian target. target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32" @@ -597,11 +597,11 @@ if.end: ;; Load Widening ;;===----------------------------------------------------------------------===;; -%widening1 = type { i32, i8, i8 } +%widening1 = type { i32, i8, i8, i8, i8 } @f = global %widening1 zeroinitializer, align 4 -define i32 @test_widening1() nounwind ssp noredzone { +define i32 @test_widening1(i8* %P) nounwind ssp noredzone { entry: %tmp = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 1), align 4 %conv = zext i8 %tmp to i32 @@ -616,4 +616,27 @@ entry: ; CHECK-ret i32 } +define i32 @test_widening2() nounwind ssp noredzone { +entry: + %tmp = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 1), align 4 + %conv = zext i8 %tmp to i32 + %tmp1 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 2), align 1 + %conv2 = zext i8 %tmp1 to i32 + %add = add nsw i32 %conv, %conv2 + + %tmp2 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 3), align 2 + %conv3 = zext i8 %tmp2 to i32 + %add2 = add nsw i32 %add, %conv3 + + %tmp3 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 4), align 1 + %conv4 = zext i8 %tmp3 to i32 + %add3 = add nsw i32 %add2, %conv3 + + ret i32 %add3 +; CHECK: @test_widening2 +; CHECK-NOT: load +; CHECK: load i32* +; CHECK-NOT: load +; CHECK-ret i32 +} diff --git a/test/Transforms/PhaseOrdering/basic.ll b/test/Transforms/PhaseOrdering/basic.ll index aeb2bc4d1a2..c66e150aa79 100644 --- a/test/Transforms/PhaseOrdering/basic.ll +++ b/test/Transforms/PhaseOrdering/basic.ll @@ -60,7 +60,7 @@ if.then: ; preds = %land.lhs.true17 %call25 = call i32 (...)* @doo() br label %if.end -if.end: ; preds = %if.then, %land.lhs.true17, %land.lhs.true9, %land.lhs.true, %entry +if.end: ret void ; CHECK: @test2 @@ -71,3 +71,47 @@ if.end: ; preds = %if.then, %land.lhs. declare i32 @doo(...) +; PR6627 - This should all be flattened down to one compare. This is the same +; as test2, except that the initial load is done as an i8 instead of i32, thus +; requiring widening. +define void @test2a(i8* %arrayidx) nounwind ssp { +entry: + %x1 = load i8* %arrayidx, align 4 + %conv = zext i8 %x1 to i32 + %cmp = icmp eq i32 %conv, 127 + br i1 %cmp, label %land.lhs.true, label %if.end + +land.lhs.true: ; preds = %entry + %arrayidx4 = getelementptr inbounds i8* %arrayidx, i64 1 + %tmp5 = load i8* %arrayidx4, align 1 + %conv6 = zext i8 %tmp5 to i32 + %cmp7 = icmp eq i32 %conv6, 69 + br i1 %cmp7, label %land.lhs.true9, label %if.end + +land.lhs.true9: ; preds = %land.lhs.true + %arrayidx12 = getelementptr inbounds i8* %arrayidx, i64 2 + %tmp13 = load i8* %arrayidx12, align 1 + %conv14 = zext i8 %tmp13 to i32 + %cmp15 = icmp eq i32 %conv14, 76 + br i1 %cmp15, label %land.lhs.true17, label %if.end + +land.lhs.true17: ; preds = %land.lhs.true9 + %arrayidx20 = getelementptr inbounds i8* %arrayidx, i64 3 + %tmp21 = load i8* %arrayidx20, align 1 + %conv22 = zext i8 %tmp21 to i32 + %cmp23 = icmp eq i32 %conv22, 70 + br i1 %cmp23, label %if.then, label %if.end + +if.then: ; preds = %land.lhs.true17 + %call25 = call i32 (...)* @doo() + br label %if.end + +if.end: + ret void + +; CHECK: @test2a +; CHECK: %x1 = load i32* {{.*}}, align 4 +; CHECK-NEXT: icmp eq i32 %x1, 1179403647 +; CHECK-NEXT: br i1 {{.*}}, label %if.then, label %if.end +} + -- 2.34.1