[PATCH] D100496: Add emitWcsLen helper function

Anjan Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 15 07:13:10 PDT 2021


anjankgk updated this revision to Diff 337756.
anjankgk retitled this revision from "Add emitWcsLen and getInt[16|32]PtrTy helper functions" to "Add emitWcsLen helper function".
anjankgk edited the summary of this revision.

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,31 @@
                      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);
+  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.337756.patch
Type: text/x-patch
Size: 2165 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210415/774b234a/attachment.bin>


More information about the llvm-commits mailing list