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
This commit is contained in:
Amy Huang 2021-03-10 16:36:07 -08:00
parent 264ff539f3
commit f5352dd9da
2 changed files with 78 additions and 0 deletions

View File

@ -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.

View File

@ -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);
}