LLVMContext-ification.
[oota-llvm.git] / lib / VMCore / AutoUpgrade.cpp
index fd7d6dcd4c2f08d1c3cd142976b196e7b4b967a7..378df839ad4a1bd07a00833cb51fc1b51124a60e 100644 (file)
@@ -14,6 +14,7 @@
 #include "llvm/AutoUpgrade.h"
 #include "llvm/Constants.h"
 #include "llvm/Function.h"
+#include "llvm/LLVMContext.h"
 #include "llvm/Module.h"
 #include "llvm/Instructions.h"
 #include "llvm/Intrinsics.h"
@@ -25,6 +26,8 @@ using namespace llvm;
 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
   assert(F && "Illegal to upgrade a non-existent Function.");
 
+  LLVMContext* Context = F->getContext();
+
   // Get the Function's name.
   const std::string& Name = F->getName();
 
@@ -40,24 +43,38 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
   switch (Name[5]) {
   default: break;
   case 'a':
-    // This upgrades the llvm.atomic.lcs, llvm.atomic.las, and llvm.atomic.lss
-    // to their new function name
-    if (Name.compare(5,8,"atomic.l",8) == 0) {
+    // This upgrades the llvm.atomic.lcs, llvm.atomic.las, llvm.atomic.lss,
+    // and atomics with default address spaces to their new names to their new
+    // function name (e.g. llvm.atomic.add.i32 => llvm.atomic.add.i32.p0i32)
+    if (Name.compare(5,7,"atomic.",7) == 0) {
       if (Name.compare(12,3,"lcs",3) == 0) {
         std::string::size_type delim = Name.find('.',12);
-        F->setName("llvm.atomic.cmp.swap"+Name.substr(delim));
+        F->setName("llvm.atomic.cmp.swap" + Name.substr(delim) +
+                   ".p0" + Name.substr(delim+1));
         NewFn = F;
         return true;
       }
       else if (Name.compare(12,3,"las",3) == 0) {
         std::string::size_type delim = Name.find('.',12);
-        F->setName("llvm.atomic.load.add"+Name.substr(delim));
+        F->setName("llvm.atomic.load.add"+Name.substr(delim)
+                   + ".p0" + Name.substr(delim+1));
         NewFn = F;
         return true;
       }
       else if (Name.compare(12,3,"lss",3) == 0) {
         std::string::size_type delim = Name.find('.',12);
-        F->setName("llvm.atomic.load.sub"+Name.substr(delim));
+        F->setName("llvm.atomic.load.sub"+Name.substr(delim)
+                   + ".p0" + Name.substr(delim+1));
+        NewFn = F;
+        return true;
+      }
+      else if (Name.rfind(".p") == std::string::npos) {
+        // We don't have an address space qualifier so this has be upgraded
+        // to the new name.  Copy the type name at the end of the intrinsic
+        // and add to it
+        std::string::size_type delim = Name.find_last_of('.');
+        assert(delim != std::string::npos && "can not find type");
+        F->setName(Name + ".p0" + Name.substr(delim+1));
         NewFn = F;
         return true;
       }
@@ -148,7 +165,8 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
          Name.compare(13,4,"psra", 4) == 0 ||
          Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
       
-      const llvm::Type *VT = VectorType::get(IntegerType::get(64), 1);
+      const llvm::Type *VT =
+                        Context->getVectorType(Context->getIntegerType(64), 1);
       
       // We don't have to do anything if the parameter already has
       // the correct type.
@@ -203,8 +221,8 @@ bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
   // Upgrade intrinsic attributes.  This does not change the function.
   if (NewFn)
     F = NewFn;
-  if (unsigned id = F->getIntrinsicID(true))
-    F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id));
+  if (unsigned id = F->getIntrinsicID())
+    F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
   return Upgraded;
 }
 
@@ -213,6 +231,8 @@ bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
 // order to seamlessly integrate with existing context.
 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
   Function *F = CI->getCalledFunction();
+  LLVMContext* Context = F->getContext();
+  
   assert(F && "CallInst has no function associated with it.");
 
   if (!NewFn) {
@@ -245,59 +265,60 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
       Value *Op0 = CI->getOperand(1);
       ShuffleVectorInst *SI = NULL;
       if (isLoadH || isLoadL) {
-        Value *Op1 = UndefValue::get(Op0->getType());
+        Value *Op1 = Context->getUndef(Op0->getType());
         Value *Addr = new BitCastInst(CI->getOperand(2), 
-                                      PointerType::getUnqual(Type::DoubleTy),
+                                  Context->getPointerTypeUnqual(Type::DoubleTy),
                                       "upgraded.", CI);
         Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
-        Value *Idx = ConstantInt::get(Type::Int32Ty, 0);
+        Value *Idx = Context->getConstantInt(Type::Int32Ty, 0);
         Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
 
         if (isLoadH) {
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 0));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
         } else {
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
         }
