ADDS{D|S}rr_Int and MULS{D|S}rr_Int are not commutable. The users of these intrinsics...
[oota-llvm.git] / lib / CodeGen / IntrinsicLowering.cpp
index 64162f8ea2d2f7d8f56c8906d260e72c7713196d..5b4de27b8a9db6ca2ad17016329d0788cbc2faa3 100644 (file)
@@ -34,22 +34,25 @@ static void EnsureFunctionExists(Module &M, const char *Name,
   M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
 }
 
-static void EnsureFPIntrinsicsExist(Module &M, Module::iterator I,
-                                 const char *FName,
-                                 const char *DName, const char *LDName) {
+static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
+                                    const char *FName,
+                                    const char *DName, const char *LDName) {
   // Insert definitions for all the floating point types.
-  switch((int)I->arg_begin()->getType()->getTypeID()) {
+  switch((int)Fn->arg_begin()->getType()->getTypeID()) {
   case Type::FloatTyID:
-    EnsureFunctionExists(M, FName, I->arg_begin(), I->arg_end(),
+    EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
                          Type::FloatTy);
+    break;
   case Type::DoubleTyID:
-    EnsureFunctionExists(M, DName, I->arg_begin(), I->arg_end(),
+    EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
                          Type::DoubleTy);
+    break;
   case Type::X86_FP80TyID:
   case Type::FP128TyID:
   case Type::PPC_FP128TyID:
-    EnsureFunctionExists(M, LDName, I->arg_begin(), I->arg_end(),
-                         I->arg_begin()->getType());
+    EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
+                         Fn->arg_begin()->getType());
+    break;
   }
 }
 
@@ -98,22 +101,19 @@ void IntrinsicLowering::AddPrototypes(Module &M) {
         EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
                              Type::VoidTy);
         break;
-      case Intrinsic::memcpy_i32:
-      case Intrinsic::memcpy_i64:
+      case Intrinsic::memcpy:
         M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty),
                               PointerType::getUnqual(Type::Int8Ty), 
                               PointerType::getUnqual(Type::Int8Ty), 
                               TD.getIntPtrType(), (Type *)0);
         break;
-      case Intrinsic::memmove_i32:
-      case Intrinsic::memmove_i64:
+      case Intrinsic::memmove:
         M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty),
                               PointerType::getUnqual(Type::Int8Ty), 
                               PointerType::getUnqual(Type::Int8Ty), 
                               TD.getIntPtrType(), (Type *)0);
         break;
-      case Intrinsic::memset_i32:
-      case Intrinsic::memset_i64:
+      case Intrinsic::memset:
         M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty),
                               PointerType::getUnqual(Type::Int8Ty), 
                               Type::Int32Ty, 
@@ -536,6 +536,8 @@ static Instruction *LowerPartSet(CallInst *CI) {
       Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
     } else if (ValBits < 32) {
       Lo = new TruncInst(Lo_pn, ValTy, "", entry);
+    } else {
+      Lo = Lo_pn;
     }
     // Determine if the replacement bits are larger than the number of bits we
     // are replacing and deal with it.
@@ -629,6 +631,28 @@ static Instruction *LowerPartSet(CallInst *CI) {
   return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
 }
 
+static void ReplaceFPIntrinsicWithCall(CallInst *CI, Constant *FCache,
+                                       Constant *DCache, Constant *LDCache,
+                                       const char *Fname, const char *Dname,
+                                       const char *LDname) {
+  switch (CI->getOperand(1)->getType()->getTypeID()) {
+  default: assert(0 && "Invalid type in intrinsic"); abort();
+  case Type::FloatTyID:
+    ReplaceCallWith(Fname, CI, CI->op_begin()+1, CI->op_end(),
+                  Type::FloatTy, FCache);
+    break;
+  case Type::DoubleTyID:
+    ReplaceCallWith(Dname, CI, CI->op_begin()+1, CI->op_end(),
+                  Type::DoubleTy, DCache);
+    break;
+  case Type::X86_FP80TyID:
+  case Type::FP128TyID:
+  case Type::PPC_FP128TyID:
+    ReplaceCallWith(LDname, CI, CI->op_begin()+1, CI->op_end(),
+                  CI->getOperand(1)->getType(), LDCache);
+    break;
+  }
+}
 
 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
   Function *Callee = CI->getCalledFunction();
