While folding unconditional return move DbgRegionEndInst into the predecessor, instea...
[oota-llvm.git] / lib / Transforms / Utils / LowerAllocations.cpp
index 7ce247909c5379c42e54445f19d06a5365df3f5c..9a7f36692fd32cc810663acdee0b561d049391c1 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -37,8 +37,8 @@ namespace {
     bool LowerMallocArgToInteger;
   public:
     static char ID; // Pass ID, replacement for typeid
-    LowerAllocations(bool LowerToInt = false)
-      : BasicBlockPass((intptr_t)&ID), MallocFunc(0), FreeFunc(0), 
+    explicit LowerAllocations(bool LowerToInt = false)
+      : BasicBlockPass(&ID), MallocFunc(0), FreeFunc(0), 
         LowerMallocArgToInteger(LowerToInt) {}
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
@@ -48,7 +48,6 @@ namespace {
       // This is a cluster of orthogonal Transforms:
       AU.addPreserved<UnifyFunctionExitNodes>();
       AU.addPreservedID(PromoteMemoryToRegisterID);
-      AU.addPreservedID(LowerSelectID);
       AU.addPreservedID(LowerSwitchID);
       AU.addPreservedID(LowerInvokePassID);
     }
@@ -59,7 +58,7 @@ namespace {
     bool doInitialization(Module &M);
 
     virtual bool doInitialization(Function &F) {
-      return BasicBlockPass::doInitialization(F);
+      return doInitialization(*F.getParent());
     }
 
     /// runOnBasicBlock - This method does the actual work of converting
@@ -67,14 +66,14 @@ namespace {
     ///
     bool runOnBasicBlock(BasicBlock &BB);
   };
-
-  char LowerAllocations::ID = 0;
-  RegisterPass<LowerAllocations>
-  X("lowerallocs", "Lower allocations from instructions to calls");
 }
 
+char LowerAllocations::ID = 0;
+static RegisterPass<LowerAllocations>
+X("lowerallocs", "Lower allocations from instructions to calls");
+
 // Publically exposed interface to pass...
-const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
+const PassInfo *const llvm::LowerAllocationsID = &X;
 // createLowerAllocationsPass - Interface to this file...
 Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
   return new LowerAllocations(LowerMallocArgToInteger);
@@ -87,7 +86,7 @@ Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
 // This function is always successful.
 //
 bool LowerAllocations::doInitialization(Module &M) {
-  const Type *BPTy = PointerType::get(Type::Int8Ty);
+  const Type *BPTy = PointerType::getUnqual(Type::Int8Ty);
   // Prototype malloc as "char* malloc(...)", because we don't know in
   // doInitialization whether size_t is int or long.
   FunctionType *FT = FunctionType::get(BPTy, std::vector<const Type*>(), true);
@@ -116,7 +115,8 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
       // malloc(type) becomes sbyte *malloc(size)
       Value *MallocArg;
       if (LowerMallocArgToInteger)
-        MallocArg = ConstantInt::get(Type::Int64Ty, TD.getTypeSize(AllocTy));
+        MallocArg = ConstantInt::get(Type::Int64Ty,
+                                     TD.getTypePaddedSize(AllocTy));
       else
         MallocArg = ConstantExpr::getSizeOf(AllocTy);
       MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg), 
@@ -132,17 +132,17 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
         } else {
           Value *Scale = MI->getOperand(0);
           if (Scale->getType() != IntPtrTy)
-            Scale = CastInst::createIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
+            Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
                                                 "", I);
 
           // Multiply it by the array size if necessary...
-          MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
+          MallocArg = BinaryOperator::Create(Instruction::Mul, Scale,
                                              MallocArg, "", I);
         }
       }
 
       // Create the call to Malloc.
-      CallInst *MCall = new CallInst(MallocFunc, MallocArg, "", I);
+      CallInst *MCall = CallInst::Create(MallocFunc, MallocArg, "", I);
       MCall->setTailCall();
 
       // Create a cast instruction to convert to the right type...
@@ -158,11 +158,12 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
       Changed = true;
       ++NumLowered;
     } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
-      Value *PtrCast = new BitCastInst(FI->getOperand(0),
-                                       PointerType::get(Type::Int8Ty), "", I);
+      Value *PtrCast = 
+        new BitCastInst(FI->getOperand(0),
+                        PointerType::getUnqual(Type::Int8Ty), "", I);
 
       // Insert a call to the free function...
-      (new CallInst(FreeFunc, PtrCast, "", I))->setTailCall();
+      CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
 
       // Delete the old free instruction
       I = --BBIL.erase(I);