aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Transforms
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2001-11-05 21:13:30 +0000
committerChris Lattner <sabre@nondot.org>2001-11-05 21:13:30 +0000
commitc109d30239e647b93f95fbf57ecdbac0275e5b0d (patch)
treee96d5b64083592c2ac7032d420e8ca6a792dbd5b /lib/Transforms
parent93ee0a165ac3b87af4fd49c990a7ea55369f8ca1 (diff)
downloadexternal_llvm-c109d30239e647b93f95fbf57ecdbac0275e5b0d.zip
external_llvm-c109d30239e647b93f95fbf57ecdbac0275e5b0d.tar.gz
external_llvm-c109d30239e647b93f95fbf57ecdbac0275e5b0d.tar.bz2
Fix bug with ADD nodes and malloc promotion
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1144 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r--lib/Transforms/LevelRaise.cpp38
1 files changed, 29 insertions, 9 deletions
diff --git a/lib/Transforms/LevelRaise.cpp b/lib/Transforms/LevelRaise.cpp
index ccba32b..a40d865 100644
--- a/lib/Transforms/LevelRaise.cpp
+++ b/lib/Transforms/LevelRaise.cpp
@@ -251,10 +251,6 @@ static bool PeepholeMallocInst(BasicBlock *BB, BasicBlock::iterator &BI) {
// Loop over all of the uses of the malloc instruction, inspecting casts.
for (Value::use_iterator I = MI->use_begin(), E = MI->use_end();
I != E; ++I) {
- if (!isa<CastInst>(*I) && !isa<BinaryOperator>(*I)) {
- //cerr << "\tnon" << *I;
- return false; // A non cast user?
- }
if (CastInst *CI = dyn_cast<CastInst>(*I)) {
//cerr << "\t" << CI;
@@ -283,11 +279,35 @@ static bool PeepholeMallocInst(BasicBlock *BB, BasicBlock::iterator &BI) {
// that is of the same size as the malloc instruction.
if (!ResultTy) return false;
- PRINT_PEEPHOLE1("mall-refine:in ", MI);
- ReplaceInstWithInst(BB->getInstList(), BI,
- MI = new MallocInst(PointerType::get(ResultTy)));
- PRINT_PEEPHOLE1("mall-refine:out", MI);
- return true;
+ // Now we check to see if we can convert the return value of malloc to the
+ // specified pointer type. All this is moot if we can't.
+ //
+ ValueTypeCache ConvertedTypes;
+ if (RetValConvertableToType(MI, PointerType::get(ResultTy), ConvertedTypes)) {
+ // Yup, it's convertable, do the transformation now!
+ PRINT_PEEPHOLE1("mall-refine:in ", MI);
+
+ // Create a new malloc instruction, and insert it into the method...
+ MallocInst *NewMI = new MallocInst(PointerType::get(ResultTy));
+ NewMI->setName(MI->getName());
+ MI->setName("");
+ BI = BB->getInstList().insert(BI, NewMI)+1;
+
+ // Create a new cast instruction to cast it to the old type...
+ CastInst *NewCI = new CastInst(NewMI, MI->getType());
+ BB->getInstList().insert(BI, NewCI);
+
+ // Move all users of the old malloc instruction over to use the new cast...
+ MI->replaceAllUsesWith(NewCI);
+
+ ValueMapCache ValueMap;
+ ConvertUsersType(NewCI, NewMI, ValueMap); // This will delete MI!
+
+ BI = BB->begin(); // Rescan basic block. BI might be invalidated.
+ PRINT_PEEPHOLE1("mall-refine:out", NewMI);
+ return true;
+ }
+ return false;
}