LLVM API Documentation

Reg2Mem.cpp

Go to the documentation of this file.
00001 //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file demotes all registers to memory references.  It is intented to be
00011 // the inverse of PromoteMemoryToRegister.  By converting to loads, the only
00012 // values live accross basic blocks are allocas and loads before phi nodes.
00013 // It is intended that this should make CFG hacking much easier.
00014 // To make later hacking easier, the entry block is split into two, such that
00015 // all introduced allocas and nothing else are in the entry block.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/Transforms/Scalar.h"
00020 #include "llvm/Transforms/Utils/Local.h"
00021 #include "llvm/Pass.h"
00022 #include "llvm/Function.h"
00023 #include "llvm/Module.h"
00024 #include "llvm/BasicBlock.h"
00025 #include "llvm/Instructions.h"
00026 #include "llvm/ADT/Statistic.h"
00027 
00028 #include <list>
00029 
00030 using namespace llvm;
00031 
00032 namespace {
00033   Statistic<> NumDemoted("reg2mem", "Number of registers demoted");
00034   
00035   struct RegToMem : public FunctionPass {
00036 
00037     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00038       AU.addRequiredID(BreakCriticalEdgesID);
00039       AU.addPreservedID(BreakCriticalEdgesID);
00040     }
00041 
00042    bool valueEscapes(Instruction* i) {
00043       BasicBlock* bb = i->getParent();
00044       for(Value::use_iterator ii = i->use_begin(), ie = i->use_end();
00045           ii != ie; ++ii)
00046         if (cast<Instruction>(*ii)->getParent() != bb ||
00047       isa<PHINode>(*ii))
00048           return true;
00049       return false;
00050     }
00051 
00052     virtual bool runOnFunction(Function &F) {
00053       if (!F.isExternal()) {
00054         //give us a clean block
00055   BasicBlock* bbold = &F.getEntryBlock();
00056   BasicBlock* bbnew = new BasicBlock("allocablock", &F, &F.getEntryBlock());
00057   new BranchInst(bbold, bbnew);
00058 
00059         //find the instructions
00060         std::list<Instruction*> worklist;
00061         for (Function::iterator ibb = F.begin(), ibe = F.end();
00062              ibb != ibe; ++ibb)
00063           for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
00064                iib != iie; ++iib) {
00065             if(valueEscapes(iib))
00066               worklist.push_front(&*iib);
00067           }
00068         //demote escaped instructions
00069         NumDemoted += worklist.size();
00070         for (std::list<Instruction*>::iterator ilb = worklist.begin(), 
00071                ile = worklist.end(); ilb != ile; ++ilb)
00072           DemoteRegToStack(**ilb, false);
00073         return true;
00074       }
00075       return false;
00076     }
00077   };
00078   
00079   RegisterOpt<RegToMem> X("reg2mem", "Demote all values to stack slots");
00080 }
00081 
00082 // createDemoteRegisterToMemory - Provide an entry point to create this pass.
00083 //
00084 const PassInfo *llvm::DemoteRegisterToMemoryID = X.getPassInfo();
00085 FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
00086   return new RegToMem();
00087 }