[clang] f5352dd - Emit inline implementation of __builtin__wmemchr on MSVCRT platforms.
Amy Huang via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 15 15:32:48 PDT 2021
Author: Amy Huang
Date: 2021-03-15T15:30:55-07:00
New Revision: f5352dd9dab1ee8a2ae19a3fca3111c8b5de8ce2
URL: https://github.com/llvm/llvm-project/commit/f5352dd9dab1ee8a2ae19a3fca3111c8b5de8ce2
DIFF: https://github.com/llvm/llvm-project/commit/f5352dd9dab1ee8a2ae19a3fca3111c8b5de8ce2.diff
LOG: Emit inline implementation of __builtin__wmemchr on MSVCRT platforms.
The MSVC runtime library doesn't have a definition for wmemchr,
so provide an inline implementation.
Differential Revision: https://reviews.llvm.org/D98472
Added:
clang/test/CodeGen/wmemchr.c
Modified:
clang/lib/CodeGen/CGBuiltin.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 9827311e07d8..e5778c0c78f7 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3380,6 +3380,52 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
return RValue::get(Dest.getPointer());
}
+ case Builtin::BI__builtin_wmemchr: {
+ // The MSVC runtime library does not provide a definition of wmemchr, so we
+ // need an inline implementation.
+ if (!getTarget().getTriple().isOSMSVCRT())
+ break;
+
+ llvm::Type *WCharTy = ConvertType(getContext().WCharTy);
+ Value *Str = EmitScalarExpr(E->getArg(0));
+ Value *Chr = EmitScalarExpr(E->getArg(1));
+ Value *Size = EmitScalarExpr(E->getArg(2));
+
+ BasicBlock *Entry = Builder.GetInsertBlock();
+ BasicBlock *CmpEq = createBasicBlock("wmemchr.eq");
+ BasicBlock *Next = createBasicBlock("wmemchr.next");
+ BasicBlock *Exit = createBasicBlock("wmemchr.exit");
+ Value *SizeEq0 = Builder.CreateICmpEQ(Size, ConstantInt::get(SizeTy, 0));
+ Builder.CreateCondBr(SizeEq0, Exit, CmpEq);
+
+ EmitBlock(CmpEq);
+ PHINode *StrPhi = Builder.CreatePHI(Str->getType(), 2);
+ StrPhi->addIncoming(Str, Entry);
+ PHINode *SizePhi = Builder.CreatePHI(SizeTy, 2);
+ SizePhi->addIncoming(Size, Entry);
+ CharUnits WCharAlign =
+ getContext().getTypeAlignInChars(getContext().WCharTy);
+ Value *StrCh = Builder.CreateAlignedLoad(WCharTy, StrPhi, WCharAlign);
+ Value *FoundChr = Builder.CreateConstInBoundsGEP1_32(WCharTy, StrPhi, 0);
+ Value *StrEqChr = Builder.CreateICmpEQ(StrCh, Chr);
+ Builder.CreateCondBr(StrEqChr, Exit, Next);
+
+ EmitBlock(Next);
+ Value *NextStr = Builder.CreateConstInBoundsGEP1_32(WCharTy, StrPhi, 1);
+ Value *NextSize = Builder.CreateSub(SizePhi, ConstantInt::get(SizeTy, 1));
+ Value *NextSizeEq0 =
+ Builder.CreateICmpEQ(NextSize, ConstantInt::get(SizeTy, 0));
+ Builder.CreateCondBr(NextSizeEq0, Exit, CmpEq);
+ StrPhi->addIncoming(NextStr, Next);
+ SizePhi->addIncoming(NextSize, Next);
+
+ EmitBlock(Exit);
+ PHINode *Ret = Builder.CreatePHI(Str->getType(), 3);
+ Ret->addIncoming(llvm::Constant::getNullValue(Str->getType()), Entry);
+ Ret->addIncoming(llvm::Constant::getNullValue(Str->getType()), Next);
+ Ret->addIncoming(FoundChr, CmpEq);
+ return RValue::get(Ret);
+ }
case Builtin::BI__builtin_wmemcmp: {
// The MSVC runtime library does not provide a definition of wmemcmp, so we
// need an inline implementation.
diff --git a/clang/test/CodeGen/wmemchr.c b/clang/test/CodeGen/wmemchr.c
new file mode 100644
index 000000000000..3ff3fba34043
--- /dev/null
+++ b/clang/test/CodeGen/wmemchr.c
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 %s -triple x86_64-pc-win32 -emit-llvm -o - | FileCheck %s
+
+typedef __SIZE_TYPE__ size_t;
+typedef __WCHAR_TYPE__ wchar_t;
+
+const wchar_t *wmemchr_test(const wchar_t *s, const wchar_t c, size_t n) {
+ // CHECK-LABEL: define dso_local i16* @wmemchr_test
+ // CHECK: [[S:%.*]] = load
+ // CHECK: [[C:%.*]] = load
+ // CHECK: [[N:%.*]] = load
+ // CHECK: [[N0:%.*]] = icmp eq i64 [[N]], 0
+ // CHECK: br i1 [[N0]], label %[[EXIT:.*]], label %[[EQ:.*]]
+
+ // CHECK: [[EQ]]:
+ // CHECK: [[SP:%.*]] = phi i16* [ [[S]], %[[ENTRY:.*]] ], [ [[SN:.*]], %[[NEXT:.*]] ]
+ // CHECK: [[NP:%.*]] = phi i64 [ [[N]], %[[ENTRY]] ], [ [[NN:.*]], %[[NEXT]] ]
+ // CHECK: [[SL:%.*]] = load i16, i16* [[SP]], align 2
+ // CHECK: [[RES:%.*]] = getelementptr inbounds i16, i16* [[SP]], i32 0
+ // CHECK: [[CMPEQ:%.*]] = icmp eq i16 [[SL]], [[C]]
+ // CHECK: br i1 [[CMPEQ]], label %[[EXIT]], label %[[LT:.*]]
+
+ // CHECK: [[NEXT]]:
+ // CHECK: [[SN]] = getelementptr inbounds i16, i16* [[SP]], i32 1
+ // CHECK: [[NN]] = sub i64 [[NP]], 1
+ // CHECK: [[NN0:%.*]] = icmp eq i64 [[NN]], 0
+ // CHECK: br i1 [[NN0]], label %[[EXIT]], label %[[EQ]]
+ //
+ // CHECK: [[EXIT]]:
+ // CHECK: [[RV:%.*]] = phi i16* [ null, %[[ENTRY]] ], [ null, %[[NEXT]] ], [ [[RES]], %[[EQ]] ]
+ // CHECK: ret i16* [[RV]]
+ return __builtin_wmemchr(s, c, n);
+}
More information about the cfe-commits
mailing list