diff options
author | Petar Jovanovic <petar.jovanovic@imgtec.com> | 2016-03-09 14:12:47 +0000 |
---|---|---|
committer | Petar Jovanovic <petar.jovanovic@imgtec.com> | 2016-03-09 14:12:47 +0000 |
commit | 921c2b4eb338100ca756c11995ebda62fea4eda4 (patch) | |
tree | c89380af0324c11b10bf9b0ed3bcf881a1a8375a /llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | |
parent | 5b7b9e946c2967c693dbcc7d7ca99897c2516008 (diff) | |
download | llvm-921c2b4eb338100ca756c11995ebda62fea4eda4.zip llvm-921c2b4eb338100ca756c11995ebda62fea4eda4.tar.gz llvm-921c2b4eb338100ca756c11995ebda62fea4eda4.tar.bz2 |
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
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstructionCombining.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index f25f063..af8d569 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<WeakVH, 64> Users; if (isAllocSiteRemovable(&MI, Users, TLI)) { for (unsigned i = 0, e = Users.size(); i != e; ++i) { - Instruction *I = cast_or_null<Instruction>(&*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<Instruction>(&*Users[i]); + + if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { + if (II->getIntrinsicID() == Intrinsic::objectsize) { + uint64_t Size; + if (!getObjectSize(II->getArgOperand(0), Size, DL, TLI)) { + ConstantInt *CI = cast<ConstantInt>(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<Instruction>(&*Users[i]); if (ICmpInst *C = dyn_cast<ICmpInst>(I)) { replaceInstUsesWith(*C, @@ -1951,12 +1974,6 @@ Instruction *InstCombiner::visitAllocSite(Instruction &MI) { C->isFalseWhenEqual())); } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I)) { replaceInstUsesWith(*I, UndefValue::get(I->getType())); - } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { - if (II->getIntrinsicID() == Intrinsic::objectsize) { - ConstantInt *CI = cast<ConstantInt>(II->getArgOperand(1)); - uint64_t DontKnow = CI->isZero() ? -1ULL : 0; - replaceInstUsesWith(*I, ConstantInt::get(I->getType(), DontKnow)); - } } eraseInstFromFunction(*I); } |