summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/llvm/IR/CallingConv.h4
-rw-r--r--lib/IR/Function.cpp8
-rw-r--r--lib/Transforms/Utils/BuildLibCalls.cpp281
-rw-r--r--test/Transforms/InstCombine/simplify-libcalls-aapcs.ll23
4 files changed, 268 insertions, 48 deletions
diff --git a/include/llvm/IR/CallingConv.h b/include/llvm/IR/CallingConv.h
index af44e8a30c..0c305439dd 100644
--- a/include/llvm/IR/CallingConv.h
+++ b/include/llvm/IR/CallingConv.h
@@ -145,6 +145,10 @@ namespace CallingConv {
X86_CDeclMethod = 80
};
+
+ /// isARMTargetCC - Return true if the specific calling convention is one of
+ /// ARM target specific calling convention.
+ bool isARMTargetCC(ID id);
} // End CallingConv namespace
} // End llvm namespace
diff --git a/lib/IR/Function.cpp b/lib/IR/Function.cpp
index 865970c469..7e45e6270a 100644
--- a/lib/IR/Function.cpp
+++ b/lib/IR/Function.cpp
@@ -779,3 +779,11 @@ void Function::setPrefixData(Constant *PrefixData) {
}
setValueSubclassData(SCData);
}
+
+
+/// isARMTargetCC - Return true if the specific calling convention is one of
+/// ARM target specific calling convention.
+/// There isn't a CallingConv.cpp so we are adding this utility routine here.
+bool CallingConv::isARMTargetCC(ID id) {
+ return id == ARM_APCS || id == ARM_AAPCS || id == ARM_AAPCS_VFP;
+}
diff --git a/lib/Transforms/Utils/BuildLibCalls.cpp b/lib/Transforms/Utils/BuildLibCalls.cpp
index 82384a1edf..8b126a4d9c 100644
--- a/lib/Transforms/Utils/BuildLibCalls.cpp
+++ b/lib/Transforms/Utils/BuildLibCalls.cpp
@@ -37,7 +37,8 @@ Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
if (!TLI->has(LibFunc::strlen))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[2];
AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
@@ -52,8 +53,18 @@ Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
B.getInt8PtrTy(),
NULL);
CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
- if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
return CI;
}
@@ -66,7 +77,8 @@ Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
if (!TLI->has(LibFunc::strnlen))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[2];
AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
@@ -82,8 +94,18 @@ Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
TD->getIntPtrType(Context),
NULL);
CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
- if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
return CI;
}
@@ -96,7 +118,8 @@ Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
if (!TLI->has(LibFunc::strchr))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
AttributeSet AS =
AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -110,8 +133,19 @@ 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 (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
+
return CI;
}
@@ -122,7 +156,8 @@ Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
if (!TLI->has(LibFunc::strncmp))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[3];
AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
@@ -141,8 +176,18 @@ Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
CastToCStr(Ptr2, B), Len, "strncmp");
- if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
return CI;
}
@@ -155,7 +200,8 @@ Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
if (!TLI->has(LibFunc::strcpy))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[2];
AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -166,8 +212,19 @@ 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 (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
+
return CI;
}
@@ -179,7 +236,8 @@ Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
if (!TLI->has(LibFunc::strncpy))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[2];
AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -192,8 +250,19 @@ 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 (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
+
return CI;
}
@@ -206,7 +275,8 @@ Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
if (!TLI->has(LibFunc::memcpy_chk))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS;
AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
Attribute::NoUnwind);
@@ -221,8 +291,19 @@ 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 (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
+
return CI;
}
@@ -234,7 +315,8 @@ Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
if (!TLI->has(LibFunc::memchr))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS;
Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -249,8 +331,18 @@ Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
NULL);
CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
- if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
return CI;
}
@@ -262,7 +354,8 @@ Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
if (!TLI->has(LibFunc::memcmp))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[3];
AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
@@ -280,8 +373,18 @@ Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
Len, "memcmp");
- if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
return CI;
}
@@ -310,13 +413,24 @@ Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
SmallString<20> NameBuffer;
AppendTypeSuffix(Op, Name, NameBuffer);
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Op->getType(), NULL);
CallInst *CI = B.CreateCall(Callee, Op, Name);
CI->setAttributes(Attrs);
- if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
return CI;
}
@@ -331,13 +445,24 @@ Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
SmallString<20> NameBuffer;
AppendTypeSuffix(Op1, Name, NameBuffer);
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->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 (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
return CI;
}
@@ -349,7 +474,8 @@ Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
if (!TLI->has(LibFunc::putchar))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
B.getInt32Ty(), NULL);
CallInst *CI = B.CreateCall(PutChar,
@@ -359,8 +485,19 @@ Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
"chari"),
"putchar");
- if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
+
return CI;
}
@@ -371,7 +508,8 @@ Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
if (!TLI->has(LibFunc::puts))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[2];
AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -383,8 +521,19 @@ Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
B.getInt8PtrTy(),
NULL);
CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
- if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
- CI->setCallingConv(F->getCallingConv());
+ 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);
+ }
+
return CI;
}
@@ -395,7 +544,8 @@ Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
if (!TLI->has(LibFunc::fputc))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[2];
AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
@@ -416,8 +566,19 @@ Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
"chari");
CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
- if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
- CI->setCallingConv(Fn->getCallingConv());
+ 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);
+ }
+
return CI;
}
@@ -428,7 +589,8 @@ Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
if (!TLI->has(LibFunc::fputs))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[3];
AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
@@ -448,8 +610,19 @@ Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
File->getType(), NULL);
CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
- if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
- CI->setCallingConv(Fn->getCallingConv());
+ 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);
+ }
+
return CI;
}
@@ -461,7 +634,8 @@ Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
if (!TLI->has(LibFunc::fwrite))
return 0;
- Module *M = B.GetInsertBlock()->getParent()->getParent();
+ Function *CallerF = B.GetInsertBlock()->getParent();
+ Module *M = CallerF->getParent();
AttributeSet AS[3];
AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
@@ -487,8 +661,19 @@ 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 (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
- CI->setCallingConv(Fn->getCallingConv());
+ 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);
+ }
+
return CI;
}
diff --git a/test/Transforms/InstCombine/simplify-libcalls-aapcs.ll b/test/Transforms/InstCombine/simplify-libcalls-aapcs.ll
new file mode 100644
index 0000000000..b2bd1b35de
--- /dev/null
+++ b/test/Transforms/InstCombine/simplify-libcalls-aapcs.ll
@@ -0,0 +1,23 @@
+; RUN: opt -S < %s -instcombine | FileCheck %s
+
+; When simplify-libcall change an intrinsic call to a call to a library
+; routine, it needs to set the proper calling convention for callers
+; which use ARM target specific calling conventions.
+; rdar://16261856
+
+target triple = "thumbv7-apple-ios7"
+
+; Function Attrs: nounwind ssp
+define arm_aapcs_vfpcc double @t(double %x) #0 {
+entry:
+; CHECK-LABEL: @t
+; CHECK: call arm_aapcs_vfpcc double @__exp10
+ %0 = call double @llvm.pow.f64(double 1.000000e+01, double %x)
+ ret double %0
+}
+
+; Function Attrs: nounwind readnone
+declare double @llvm.pow.f64(double, double) #1
+
+attributes #0 = { nounwind ssp }
+attributes #1 = { nounwind readnone }