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

Chris Lattner sabre at nondot.org
Sun Jan 7 00:12:20 PST 2007



Changes in directory llvm/lib/Transforms/Utils:

LowerAllocations.cpp updated: 1.69 -> 1.70
LowerInvoke.cpp updated: 1.49 -> 1.50
---
Log message:

Change the interface to Module::getOrInsertFunction to be easier to use,to resolve PR1088: http://llvm.org/PR1088 , and to help PR411: http://llvm.org/PR411 .
This simplifies many clients also



---
Diffs of the changes:  (+30 -100)

 LowerAllocations.cpp |   64 ++++++++++---------------------------------------
 LowerInvoke.cpp      |   66 +++++++++++++--------------------------------------
 2 files changed, 30 insertions(+), 100 deletions(-)


Index: llvm/lib/Transforms/Utils/LowerAllocations.cpp
diff -u llvm/lib/Transforms/Utils/LowerAllocations.cpp:1.69 llvm/lib/Transforms/Utils/LowerAllocations.cpp:1.70
--- llvm/lib/Transforms/Utils/LowerAllocations.cpp:1.69	Sat Dec 30 23:48:39 2006
+++ llvm/lib/Transforms/Utils/LowerAllocations.cpp	Sun Jan  7 02:12:01 2007
@@ -32,8 +32,8 @@
   /// %free calls.
   ///
   class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
-    Function *MallocFunc;   // Functions in the module we are processing
-    Function *FreeFunc;     // Initialized by doInitialization
+    Constant *MallocFunc;   // Functions in the module we are processing
+    Constant *FreeFunc;     // Initialized by doInitialization
     bool LowerMallocArgToInteger;
   public:
     LowerAllocations(bool LowerToInt = false)
@@ -84,19 +84,12 @@
 // This function is always successful.
 //
 bool LowerAllocations::doInitialization(Module &M) {
-  const Type *SBPTy = PointerType::get(Type::Int8Ty);
-  MallocFunc = M.getNamedFunction("malloc");
-  FreeFunc   = M.getNamedFunction("free");
-
-  if (MallocFunc == 0) {
-    // Prototype malloc as "void* malloc(...)", because we don't know in
-    // doInitialization whether size_t is int or long.
-    FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
-    MallocFunc = M.getOrInsertFunction("malloc", FT);
-  }
-  if (FreeFunc == 0)
-    FreeFunc = M.getOrInsertFunction("free"  , Type::VoidTy, SBPTy, (Type *)0);
-
+  const Type *BPTy = PointerType::get(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);
+  MallocFunc = M.getOrInsertFunction("malloc", FT);
+  FreeFunc = M.getOrInsertFunction("free"  , Type::VoidTy, BPTy, (Type *)0);
   return true;
 }
 
@@ -145,27 +138,8 @@
         }
       }
 
-      const FunctionType *MallocFTy = MallocFunc->getFunctionType();
-      std::vector<Value*> MallocArgs;
-
-      if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
-        if (MallocFTy->isVarArg()) {
-          if (MallocArg->getType() != IntPtrTy)
-            MallocArg = CastInst::createIntegerCast(MallocArg, IntPtrTy, 
-                                                    false /*ZExt*/, "", I);
-        } else if (MallocFTy->getNumParams() > 0 &&
-                   MallocFTy->getParamType(0) != Type::Int32Ty)
-          MallocArg = CastInst::createIntegerCast(
-              MallocArg, MallocFTy->getParamType(0), false/*ZExt*/, "",I);
-        MallocArgs.push_back(MallocArg);
-      }
-
-      // If malloc is prototyped to take extra arguments, pass nulls.
-      for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
-       MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
-
-      // Create the call to Malloc...
-      CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
+      // Create the call to Malloc.
+      CallInst *MCall = new CallInst(MallocFunc, MallocArg, "", I);
       MCall->setTailCall();
 
       // Create a cast instruction to convert to the right type...
@@ -181,23 +155,11 @@
       Changed = true;
       ++NumLowered;
     } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
-      const FunctionType *FreeFTy = FreeFunc->getFunctionType();
-      std::vector<Value*> FreeArgs;
-
-      if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
-        Value *MCast = FI->getOperand(0);
-        if (FreeFTy->getNumParams() > 0 &&
-            FreeFTy->getParamType(0) != MCast->getType())
-          MCast = new BitCastInst(MCast, FreeFTy->getParamType(0), "", I);
-        FreeArgs.push_back(MCast);
-      }
-
-      // If malloc is prototyped to take extra arguments, pass nulls.
-      for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
-       FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
+      Value *PtrCast = new BitCastInst(FI->getOperand(0),
+                                       PointerType::get(Type::Int8Ty), "", I);
 
       // Insert a call to the free function...