@@ -762,8 +786,7 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
   case Intrinsic::var_annotation:
     break;   // Strip out annotate intrinsic
     
-  case Intrinsic::memcpy_i32:
-  case Intrinsic::memcpy_i64: {
+  case Intrinsic::memcpy: {
     static Constant *MemcpyFCache = 0;
     Value *Size = CI->getOperand(3);
     const Type *IntPtr = TD.getIntPtrType();
@@ -781,8 +804,7 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
                     MemcpyFCache);
     break;
   }
-  case Intrinsic::memmove_i32: 
-  case Intrinsic::memmove_i64: {
+  case Intrinsic::memmove: {
     static Constant *MemmoveFCache = 0;
     Value *Size = CI->getOperand(3);
     const Type *IntPtr = TD.getIntPtrType();
@@ -800,8 +822,7 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
                     MemmoveFCache);
     break;
   }
-  case Intrinsic::memset_i32:
-  case Intrinsic::memset_i64: {
+  case Intrinsic::memset: {
     static Constant *MemsetFCache = 0;
     Value *Size = CI->getOperand(3);
     const Type *IntPtr = TD.getIntPtrType();
@@ -821,164 +842,59 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
     break;
   }
   case Intrinsic::sqrt: {
-    static Constant *sqrtfFCache = 0;
     static Constant *sqrtFCache = 0;
+    static Constant *sqrtDCache = 0;
     static Constant *sqrtLDCache = 0;
-    switch (CI->getOperand(1)->getType()->getTypeID()) {
-    default: assert(0 && "Invalid type in sqrt"); abort();
-    case Type::FloatTyID:
-      ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::FloatTy, sqrtfFCache);
-      break;
-    case Type::DoubleTyID:
-      ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::DoubleTy, sqrtFCache);
-      break;
-    case Type::X86_FP80TyID:
-    case Type::FP128TyID:
-    case Type::PPC_FP128TyID:
-      ReplaceCallWith("sqrtl", CI, CI->op_begin()+1, CI->op_end(),
-                    CI->getOperand(1)->getType(), sqrtLDCache);
-      break;
-    }
+    ReplaceFPIntrinsicWithCall(CI, sqrtFCache, sqrtDCache, sqrtLDCache,
+                               "sqrtf", "sqrt", "sqrtl");
     break;
   }
   case Intrinsic::log: {
-    static Constant *logfFCache = 0;
     static Constant *logFCache = 0;
+    static Constant *logDCache = 0;
     static Constant *logLDCache = 0;
-    switch (CI->getOperand(1)->getType()->getTypeID()) {
-    default: assert(0 && "Invalid type in log"); abort();
-    case Type::FloatTyID:
-      ReplaceCallWith("logf", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::FloatTy, logfFCache);
-      break;
-    case Type::DoubleTyID:
-      ReplaceCallWith("log", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::DoubleTy, logFCache);
-      break;
-    case Type::X86_FP80TyID:
-    case Type::FP128TyID:
-    case Type::PPC_FP128TyID:
-      ReplaceCallWith("logl", CI, CI->op_begin()+1, CI->op_end(),
-                    CI->getOperand(1)->getType(), logLDCache);
-      break;
-    }
+    ReplaceFPIntrinsicWithCall(CI, logFCache, logDCache, logLDCache,
+                               "logf", "log", "logl");
     break;
   }
   case Intrinsic::log2: {
-    static Constant *log2fFCache = 0;
     static Constant *log2FCache = 0;
+    static Constant *log2DCache = 0;
     static Constant *log2LDCache = 0;
-    switch (CI->getOperand(1)->getType()->getTypeID()) {
-    default: assert(0 && "Invalid type in log2"); abort();
-    case Type::FloatTyID:
-      ReplaceCallWith("log2f", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::FloatTy, log2fFCache);
-      break;
-    case Type::DoubleTyID:
-      ReplaceCallWith("log2", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::DoubleTy, log2FCache);
-      break;
-    case Type::X86_FP80TyID:
-    case Type::FP128TyID:
-    case Type::PPC_FP128TyID:
-      ReplaceCallWith("log2l", CI, CI->op_begin()+1, CI->op_end(),
-                    CI->getOperand(1)->getType(), log2LDCache);
-      break;
-    }
+    ReplaceFPIntrinsicWithCall(CI, log2FCache, log2DCache, log2LDCache,
+                               "log2f", "log2", "log2l");
     break;
   }
   case Intrinsic::log10: {
-    static Constant *log10fFCache = 0;
     static Constant *log10FCache = 0;
+    static Constant *log10DCache = 0;
     static Constant *log10LDCache = 0;
-    switch (CI->getOperand(1)->getType()->getTypeID()) {
-    default: assert(0 && "Invalid type in log10"); abort();
-    case Type::FloatTyID:
-      ReplaceCallWith("log10f", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::FloatTy, log10fFCache);
-      break;
-    case Type::DoubleTyID:
-      ReplaceCallWith("log10", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::DoubleTy, log10FCache);
-      break;
-    case Type::X86_FP80TyID:
-    case Type::FP128TyID:
-    case Type::PPC_FP128TyID:
-      ReplaceCallWith("log10l", CI, CI->op_begin()+1, CI->op_end(),
-                    CI->getOperand(1)->getType(), log10LDCache);
-      break;
-    }
+    ReplaceFPIntrinsicWithCall(CI, log10FCache, log10DCache, log10LDCache,
+                               "log10f", "log10", "log10l");
     break;
   }
   case Intrinsic::exp: {
-    static Constant *expfFCache = 0;
     static Constant *expFCache = 0;
+    static Constant *expDCache = 0;
     static Constant *expLDCache = 0;
-    switch (CI->getOperand(1)->getType()->getTypeID()) {
-    default: assert(0 && "Invalid type in exp"); abort();
-    case Type::FloatTyID:
-      ReplaceCallWith("expf", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::FloatTy, expfFCache);
-      break;
-    case Type::DoubleTyID:
-      ReplaceCallWith("exp", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::DoubleTy, expFCache);
-      break;
-    case Type::X86_FP80TyID:
-    case Type::FP128TyID:
-    case Type::PPC_FP128TyID:
-      ReplaceCallWith("expl", CI, CI->op_begin()+1, CI->op_end(),
-                    CI->getOperand(1)->getType(), expLDCache);
-      break;
-    }
+    ReplaceFPIntrinsicWithCall(CI, expFCache, expDCache, expLDCache,
+                               "expf", "exp", "expl");
     break;
   }
   case Intrinsic::exp2: {
-    static Constant *exp2fFCache = 0;
     static Constant *exp2FCache = 0;
+    static Constant *exp2DCache = 0;
     static Constant *exp2LDCache = 0;
-    switch (CI->getOperand(1)->getType()->getTypeID()) {
-    default: assert(0 && "Invalid type in exp2"); abort();
-    case Type::FloatTyID:
-      ReplaceCallWith("exp2f", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::FloatTy, exp2fFCache);
-      break;
-    case Type::DoubleTyID:
-      ReplaceCallWith("exp2", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::DoubleTy, exp2FCache);
-      break;
-    case Type::X86_FP80TyID:
-    case Type::FP128TyID:
-    case Type::PPC_FP128TyID:
-      ReplaceCallWith("exp2l", CI, CI->op_begin()+1, CI->op_end(),
-                    CI->getOperand(1)->getType(), exp2LDCache);
-      break;
-    }
+    ReplaceFPIntrinsicWithCall(CI, exp2FCache, exp2DCache, exp2LDCache,
+                               "exp2f", "exp2", "exp2l");
     break;
   }
   case Intrinsic::pow: {
-    static Constant *powfFCache = 0;
     static Constant *powFCache = 0;
+    static Constant *powDCache = 0;
     static Constant *powLDCache = 0;
-    switch (CI->getOperand(1)->getType()->getTypeID()) {
-    default: assert(0 && "Invalid type in pow"); abort();
-    case Type::FloatTyID:
-      ReplaceCallWith("powf", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::FloatTy, powfFCache);
-      break;
-    case Type::DoubleTyID:
-      ReplaceCallWith("pow", CI, CI->op_begin()+1, CI->op_end(),
-                    Type::DoubleTy, powFCache);
-      break;
-    case Type::X86_FP80TyID:
-    case Type::FP128TyID:
-    case Type::PPC_FP128TyID:
-      ReplaceCallWith("powl", CI, CI->op_begin()+1, CI->op_end(),
-                    CI->getOperand(1)->getType(), powLDCache);
-      break;
-    }
+    ReplaceFPIntrinsicWithCall(CI, powFCache, powDCache, powLDCache,
+                               "powf", "pow", "powl");
     break;
   }
   case Intrinsic::flt_rounds: