LLVMContext-ification.
[oota-llvm.git] / lib / VMCore / AutoUpgrade.cpp
index bed8fee8bbeb2a38966efe7cae1f14837416377e..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();
 
@@ -39,6 +42,44 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
   Module *M = F->getParent();
   switch (Name[5]) {
   default: break;
+  case 'a':
+    // 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) +
+                   ".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)
+                   + ".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)
+                   + ".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;
+      }
+    }
+    break;
   case 'b':
     //  This upgrades the name of the llvm.bswap intrinsic function to only use 
     //  a single type name for overloading. We only care about the old format
@@ -124,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.
@@ -148,8 +190,16 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
                                                     VT,
                                                     (Type *)0));
       return true;
-    } else if (Name.compare(5,16,"x86.sse2.movl.dq",16) == 0) {
-      // Calls to this intrinsic are transformed into ShuffleVector's.
+    } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
+               Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
+               Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
+               Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
+               Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
+               Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
+               Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
+               Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
+               Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
+      // Calls to these intrinsics are transformed into ShuffleVector's.
       NewFn = 0;
       return true;
     }
@@ -171,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;
 }
 
@@ -181,26 +231,98 @@ 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) {
-    if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0) {
+    bool isLoadH = false, isLoadL = false, isMovL = false;
+    bool isMovSD = false, isShufPD = false;
+    bool isUnpckhPD = false, isUnpcklPD = false;
+    bool isPunpckhQPD = false, isPunpcklQPD = false;
+    if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadh.pd") == 0)
+      isLoadH = true;
+    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadl.pd") == 0)
+      isLoadL = true;
+    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0)
+      isMovL = true;
+    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movs.d") == 0)
+      isMovSD = true;
+    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.shuf.pd") == 0)
+      isShufPD = true;
+    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckh.pd") == 0)
+      isUnpckhPD = true;
+    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckl.pd") == 0)
+      isUnpcklPD = true;
+    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.punpckh.qdq") == 0)
+      isPunpckhQPD = true;
+    else if (strcmp(F->getNameStart(), "llvm.x86.sse2.punpckl.qdq") == 0)
+      isPunpcklQPD = true;
+
+    if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
+        isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
       std::vector<Constant*> Idxs;
-      Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
-      Idxs.push_back(Zero);
-      Idxs.push_back(Zero);
-      Idxs.push_back(Zero);
-      Idxs.push_back(Zero);
-      Value *ZeroV = ConstantVector::get(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);
-      ShuffleVectorInst *SI = new ShuffleVectorInst(ZeroV, CI->getOperand(1),
-                                                    Mask, "upgraded", CI);
+      Value *Op0 = CI->getOperand(1);
+      ShuffleVectorInst *SI = NULL;
+      if (isLoadH || isLoadL) {
+        Value *Op1 = Context->getUndef(Op0->getType());
+        Value *Addr = new BitCastInst(CI->getOperand(2), 
+                                  Context->getPointerTypeUnqual(Type::DoubleTy),
+                                      "upgraded.", CI);
+        Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
+        Value *Idx = Context->getConstantInt(Type::Int32Ty, 0);
+        Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
+
+        if (isLoadH) {
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 0));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
+        } else {
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
+        }
+        Value *Mask = Context->getConstantVector(Idxs);
+        SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
+      } else if (isMovL) {
+        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 = Context->getConstantVector(Idxs);
+
+        Idxs.clear(); 
+        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(Context->getConstantInt(Type::Int32Ty, 2));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
+        } else if (isUnpckhPD || isPunpckhQPD) {
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 3));
+        } else {
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 0));
+          Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
+        }
+        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(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);
+      }
+
+      assert(SI && "Unexpected!");
 
       // Handle any uses of the old CallInst.
       if (!CI->use_empty())
@@ -233,7 +355,7 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
     // Cast the second parameter to the correct type.
     BitCastInst *BC = new BitCastInst(CI->getOperand(2), 
                                       NewFn->getFunctionType()->getParamType(1),
-                                      "upgraded", CI);
+                                      "upgraded.", CI);
     Operands[1] = BC;
     
     //  Construct a new CallInst
@@ -268,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(
@@ -313,4 +435,3 @@ void llvm::UpgradeCallsToIntrinsic(Function* F) {
     }
   }
 }
-