Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 1 | //===-- PIC16MemSelOpt.cpp - PIC16 banksel optimizer --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the pass which optimizes the emitting of banksel |
| 11 | // instructions before accessing data memory. This currently works within |
| 12 | // a basic block only and keep tracks of the last accessed memory bank. |
| 13 | // If memory access continues to be in the same bank it just makes banksel |
| 14 | // immediate, which is a part of the insn accessing the data memory, from 1 |
| 15 | // to zero. The asm printer emits a banksel only if that immediate is 1. |
| 16 | // |
| 17 | // FIXME: this is not implemented yet. The banksel pass only works on local |
| 18 | // basic blocks. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #define DEBUG_TYPE "pic16-codegen" |
| 23 | #include "PIC16.h" |
| 24 | #include "PIC16InstrInfo.h" |
| 25 | #include "PIC16TargetAsmInfo.h" |
| 26 | #include "PIC16TargetMachine.h" |
| 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 29 | #include "llvm/CodeGen/Passes.h" |
| 30 | #include "llvm/Target/TargetInstrInfo.h" |
| 31 | #include "llvm/Target/TargetMachine.h" |
| 32 | #include "llvm/GlobalValue.h" |
| 33 | #include "llvm/DerivedTypes.h" |
| 34 | #include "llvm/Support/Compiler.h" |
| 35 | |
| 36 | using namespace llvm; |
| 37 | |
| 38 | namespace { |
| 39 | struct VISIBILITY_HIDDEN MemSelOpt : public MachineFunctionPass { |
| 40 | static char ID; |
| 41 | MemSelOpt() : MachineFunctionPass(&ID) {} |
| 42 | |
| 43 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 44 | AU.addPreservedID(MachineLoopInfoID); |
| 45 | AU.addPreservedID(MachineDominatorsID); |
| 46 | MachineFunctionPass::getAnalysisUsage(AU); |
| 47 | } |
| 48 | |
| 49 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 50 | |
| 51 | virtual const char *getPassName() const { |
| 52 | return "PIC16 Memsel Optimizer"; |
| 53 | } |
| 54 | |
| 55 | bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB); |
| 56 | bool processInstruction(MachineInstr *MI); |
| 57 | |
| 58 | private: |
| 59 | const TargetInstrInfo *TII; // Machine instruction info. |
| 60 | MachineBasicBlock *MBB; // Current basic block |
| 61 | std::string CurBank; |
| 62 | |
| 63 | }; |
| 64 | char MemSelOpt::ID = 0; |
| 65 | } |
| 66 | |
| 67 | FunctionPass *llvm::createPIC16MemSelOptimizerPass() { |
| 68 | return new MemSelOpt(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP |
| 73 | /// register references into FP stack references. |
| 74 | /// |
| 75 | bool MemSelOpt::runOnMachineFunction(MachineFunction &MF) { |
| 76 | TII = MF.getTarget().getInstrInfo(); |
| 77 | bool Changed = false; |
| 78 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); |
| 79 | I != E; ++I) { |
| 80 | Changed |= processBasicBlock(MF, *I); |
| 81 | } |
| 82 | |
| 83 | return Changed; |
| 84 | } |
| 85 | |
| 86 | /// processBasicBlock - Loop over all of the instructions in the basic block, |
| 87 | /// transforming FP instructions into their stack form. |
| 88 | /// |
| 89 | bool MemSelOpt::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) { |
| 90 | bool Changed = false; |
| 91 | MBB = &BB; |
| 92 | |
| 93 | // Let us assume that when entering a basic block now bank is selected. |
| 94 | // Ideally we should look at the predecessors for this information. |
| 95 | CurBank=""; |
| 96 | |
| 97 | for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) { |
| 98 | Changed |= processInstruction(I); |
| 99 | } |
| 100 | return Changed; |
| 101 | } |
| 102 | |
| 103 | bool MemSelOpt::processInstruction(MachineInstr *MI) { |
| 104 | bool Changed = false; |
| 105 | |
| 106 | unsigned NumOperands = MI->getNumOperands(); |
Sanjiv Gupta | ed4f4fb | 2009-05-12 04:30:38 +0000 | [diff] [blame^] | 107 | if (NumOperands == 0) return false; |
| 108 | |
| 109 | |
| 110 | // If this insn is not going to access any memory, return. |
| 111 | const TargetInstrDesc &TID = TII->get(MI->getOpcode()); |
| 112 | if (! (TID.isCall() || TID.mayLoad() || TID.mayStore())) |
| 113 | return false; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 114 | |
| 115 | // Scan for the memory address operand. |
| 116 | // FIXME: Should we use standard interfaces like memoperands_iterator, |
| 117 | // hasMemOperand() etc ? |
| 118 | int MemOpPos = -1; |
| 119 | for (unsigned i = 0; i < NumOperands; i++) { |
| 120 | MachineOperand Op = MI->getOperand(i); |
| 121 | if (Op.getType() == MachineOperand::MO_GlobalAddress || |
| 122 | Op.getType() == MachineOperand::MO_ExternalSymbol) { |
| 123 | // We found one mem operand. Next one should be BS. |
| 124 | MemOpPos = i; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // If we did not find an insn accessing memory. Continue. |
| 130 | if (MemOpPos == -1) return Changed; |
| 131 | |
| 132 | // Get the MemOp. |
| 133 | MachineOperand &Op = MI->getOperand(MemOpPos); |
| 134 | |
| 135 | // If this is a pagesel material, handle it first. |
| 136 | if (MI->getOpcode() == PIC16::CALL) { |
| 137 | DebugLoc dl = MI->getDebugLoc(); |
| 138 | BuildMI(*MBB, MI, dl, TII->get(PIC16::pagesel)). |
| 139 | addOperand(Op); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | // Get the section name(NewBank) for MemOp. |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 144 | // This assumes that the section names for globals are laready set by |
| 145 | // AsmPrinter->doInitialization. |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 146 | std::string NewBank = CurBank; |
| 147 | if (Op.getType() == MachineOperand::MO_GlobalAddress && |
| 148 | Op.getGlobal()->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) { |
| 149 | NewBank = Op.getGlobal()->getSection(); |
| 150 | } else if (Op.getType() == MachineOperand::MO_ExternalSymbol) { |
| 151 | // External Symbol is generated for temp data and arguments. They are |
| 152 | // in fpdata.<functionname>.# section. |
| 153 | std::string Sym = Op.getSymbolName(); |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 154 | NewBank = PAN::getSectionNameForSym(Sym); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // If the previous and new section names are same, we don't need to |
| 158 | // emit banksel. |
| 159 | if (NewBank.compare(CurBank) != 0 ) { |
| 160 | DebugLoc dl = MI->getDebugLoc(); |
| 161 | BuildMI(*MBB, MI, dl, TII->get(PIC16::banksel)). |
| 162 | addOperand(Op); |
| 163 | Changed = true; |
| 164 | CurBank = NewBank; |
| 165 | } |
| 166 | |
| 167 | return Changed; |
| 168 | } |
| 169 | |