From 4aebaee0e40f2457f1a6588679655a3c600a553b Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 27 Nov 2008 08:56:30 +0000 Subject: [PATCH] switch InstCombine::visitLoadInst to use FindAvailableLoadedValue git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60169 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 72 ++++++++----------- lib/Transforms/Utils/BasicBlockUtils.cpp | 27 ++++++- 2 files changed, 54 insertions(+), 45 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 717a46eb82c..5631bd0ee00 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -10762,31 +10762,6 @@ static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { return false; } -/// equivalentAddressValues - Test if A and B will obviously have the same -/// value. This includes recognizing that %t0 and %t1 will have the same -/// value in code like this: -/// %t0 = getelementptr @a, 0, 3 -/// store i32 0, i32* %t0 -/// %t1 = getelementptr @a, 0, 3 -/// %t2 = load i32* %t1 -/// -static bool equivalentAddressValues(Value *A, Value *B) { - // Test if the values are trivially equivalent. - if (A == B) return true; - - // Test if the values come form identical arithmetic instructions. - if (isa(A) || - isa(A) || - isa(A) || - isa(A)) - if (Instruction *BI = dyn_cast(B)) - if (cast(A)->isIdenticalTo(BI)) - return true; - - // Otherwise they may not be equivalent. - return false; -} - Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { Value *Op = LI.getOperand(0); @@ -10809,22 +10784,8 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { // where there are several consequtive memory accesses to the same location, // separated by a few arithmetic operations. BasicBlock::iterator BBI = &LI; - for (unsigned ScanInsts = 6; BBI != LI.getParent()->begin() && ScanInsts; - --ScanInsts) { - --BBI; - - if (StoreInst *SI = dyn_cast(BBI)) { - if (equivalentAddressValues(SI->getOperand(1), LI.getOperand(0))) - return ReplaceInstUsesWith(LI, SI->getOperand(0)); - } else if (LoadInst *LIB = dyn_cast(BBI)) { - if (equivalentAddressValues(LIB->getOperand(0), LI.getOperand(0))) - return ReplaceInstUsesWith(LI, LIB); - } - - // Don't skip over things that can modify memory. - if (BBI->mayWriteToMemory()) - break; - } + if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6)) + return ReplaceInstUsesWith(LI, AvailableVal); if (GetElementPtrInst *GEPI = dyn_cast(Op)) { const Value *GEPI0 = GEPI->getOperand(0); @@ -10991,6 +10952,31 @@ static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { return 0; } +/// equivalentAddressValues - Test if A and B will obviously have the same +/// value. This includes recognizing that %t0 and %t1 will have the same +/// value in code like this: +/// %t0 = getelementptr @a, 0, 3 +/// store i32 0, i32* %t0 +/// %t1 = getelementptr @a, 0, 3 +/// %t2 = load i32* %t1 +/// +static bool equivalentAddressValues(Value *A, Value *B) { + // Test if the values are trivially equivalent. + if (A == B) return true; + + // Test if the values come form identical arithmetic instructions. + if (isa(A) || + isa(A) || + isa(A) || + isa(A)) + if (Instruction *BI = dyn_cast(B)) + if (cast(A)->isIdenticalTo(BI)) + return true; + + // Otherwise they may not be equivalent. + return false; +} + Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { Value *Val = SI.getOperand(0); Value *Ptr = SI.getOperand(1); @@ -11036,8 +11022,8 @@ Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { if (StoreInst *PrevSI = dyn_cast(BBI)) { // Prev store isn't volatile, and stores to the same location? - if (!PrevSI->isVolatile() && equivalentAddressValues(PrevSI->getOperand(1), - SI.getOperand(1))) { + if (!PrevSI->isVolatile() &&equivalentAddressValues(PrevSI->getOperand(1), + SI.getOperand(1))) { ++NumDeadStore; ++BBI; EraseInstFromFunction(*PrevSI); diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp index d38099f4eb1..bd32a99516e 100644 --- a/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -373,6 +373,29 @@ BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, return NewBB; } +/// AreEquivalentAddressValues - Test if A and B will obviously have the same +/// value. This includes recognizing that %t0 and %t1 will have the same +/// value in code like this: +/// %t0 = getelementptr @a, 0, 3 +/// store i32 0, i32* %t0 +/// %t1 = getelementptr @a, 0, 3 +/// %t2 = load i32* %t1 +/// +static bool AreEquivalentAddressValues(const Value *A, const Value *B) { + // Test if the values are trivially equivalent. + if (A == B) return true; + + // Test if the values come form identical arithmetic instructions. + if (isa(A) || isa(A) || + isa(A) || isa(A)) + if (const Instruction *BI = dyn_cast(B)) + if (cast(A)->isIdenticalTo(BI)) + return true; + + // Otherwise they may not be equivalent. + return false; +} + /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the /// instruction before ScanFrom) checking to see if we have the value at the /// memory address *Ptr locally available within a small number of instructions. @@ -407,12 +430,12 @@ Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, // If this is a load of Ptr, the loaded value is available. if (LoadInst *LI = dyn_cast(Inst)) - if (LI->getOperand(0) == Ptr) + if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) return LI; if (StoreInst *SI = dyn_cast(Inst)) { // If this is a store through Ptr, the value is available! - if (SI->getOperand(1) == Ptr) + if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) return SI->getOperand(0); // If Ptr is an alloca and this is a store to a different alloca, ignore -- 2.34.1