From 921c2b4eb338100ca756c11995ebda62fea4eda4 Mon Sep 17 00:00:00 2001 From: Petar Jovanovic Date: Wed, 9 Mar 2016 14:12:47 +0000 Subject: Reland r262337 "calculate builtin_object_size if arg is a removable pointer" Original commit message: calculate builtin_object_size if argument is a removable pointer This patch fixes calculating correct value for builtin_object_size function when pointer is used only in builtin_object_size function call and never after that. Patch by Strahinja Petrovic. Differential Revision: http://reviews.llvm.org/D17337 Reland the original change with a small modification (first do a null check and then do the cast) to satisfy ubsan. llvm-svn: 263011 --- .../InstCombine/InstructionCombining.cpp | 33 ++++++++++++++++------ 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'llvm/lib') diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index f25f063e1be..af8d5690ff6 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1942,8 +1942,31 @@ Instruction *InstCombiner::visitAllocSite(Instruction &MI) { SmallVector Users; if (isAllocSiteRemovable(&MI, Users, TLI)) { for (unsigned i = 0, e = Users.size(); i != e; ++i) { - Instruction *I = cast_or_null(&*Users[i]); - if (!I) continue; + // Lowering all @llvm.objectsize calls first because they may + // use a bitcast/GEP of the alloca we are removing. + if (!Users[i]) + continue; + + Instruction *I = cast(&*Users[i]); + + if (IntrinsicInst *II = dyn_cast(I)) { + if (II->getIntrinsicID() == Intrinsic::objectsize) { + uint64_t Size; + if (!getObjectSize(II->getArgOperand(0), Size, DL, TLI)) { + ConstantInt *CI = cast(II->getArgOperand(1)); + Size = CI->isZero() ? -1ULL : 0; + } + replaceInstUsesWith(*I, ConstantInt::get(I->getType(), Size)); + eraseInstFromFunction(*I); + Users[i] = nullptr; // Skip examining in the next loop. + } + } + } + for (unsigned i = 0, e = Users.size(); i != e; ++i) { + if (!Users[i]) + continue; + + Instruction *I = cast(&*Users[i]); if (ICmpInst *C = dyn_cast(I)) { replaceInstUsesWith(*C, @@ -1951,12 +1974,6 @@ Instruction *InstCombiner::visitAllocSite(Instruction &MI) { C->isFalseWhenEqual())); } else if (isa(I) || isa(I)) { replaceInstUsesWith(*I, UndefValue::get(I->getType())); - } else if (IntrinsicInst *II = dyn_cast(I)) { - if (II->getIntrinsicID() == Intrinsic::objectsize) { - ConstantInt *CI = cast(II->getArgOperand(1)); - uint64_t DontKnow = CI->isZero() ? -1ULL : 0; - replaceInstUsesWith(*I, ConstantInt::get(I->getType(), DontKnow)); - } } eraseInstFromFunction(*I); } -- cgit v1.2.3