Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -43,7 +43,8 @@ return Val; } - if (ConstantInt *RHS = dyn_cast(I->getOperand(1))) { + ConstantInt *RHS = dyn_cast(I->getOperand(1)); + if (RHS && !RHS->isNegative()) { if (I->getOpcode() == Instruction::Shl) { // This is a value scaled by '1 << the shift amt'. Scale = UINT64_C(1) << RHS->getZExtValue(); Index: llvm/test/Transforms/InstCombine/neg-alloca.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/InstCombine/neg-alloca.ll @@ -0,0 +1,29 @@ +; RUN: opt -S -passes=instcombine < %s | FileCheck %s +@test = external global [1 x i32], align 16 + +define void @foo() { + br label %b0 + +b0: + %1 = phi i64 [ 1, %0 ], [ %9, %b0 ] +; Currently we cannot handle expressions of the form Offset - X * Scale. +; CHECK: mul nsw i64 %1, -4 +; CHECK: alloca i8 + %2 = mul nsw i64 %1, -4 + %3 = add nsw i64 %2, 24 + %4 = alloca i8, i64 %3, align 4 + %5 = bitcast i8* %4 to i32* + store i32 undef, i32* %5, align 4 + + %6 = load i8, i8* %4, align 4 + %7 = getelementptr inbounds [1 x i32], [1 x i32]* @test, i64 0, i8 %6 + %8 = load i32, i32* %7, align 4 + store i32 %8, i32* undef, align 4 + + %9 = add nuw nsw i64 %1, 1 + br i1 undef, label %bend, label %b0 + +bend: + ret void +} +