zhengjian zhang
2009-May-12 07:03 UTC
[LLVMdev] How distinguish Catch all llvm-IR from other catch type ?
Hi, catch_all.cpp: 1 int main() 2 { 3 try { 4 throw 34; 5 } 6 catch (...) {} 7 } llvm-gcc -O3 -S -emit-llvm catch_all.cpp -o catch_all.ll: 1 ; ModuleID = 'catch_all.cpp' 2 target datalayout "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32" 3 target triple = "i386-pc-linux-gnu" 4 %struct.__fundamental_type_info_pseudo = type { %struct.__type_info_pseudo } 5 %struct.__type_info_pseudo = type { i8*, i8* } 6 @_ZTIi = external constant %struct.__fundamental_type_info_pseudo ; <%struct.__fundamental_type_info_pseudo*> [#uses=1] 7 8 define i32 @main() { 9 entry: 10 %0 = tail call i8* @__cxa_allocate_exception(i32 4) nounwind ; <i8*> [#uses=2] 11 %1 = bitcast i8* %0 to i32* ; <i32*> [#uses=1] 12 store i32 34, i32* %1, align 4 13 invoke void @__cxa_throw(i8* %0, i8* bitcast (%struct.__fundamental_type_info_pseudo* @_ZTIi to i8*), void (i8*)* null) noreturn 14 to label %invcont unwind label %lpad 15 16 invcont: ; preds = %entry 17 unreachable 18 19 lpad: ; preds = %entry 20 %eh_ptr = tail call i8* @llvm.eh.exception() ; <i8*> [#uses=2] 21 %eh_select = tail call i32 (i8*, i8*, ...)* @llvm.eh.selector.i32(i8* %eh_ptr, i8* bitcast (i32 (...)* @__gxx_personality_sj0 to i8*)) ; <i32> [#uses=0] 22 %2 = tail call i8* @__cxa_begin_catch(i8* %eh_ptr) nounwind ; <i8*> [#uses=0] 23 tail call void @__cxa_end_catch() 24 ret i32 0 25 } 26 27 declare i8* @__cxa_allocate_exception(i32) nounwind 28 29 declare void @__cxa_throw(i8*, i8*, void (i8*)*) noreturn 30 31 declare i8* @__cxa_begin_catch(i8*) nounwind 32 33 declare i8* @llvm.eh.exception() nounwind 34 35 declare i32 @llvm.eh.selector.i32(i8*, i8*, ...) nounwind 36 37 declare void @__cxa_end_catch() 38 39 declare i32 @__gxx_personality_sj0(...) from the LLVM-IR, catch all was translated to LLVM-IR : %eh_select = tail call i32 (i8*, i8*, ...)* @llvm.eh.selector.i32(i8* %eh_ptr, i8* bitcast (i32 (...)* @__gxx_personality_sj0 to i8*)) here llvm.eh.selector.i32 has only two args, but from the doc, it says llvm.eh.selector takes a minimum of three arguments. The first argument is the reference to the exception structure. The second argument is a reference to the personality function to be used for this try catch sequence. Each of the remaining arguments is either a reference to the type info for a catch statement, a filter expression, or the number zero representing a cleanup. are llvm.eh.selector.i32 has other semantics ? may be llvm-gcc deal call all as a special case ? best regards zhangzw
Török Edwin
2009-May-12 07:13 UTC
[LLVMdev] How distinguish Catch all llvm-IR from other catch type ?
On 2009-05-12 10:03, zhengjian zhang wrote:> Hi, > > catch_all.cpp: > > 1 int main() > 2 { > 3 try { > 4 throw 34; > 5 } > 6 catch (...) {} > 7 } > > > llvm-gcc -O3 -S -emit-llvm catch_all.cpp -o catch_all.ll: > > 1 ; ModuleID = 'catch_all.cpp' > 2 target datalayout > "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32" > 3 target triple = "i386-pc-linux-gnu" > 4 %struct.__fundamental_type_info_pseudo = type { > %struct.__type_info_pseudo } > 5 %struct.__type_info_pseudo = type { i8*, i8* } > 6 @_ZTIi = external constant > %struct.__fundamental_type_info_pseudo ; > <%struct.__fundamental_type_info_pseudo*> [#uses=1] > 7 > 8 define i32 @main() { > 9 entry: > 10 %0 = tail call i8* @__cxa_allocate_exception(i32 4) > nounwind ; <i8*> [#uses=2] > 11 %1 = bitcast i8* %0 to i32* ; <i32*> [#uses=1] > 12 store i32 34, i32* %1, align 4 > 13 invoke void @__cxa_throw(i8* %0, i8* bitcast > (%struct.__fundamental_type_info_pseudo* @_ZTIi to i8*), void (i8*)* > null) noreturn > 14 to label %invcont unwind label %lpad > 15 > 16 invcont: ; preds = %entry > 17 unreachable > 18 > 19 lpad: ; preds = %entry > 20 %eh_ptr = tail call i8* @llvm.eh.exception() > ; <i8*> [#uses=2] > 21 %eh_select = tail call i32 (i8*, i8*, ...)* > @llvm.eh.selector.i32(i8* %eh_ptr, i8* bitcast (i32 (...)* > @__gxx_personality_sj0 to i8*)) ; <i32> [#uses=0] > 22 %2 = tail call i8* @__cxa_begin_catch(i8* %eh_ptr) > nounwind ; <i8*> [#uses=0] > 23 tail call void @__cxa_end_catch() > 24 ret i32 0 > 25 } > 26 > 27 declare i8* @__cxa_allocate_exception(i32) nounwind > 28 > 29 declare void @__cxa_throw(i8*, i8*, void (i8*)*) noreturn > 30 > 31 declare i8* @__cxa_begin_catch(i8*) nounwind > 32 > 33 declare i8* @llvm.eh.exception() nounwind > 34 > 35 declare i32 @llvm.eh.selector.i32(i8*, i8*, ...) nounwind > 36 > 37 declare void @__cxa_end_catch() > 38 > 39 declare i32 @__gxx_personality_sj0(...) > > > from the LLVM-IR, catch all was translated to LLVM-IR : > %eh_select = tail call i32 (i8*, i8*, ...)* @llvm.eh.selector.i32(i8* > %eh_ptr, i8* bitcast (i32 (...)* @__gxx_personality_sj0 to i8*)) > here llvm.eh.selector.i32 has only two args, but from the doc, it > says llvm.eh.selector takes a minimum of three arguments. >I get an extra null arg, I don't know why you don't: %eh_select = tail call i32 (i8*, i8*, ...)* @llvm.eh.selector.i32(i8* %eh_ptr, i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* null) ; <i32> [#uses=0] Compare it with the IR generated for catch(int) or catch(someOtherType). Here is how catch(int) looks like for me, notice the additional @_ZTIi parameter, and the icmp for the typeid: bb: ; preds = %lpad %2 = tail call i8* @__cxa_begin_catch(i8* %eh_ptr) nounwind ; <i8*> [#uses=0] tail call void @__cxa_end_catch() ret i32 0 lpad: %eh_ptr = tail call i8* @llvm.eh.exception() ; <i8*> [#uses=3] %eh_select = tail call i32 (i8*, i8*, ...)* @llvm.eh.selector.i32(i8* %eh_ptr, i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* bitcast (%struct.__fundamental_type_info_pseudo* @_ZTIi to i8*), i8* null) ; <i32> [#uses=1] %eh_typeid = tail call i32 @llvm.eh.typeid.for.i32(i8* bitcast (%struct.__fundamental_type_info_pseudo* @_ZTIi to i8*)) ; <i32> [#uses=1] %3 = icmp eq i32 %eh_select, %eh_typeid ; <i1> [#uses=1] br i1 %3, label %bb, label %Unwind Unwind: ; preds = %lpad tail call void @_Unwind_Resume(i8* %eh_ptr) unreachable Best regards, --Edwin