-        Value *Mask = ConstantVector::get(Idxs);
+        Value *Mask = Context->getConstantVector(Idxs);
         SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
       } else if (isMovL) {
-        Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
+        Constant *Zero = Context->getConstantInt(Type::Int32Ty, 0);
         Idxs.push_back(Zero);
         Idxs.push_back(Zero);
         Idxs.push_back(Zero);
         Idxs.push_back(Zero);
-        Value *ZeroV = ConstantVector::get(Idxs);
+        Value *ZeroV = Context->getConstantVector(Idxs);
 
         Idxs.clear(); 
-        Idxs.push_back(ConstantInt::get(Type::Int32Ty, 4));
-        Idxs.push_back(ConstantInt::get(Type::Int32Ty, 5));
-        Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
-        Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3));
-        Value *Mask = ConstantVector::get(Idxs);
+        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 4));
+        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 5));
+        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
+        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 3));
+        Value *Mask = Context->getConstantVector(Idxs);
         SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
       } else if (isMovSD ||
                  isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
         Value *Op1 = CI->getOperand(2);
         if (isMovSD) {
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
         } else if (isUnpckhPD || isPunpckhQPD) {
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1));
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 3));
         } else {
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-          Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 0));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
         }
-        Value *Mask = ConstantVector::get(Idxs);
+        Value *Mask = Context->getConstantVector(Idxs);
         SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
       } else if (isShufPD) {
         Value *Op1 = CI->getOperand(2);
         unsigned MaskVal = cast<ConstantInt>(CI->getOperand(3))->getZExtValue();
-        Idxs.push_back(ConstantInt::get(Type::Int32Ty, MaskVal & 1));
-        Idxs.push_back(ConstantInt::get(Type::Int32Ty, ((MaskVal >> 1) & 1)+2));
-        Value *Mask = ConstantVector::get(Idxs);
+        Idxs.push_back(Context->getConstantInt(Type::Int32Ty, MaskVal & 1));
+        Idxs.push_back(Context->getConstantInt(Type::Int32Ty,
+                                               ((MaskVal >> 1) & 1)+2));
+        Value *Mask = Context->getConstantVector(Idxs);
         SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
       }
 
@@ -369,8 +390,8 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
     //  Handle any uses of the old CallInst.
     if (!CI->use_empty()) {
       //  Check for sign extend parameter attributes on the return values.
-      bool SrcSExt = NewFn->getParamAttrs().paramHasAttr(0, ParamAttr::SExt);
-      bool DestSExt = F->getParamAttrs().paramHasAttr(0, ParamAttr::SExt);
+      bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
+      bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
       
       //  Construct an appropriate cast from the new return type to the old.
       CastInst *RetCast = CastInst::Create(
@@ -414,31 +435,3 @@ void llvm::UpgradeCallsToIntrinsic(Function* F) {
     }
   }
 }
-
-/// This is an auto-upgrade hook for mutiple-value return statements.
-/// This function auto-upgrades all such return statements in the given
-/// function to use aggregate return values built with insertvalue
-/// instructions.
-void llvm::UpgradeMultipleReturnValues(Function *CurrentFunction) {
-  const Type *ReturnType = CurrentFunction->getReturnType();
-  for (Function::iterator I = CurrentFunction->begin(),
-       E = CurrentFunction->end(); I != E; ++I) {
-    BasicBlock *BB = I;
-    if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
-      unsigned NumVals = RI->getNumOperands();
-      if (NumVals > 1 ||
-          (isa<StructType>(ReturnType) &&
-           (NumVals == 0 || RI->getOperand(0)->getType() != ReturnType))) {
-        std::vector<const Type *> Types(NumVals);
-        for (unsigned i = 0; i != NumVals; ++i)
-          Types[i] = RI->getOperand(i)->getType();
-        const Type *ReturnType = StructType::get(Types);
-        Value *RV = UndefValue::get(ReturnType);
-        for (unsigned i = 0; i != NumVals; ++i)
-          RV = InsertValueInst::Create(RV, RI->getOperand(i), i, "mrv", RI);
-        ReturnInst::Create(RV, RI);
-        RI->eraseFromParent();
-      }
-    }
-  }
-}