Index: lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -964,8 +964,25 @@ // separated by a few arithmetic operations. BasicBlock::iterator BBI(LI); bool IsLoadCSE = false; - if (Value *AvailableVal = FindAvailableLoadedValue( - &LI, LI.getParent(), BBI, DefMaxInstsToScan, AA, &IsLoadCSE)) { + unsigned NumScannedInst = 0; + Value *AvailableVal = + FindAvailableLoadedValue(&LI, LI.getParent(), BBI, DefMaxInstsToScan, AA, + &IsLoadCSE, &NumScannedInst); + // If the current block has a single predecessor, continue scanning through + // the single precessor. + BasicBlock *SinglePredBB = LI.getParent(); + while (!AvailableVal && SinglePredBB && BBI == SinglePredBB->begin() && + NumScannedInst < DefMaxInstsToScan) { + SinglePredBB = SinglePredBB->getSinglePredecessor(); + if (SinglePredBB) { + BBI = SinglePredBB->end(); + AvailableVal = FindAvailableLoadedValue( + &LI, SinglePredBB, BBI, (DefMaxInstsToScan - NumScannedInst), + nullptr, &IsLoadCSE, &NumScannedInst); + } + } + + if (AvailableVal) { if (IsLoadCSE) combineMetadataForCSE(cast(AvailableVal), &LI); Index: test/Transforms/InstCombine/2011-06-13-nsw-alloca.ll =================================================================== --- test/Transforms/InstCombine/2011-06-13-nsw-alloca.ll +++ test/Transforms/InstCombine/2011-06-13-nsw-alloca.ll @@ -1,4 +1,4 @@ -; RUN: opt -S -instcombine < %s | FileCheck %s +; RUN: opt -S -instcombine -available-load-scan-limit=3 < %s | FileCheck %s 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" target triple = "i386-apple-darwin10.0.0" Index: test/Transforms/InstCombine/load.ll =================================================================== --- test/Transforms/InstCombine/load.ll +++ test/Transforms/InstCombine/load.ll @@ -238,3 +238,18 @@ store %swift.error* %err.res, %swift.error** %err, align 8 ret void } + +; Check that %ld is removed as it loads from the same location to which %a is stored in +; %entry. +; CHECK-LABEL: @test20( +; CHECK-LABEL: return +; CHECK-NOT: load +define i32 @test20(i32* %P, i32 %a) { +entry: + store i32 %a, i32 * %P + br label %return + +return: ; preds = %entry + %ld = load i32 , i32* %P + ret i32 %ld +}