From: Erik Pilkington Date: Thu, 23 Feb 2017 21:08:08 +0000 (+0000) Subject: [ObjC][CodeGen] CodeGen support for @available. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=11e7407389c4b2a6f51cb8983972b591573a4645;p=clang [ObjC][CodeGen] CodeGen support for @available. CodeGens uses of @available into calls to the compiler-rt function __isOSVersionAtLeast. This commit is part of a feature that I proposed here: http://lists.llvm.org/pipermail/cfe-dev/2016-July/049851.html Differential revision: https://reviews.llvm.org/D27827 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@296015 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index 12b14bea0e..63031d4464 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -300,6 +300,24 @@ public: return V; } + Value *VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) { + VersionTuple Version = E->getVersion(); + + // If we're checking for a platform older than our minimum deployment + // target, we can fold the check away. + if (Version <= CGF.CGM.getTarget().getPlatformMinVersion()) + return llvm::ConstantInt::get(Builder.getInt1Ty(), 1); + + Optional Min = Version.getMinor(), SMin = Version.getSubminor(); + llvm::Value *Args[] = { + llvm::ConstantInt::get(CGF.CGM.Int32Ty, Version.getMajor()), + llvm::ConstantInt::get(CGF.CGM.Int32Ty, Min ? *Min : 0), + llvm::ConstantInt::get(CGF.CGM.Int32Ty, SMin ? *SMin : 0), + }; + + return CGF.EmitBuiltinAvailable(Args); + } + Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); Value *VisitConvertVectorExpr(ConvertVectorExpr *E); diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp index 913a2ea72e..adeb6d1839 100644 --- a/lib/CodeGen/CGObjC.cpp +++ b/lib/CodeGen/CGObjC.cpp @@ -3399,5 +3399,21 @@ CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { return Val; } +llvm::Value * +CodeGenFunction::EmitBuiltinAvailable(ArrayRef Args) { + assert(Args.size() == 3 && "Expected 3 argument here!"); + + if (!CGM.IsOSVersionAtLeastFn) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int32Ty, {Int32Ty, Int32Ty, Int32Ty}, false); + CGM.IsOSVersionAtLeastFn = + CGM.CreateRuntimeFunction(FTy, "__isOSVersionAtLeast"); + } + + llvm::Value *CallRes = + EmitNounwindRuntimeCall(CGM.IsOSVersionAtLeastFn, Args); + + return Builder.CreateICmpNE(CallRes, llvm::Constant::getNullValue(Int32Ty)); +} CGObjCRuntime::~CGObjCRuntime() {} diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 252561f488..3bb80887a4 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -3169,6 +3169,8 @@ private: public: llvm::Value *EmitMSVCBuiltinExpr(MSVCIntrin BuiltinID, const CallExpr *E); + llvm::Value *EmitBuiltinAvailable(ArrayRef Args); + llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E); llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E); diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index 465d0b4bb5..8faebe1ea7 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -546,6 +546,10 @@ public: return *ObjCData; } + // Version checking function, used to implement ObjC's @available: + // i32 @__isOSVersionAtLeast(i32, i32, i32) + llvm::Constant *IsOSVersionAtLeastFn = nullptr; + InstrProfStats &getPGOStats() { return PGOStats; } llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); } diff --git a/test/CodeGenObjC/availability-check.m b/test/CodeGenObjC/availability-check.m new file mode 100644 index 0000000000..71c5ff77c9 --- /dev/null +++ b/test/CodeGenObjC/availability-check.m @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -triple x86_64-apple-macosx10.11 -emit-llvm -o - %s | FileCheck %s + +void use_at_available() { + // CHECK: call i32 @__isOSVersionAtLeast(i32 10, i32 12, i32 0) + // CHECK-NEXT: icmp ne + if (__builtin_available(macos 10.12, *)) + ; + + // CHECK: call i32 @__isOSVersionAtLeast(i32 10, i32 12, i32 0) + // CHECK-NEXT: icmp ne + if (@available(macos 10.12, *)) + ; + + // CHECK: call i32 @__isOSVersionAtLeast(i32 10, i32 12, i32 42) + // CHECK-NEXT: icmp ne + if (__builtin_available(ios 10, macos 10.12.42, *)) + ; + + // CHECK-NOT: call i32 @__isOSVersionAtLeast + // CHECK: br i1 true + if (__builtin_available(ios 10, *)) + ; + + // This check should be folded: our deployment target is 10.11. + // CHECK-NOT: call i32 @__isOSVersionAtLeast + // CHECK: br i1 true + if (__builtin_available(macos 10.11, *)) + ; +} + +// CHECK: declare i32 @__isOSVersionAtLeast(i32, i32, i32)