1 //===-- PIC16MemSelOpt.cpp - PIC16 banksel optimizer --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
17 // FIXME: this is not implemented yet. The banksel pass only works on local
20 //===----------------------------------------------------------------------===//
22 #define DEBUG_TYPE "pic16-codegen"
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"
39 struct VISIBILITY_HIDDEN MemSelOpt : public MachineFunctionPass {
41 MemSelOpt() : MachineFunctionPass(&ID) {}
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.addPreservedID(MachineLoopInfoID);
45 AU.addPreservedID(MachineDominatorsID);
46 MachineFunctionPass::getAnalysisUsage(AU);
49 virtual bool runOnMachineFunction(MachineFunction &MF);
51 virtual const char *getPassName() const {
52 return "PIC16 Memsel Optimizer";
55 bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
56 bool processInstruction(MachineInstr *MI);
59 const TargetInstrInfo *TII; // Machine instruction info.
60 MachineBasicBlock *MBB; // Current basic block
64 char MemSelOpt::ID = 0;
67 FunctionPass *llvm::createPIC16MemSelOptimizerPass() {
68 return new MemSelOpt();
72 /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
73 /// register references into FP stack references.
75 bool MemSelOpt::runOnMachineFunction(MachineFunction &MF) {
76 TII = MF.getTarget().getInstrInfo();
78 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
80 Changed |= processBasicBlock(MF, *I);
86 /// processBasicBlock - Loop over all of the instructions in the basic block,
87 /// transforming FP instructions into their stack form.
89 bool MemSelOpt::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
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.
97 for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
98 Changed |= processInstruction(I);
103 bool MemSelOpt::processInstruction(MachineInstr *MI) {
104 bool Changed = false;
106 unsigned NumOperands = MI->getNumOperands();
107 // If this insn has only one operand, probably it is not going to
108 // access any data memory.
109 if (PIC16InstrInfo::hasNoMemOperand(*MI)) return Changed;
111 // Scan for the memory address operand.
112 // FIXME: Should we use standard interfaces like memoperands_iterator,
113 // hasMemOperand() etc ?
115 for (unsigned i = 0; i < NumOperands; i++) {
116 MachineOperand Op = MI->getOperand(i);
117 if (Op.getType() == MachineOperand::MO_GlobalAddress ||
118 Op.getType() == MachineOperand::MO_ExternalSymbol) {
119 // We found one mem operand. Next one should be BS.
125 // If we did not find an insn accessing memory. Continue.
126 if (MemOpPos == -1) return Changed;
129 MachineOperand &Op = MI->getOperand(MemOpPos);
131 // If this is a pagesel material, handle it first.
132 if (MI->getOpcode() == PIC16::CALL) {
133 DebugLoc dl = MI->getDebugLoc();
134 BuildMI(*MBB, MI, dl, TII->get(PIC16::pagesel)).
139 // Get the section name(NewBank) for MemOp.
140 std::string NewBank = CurBank;
141 if (Op.getType() == MachineOperand::MO_GlobalAddress &&
142 Op.getGlobal()->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) {
143 NewBank = Op.getGlobal()->getSection();
144 } else if (Op.getType() == MachineOperand::MO_ExternalSymbol) {
145 // External Symbol is generated for temp data and arguments. They are
146 // in fpdata.<functionname>.# section.
147 std::string Sym = Op.getSymbolName();
148 NewBank = getSectionNameForSym(Sym);
151 // If the previous and new section names are same, we don't need to
153 if (NewBank.compare(CurBank) != 0 ) {
154 DebugLoc dl = MI->getDebugLoc();
155 BuildMI(*MBB, MI, dl, TII->get(PIC16::banksel)).