<div dir="auto">This is so cool! I once had a similar idea but the way I was thinking about it ended up more complex than I had time to implement (I sketched it here: <a href="http://lists.llvm.org/pipermail/llvm-dev/2013-November/067720.html" target="_blank">http://lists.llvm.org/<wbr>pipermail/llvm-dev/2013-<wbr>November/067720.html</a>).<div dir="auto"><br></div><div dir="auto">Good idea using xpath to simplify the implementation and reuse existing languages/libraries as a starting point!</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Oct 29, 2017 6:47 AM, "Alessandro Di Federico via llvm-dev" <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi, sometimes when dealing with LLVM IR getting to a desired point of<br>
the code is a bit cumbersome, in particular if you're instrumenting<br>
existing code. A lot of nested loops and if checks.<br>
<br>
Maybe all of this could be avoided by employing a query language. Since<br>
an LLVM module can be seen as a sort of tree with attributes, I think<br>
that reusing an existing query language for XML would be appropriate.<br>
<br>
In particular I choose XPath [1] since it's more expressive than, say,<br>
CSS selectors (e.g., you can move from the current element to the<br>
parent).<br>
<br>
Therefore, in a spare night, I took pugixml [2], a lightweight XML parser<br>
with XPath support, stripped away everything was XML-specific and<br>
adapted it so that it could query an arbitrary tree, as long as a class<br>
providing certain traits is provided.<br>
<br>
Attached you can find the class to query a LLVM module and example LLVM<br>
module (using LLVM 3.8, but newer versions should do to).<br>
<br>
The current implementation pretends that a module looks like the<br>
following XML tree (more or less):<br>
<br>
    <main.ll><br>
      <main><br>
        <basicblock1><br>
          <alloca /><br>
          <alloca /><br>
          ...<br>
        </basicblock1><br>
        ...<br>
      </main><br>
    </main.ll><br>
<br>
Additional information could be encoded in attributes.<br>
Please note that the queries are done on the LLVM IR directly, no XML<br>
tree is materialized.<br>
<br>
In the following you can find some examples:<br>
<br>
    $ # Find all the basic blocks containing at least an alloca<br>
    $ llvm-xpath '/main/*[count(alloca) > 0]' main.ll<br>
<br>
      %1 = alloca i32, align 4<br>
      %2 = alloca i32, align 4<br>
      %i = alloca i32, align 4<br>
      store i32 0, i32* %1, align 4<br>
      store i32 %argc, i32* %2, align 4<br>
      %3 = load i32, i32* %2, align 4<br>
      store i32 %3, i32* %i, align 4<br>
      br label %4<br>
<br>
    $ # Find all store instructions<br>
    $ llvm-xpath '/*/*/store'<br>
      store i32 0, i32* %1, align 4<br>
      store i32 %argc, i32* %2, align 4<br>
      store i32 %3, i32* %i, align 4<br>
      store i32 %6, i32* %i, align 4<br>
<br>
Obviously this doesn't have to be exclusively a command line tool, but<br>
we could have something like:<br>
<br>
    for (auto *Store : TheModule.xpath<StoreInst>("/*<wbr>/*/store"))<br>
      /* ... */<br>
<br>
I'm not releasing the full code yet since it's very much work in<br>
progress, but if anyone is interested in such a thing, just ping me.<br>
The applications could range from using it in existing code to just<br>
provide it for fast prototyping, e.g., in llvmcpy [3].<br>
<br>
Obviously there are some open questions, such as how to deal with<br>
operands, which could lead to an infinite tree, or how to organize<br>
attributes. But it should be doable.<br>
<br>
---<br>
Alessandro Di Federico<br>
PhD student at Politecnico di Milano<br>
<br>
[1] <a href="https://en.wikipedia.org/wiki/XPath" rel="noreferrer" target="_blank">https://en.wikipedia.org/wiki/<wbr>XPath</a><br>
[2] <a href="https://pugixml.org/" rel="noreferrer" target="_blank">https://pugixml.org/</a><br>
[3] <a href="https://github.com/revng/llvmcpy" rel="noreferrer" target="_blank">https://github.com/revng/<wbr>llvmcpy</a><br>
<br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div></div>