-2

I am trying to count number of zeros in a register from left to right. For this i need a loop. Getting out of this loop is difficult as i do not know how to put if statement on llvm::value?

llvm::Value* intermediateValue = llvm::ConstantInt::get(llvm::Type::getInt32Ty(llvm::getGlobalContext()), 1, true); 

  for(int64_t i =31; i>=0; i--)
  {
  //Shifting the register right
   CountFirstOne = irBuilder->CreateLShr(llvmRegFirstOperand,i,"CountFirstOneCal");
   if(CountFirstOne == intermediateValue)
    {
      break;
    } 
    count = count+1;  // count has number of zeros
  }
Zeeshan Haider
  • 101
  • 1
  • 9

1 Answers1

0

It looks like you're mixing up what you want the compiler to do and what you want the compiled code to do. For example, your loop exists in your compiler pass (e.g. the compiler will run through this loop at compile time) and not in your LVM IR. However, you are creating new LLVM IR instructions (left shift) in each of these loop passes and the number of loop passes depends on a register's value (which isn't known until runtime).

  • I think i have to use jump or branches from llvm library for making the loop? – Zeeshan Haider Oct 28 '14 at 15:26
  • I would start by writing the equivalent C code of whatever it is you want to do, then run it through clang with `-O0` and have it emit LLVM IR. This will give you an idea of what the basic un-optimized IR that you want to construct should look like. The command to do this is `clang -O0 -c -emit-llvm code.c`. This will create a `code.bc` file, which contains the LLVM IR binary. You can then run `llvm-dis code.bc`, which will generate `code.ll`. This `code.ll` file contains the LLVM IR, which is human readable. You can also run `llc -march=cpp code.bc` to generate the equivalent C++ API calls. – Mike C. Delorme Oct 29 '14 at 00:45
  • This is assuming you want to add your instructions at the LLVM IR layer instead of in the backend machine code layer. – Mike C. Delorme Oct 29 '14 at 00:47