From 1fea77c6fc6ab0d5557078656be6c54eba769b42 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Sat, 25 Jun 2016 07:37:27 +0000 Subject: [SimplifyCFG] Replace calls to null/undef with unreachable Calling null is undefined behavior, a call to undef can be trivially treated as a call to null. llvm-svn: 273776 --- llvm/lib/Transforms/Utils/Local.cpp | 6 ++++++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Transforms/Utils') diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 4d76aae2971..edacfd20f99 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1414,6 +1414,12 @@ static bool markAliveBlocks(Function &F, } if (CallInst *CI = dyn_cast(BBI)) { + Value *Callee = CI->getCalledValue(); + if (isa(Callee) || isa(Callee)) { + changeToUnreachable(CI, /*UseLLVMTrap=*/false); + Changed = true; + break; + } if (CI->doesNotReturn()) { // If we found a call to a no-return function, insert an unreachable // instruction after it. Make sure there isn't *already* one there diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index d22f5c63dee..7924c33038a 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -5388,7 +5388,7 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) { if (I->use_empty()) return false; - if (C->isNullValue()) { + if (C->isNullValue() || isa(C)) { // Only look at the first use, avoid hurting compile time with long uselists User *Use = *I->user_begin(); @@ -5417,6 +5417,10 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) { if (!SI->isVolatile()) return SI->getPointerAddressSpace() == 0 && SI->getPointerOperand() == I; + + // A call to null is undefined. + if (auto CS = CallSite(Use)) + return CS.getCalledValue() == I; } return false; } -- cgit v1.2.3