LLVM IRs AST LLVM IR C Fortran Code
- Slides: 16
LLVM IRs AST LLVM IR C Fortran Code. Gen IR X 86 Backend Optimizer Ada Power PC Backend ARM Backend 7
Основные объекты LLVM IR LLVMContext - Instruction constants maps and predefined constants types maps and predefined types map of value handles list of global variables and aliases values and types sym-tabs list of functions Function - list of arguments list of basic blocks values sym-tab Basic. Block - list of uses result type Use - pointer to argument (Value) Module - - list of instructions 8 Constant Type
Типы Type* Derived. Type Integer. Type Composite. Type Struct. Type Array. Type Function. Type Opaque. Type Sequential. Type Vector. Type *Type has a list of it’s uses 9 Pointer. Type
Значения Value Argument User* Constant Basic. Block Instruction * User has a list of it’s uses 10
Константы Constant. Int Constant. FP Constant. Array Constant. Struct Constant. Vector Block. Address Constant. Expr Unary. Constant. Expr Binary. Constant. Expr Get. Element. Ptr. Constant. Expr Select. Constant. Expr Extract. Element. Constant. Expr Insert. Element. Constant. Expr Shuffle. Vector. Constant. Expr Extract. Value. Constant. Expr Insert. Value. Constant. Expr Compare. Constant. Expr 11 Global. Value Block. Address Global. Variable Global. Alias Function
Инструкции Instruction Store. Inst Get. Element. Ptr. Inst Select. Inst Binary. Operator Alloca. Inst Load. Inst VAArg. Inst Extract. Value. Inst Call. Inst Unary. Instruction Terminator. Inst Cmp. Inst _other_ Cast. Inst Trunc. Inst ZExt. Inst SExt. Inst FPTrunc. Inst FPExt. Inst UITo. FPInst SITo. FPInst FPTo. UIInst FPTo. SIInst Int. To. Ptr. Inst Ptr. To. Int. Inst Bit. Cast. Inst ICmp. Inst FCmp. Inst Return. Inst Branch. Inst Switch. Inst Indirect. Inst Invoke. Inst Unwind. Inst Unreachable. Inst 12
Специальные инструкции (intrinsic) Call. Inst Intrinsic. Inst Mem. Intrinsic EHException. Intrinsic EHSelect. Intrinsic Mem. Set. Inst Mem. Transfer. Inst Mem. Cpy. Inst Mem. Move. Inst 13 Dbg. Info. Intrinsic Dbg. Declare. Inst Dbg. Value. Inst
Пример IR int foo( int *a, int size) { int i, sum = 0; for( i=0; i<size; i++) sum += a[i]; return sum; define i 32 @foo(i 32* %a, i 32 %size) { ; <label>: 0 %1 = icmp sgt i 32 %size, 0 br i 1 %1, label %3 ; <label>: 1 ; preds = %1, %0 %i. 1 = phi i 32 [ 0, %0 ], [ %i. 2, %1 ] %sum. 1 = phi i 32 [ 0, %1 ], [ %4, %1 ] } %gep = getelementptr i 32* %a, i 32 %i. 1 %3 = load i 32* %gep %4 = add i 32 %3, %sum. 1 %i. 2 = add i 32 %i. 1, 1 %p = icmp eq i 32 %i. 2, %size br i 1 %p, label %2, label %1 ; <label>: 2 ; preds = %1, %0 %sum. 2 = phi i 32 [ 0, %0 ], [ %4, %1 ] ret i 32 %sum. 2 } 14
Приведения объектов IR (isa, cast, dyn_cast) Каждый класс реализует метод static inline bool classof( Value* val); template < class X, class Y > inline bool isa( Y *val); template < class X, class Y > inline X cast( Y *val); template < class X, class Y> inline X dyn_cast( Y *val); for ( Basic. Block: : iterator it = bb->begin(), …) { Instruction *inst = it; if ( isa< Branch. Inst >( inst) ) { // inst можно привести к Branch. Inst *br = cast< Branch. Inst >( inst); } if ( Invoke. Inst *invoke = dyn_cast< Invoke. Inst >( inst) ) { // invoke приведен к Invoke. Inst } } 15
QA 16