llvm.org GIT mirror llvm / 4fefaae
[LLVM-C] Improve Intrinsics Bindings Summary: Improve the intrinsic bindings with operations for - Retrieving and automatically inserting the declaration of an intrinsic by ID - Retrieving the name of a non-overloaded intrinsic by ID - Retrieving the name of an overloaded intrinsic by ID and overloaded parameter types Improve the echo test to copy non-overloaded intrinsics by ID. Reviewers: whitequark, deadalnix Reviewed By: whitequark Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D53626 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346195 91177308-0d34-0410-b5e6-96231b3b80d8 Robert Widmann 2 years ago
4 changed file(s) with 119 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
23812381 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn);
23822382
23832383 /**
2384 * Create or insert the declaration of an intrinsic. For overloaded intrinsics,
2385 * parameter types must be provided to uniquely identify an overload.
2386 *
2387 * @see llvm::Intrinsic::getDeclaration()
2388 */
2389 LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
2390 unsigned ID,
2391 LLVMTypeRef *ParamTypes,
2392 size_t ParamCount);
2393
2394 /**
2395 * Retrieves the type of an intrinsic. For overloaded intrinsics, parameter
2396 * types must be provided to uniquely identify an overload.
2397 *
2398 * @see llvm::Intrinsic::getType()
2399 */
2400 LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2401 LLVMTypeRef *ParamTypes, size_t ParamCount);
2402
2403 /**
2404 * Retrieves the name of an intrinsic.
2405 *
2406 * @see llvm::Intrinsic::getName()
2407 */
2408 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength);
2409
2410 /**
2411 * Copies the name of an overloaded intrinsic identified by a given list of
2412 * parameter types.
2413 *
2414 * Unlike LLVMIntrinsicGetName, the caller is responsible for freeing the
2415 * returned string.
2416 *
2417 * @see llvm::Intrinsic::getName()
2418 */
2419 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
2420 LLVMTypeRef *ParamTypes,
2421 size_t ParamCount,
2422 size_t *NameLength);
2423
2424 /**
2425 * Obtain if the intrinsic identified by the given ID is overloaded.
2426 *
2427 * @see llvm::Intrinsic::isOverloaded()
2428 */
2429 LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID);
2430
2431 /**
23842432 * Obtain the calling function of a function.
23852433 *
23862434 * The returned value corresponds to the LLVMCallConv enumeration.
22792279 return 0;
22802280 }
22812281
2282 static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {
2283 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2284 return llvm::Intrinsic::ID(ID);
2285 }
2286
2287 LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
2288 unsigned ID,
2289 LLVMTypeRef *ParamTypes,
2290 size_t ParamCount) {
2291 ArrayRef Tys(unwrap(ParamTypes), ParamCount);
2292 auto IID = llvm_map_to_intrinsic_id(ID);
2293 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2294 }
2295
2296 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2297 auto IID = llvm_map_to_intrinsic_id(ID);
2298 auto Str = llvm::Intrinsic::getName(IID);
2299 *NameLength = Str.size();
2300 return Str.data();
2301 }
2302
2303 LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2304 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2305 auto IID = llvm_map_to_intrinsic_id(ID);
2306 ArrayRef Tys(unwrap(ParamTypes), ParamCount);
2307 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2308 }
2309
2310 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
2311 LLVMTypeRef *ParamTypes,
2312 size_t ParamCount,
2313 size_t *NameLength) {
2314 auto IID = llvm_map_to_intrinsic_id(ID);
2315 ArrayRef Tys(unwrap(ParamTypes), ParamCount);
2316 auto Str = llvm::Intrinsic::getName(IID, Tys);
2317 *NameLength = Str.length();
2318 return strndup(Str.c_str(), Str.length());
2319 }
2320
2321 LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {
2322 auto IID = llvm_map_to_intrinsic_id(ID);
2323 return llvm::Intrinsic::isOverloaded(IID);
2324 }
2325
22822326 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
22832327 return unwrap(Fn)->getCallingConv();
22842328 }
169169 ret void, !dbg !7
170170 }
171171
172 declare i8* @llvm.stacksave()
173 declare void @llvm.stackrestore(i8*)
174 declare void @llvm.lifetime.start.p0i8(i64, i8*)
175 declare void @llvm.lifetime.end.p0i8(i64, i8*)
176
177 define void @test_intrinsics() {
178 entry:
179 %sp = call i8* @llvm.stacksave()
180 %x = alloca i32
181 %0 = bitcast i32* %x to i8*
182 call void @llvm.lifetime.start.p0i8(i64 4, i8* %0)
183 call void @llvm.lifetime.end.p0i8(i64 4, i8* %0)
184 call void @llvm.stackrestore(i8* %sp)
185 ret void
186 }
187
172188 !llvm.dbg.cu = !{!0, !2}
173189 !llvm.module.flags = !{!3}
174190
239239 // Try function
240240 if (LLVMIsAFunction(Cst)) {
241241 check_value_kind(Cst, LLVMFunctionValueKind);
242 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
242
243 LLVMValueRef Dst = nullptr;
244 // Try an intrinsic
245 unsigned ID = LLVMGetIntrinsicID(Cst);
246 if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) {
247 Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0);
248 } else {
249 // Try a normal function
250 Dst = LLVMGetNamedFunction(M, Name);
251 }
252
243253 if (Dst)
244254 return Dst;
245255 report_fatal_error("Could not find function");