Answer to Philip Reames comments
[oota-llvm.git] / lib / Transforms / Utils / BuildLibCalls.cpp
index 8b126a4d9c4c029ae3330b47c65cd3848e5bd053..be00b6956199db698fbf1117829026b460f08797 100644 (file)
@@ -27,7 +27,8 @@ using namespace llvm;
 
 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
-  return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
+  unsigned AS = V->getType()->getPointerAddressSpace();
+  return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
 }
 
 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
@@ -35,10 +36,9 @@ Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
                         const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::strlen))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[2];
   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
@@ -53,18 +53,8 @@ Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
                                             B.getInt8PtrTy(),
                                             NULL);
   CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
-  if (Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
+  if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
 
   return CI;
 }
@@ -75,10 +65,9 @@ Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
 Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
                          const DataLayout *TD, const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::strnlen))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[2];
   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
@@ -94,18 +83,8 @@ Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
                                              TD->getIntPtrType(Context),
                                              NULL);
   CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
-  if (Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
+  if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
 
   return CI;
 }
@@ -116,10 +95,9 @@ Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
                         const DataLayout *TD, const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::strchr))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
   AttributeSet AS =
     AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -133,19 +111,8 @@ Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
                                             I8Ptr, I8Ptr, I32Ty, NULL);
   CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
                                ConstantInt::get(I32Ty, C), "strchr");
-  if (Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
   return CI;
 }
 
@@ -154,10 +121,9 @@ Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
                          IRBuilder<> &B, const DataLayout *TD,
                          const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::strncmp))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[3];
   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
@@ -176,18 +142,8 @@ Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
   CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
                                CastToCStr(Ptr2, B), Len, "strncmp");
 
-  if (Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
+  if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
 
   return CI;
 }
@@ -198,10 +154,9 @@ Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
                         const DataLayout *TD, const TargetLibraryInfo *TLI,
                         StringRef Name) {
   if (!TLI->has(LibFunc::strcpy))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[2];
   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -212,19 +167,8 @@ Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
                                          I8Ptr, I8Ptr, I8Ptr, NULL);
   CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
                                Name);
-  if (Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
   return CI;
 }
 
@@ -234,10 +178,9 @@ Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
                          IRBuilder<> &B, const DataLayout *TD,
                          const TargetLibraryInfo *TLI, StringRef Name) {
   if (!TLI->has(LibFunc::strncpy))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[2];
   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -250,19 +193,8 @@ Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
                                           Len->getType(), NULL);
   CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
                                Len, "strncpy");
-  if (Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
   return CI;
 }
 
@@ -273,10 +205,9 @@ Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
                            IRBuilder<> &B, const DataLayout *TD,
                            const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::memcpy_chk))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS;
   AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
                          Attribute::NoUnwind);
@@ -291,19 +222,8 @@ Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
   Dst = CastToCStr(Dst, B);
   Src = CastToCStr(Src, B);
   CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
-  if (Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
   return CI;
 }
 
@@ -313,10 +233,9 @@ Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
                         Value *Len, IRBuilder<> &B, const DataLayout *TD,
                         const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::memchr))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS;
   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
   AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -331,18 +250,8 @@ Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
                                          NULL);
   CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
 
-  if (Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
+  if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
 
   return CI;
 }
@@ -352,10 +261,9 @@ Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
                         Value *Len, IRBuilder<> &B, const DataLayout *TD,
                         const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::memcmp))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[3];
   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
@@ -373,18 +281,8 @@ Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
   CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
                                Len, "memcmp");
 
-  if (Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
+  if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
 
   return CI;
 }
@@ -413,24 +311,13 @@ Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
   SmallString<20> NameBuffer;
   AppendTypeSuffix(Op, Name, NameBuffer);   
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
                                          Op->getType(), NULL);
   CallInst *CI = B.CreateCall(Callee, Op, Name);
   CI->setAttributes(Attrs);
-  if (Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
+  if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
 
   return CI;
 }
@@ -445,24 +332,13 @@ Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
   SmallString<20> NameBuffer;
   AppendTypeSuffix(Op1, Name, NameBuffer);   
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
                                          Op1->getType(), Op2->getType(), NULL);
   CallInst *CI = B.CreateCall2(Callee, Op1, Op2, Name);
   CI->setAttributes(Attrs);
-  if (Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
+  if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
 
   return CI;
 }
@@ -472,10 +348,9 @@ Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
                          const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::putchar))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
                                           B.getInt32Ty(), NULL);
   CallInst *CI = B.CreateCall(PutChar,
@@ -485,19 +360,8 @@ Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
                               "chari"),
                               "putchar");
 
-  if (Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
   return CI;
 }
 
@@ -506,10 +370,9 @@ Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
                       const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::puts))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[2];
   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -521,19 +384,8 @@ Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
                                        B.getInt8PtrTy(),
                                        NULL);
   CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
-  if (Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) {
-    CallingConv::ID CC = F->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      F->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
   return CI;
 }
 
@@ -542,10 +394,9 @@ Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
                        const DataLayout *TD, const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::fputc))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[2];
   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -566,19 +417,8 @@ Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
                          "chari");
   CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
 
-  if (Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) {
-    CallingConv::ID CC = Fn->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      Fn->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
+    CI->setCallingConv(Fn->getCallingConv());
   return CI;
 }
 
@@ -587,10 +427,9 @@ Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
                        const DataLayout *TD, const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::fputs))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[3];
   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
@@ -610,19 +449,8 @@ Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
                                File->getType(), NULL);
   CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
 
-  if (Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) {
-    CallingConv::ID CC = Fn->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      Fn->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
+    CI->setCallingConv(Fn->getCallingConv());
   return CI;
 }
 
@@ -632,10 +460,9 @@ Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
                         IRBuilder<> &B, const DataLayout *TD,
                         const TargetLibraryInfo *TLI) {
   if (!TLI->has(LibFunc::fwrite))
-    return 0;
+    return nullptr;
 
-  Function *CallerF = B.GetInsertBlock()->getParent();
-  Module *M = CallerF->getParent();
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
   AttributeSet AS[3];
   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
   AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
@@ -661,19 +488,8 @@ Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
   CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
                         ConstantInt::get(TD->getIntPtrType(Context), 1), File);
 
-  if (Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) {
-    CallingConv::ID CC = Fn->getCallingConv();
-    CallingConv::ID CallerCC = CallerF->getCallingConv();
-    if (CC == CallingConv::C && CallingConv::isARMTargetCC(CallerCC)) {
-      // If caller is using ARM target specific CC such as AAPCS-VFP,
-      // make sure the call uses it or it would introduce a calling
-      // convention mismatch.
-      CI->setCallingConv(CallerCC);
-      Fn->setCallingConv(CallerCC);
-    } else
-      CI->setCallingConv(CC);
-  }
-
+  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
+    CI->setCallingConv(Fn->getCallingConv());
   return CI;
 }