[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp

Chris Lattner lattner at cs.uiuc.edu
Mon Nov 4 10:20:02 PST 2002


Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.63 -> 1.64

---
Log message:

Add a transformation to turn:
  malloc Ty, C
int
  malloc [C x Ty], 1



---
Diffs of the changes:

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.63 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.64
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.63	Tue Oct 29 17:03:20 2002
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Mon Nov  4 10:18:53 2002
@@ -23,6 +23,7 @@
 #include "llvm/iPHINode.h"
 #include "llvm/iOperators.h"
 #include "llvm/Pass.h"
+#include "llvm/DerivedTypes.h"
 #include "llvm/Support/InstIterator.h"
 #include "llvm/Support/InstVisitor.h"
 #include "Support/Statistic.h"
@@ -76,6 +77,7 @@
     Instruction *visitCastInst(CastInst &CI);
     Instruction *visitPHINode(PHINode &PN);
     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
+    Instruction *visitAllocationInst(AllocationInst &AI);
 
     // visitInstruction - Specify what to return for unhandled instructions...
     Instruction *visitInstruction(Instruction &I) { return 0; }
@@ -710,6 +712,40 @@
 
   return 0;
 }
+
+Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
+  // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
+  if (AI.isArrayAllocation())    // Check C != 1
+    if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
+      const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
+      AllocationInst *New;
+
+      // Create and insert the replacement instruction...
+      if (isa<MallocInst>(AI))
+        New = new MallocInst(NewTy, 0, AI.getName(), &AI);
+      else if (isa<AllocaInst>(AI))
+        New = new AllocaInst(NewTy, 0, AI.getName(), &AI);
+      
+      // Scan to the end of the allocation instructions, to skip over a block of
+      // allocas if possible...
+      //
+      BasicBlock::iterator It = New;
+      while (isa<AllocationInst>(*It)) ++It;
+
+      // Now that I is pointing to the first non-allocation-inst in the block,
+      // insert our getelementptr instruction...
+      //
+      std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy));
+      Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It);
+
+      // Now make everything use the getelementptr instead of the original
+      // allocation.
+      ReplaceInstUsesWith(AI, V);
+      return &AI;
+    }
+  return 0;
+}
+
 
 
 void InstCombiner::removeFromWorkList(Instruction *I) {





More information about the llvm-commits mailing list