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

Chris Lattner lattner at cs.uiuc.edu
Wed Jul 14 18:08:19 PDT 2004



Changes in directory llvm/lib/Transforms/Scalar:

LowerAllocations.cpp updated: 1.46 -> 1.47

---
Log message:

Now that we codegen the portable "sizeof" efficiently, we can use it for 
malloc lowering.  This means that lowerallocations doesn't need targetdata
anymore.  yaay.


---
Diffs of the changes:  (+22 -18)

Index: llvm/lib/Transforms/Scalar/LowerAllocations.cpp
diff -u llvm/lib/Transforms/Scalar/LowerAllocations.cpp:1.46 llvm/lib/Transforms/Scalar/LowerAllocations.cpp:1.47
--- llvm/lib/Transforms/Scalar/LowerAllocations.cpp:1.46	Tue Mar  2 19:40:53 2004
+++ llvm/lib/Transforms/Scalar/LowerAllocations.cpp	Wed Jul 14 20:08:08 2004
@@ -19,7 +19,6 @@
 #include "llvm/iOther.h"
 #include "llvm/Constants.h"
 #include "llvm/Pass.h"
-#include "llvm/Target/TargetData.h"
 #include "Support/Statistic.h"
 using namespace llvm;
 
@@ -35,10 +34,6 @@
   public:
     LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
 
-    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-      AU.addRequired<TargetData>();
-    }
-
     /// doPassInitialization - For the lower allocations pass, this ensures that
     /// a module contains a declaration for a malloc and a free function.
     ///
@@ -78,6 +73,14 @@
   return true;
 }
 
+static Constant *getSizeof(const Type *Ty) {
+  Constant *Ret = ConstantPointerNull::get(PointerType::get(Ty));
+  std::vector<Constant*> Idx;
+  Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
+  Ret = ConstantExpr::getGetElementPtr(Ret, Idx);
+  return ConstantExpr::getCast(Ret, Type::UIntTy);
+}
+
 // runOnBasicBlock - This method does the actual work of converting
 // instructions over, assuming that the pass has already been initialized.
 //
@@ -86,25 +89,26 @@
   assert(MallocFunc && FreeFunc && "Pass not initialized!");
 
   BasicBlock::InstListType &BBIL = BB.getInstList();
-  TargetData &DataLayout = getAnalysis<TargetData>();
 
   // Loop over all of the instructions, looking for malloc or free instructions
   for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
     if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
       const Type *AllocTy = MI->getType()->getElementType();
       
-      // Get the number of bytes to be allocated for one element of the
-      // requested type...
-      unsigned Size = DataLayout.getTypeSize(AllocTy);
-      
-      // malloc(type) becomes sbyte *malloc(constint)
-      Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
-      if (MI->getNumOperands() && Size == 1) {
-        MallocArg = MI->getOperand(0);         // Operand * 1 = Operand
-      } else if (MI->isArrayAllocation()) {
-        // Multiply it by the array size if necessary...
-        MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
-                                           MallocArg, "", I);
+      // malloc(type) becomes sbyte *malloc(size)
+      Value *MallocArg = getSizeof(AllocTy);
+      if (MI->isArrayAllocation()) {
+        if (isa<ConstantUInt>(MallocArg) &&
+            cast<ConstantUInt>(MallocArg)->getValue() == 1) {
+          MallocArg = MI->getOperand(0);         // Operand * 1 = Operand
+        } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
+          MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
+        } else {
+          // Multiply it by the array size if necessary...
+          MallocArg = BinaryOperator::create(Instruction::Mul,
+                                             MI->getOperand(0),
+                                             MallocArg, "", I);
+        }
       }
 
       const FunctionType *MallocFTy = MallocFunc->getFunctionType();





More information about the llvm-commits mailing list