[PATCH] D100496: Add emitWcsLen and getInt[16|32]PtrTy helper functions

Anjan Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 14 11:16:59 PDT 2021


anjankgk created this revision.
anjankgk added reviewers: Whitney, etiotto, hiraditya, efriedma.
anjankgk requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Add implementations for generic functions of emitWcsLen and getInt16PtrTy/getInt32PtrTy on the similar lines to the existing helper functions of different types.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100496

Files:
  llvm/include/llvm/IR/IRBuilder.h
  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 = B.getInt16PtrTy();
+    break;
+  case 4:
+    PtrTy = B.getInt32PtrTy();
+    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.
Index: llvm/include/llvm/IR/IRBuilder.h
===================================================================
--- llvm/include/llvm/IR/IRBuilder.h
+++ llvm/include/llvm/IR/IRBuilder.h
@@ -562,6 +562,16 @@
     return Type::getInt8PtrTy(Context, AddrSpace);
   }
 
+  /// Fetch the type representing a pointer to an 16-bit integer value.
+  PointerType *getInt16PtrTy(unsigned AddrSpace = 0) {
+    return Type::getInt16PtrTy(Context, AddrSpace);
+  }
+
+  /// Fetch the type representing a pointer to an 32-bit integer value.
+  PointerType *getInt32PtrTy(unsigned AddrSpace = 0) {
+    return Type::getInt32PtrTy(Context, AddrSpace);
+  }
+
   /// Fetch the type representing a pointer to an integer value.
   IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
     return DL.getIntPtrType(Context, AddrSpace);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100496.337493.patch
Type: text/x-patch
Size: 2975 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210414/2e525d8b/attachment.bin>


More information about the llvm-commits mailing list