From: Stuart Hastings Date: Mon, 13 Jun 2011 18:48:49 +0000 (+0000) Subject: Avoid fusing bitcasts with dynamic allocas if the amount-to-allocate X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=7ac8f8f3418c9ee7cb3f27aa9e5033e9b73ef84f;p=oota-llvm.git Avoid fusing bitcasts with dynamic allocas if the amount-to-allocate might overflow. Re-typing the alloca to a larger type (e.g. double) hoists a shift into the alloca, potentially exposing overflow in the expression. rdar://problem/9265821 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@132926 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineCasts.cpp b/lib/Transforms/InstCombine/InstCombineCasts.cpp index 199902aa41f..601d9b42f31 100644 --- a/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -71,6 +71,11 @@ Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, // This requires TargetData to get the alloca alignment and size information. if (!TD) return 0; + // Insist that the amount-to-allocate not overflow. + OverflowingBinaryOperator *OBI = + dyn_cast(AI.getOperand(0)); + if (OBI && !(OBI->hasNoSignedWrap() || OBI->hasNoUnsignedWrap())) return 0; + const PointerType *PTy = cast(CI.getType()); BuilderTy AllocaBuilder(*Builder); diff --git a/test/Transforms/InstCombine/2011-06-13-nsw-alloca.ll b/test/Transforms/InstCombine/2011-06-13-nsw-alloca.ll new file mode 100644 index 00000000000..2f72b73801d --- /dev/null +++ b/test/Transforms/InstCombine/2011-06-13-nsw-alloca.ll @@ -0,0 +1,60 @@ +; RUN: opt -S -instcombine < %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" + +define void @fu1(i32 %parm) nounwind ssp { + %1 = alloca i32, align 4 + %ptr = alloca double*, align 4 + store i32 %parm, i32* %1, align 4 + store double* null, double** %ptr, align 4 + %2 = load i32* %1, align 4 + %3 = icmp ne i32 %2, 0 + br i1 %3, label %4, label %10 + +;