[LLVMdev] Loops over operands

Gabor Greif gabor at mac.com
Wed May 21 14:03:13 PDT 2008


Hi all,

there are about 800 calls to getNumOperands() in lib/. Of course one
has to leave out those to MachineInstruction, but nevertheless...

A very substantial amount of those calls are in loop contexts, like:

     for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i, + 
+GTI) {
       ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(i));
       ...
     }


Usually the induction variable "i" is not even used, apart from being
the argument to getOperand.

Now, unless we have a "Sufficiently Smart Compiler (TM)" I doubt that
this gets compiled to efficient code. I suggest that loops get rewritten
to iterator-style:

     for (op_iterator i = CE->op_begin() + 1, e = CE->op_end(); i !=  
e; ++i, ++GTI) {
       ConstantInt *CI = dyn_cast<ConstantInt>(*i);
       ...
     }

This style potentially saves us multiplications by 12 (24 on 64-bit  
systems).

Any comments? Can I go ahead and make changes in lib/Analysis ?

Cheers,

	Gabor





More information about the llvm-dev mailing list