0

I want to make a pass that changing control flow.

The pass should change the if condition.

Let's assume the original code is like below.

int main(int argc, char *argv[])
{
  if (atoi(argv[1]) % 2 == 0)
    printf("even\n");
  else
    printf("odd\n");

  return 0;
}

After apply my pass the code should be changed to below. (Not mean that changing the source code, but IR code in real.)

int main(int argc, char *argv[])
{
  if (atoi(argv[1]) % 2 == 1)    //the condition of if statement is changed to 1
    printf("even\n");
  else
    printf("odd\n");

  return 0;
}

this is just a toy example of what I really want to do but I have difficulties about

  1. finding appropriate instructions want to change
  2. and changing the control flows.
1

1 Answer 1

2

Start with compiling this code to LLVM IR to get an idea what you are going to work on:

 # clang -S -emit-llvm -o - main.c

Then you will see that you are interested in icmp instruction and its operands. In your pass, iterate over all instructions in a Function, search for ICmpInst using isa<> or dyn_cast<>, then analyze its operands with getOperand() method and replace ConstantInt operand with 0 value by the same ConstantInt with 1 value.

Sign up to request clarification or add additional context in comments.

1 Comment

really appreciate about your answer! much better understanding about pass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.