Banksel optimization is now based on the section names of symbols, since the symbols in one section will always be put into one bank.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66761 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjiv Gupta 2009-03-12 02:10:45 +00:00
parent 1379cadf8d
commit d076570a66

View File

@ -25,60 +25,63 @@
using namespace llvm; using namespace llvm;
#include "PIC16GenAsmWriter.inc" #include "PIC16GenAsmWriter.inc"
bool PIC16AsmPrinter::inSameBank (char *s1, char *s2){
assert (s1 && s2 && "Null pointer assignment"); inline static bool isLocalToFunc (std::string &FuncName, std::string &VarName) {
if (VarName.find(FuncName + ".auto.") != std::string::npos
if ((*s1 == '.') && (*s2 == '.')) { //skip if they both start with '.' || VarName.find(FuncName + ".arg.") != std::string::npos)
s1++; return true;
s2++;
} return false;
while (*s1 && *s2) { }
if (*s1 != *s2)
goto _NotInSameBank; inline static bool isLocalName (std::string &Name) {
if (Name.find(".auto.") != std::string::npos
if ((*s1 == '.') && (*s2 == '.')) //both symbols in same function || Name.find(".arg.") != std::string::npos)
goto _InSameBank; //in the same bank
s1++;
s2++;
}
if (*s1 && *s1) {
_InSameBank:
return true; return true;
}
_NotInSameBank:
return false; return false;
} }
bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) { bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
std::string NewBankselLabel; std::string NewBank = "";
unsigned Operands = MI->getNumOperands(); unsigned Operands = MI->getNumOperands();
if (Operands > 1) { if (Operands > 1) {
// Global address or external symbol should be second operand from last // Global address or external symbol should be second operand from last
// if we want to print banksel for it. // if we want to print banksel for it.
const MachineOperand &Op = MI->getOperand(Operands-2); unsigned BankSelVar = Operands - 2;
// In cases where an instruction has a def or use defined in td file,
// that def or use becomes a machine instruction operand.
// eg. addfw_1 instruction defines STATUS register. So the machine
// instruction for it has MO_Register Operand as its last operand.
while ((MI->getOperand(BankSelVar + 1).getType() ==
MachineOperand::MO_Register) && (BankSelVar > 0))
BankSelVar--;
const MachineOperand &Op = MI->getOperand(BankSelVar);
unsigned OpType = Op.getType(); unsigned OpType = Op.getType();
if (OpType == MachineOperand::MO_GlobalAddress || if (OpType == MachineOperand::MO_GlobalAddress ||
OpType == MachineOperand::MO_ExternalSymbol) { OpType == MachineOperand::MO_ExternalSymbol) {
if (OpType == MachineOperand::MO_GlobalAddress ) if (OpType == MachineOperand::MO_GlobalAddress )
NewBankselLabel = Mang->getValueName(Op.getGlobal()); NewBank = Op.getGlobal()->getSection();
else else {
NewBankselLabel = Op.getSymbolName(); // External Symbol is generated for temp data. Temp data in in
// fdata.<functionname>.# section.
NewBank = "fdata." + CurrentFnName +".#";
}
// Operand after global address or external symbol should be banksel. // Operand after global address or external symbol should be banksel.
// Value 1 for this operand means we need to generate banksel else do not // Value 1 for this operand means we need to generate banksel else do not
// generate banksel. // generate banksel.
const MachineOperand &BS = MI->getOperand(Operands-1); const MachineOperand &BS = MI->getOperand(BankSelVar+1);
// If Section names are same then the variables are in same section.
// This is not true for external variables as section names for global
// variables in all files are same at this time. For eg. initialized
// data in put in idata.# section in all files.
if (((int)BS.getImm() == 1) && if (((int)BS.getImm() == 1) &&
(!inSameBank ((char *)CurrentBankselLabelInBasicBlock.c_str(), ((Op.isGlobal() && Op.getGlobal()->hasExternalLinkage()) ||
(char *)NewBankselLabel.c_str()))) { (NewBank.compare(CurBank) != 0))) {
CurrentBankselLabelInBasicBlock = NewBankselLabel;
O << "\tbanksel "; O << "\tbanksel ";
printOperand(MI, Operands-2); printOperand(MI, BankSelVar);
O << "\n"; O << "\n";
CurBank = NewBank;
} }
} }
} }
@ -119,7 +122,7 @@ bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
} }
else else
O << CurrentFnName << ":\n"; O << CurrentFnName << ":\n";
CurrentBankselLabelInBasicBlock = ""; CurBank = "";
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
II != E; ++II) { II != E; ++II) {
// Print the assembly for the instruction. // Print the assembly for the instruction.
@ -219,7 +222,7 @@ void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
// Any variables reaching here with ".auto." in its name is a local scope // Any variables reaching here with ".auto." in its name is a local scope
// variable and should not be printed in global data section. // variable and should not be printed in global data section.
std::string Name = Mang->getValueName(I); std::string Name = Mang->getValueName(I);
if (Name.find(".auto.") != std::string::npos) if (isLocalName (Name))
continue; continue;
if (I->isDeclaration()) if (I->isDeclaration())
@ -231,7 +234,7 @@ void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
void PIC16AsmPrinter::EmitInitData (Module &M) { void PIC16AsmPrinter::EmitInitData (Module &M) {
SwitchToSection(TAI->getDataSection()); SwitchToSection(TAI->getDataSection());
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) { I != E; ++I) {
if (!I->hasInitializer()) // External global require no code. if (!I->hasInitializer()) // External global require no code.
continue; continue;
@ -247,11 +250,12 @@ void PIC16AsmPrinter::EmitInitData (Module &M) {
// Any variables reaching here with "." in its name is a local scope // Any variables reaching here with "." in its name is a local scope
// variable and should not be printed in global data section. // variable and should not be printed in global data section.
std::string name = Mang->getValueName(I); std::string Name = Mang->getValueName(I);
if (name.find(".auto.") != std::string::npos) if (isLocalName(Name))
continue; continue;
O << name; I->setSection(TAI->getDataSection()->getName());
O << Name;
EmitGlobalConstant(C, AddrSpace); EmitGlobalConstant(C, AddrSpace);
} }
} }
@ -261,7 +265,7 @@ void PIC16AsmPrinter::EmitRomData (Module &M)
{ {
SwitchToSection(TAI->getReadOnlySection()); SwitchToSection(TAI->getReadOnlySection());
IsRomData = true; IsRomData = true;
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) { I != E; ++I) {
if (!I->hasInitializer()) // External global require no code. if (!I->hasInitializer()) // External global require no code.
continue; continue;
@ -280,6 +284,7 @@ void PIC16AsmPrinter::EmitRomData (Module &M)
if (name.find(".") != std::string::npos) if (name.find(".") != std::string::npos)
continue; continue;
I->setSection(TAI->getReadOnlySection()->getName());
O << name; O << name;
EmitGlobalConstant(C, AddrSpace); EmitGlobalConstant(C, AddrSpace);
O << "\n"; O << "\n";
@ -293,7 +298,7 @@ void PIC16AsmPrinter::EmitUnInitData (Module &M)
SwitchToSection(TAI->getBSSSection_()); SwitchToSection(TAI->getBSSSection_());
const TargetData *TD = TM.getTargetData(); const TargetData *TD = TM.getTargetData();
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) { I != E; ++I) {
if (!I->hasInitializer()) // External global require no code. if (!I->hasInitializer()) // External global require no code.
continue; continue;
@ -310,6 +315,8 @@ void PIC16AsmPrinter::EmitUnInitData (Module &M)
if (name.find(".") != std::string::npos) if (name.find(".") != std::string::npos)
continue; continue;
I->setSection(TAI->getBSSSection_()->getName());
const Type *Ty = C->getType(); const Type *Ty = C->getType();
unsigned Size = TD->getTypePaddedSize(Ty); unsigned Size = TD->getTypePaddedSize(Ty);
@ -328,7 +335,7 @@ bool PIC16AsmPrinter::doFinalization(Module &M) {
void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) { void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
const Function *F = MF.getFunction(); const Function *F = MF.getFunction();
std::string FuncName = Mang->getValueName(F); std::string FuncName = Mang->getValueName(F);
const Module *M = F->getParent(); Module *M = const_cast<Module *>(F->getParent());
const TargetData *TD = TM.getTargetData(); const TargetData *TD = TM.getTargetData();
unsigned FrameSize = 0; unsigned FrameSize = 0;
// Emit the data section name. // Emit the data section name.
@ -354,7 +361,7 @@ void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
// Therefore to get the variable belonging to this function entire // Therefore to get the variable belonging to this function entire
// global list will be traversed and variables belonging to this function // global list will be traversed and variables belonging to this function
// will be emitted in the current data section. // will be emitted in the current data section.
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); for (Module::global_iterator I = M->global_begin(), E = M->global_end();
I != E; ++I) { I != E; ++I) {
std::string VarName = Mang->getValueName(I); std::string VarName = Mang->getValueName(I);
@ -363,9 +370,10 @@ void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
// Static local varilabes of a function does not have .auto. in their // Static local varilabes of a function does not have .auto. in their
// name. They are not printed as part of function data but module // name. They are not printed as part of function data but module
// level global data. // level global data.
if (!(VarName.find(FuncName + ".auto.") == 0 ? true : false)) if (! isLocalToFunc(FuncName, VarName))
continue; continue;
I->setSection("fdata." + CurrentFnName + ".#");
Constant *C = I->getInitializer(); Constant *C = I->getInitializer();
const Type *Ty = C->getType(); const Type *Ty = C->getType();
unsigned Size = TD->getTypePaddedSize(Ty); unsigned Size = TD->getTypePaddedSize(Ty);