[llvm-dev] Find the instructions where a particular value is defined

Daniel Berlin via llvm-dev llvm-dev at lists.llvm.org
Wed Jan 27 19:04:09 PST 2016


This is only the IR if you don't call mem2reg, which does SSA conversion.

The IR you have listed operates on memory, and memory is not in SSA.  If
you run mem2reg on it, you will see what John mentioned, and that is what
most people would refer to.

The answer for memory operations is "no, you cannot find all the places a
given memory is *actually* defined". You can use memorydependence and find
the places it *may be defined*, but this may be a large set and include
false positives"
The question of finding which memory locations are must-modified is
statically undecidable :)



On Wed, Jan 27, 2016 at 6:58 PM, Syed Rafiul Hussain via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> Thank you all for your reply.
>
> if(a>10)
>  b=10;
> else if (a<10)
>  b = 5;
> Here is the IR of the if-elseif:
>
>  56   %0 = load i32, i32* %a, align 4
>  57   %cmp = icmp sgt i32 %0, 10
>  58   br i1 %cmp, label %if.then, label %if.else
>  60 if.then:                                          ; preds = %entry
>  61   store i32 10, i32* %b, align 4
>  62   br label %if.end.4
>  63
>  64 if.else:                                          ; preds = %entry
>  65   %1 = load i32, i32* %a, align 4
>  66   %cmp2 = icmp slt i32 %1, 10
>  67   br i1 %cmp2, label %if.then.3, label %if.end
>  68
>  69 if.then.3:                                        ; preds = %if.else
>  70   store i32 5, i32* %b, align 4
>  71   br label %if.end
>  72
>  73 if.end:                                           ; preds =
> %if.then.3, %if.else
>  74   br label %if.end.4
>  75
>  76 if.end.4:                                         ; preds =
> %if.end, %if.then
>  77   %2 = load i32, i32* %a, align 4
>  78   %call5 = call dereferenceable(272) %"class.std::basic_ostream"*
> @_ZNSolsEi(%"class.std::basic_ostream"* @_ZSt4cout, i32 %2)
>  79   %3 = load i32, i32* %b, align 4
>  80   %call6 = call dereferenceable(272) %"class.std::basic_ostream"*
> @_ZNSolsEi(%"class.std::basic_ostream"* %call5, i32 %3)
>  81   ret i32 0
>
>
> at line 79 of the IR, I have found %b. Now I would like to find lines
> 61 and 70 where %b has been assigned with some values using store
> instruction. Can I achieve this at IR level?
>
>
> On Wed, Jan 27, 2016 at 9:15 PM, John Criswell <jtcriswel at gmail.com>
> wrote:
> > On 1/27/16 8:42 PM, Syed Rafiul Hussain via llvm-dev wrote:
> >>
> >> Sorry, I should ask the following:
> >> finds all the instructions of a function where a particular variable is
> >> defined?
> >> Lets consider the following code snippet:
> >>
> >> 1. void foo(){
> >> 2. int a, b;
> >> 3. if(a > 10)
> >> 4.   b = 10;
> >> 5. if(a<10)
> >> 6.   b = 5;
> >> 7. cout << b;
> >> 8. }
> >>
> >> I would like to know the instructions where variable b can be be
> >> defined, i.e, in this case, the instructions 4 and 6.
> >
> >
> > The LLVM IR is not best suited for this.  During the conversion to SSA
> form,
> > the variable b will be replaced with several variables, each with a
> unique
> > definition.  A phi-node will merge the two b values at line 7.  If you
> use
> > clang -emit-llvm -S on this input file, you will see.
> >
> > You may want to do your analysis on the AST generated by Clang; Clang
> ASTs
> > represent the original C code and its variables.
> >
> > Regards,
> >
> > John Criswell
> >
> >
> >
> >>
> >> On Wed, Jan 27, 2016 at 8:15 PM, Tim Northover <t.p.northover at gmail.com
> >
> >> wrote:
> >>>
> >>> On 27 January 2016 at 17:08, Syed Rafiul Hussain via llvm-dev
> >>> <llvm-dev at lists.llvm.org> wrote:
> >>>>
> >>>> I am wondering if there is anything like def-use chain which finds all
> >>>> the instructions of a function where a particular value is defined?
> >>>
> >>> How do you mean? LLVM IR is in SSA form, which means that each Value
> >>> has precisely one definition (which may or may not be an instruction).
> >>> If you've got a "Value *V" in C++ you can just
> >>> "dyn_cast<Instruction>(V)" to find the instruction (again, if it
> >>> exists).
> >>>
> >>> Cheers.
> >>>
> >>> Tim.
> >>
> >>
> >>
> >
> >
> > --
> > John Criswell
> > Assistant Professor
> > Department of Computer Science, University of Rochester
> > http://www.cs.rochester.edu/u/criswell
> >
>
>
>
> --
> Rafi
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160127/c5eea6d5/attachment-0001.html>


More information about the llvm-dev mailing list