On 16 February 2010 20:49, Trevor Harmon <Trevor.W.Harmon at nasa.gov> wrote:> We were discussing that a few days ago: > > http://lists.cs.uiuc.edu/pipermail/llvmdev/2010-February/029245.htmlHi Trevor, On a similar question, I'd want to see what line is being compiled to which instructions. Is it possible to print the before the instructions as comment? For example: array.c: ------------------------------ int array[5] = { 1, 2, 3, 4, 5 }; int main () { array[1] = 5; array[4] = array[1] + array[3]; array[0] = array[4] * array[1]; array[2] = array[0] + 1; return array[2]; } ------------------------------ $ clang -emit-llvm -O1 -c array.c -o - | llvm-dis - ; array.c:1: int array[5] = { 1, 2, 3, 4, 5 }; @array = global [5 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5], align 4 ; <[5 x i32]*> [#uses=5] ; array.c:2: int main () { define i32 @main() nounwind { entry: ; array.c:3: array[1] = 5; store i32 5, i32* getelementptr inbounds ([5 x i32]* @array, i64 0, i64 1) ; array.c:4: array[4] = array[1] + array[3]; %tmp1 = load i32* getelementptr inbounds ([5 x i32]* @array, i64 0, i64 3) ; <i32> [#uses=1] %add = add nsw i32 %tmp1, 5 ; <i32> [#uses=2] ; array.c:5: array[0] = array[4] * array[1]; store i32 %add, i32* getelementptr inbounds ([5 x i32]* @array, i64 0, i64 4) %tmp3 = load i32* getelementptr inbounds ([5 x i32]* @array, i64 0, i64 1) ; <i32> [#uses=1] %mul = mul i32 %add, %tmp3 ; <i32> [#uses=2] store i32 %mul, i32* getelementptr inbounds ([5 x i32]* @array, i64 0, i64 0) ; array.c:6: array[2] = array[0] + 1; %add5 = add nsw i32 %mul, 1 ; <i32> [#uses=2] store i32 %add5, i32* getelementptr inbounds ([5 x i32]* @array, i64 0, i64 2) ; array.c:7: return array[2]; ret i32 %add5 } When lines get re-arranged with basic optimizations, the output could be messy, but at least on -O0, the majority of lines can still be quite readable, especially when you're testing very simple test cases to assure correct instruction generation. If there isn't such thing, is it easy to add as an extra option (only when -O0 is selected)? I'm willing to implement that, in case it's possible. -- cheers, --renato http://systemcall.org/ Reclaim your digital rights, eliminate DRM, learn more at http://www.defectivebydesign.org/what_is_drm
On Wed, Feb 17, 2010 at 3:07 AM, Renato Golin <rengolin at systemcall.org> wrote:> On 16 February 2010 20:49, Trevor Harmon <Trevor.W.Harmon at nasa.gov> wrote: >> We were discussing that a few days ago: >> >> http://lists.cs.uiuc.edu/pipermail/llvmdev/2010-February/029245.html > > Hi Trevor, > > On a similar question, I'd want to see what line is being compiled to > which instructions.LLVM instruction or machine instruction ? If LLVM instruction then it is possible. Add -g on the command line along with -O0. Using your example, you'll get following with clang. define i32 @main() nounwind ssp { entry: %retval = alloca i32 ; <i32*> [#uses=3] store i32 0, i32* %retval store i32 5, i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i64 1), !dbg !6 %tmp = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i64 1), !dbg !11 ; <i32> [#uses=1] %tmp1 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i64 3), !dbg !11 ; <i32> [#uses=1] %add = add nsw i32 %tmp, %tmp1, !dbg !11 ; <i32> [#uses=1] store i32 %add, i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i64 4), !dbg !11 %tmp2 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i64 4), !dbg !12 ; <i32> [#uses=1] %tmp3 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i64 1), !dbg !12 ; <i32> [#uses=1] %mul = mul i32 %tmp2, %tmp3, !dbg !12 ; <i32> [#uses=1] store i32 %mul, i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i32 0), !dbg !12 %tmp4 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i32 0), !dbg !13 ; <i32> [#uses=1] %add5 = add nsw i32 %tmp4, 1, !dbg !13 ; <i32> [#uses=1] store i32 %add5, i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i64 2), !dbg !13 %tmp6 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0, i64 2), !dbg !14 ; <i32> [#uses=1] store i32 %tmp6, i32* %retval, !dbg !14 %0 = load i32* %retval, !dbg !15 ; <i32> [#uses=1] ret i32 %0, !dbg !15 } and list of metadata. Now to get the location of %add = add nsw i32 %tmp, %tmp1, !dbg !11 ; <i32> [#uses=1] follow !11 from !dbg !11, which is !11 = metadata !{i32 4, i32 8, metadata !7, null} This says the above LLVM instruction corresponds to source line no 4, column no 8 inside a scope marked as !7. Which is ... !7 = metadata !{i32 458763, metadata !8, i32 2, i32 13} ; [ DW_TAG_lexical_block ] ... a lexical block starting at line no 2, column 13. This lexical block is part of !8, which is !8 = metadata !{i32 458798, i32 0, metadata !1, metadata !"main", metadata !"main", metadata !"main", metadata !1, i32 2, metadata !9, i1 false, i1 true, i32 0, i32 0, null, i1 false} ; [ DW_TAG_subprogram ] ... a function (subprogram) named "main". - Devang
On 17 February 2010 17:32, Devang Patel <devang.patel at gmail.com> wrote:> LLVM instruction or machine instruction ? If LLVM instruction then it > is possible. Add -g on the command line along with -O0. Using your > example, you'll get following with clang.Yeah, I did that, but the output really doesn't help much. What I wanted was exactly what I had in my email, source code lines intermixed with their respective LLVM IR instructions. No machine instructions, asm or binary. In simple cases I can normally know where each line starts/ends, but if I'm mixing too many similar commands, it can get fuzzy, especially after a few hours looking at the same code with subtle changes... ;) cheers, --renato http://systemcall.org/ Reclaim your digital rights, eliminate DRM, learn more at http://www.defectivebydesign.org/what_is_drm