Index: llvm/include/llvm/Transforms/Utils/BuildLibCalls.h =================================================================== --- llvm/include/llvm/Transforms/Utils/BuildLibCalls.h +++ llvm/include/llvm/Transforms/Utils/BuildLibCalls.h @@ -49,6 +49,12 @@ Value *emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI); + /// Emit a call to the wcslen function to the builder, for the specified + /// pointer. Ptr is required to be wchar pointer type, and the return value + /// has 'intptr_t' type. + Value *emitWcsLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, + const TargetLibraryInfo *TLI); + /// Emit a call to the strdup function to the builder, for the specified /// pointer. Ptr is required to be some pointer type, and the return value has /// 'i8*' type. Index: llvm/lib/Transforms/Utils/BuildLibCalls.cpp =================================================================== --- llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -1237,6 +1237,35 @@ B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI); } +Value *llvm::emitWcsLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, + const TargetLibraryInfo *TLI) { + LLVMContext &Context = B.GetInsertBlock()->getContext(); + + // know the wchar size before emiting the call to wcslen + Module &M = *B.GetInsertBlock()->getModule(); + unsigned WCharSize = TLI->getWCharSize(M); + + assert(Ptr->getType()->getPointerElementType()->isIntegerTy(WCharSize * 8) && + "Pointer parameter must be of WChar type"); + + Type *PtrTy = nullptr; + switch (WCharSize) { + default: + // We cannot perform this optimization without wchar_size metadata. + return nullptr; + case 2: + PtrTy = Type::getInt16PtrTy(Context); + break; + case 4: + PtrTy = Type::getInt32PtrTy(Context); + break; + } + + // emit the appropriate wcslen call + return emitLibCall(LibFunc_wcslen, DL.getIntPtrType(Context), PtrTy, Ptr, B, + TLI); +} + Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI) { return emitLibCall(LibFunc_strdup, B.getInt8PtrTy(), B.getInt8PtrTy(),