-      (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
+      (new CallInst(FreeFunc, PtrCast, "", I))->setTailCall();
 
       // Delete the old free instruction
       I = --BBIL.erase(I);


Index: llvm/lib/Transforms/Utils/LowerInvoke.cpp
diff -u llvm/lib/Transforms/Utils/LowerInvoke.cpp:1.49 llvm/lib/Transforms/Utils/LowerInvoke.cpp:1.50
--- llvm/lib/Transforms/Utils/LowerInvoke.cpp:1.49	Sat Dec 30 23:48:39 2006
+++ llvm/lib/Transforms/Utils/LowerInvoke.cpp	Sun Jan  7 02:12:01 2007
@@ -60,15 +60,15 @@
 namespace {
   class VISIBILITY_HIDDEN LowerInvoke : public FunctionPass {
     // Used for both models.
-    Function *WriteFn;
-    Function *AbortFn;
+    Constant *WriteFn;
+    Constant *AbortFn;
     Value *AbortMessage;
     unsigned AbortMessageLength;
 
     // Used for expensive EH support.
     const Type *JBLinkTy;
     GlobalVariable *JBListHead;
-    Function *SetJmpFn, *LongJmpFn;
+    Constant *SetJmpFn, *LongJmpFn;
     
     // We peek in TLI to grab the target's jmp_buf size and alignment
     const TargetLowering *TLI;
@@ -87,7 +87,7 @@
     }
        
   private:
-    void createAbortMessage();
+    void createAbortMessage(Module *M);
     void writeAbortMessage(Instruction *IB);
     bool insertCheapEHSupport(Function &F);
     void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
@@ -148,29 +148,12 @@
 
   // We need the 'write' and 'abort' functions for both models.
   AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, (Type *)0);
-
-  // Unfortunately, 'write' can end up being prototyped in several different
-  // ways.  If the user defines a three (or more) operand function named 'write'
-  // we will use their prototype.  We _do not_ want to insert another instance
-  // of a write prototype, because we don't know that the funcresolve pass will
-  // run after us.  If there is a definition of a write function, but it's not
-  // suitable for our uses, we just don't emit write calls.  If there is no
-  // write prototype at all, we just add one.
-  if (Function *WF = M.getNamedFunction("write")) {
-    if (WF->getFunctionType()->getNumParams() > 3 ||
-        WF->getFunctionType()->isVarArg())
-      WriteFn = WF;
-    else
-      WriteFn = 0;
-  } else {
-    WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::Int32Ty,
-                                    VoidPtrTy, Type::Int32Ty, (Type *)0);
-  }
+  WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::Int32Ty,
+                                  VoidPtrTy, Type::Int32Ty, (Type *)0);
   return true;
 }
 
-void LowerInvoke::createAbortMessage() {
-  Module &M = *WriteFn->getParent();
+void LowerInvoke::createAbortMessage(Module *M) {
   if (ExpensiveEHSupport) {
     // The abort message for expensive EH support tells the user that the
     // program 'unwound' without an 'invoke' instruction.
@@ -180,7 +163,7 @@
 
     GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
                                                GlobalValue::InternalLinkage,
-                                               Msg, "abortmsg", &M);
+                                               Msg, "abortmsg", M);
     std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
     AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
   } else {
@@ -193,7 +176,7 @@
 
     GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
                                                GlobalValue::InternalLinkage,
-                                               Msg, "abortmsg", &M);
+                                               Msg, "abortmsg", M);
     std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
     AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
   }
@@ -201,30 +184,15 @@
 
 
 void LowerInvoke::writeAbortMessage(Instruction *IB) {
-  if (WriteFn) {
-    if (AbortMessage == 0) createAbortMessage();
+  if (AbortMessage == 0)
+    createAbortMessage(IB->getParent()->getParent()->getParent());
 
-    // These are the arguments we WANT...
-    std::vector<Value*> Args;
-    Args.push_back(ConstantInt::get(Type::Int32Ty, 2));
-    Args.push_back(AbortMessage);
-    Args.push_back(ConstantInt::get(Type::Int32Ty, AbortMessageLength));
-
-    // If the actual declaration of write disagrees, insert casts as
-    // appropriate.
-    const FunctionType *FT = WriteFn->getFunctionType();
-    unsigned NumArgs = FT->getNumParams();
-    for (unsigned i = 0; i != 3; ++i)
-      if (i < NumArgs && FT->getParamType(i) != Args[i]->getType())
-        if (Args[i]->getType()->isInteger())
-          Args[i] = ConstantExpr::getIntegerCast(cast<Constant>(Args[i]),
-                                                 FT->getParamType(i), true);
-        else
-          Args[i] = ConstantExpr::getBitCast(cast<Constant>(Args[i]),
-                                             FT->getParamType(i));
-
-    (new CallInst(WriteFn, Args, "", IB))->setTailCall();
-  }
+  // These are the arguments we WANT...
+  std::vector<Value*> Args;
+  Args.push_back(ConstantInt::get(Type::Int32Ty, 2));
+  Args.push_back(AbortMessage);
+  Args.push_back(ConstantInt::get(Type::Int32Ty, AbortMessageLength));
+  (new CallInst(WriteFn, Args, "", IB))->setTailCall();
 }
 
 bool LowerInvoke::insertCheapEHSupport(Function &F) {






More information about the llvm-commits mailing list