From 4458ab0d83c33a649c61355a4d80e94014193856 Mon Sep 17 00:00:00 2001 From: Dale Johannesen Date: Tue, 14 Jul 2009 00:45:38 +0000 Subject: [PATCH] Don't delete asm's just because their inputs are undefined; xor R, R is a common and valid idiom for zeroing a register, for example. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75571 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/SimplifyCFGPass.cpp | 25 +++++++++++++------ .../SimplifyCFG/2009-07-13-no-delete-asm.ll | 11 ++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 test/Transforms/SimplifyCFG/2009-07-13-no-delete-asm.ll diff --git a/lib/Transforms/Scalar/SimplifyCFGPass.cpp b/lib/Transforms/Scalar/SimplifyCFGPass.cpp index f6cffdd0ef4..155d5d81e30 100644 --- a/lib/Transforms/Scalar/SimplifyCFGPass.cpp +++ b/lib/Transforms/Scalar/SimplifyCFGPass.cpp @@ -109,6 +109,8 @@ static bool MarkAliveBlocks(BasicBlock *BB, if (!Reachable.insert(BB)) continue; + bool AsmFound = false; + StoreInst *UndefStore = 0; // Do a quick scan of the basic block, turning any obviously unreachable // instructions into LLVM unreachable insts. The instruction combining pass // canonicalizes unreachable insts into stores to null or undef. @@ -125,20 +127,27 @@ static bool MarkAliveBlocks(BasicBlock *BB, } break; } + if (isa(CI->getOperand(0))) + AsmFound = true; } if (StoreInst *SI = dyn_cast(BBI)) { Value *Ptr = SI->getOperand(1); - - if (isa(Ptr) || - (isa(Ptr) && - cast(Ptr->getType())->getAddressSpace() == 0)) { - ChangeToUnreachable(SI, Context); - Changed = true; - break; - } + + if ((isa(Ptr) || + (isa(Ptr) && + cast(Ptr->getType())->getAddressSpace() == 0)) && + !UndefStore) + UndefStore = SI; } } + // We can't delete asm's just because their inputs are undefined; + // xor R, R is a common idiom for zeroing a register, for example. + // Assume user knows what he is doing. + if (UndefStore && !AsmFound) { + ChangeToUnreachable(UndefStore, Context); + Changed = true; + } // Turn invokes that call 'nounwind' functions into ordinary calls. if (InvokeInst *II = dyn_cast(BB->getTerminator())) diff --git a/test/Transforms/SimplifyCFG/2009-07-13-no-delete-asm.ll b/test/Transforms/SimplifyCFG/2009-07-13-no-delete-asm.ll new file mode 100644 index 00000000000..4664ee0348c --- /dev/null +++ b/test/Transforms/SimplifyCFG/2009-07-13-no-delete-asm.ll @@ -0,0 +1,11 @@ +; RUN: llvm-as < %s | opt -simplifycfg | llvm-dis | grep xor +; ModuleID = '' +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" +target triple = "i386-apple-darwin9.6" + +define void @bar() nounwind { +entry: + store i16 undef, i16* null + %asmtmp = call i32 asm sideeffect "xor $0, $0", "=={bx},rm,~{dirflag},~{fpsr},~{flags},~{memory}"(i16 undef) nounwind ; [#uses=0] + ret void +}