[PATCH] D100496: Add emitWcsLen helper function
Anjan Kumar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 19 07:07:41 PDT 2021
anjankgk updated this revision to Diff 338511.
anjankgk added a comment.
Add an assert to check the pointer param
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D100496/new/
https://reviews.llvm.org/D100496
Files:
llvm/include/llvm/Transforms/Utils/BuildLibCalls.h
llvm/lib/Transforms/Utils/BuildLibCalls.cpp
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(),
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100496.338511.patch
Type: text/x-patch
Size: 2304 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210419/8c88d9ba/attachment-0001.bin>
More information about the llvm-commits
mailing list