[LLVMdev] Optimisation of select/cmp/br

Pete Calvert prc33 at cam.ac.uk
Wed Feb 5 05:22:01 PST 2014


> But what happens if %data is null? Can LLVM prove that it isn't?

It can't be null, but I admit that I hadn't considered whether LLVM 
could prove this. The actual code sequence in my case is as follows, and 
LLVM seems to be able to deduce that it is always non-null (if I add 
"if(cell == NULL) printf("HELLO");" to the function lower down this 
email, before "return cell", then the call to printf is not in the 
optimised bitcode).

   %temp.raw = getelementptr inbounds %instance.fast.1* %inst, i64 0, i32 
2
   %temp = bitcast { i8*, { { void (%struct.worker_t*, %instance*, { i32 
})*, %instance* } } }* %temp.raw to %struct.cell_t*
   %3 = bitcast { i8*, { { void (%struct.worker_t*, %instance*, { i32 
})*, %instance* } } }* %temp.raw to i32*
   %4 = load i32* %3, align 4
   %5 = icmp eq i32 %4, 1
   %cell..i = select i1 %5, %struct.cell_t* %temp, %struct.cell_t* null
   %fcheck.2.temp = icmp eq %struct.cell_t* %cell..i, null
   br i1 %fcheck.2.temp, label %post.2, label %post.find.2.temp

The C function inlined is:

   __attribute__((always_inline)) cell_t *fast_cell_find(cell_t *cell) {
     if(cell->status == FULL) {
       return cell;
     } else {
       return NULL;
     }
   }

This function forms part of the runtime library for a language that I'm 
writing a compiler for. I would prefer not to change the interface, as 
there are other more complex versions of *_find and the one chosen 
depends on some properties of the channel being compiled. It's a bit 
neater/simpler to code gen if these versions share the same signature.

Thanks
Pete



More information about the llvm-dev mailing list