What is LLVM C ++ API

I found it hard to understand the LLVM C ++ API. Would you like to ask? Is there any connection between LLVM C ++ API and LLVM IR? Also, I want to know how to use the LLVM C ++ API?

+7
source share
2 answers

To simplify (significantly), LLVM is a C ++ library for writing compilers. Its C ++ API is a front-end user that the library uses to implement its compiler.

There, the degree of symmetry between the LLVM IR and the LLVM part of the C ++ API is the part used to create the IR. A very good resource for perceiving this symmetry is http://llvm.org/demo/ . For example, you can compile this C code:

int factorial(int X) { if (X == 0) return 1; return X*factorial(X-1); } 

In LLVM IR:

 define i32 @factorial(i32 %X) nounwind uwtable readnone { %1 = icmp eq i32 %X, 0 br i1 %1, label %tailrecurse._crit_edge, label %tailrecurse tailrecurse: ; preds = %tailrecurse, %0 %X.tr2 = phi i32 [ %2, %tailrecurse ], [ %X, %0 ] %accumulator.tr1 = phi i32 [ %3, %tailrecurse ], [ 1, %0 ] %2 = add nsw i32 %X.tr2, -1 %3 = mul nsw i32 %X.tr2, %accumulator.tr1 %4 = icmp eq i32 %2, 0 br i1 %4, label %tailrecurse._crit_edge, label %tailrecurse tailrecurse._crit_edge: ; preds = %tailrecurse, %0 %accumulator.tr.lcssa = phi i32 [ 1, %0 ], [ %3, %tailrecurse ] ret i32 %accumulator.tr.lcssa } 

As for C ++ API calls (I will not embed it here because the output is long, but you can try it yourself). After completing this, you will see, for example, the icmp instruction from the IR code made as:

 ICmpInst* int1_5 = new ICmpInst(*label_4, ICmpInst::ICMP_EQ, int32_X, const_int32_1, ""); 

ICmpInst is a class that is part of the C ++ API used to create icmp instructions. A good reference for the C ++ API is the Programmer's Guide .

+12
source

You can use the CPP backend ( llc -march=cpp ) to find a mapping from any given IR to the C ++ API.

+2
source

All Articles