Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Eliminating common subexpression from machine code


The aim of the CSE algorithm is to eliminate common subexpressions to make machine code compact and remove unnecessary, duplicate code. Let's look at the code in the LLVM trunk to understand how it is implemented. The detailed code is in the lib/CodeGen/MachineCSE.cpp file.

How to do it…

  1. The MachineCSE class runs on a machine function, and hence it should inherit the MachineFunctionPass class. It has various members, such as TargetInstructionInfo, which is used to get information about the target instruction (used in performing CSE); TargetRegisterInfo, which is used to get information about the target register (whether it belongs to a reserved register class, or to more such similar classes; and MachineDominatorTree, which is used to get information about the dominator tree for the machine block:

    class MachineCSE : public MachineFunctionPass {
        const TargetInstrInfo *TII;
        const TargetRegisterInfo *TRI;
        AliasAnalysis *AA;
        MachineDominatorTree...