Index: lib/Transforms/Scalar/LoopIdiomRecognize.cpp =================================================================== --- lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -384,6 +384,13 @@ StoreRefsForMemsetPattern.clear(); StoreRefsForMemcpy.clear(); for (Instruction &I : *BB) { + if (I.mayThrow()) { + StoreRefsForMemset.clear(); + StoreRefsForMemsetPattern.clear(); + StoreRefsForMemcpy.clear(); + return; + } + StoreInst *SI = dyn_cast(&I); if (!SI) continue; Index: test/Transforms/LoopIdiom/unwind.ll =================================================================== --- /dev/null +++ test/Transforms/LoopIdiom/unwind.ll @@ -0,0 +1,51 @@ +; ModuleID = 'tmp.cpp' +; RUN: opt -loop-idiom < %s -S | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@_ZTIi = external constant i8* +@ff = global void ()* @f, align 8 +@gg = global void (i8*, i32)* @g, align 8 + +define void @f() { +entry: + %exception = tail call i8* @__cxa_allocate_exception(i64 4) #5 + %0 = bitcast i8* %exception to i32* + store i32 1, i32* %0, align 16 + tail call void @__cxa_throw(i8* %exception, i8* bitcast (i8** @_ZTIi to i8*), i8* null) + unreachable +} + +declare i8* @__cxa_allocate_exception(i64) + +declare void @__cxa_throw(i8*, i8*, i8*) + +define void @g(i8* noalias nocapture %base, i32 %size) { +entry: + %cmp4 = icmp eq i32 %size, 0 + br i1 %cmp4, label %for.cond.cleanup, label %for.body.preheader + +for.body.preheader: ; preds = %entry + br label %for.body + +for.cond.cleanup.loopexit: ; preds = %for.body + br label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry + ret void + +for.body: ; preds = %for.body.preheader, %for.body + %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %for.body.preheader ] + %0 = load void ()*, void ()** @ff, align 8 + tail call void %0() + %arrayidx = getelementptr inbounds i8, i8* %base, i64 %indvars.iv + store i8 0, i8* %arrayidx, align 1 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %lftr.wideiv = trunc i64 %indvars.iv.next to i32 + %exitcond = icmp eq i32 %lftr.wideiv, %size + br i1 %exitcond, label %for.cond.cleanup.loopexit, label %for.body +; CHECK-LABEL: @g( +; CHECK-NOT: llvm.memset +} +