aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Transforms/Scalar
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-03-19 06:08:10 +0000
committerChris Lattner <sabre@nondot.org>2004-03-19 06:08:10 +0000
commit7c881dffc9c1079f1aaa1bfba92b1c6c87386c92 (patch)
tree975e1776b7030874d549170410981d69c58e3052 /lib/Transforms/Scalar
parentf3fbddd3336660b4679e5a4da4129a6d56fd7c0a (diff)
downloadexternal_llvm-7c881dffc9c1079f1aaa1bfba92b1c6c87386c92.zip
external_llvm-7c881dffc9c1079f1aaa1bfba92b1c6c87386c92.tar.gz
external_llvm-7c881dffc9c1079f1aaa1bfba92b1c6c87386c92.tar.bz2
Teach the optimizer to delete zero sized alloca's (but not mallocs!)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12507 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar')
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp16
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index 93b2487..105ef5b 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -2367,11 +2367,13 @@ Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
// Create and insert the replacement instruction...
if (isa<MallocInst>(AI))
- New = new MallocInst(NewTy, 0, AI.getName(), &AI);
+ New = new MallocInst(NewTy, 0, AI.getName());
else {
assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
- New = new AllocaInst(NewTy, 0, AI.getName(), &AI);
+ New = new AllocaInst(NewTy, 0, AI.getName());
}
+
+ InsertNewInstBefore(New, AI);
// Scan to the end of the allocation instructions, to skip over a block of
// allocas if possible...
@@ -2387,9 +2389,15 @@ Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
// Now make everything use the getelementptr instead of the original
// allocation.
- ReplaceInstUsesWith(AI, V);
- return &AI;
+ return ReplaceInstUsesWith(AI, V);
}
+
+ // If alloca'ing a zero byte object, replace the alloca with a null pointer.
+ // Note that we only do this for alloca's, because malloc should allocate and
+ // return a unique pointer, even for a zero byte allocation.
+ if (isa<AllocaInst>(AI) && TD->getTypeSize(AI.getAllocatedType()) == 0)
+ return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
+
return 0;
}