extracting data and operands


I wish to implement dead code elimination as a pass in llvm. This is not the same as unreachable block.
For this, I plan to go ahead like this :
In each function
Identify the store and branch operations
Identify the operands involved in these operations.
Build the UD chain of these and check if there is any variable that is not used and mark its definition (the entire instruction)
Delete all marked instructions.

I have no clue how to do this using llvm. Any help would be greatly appreciated.