LLVM API Documentation

PIC16MemSelOpt.cpp

Go to the documentation of this file.
00001 //===-- PIC16MemSelOpt.cpp - PIC16 banksel optimizer  --------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the pass which optimizes the emitting of banksel 
00011 // instructions before accessing data memory. This currently works within
00012 // a basic block only and keep tracks of the last accessed memory bank.
00013 // If memory access continues to be in the same bank it just makes banksel
00014 // immediate, which is a part of the insn accessing the data memory, from 1
00015 // to zero. The asm printer emits a banksel only if that immediate is 1. 
00016 //
00017 // FIXME: this is not implemented yet.  The banksel pass only works on local
00018 // basic blocks.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #define DEBUG_TYPE "pic16-codegen"
00023 #include "PIC16.h"
00024 #include "PIC16InstrInfo.h"
00025 #include "PIC16TargetAsmInfo.h"
00026 #include "PIC16TargetMachine.h"
00027 #include "llvm/CodeGen/MachineFunctionPass.h"
00028 #include "llvm/CodeGen/MachineInstrBuilder.h"
00029 #include "llvm/CodeGen/Passes.h"
00030 #include "llvm/Target/TargetInstrInfo.h"
00031 #include "llvm/Target/TargetMachine.h"
00032 #include "llvm/GlobalValue.h"
00033 #include "llvm/DerivedTypes.h"
00034 #include "llvm/Support/Compiler.h"
00035 
00036 using namespace llvm;
00037 
00038 namespace {
00039   struct VISIBILITY_HIDDEN MemSelOpt : public MachineFunctionPass {
00040     static char ID;
00041     MemSelOpt() : MachineFunctionPass(&ID) {}
00042 
00043     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00044       AU.addPreservedID(MachineLoopInfoID);
00045       AU.addPreservedID(MachineDominatorsID);
00046       MachineFunctionPass::getAnalysisUsage(AU);
00047     }
00048 
00049     virtual bool runOnMachineFunction(MachineFunction &MF);
00050 
00051     virtual const char *getPassName() const { 
00052       return "PIC16 Memsel Optimizer"; 
00053     }
00054 
00055    bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
00056    bool processInstruction(MachineInstr *MI);
00057 
00058   private:
00059     const TargetInstrInfo *TII; // Machine instruction info.
00060     MachineBasicBlock *MBB;     // Current basic block
00061     std::string CurBank;
00062 
00063   };
00064   char MemSelOpt::ID = 0;
00065 }
00066 
00067 FunctionPass *llvm::createPIC16MemSelOptimizerPass() { 
00068   return new MemSelOpt(); 
00069 }
00070 
00071 
00072 /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
00073 /// register references into FP stack references.
00074 ///
00075 bool MemSelOpt::runOnMachineFunction(MachineFunction &MF) {
00076   TII = MF.getTarget().getInstrInfo();
00077   bool Changed = false;
00078   for (MachineFunction::iterator I = MF.begin(), E = MF.end();
00079        I != E; ++I) {
00080     Changed |= processBasicBlock(MF, *I);
00081   }
00082 
00083   return Changed;
00084 }
00085 
00086 /// processBasicBlock - Loop over all of the instructions in the basic block,
00087 /// transforming FP instructions into their stack form.
00088 ///
00089 bool MemSelOpt::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
00090   bool Changed = false;
00091   MBB = &BB;
00092 
00093   // Let us assume that when entering a basic block now bank is selected.
00094   // Ideally we should look at the predecessors for this information.
00095   CurBank=""; 
00096 
00097   for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
00098     Changed |= processInstruction(I);
00099   }
00100   return Changed;
00101 }
00102 
00103 bool MemSelOpt::processInstruction(MachineInstr *MI) {
00104   bool Changed = false;
00105 
00106   unsigned NumOperands = MI->getNumOperands();
00107   if (NumOperands == 0) return false;
00108 
00109 
00110   // If this insn is not going to access any memory, return.
00111   const TargetInstrDesc &TID = TII->get(MI->getOpcode());
00112   if (!(TID.isBranch() || TID.isCall() || TID.mayLoad() || TID.mayStore()))
00113     return false;
00114 
00115   // Scan for the memory address operand.
00116   // FIXME: Should we use standard interfaces like memoperands_iterator,
00117   // hasMemOperand() etc ?
00118   int MemOpPos = -1;
00119   for (unsigned i = 0; i < NumOperands; i++) {
00120     MachineOperand Op = MI->getOperand(i);
00121     if (Op.getType() ==  MachineOperand::MO_GlobalAddress ||
00122         Op.getType() ==  MachineOperand::MO_ExternalSymbol || 
00123         Op.getType() ==  MachineOperand::MO_MachineBasicBlock) {
00124       // We found one mem operand. Next one may be BS.
00125       MemOpPos = i;
00126       break;
00127     }
00128   }
00129 
00130   // If we did not find an insn accessing memory. Continue.
00131   if (MemOpPos == -1) return Changed;
00132  
00133   // Get the MemOp.
00134   MachineOperand &Op = MI->getOperand(MemOpPos);
00135 
00136   // If this is a pagesel material, handle it first.
00137   if (MI->getOpcode() == PIC16::CALL ||
00138       MI->getOpcode() == PIC16::br_uncond) {
00139     DebugLoc dl = MI->getDebugLoc();
00140     BuildMI(*MBB, MI, dl, TII->get(PIC16::pagesel)).
00141       addOperand(Op);
00142     return true;
00143   }
00144 
00145   // Get the section name(NewBank) for MemOp.
00146   // This assumes that the section names for globals are laready set by
00147   // AsmPrinter->doInitialization.
00148   std::string NewBank = CurBank;
00149   if (Op.getType() ==  MachineOperand::MO_GlobalAddress &&
00150       Op.getGlobal()->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) {
00151     NewBank = Op.getGlobal()->getSection();
00152   } else if (Op.getType() ==  MachineOperand::MO_ExternalSymbol) {
00153     // External Symbol is generated for temp data and arguments. They are
00154     // in fpdata.<functionname>.# section.
00155     std::string Sym = Op.getSymbolName();
00156     NewBank = PAN::getSectionNameForSym(Sym);
00157   }
00158  
00159   // If the previous and new section names are same, we don't need to
00160   // emit banksel. 
00161   if (NewBank.compare(CurBank) != 0 ) {
00162     DebugLoc dl = MI->getDebugLoc();
00163     BuildMI(*MBB, MI, dl, TII->get(PIC16::banksel)).
00164       addOperand(Op);
00165     Changed = true;
00166     CurBank = NewBank;
00167   }
00168 
00169   return Changed;
00170 }
00171