-1

I need to extract the constant involved in a branch condition.

For instance, in the branch if(a > 10), 10 is what I want to extract.

I wonder how to do that using LLVM? The corresponding LLVM instruction for the above branch is

%cmp = icmp sgt i32 %2, 10
br i1 %cmp, label %if.then, label %if.else`.

It's easy to find the branch IR, but the constant is not in the IR.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
Dingbao Xie
  • 716
  • 9
  • 21

1 Answers1

3

The comparison instruction is icmp sgt i32 %2, 10 - the constant 10 is clearly there.

Given a br, you detect the IR Value that it branches upon by calling getCondition, and then look for a constant. However, note that there won't always be a constant there... If the condition was if (a > b) where's the constant?

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412