+/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
+Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
+ IRBuilder<> &B, const DataLayout *TD,
+ const TargetLibraryInfo *TLI) {
+ if (!TLI->has(LibFunc::strncmp))
+ return 0;
+
+ Module *M = B.GetInsertBlock()->getParent()->getParent();
+ AttributeWithIndex AWI[3];
+ AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
+ AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
+ Attribute::AttrVal AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
+ AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
+ ArrayRef<Attribute::AttrVal>(AVs, 2));
+
+ LLVMContext &Context = B.GetInsertBlock()->getContext();
+ Value *StrNCmp = M->getOrInsertFunction("strncmp",
+ AttributeSet::get(M->getContext(),
+ AWI),
+ B.getInt32Ty(),
+ B.getInt8PtrTy(),
+ B.getInt8PtrTy(),
+ TD->getIntPtrType(Context), NULL);
+ 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());
+
+ return CI;
+}
+