blob: a97dc35b137a0790b14473d5392ba88cd68faea3 [file] [log] [blame]
Sanjiv Guptad8d27f42009-05-06 08:02:01 +00001//===-- 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"
Sanjiv Gupta753ec152009-10-15 19:26:25 +000024#include "PIC16ABINames.h"
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000025#include "PIC16InstrInfo.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000026#include "PIC16MCAsmInfo.h"
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000027#include "PIC16TargetMachine.h"
28#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/Passes.h"
31#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/GlobalValue.h"
34#include "llvm/DerivedTypes.h"
35#include "llvm/Support/Compiler.h"
36
37using namespace llvm;
38
39namespace {
40 struct VISIBILITY_HIDDEN MemSelOpt : public MachineFunctionPass {
41 static char ID;
42 MemSelOpt() : MachineFunctionPass(&ID) {}
43
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.addPreservedID(MachineLoopInfoID);
46 AU.addPreservedID(MachineDominatorsID);
47 MachineFunctionPass::getAnalysisUsage(AU);
48 }
49
50 virtual bool runOnMachineFunction(MachineFunction &MF);
51
52 virtual const char *getPassName() const {
53 return "PIC16 Memsel Optimizer";
54 }
55
56 bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
57 bool processInstruction(MachineInstr *MI);
58
59 private:
60 const TargetInstrInfo *TII; // Machine instruction info.
61 MachineBasicBlock *MBB; // Current basic block
62 std::string CurBank;
63
64 };
65 char MemSelOpt::ID = 0;
66}
67
68FunctionPass *llvm::createPIC16MemSelOptimizerPass() {
69 return new MemSelOpt();
70}
71
72
73/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
74/// register references into FP stack references.
75///
76bool MemSelOpt::runOnMachineFunction(MachineFunction &MF) {
77 TII = MF.getTarget().getInstrInfo();
78 bool Changed = false;
79 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
80 I != E; ++I) {
81 Changed |= processBasicBlock(MF, *I);
82 }
83
84 return Changed;
85}
86
87/// processBasicBlock - Loop over all of the instructions in the basic block,
88/// transforming FP instructions into their stack form.
89///
90bool MemSelOpt::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
91 bool Changed = false;
92 MBB = &BB;
93
94 // Let us assume that when entering a basic block now bank is selected.
95 // Ideally we should look at the predecessors for this information.
96 CurBank="";
97
98 for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
99 Changed |= processInstruction(I);
100 }
101 return Changed;
102}
103
104bool MemSelOpt::processInstruction(MachineInstr *MI) {
105 bool Changed = false;
106
107 unsigned NumOperands = MI->getNumOperands();
Sanjiv Guptaed4f4fb2009-05-12 04:30:38 +0000108 if (NumOperands == 0) return false;
109
110
111 // If this insn is not going to access any memory, return.
112 const TargetInstrDesc &TID = TII->get(MI->getOpcode());
Sanjiv Gupta8ebd4b62009-06-25 11:03:14 +0000113 if (!(TID.isBranch() || TID.isCall() || TID.mayLoad() || TID.mayStore()))
Sanjiv Guptaed4f4fb2009-05-12 04:30:38 +0000114 return false;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000115
116 // Scan for the memory address operand.
117 // FIXME: Should we use standard interfaces like memoperands_iterator,
118 // hasMemOperand() etc ?
119 int MemOpPos = -1;
120 for (unsigned i = 0; i < NumOperands; i++) {
121 MachineOperand Op = MI->getOperand(i);
122 if (Op.getType() == MachineOperand::MO_GlobalAddress ||
Sanjiv Gupta8ebd4b62009-06-25 11:03:14 +0000123 Op.getType() == MachineOperand::MO_ExternalSymbol ||
124 Op.getType() == MachineOperand::MO_MachineBasicBlock) {
125 // We found one mem operand. Next one may be BS.
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000126 MemOpPos = i;
127 break;
128 }
129 }
130
131 // If we did not find an insn accessing memory. Continue.
132 if (MemOpPos == -1) return Changed;
133
134 // Get the MemOp.
135 MachineOperand &Op = MI->getOperand(MemOpPos);
136
137 // If this is a pagesel material, handle it first.
Sanjiv Gupta8ebd4b62009-06-25 11:03:14 +0000138 if (MI->getOpcode() == PIC16::CALL ||
139 MI->getOpcode() == PIC16::br_uncond) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000140 DebugLoc dl = MI->getDebugLoc();
141 BuildMI(*MBB, MI, dl, TII->get(PIC16::pagesel)).
142 addOperand(Op);
143 return true;
144 }
145
146 // Get the section name(NewBank) for MemOp.
Sanjiv Guptaecb28f22009-10-24 18:19:41 +0000147 // This assumes that the section names for globals are laready set by
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000148 // AsmPrinter->doInitialization.
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000149 std::string NewBank = CurBank;
150 if (Op.getType() == MachineOperand::MO_GlobalAddress &&
151 Op.getGlobal()->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) {
152 NewBank = Op.getGlobal()->getSection();
153 } else if (Op.getType() == MachineOperand::MO_ExternalSymbol) {
154 // External Symbol is generated for temp data and arguments. They are
155 // in fpdata.<functionname>.# section.
156 std::string Sym = Op.getSymbolName();
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000157 NewBank = PAN::getSectionNameForSym(Sym);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000158 }
Sanjiv Guptaecb28f22009-10-24 18:19:41 +0000159
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000160 // If the previous and new section names are same, we don't need to
161 // emit banksel.
162 if (NewBank.compare(CurBank) != 0 ) {
163 DebugLoc dl = MI->getDebugLoc();
164 BuildMI(*MBB, MI, dl, TII->get(PIC16::banksel)).
165 addOperand(Op);
166 Changed = true;
167 CurBank = NewBank;
168 }
169
170 return Changed;
171}
172