PTX: Various stylistic and code readability changes recommended by Jim Grosbach.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140855 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Justin Holewinski 2011-09-30 14:36:36 +00:00
parent 8c1dac54f2
commit f51b7e5d74
10 changed files with 65 additions and 270 deletions

View File

@ -69,14 +69,15 @@ void PTXInstPrinter::printPredicate(const MCInst *MI, raw_ostream &O) {
}
int PredOp = MI->getOperand(OpIndex).getImm();
if (PredOp != PTX::PRED_NONE) {
if (PredOp == PTX::PRED_NEGATE) {
O << '!';
} else {
O << '@';
}
printOperand(MI, RegIndex, O);
}
if (PredOp == PTXPredicate::None)
return;
if (PredOp == PTXPredicate::Negate)
O << '!';
else
O << '@';
printOperand(MI, RegIndex, O);
}
void PTXInstPrinter::printCall(const MCInst *MI, raw_ostream &O) {
@ -84,29 +85,27 @@ void PTXInstPrinter::printCall(const MCInst *MI, raw_ostream &O) {
// The first two operands are the predicate slot
unsigned Index = 2;
unsigned NumRets = MI->getOperand(Index++).getImm();
for (unsigned i = 0; i < NumRets; ++i) {
if (i == 0) {
O << "(";
} else {
O << ", ";
}
printOperand(MI, Index++, O);
}
if (NumRets > 0) {
O << "(";
printOperand(MI, Index++, O);
for (unsigned i = 1; i < NumRets; ++i) {
O << ", ";
printOperand(MI, Index++, O);
}
O << "), ";
}
O << *(MI->getOperand(Index++).getExpr()) << ", (";
unsigned NumArgs = MI->getOperand(Index++).getImm();
for (unsigned i = 0; i < NumArgs; ++i) {
if (NumArgs > 0) {
printOperand(MI, Index++, O);
if (i < NumArgs-1) {
for (unsigned i = 1; i < NumArgs; ++i) {
O << ", ";
printOperand(MI, Index++, O);
}
}
O << ")";
}
@ -140,11 +139,12 @@ void PTXInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
void PTXInstPrinter::printMemOperand(const MCInst *MI, unsigned OpNo,
raw_ostream &O) {
// By definition, operand OpNo+1 is an i32imm
const MCOperand &Op2 = MI->getOperand(OpNo+1);
printOperand(MI, OpNo, O);
if (MI->getOperand(OpNo+1).isImm() && MI->getOperand(OpNo+1).getImm() == 0)
if (Op2.getImm() == 0)
return; // don't print "+0"
O << "+";
printOperand(MI, OpNo+1, O);
O << "+" << Op2.getImm();
}
void PTXInstPrinter::printRoundingMode(const MCInst *MI, unsigned OpNo,

View File

@ -20,21 +20,23 @@
#include "PTXMCTargetDesc.h"
namespace llvm {
namespace PTX {
enum StateSpace {
GLOBAL = 0, // default to global state space
CONSTANT = 1,
LOCAL = 2,
PARAMETER = 3,
SHARED = 4
namespace PTXStateSpace {
enum {
Global = 0, // default to global state space
Constant = 1,
Local = 2,
Parameter = 3,
Shared = 4
};
} // namespace PTXStateSpace
enum Predicate {
PRED_NORMAL = 0,
PRED_NEGATE = 1,
PRED_NONE = 2
namespace PTXPredicate {
enum {
Normal = 0,
Negate = 1,
None = 2
};
} // namespace PTX
} // namespace PTXPredicate
/// Namespace to hold all target-specific flags.
namespace PTXRoundingMode {

View File

@ -62,9 +62,8 @@ static MCInstPrinter *createPTXMCInstPrinter(const Target &T,
unsigned SyntaxVariant,
const MCAsmInfo &MAI,
const MCSubtargetInfo &STI) {
if (SyntaxVariant == 0)
return new PTXInstPrinter(MAI, STI);
return 0;
assert(SyntaxVariant == 0 && "We only have one syntax variant");
return new PTXInstPrinter(MAI, STI);
}
extern "C" void LLVMInitializePTXTargetMC() {

View File

@ -72,11 +72,11 @@ static const char *getRegisterTypeName(unsigned RegNo,
static const char *getStateSpaceName(unsigned addressSpace) {
switch (addressSpace) {
default: llvm_unreachable("Unknown state space");
case PTX::GLOBAL: return "global";
case PTX::CONSTANT: return "const";
case PTX::LOCAL: return "local";
case PTX::PARAMETER: return "param";
case PTX::SHARED: return "shared";
case PTXStateSpace::Global: return "global";
case PTXStateSpace::Constant: return "const";
case PTXStateSpace::Local: return "local";
case PTXStateSpace::Parameter: return "param";
case PTXStateSpace::Shared: return "shared";
}
return NULL;
}
@ -279,147 +279,11 @@ void PTXAsmPrinter::EmitFunctionBodyEnd() {
}
void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
#if 0
std::string str;
str.reserve(64);
raw_string_ostream OS(str);
DebugLoc DL = MI->getDebugLoc();
if (!DL.isUnknown()) {
const MDNode *S = DL.getScope(MF->getFunction()->getContext());
// This is taken from DwarfDebug.cpp, which is conveniently not a public
// LLVM class.
StringRef Fn;
StringRef Dir;
unsigned Src = 1;
if (S) {
DIDescriptor Scope(S);
if (Scope.isCompileUnit()) {
DICompileUnit CU(S);
Fn = CU.getFilename();
Dir = CU.getDirectory();
} else if (Scope.isFile()) {
DIFile F(S);
Fn = F.getFilename();
Dir = F.getDirectory();
} else if (Scope.isSubprogram()) {
DISubprogram SP(S);
Fn = SP.getFilename();
Dir = SP.getDirectory();
} else if (Scope.isLexicalBlock()) {
DILexicalBlock DB(S);
Fn = DB.getFilename();
Dir = DB.getDirectory();
} else
assert(0 && "Unexpected scope info");
Src = GetOrCreateSourceID(Fn, Dir);
}
OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(),
0, 0, 0, Fn);
const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc();
OS << "\t.loc ";
OS << utostr(MDL.getFileNum());
OS << " ";
OS << utostr(MDL.getLine());
OS << " ";
OS << utostr(MDL.getColumn());
OS << "\n";
}
// Emit predicate
printPredicateOperand(MI, OS);
// Write instruction to str
if (MI->getOpcode() == PTX::CALL) {
printCall(MI, OS);
} else {
printInstruction(MI, OS);
}
OS << ';';
OS.flush();
StringRef strref = StringRef(str);
OutStreamer.EmitRawText(strref);
#endif
MCInst TmpInst;
LowerPTXMachineInstrToMCInst(MI, TmpInst, *this);
OutStreamer.EmitInstruction(TmpInst);
}
void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
raw_ostream &OS) {
const MachineOperand &MO = MI->getOperand(opNum);
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
switch (MO.getType()) {
default:
llvm_unreachable("<unknown operand type>");
break;
case MachineOperand::MO_GlobalAddress:
OS << *Mang->getSymbol(MO.getGlobal());
break;
case MachineOperand::MO_Immediate:
OS << (long) MO.getImm();
break;
case MachineOperand::MO_MachineBasicBlock:
OS << *MO.getMBB()->getSymbol();
break;
case MachineOperand::MO_Register:
OS << MFI->getRegisterName(MO.getReg());
break;
case MachineOperand::MO_ExternalSymbol:
OS << MO.getSymbolName();
break;
case MachineOperand::MO_FPImmediate:
APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
// Emit 0F for 32-bit floats and 0D for 64-bit doubles.
if (isFloat) {
OS << "0F";
}
else {
OS << "0D";
}
// Emit the encoded floating-point value.
if (constFP.getZExtValue() > 0) {
OS << constFP.toString(16, false);
}
else {
OS << "00000000";
// If We have a double-precision zero, pad to 8-bytes.
if (!isFloat) {
OS << "00000000";
}
}
break;
}
}
void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
raw_ostream &OS, const char *Modifier) {
printOperand(MI, opNum, OS);
if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
return; // don't print "+0"
OS << "+";
printOperand(MI, opNum+1, OS);
}
void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
raw_ostream &OS, const char *Modifier) {
//OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
OS << "__ret";
}
void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
// Check to see if this is a special global used by LLVM, if so, emit it.
if (EmitSpecialLLVMGlobal(gv))
@ -613,63 +477,6 @@ void PTXAsmPrinter::EmitFunctionEntryLabel() {
OutStreamer.EmitRawText(Twine(decl));
}
void PTXAsmPrinter::
printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
int i = MI->findFirstPredOperandIdx();
if (i == -1)
llvm_unreachable("missing predicate operand");
unsigned reg = MI->getOperand(i).getReg();
int predOp = MI->getOperand(i+1).getImm();
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
if (reg != PTX::NoRegister) {
O << '@';
if (predOp == PTX::PRED_NEGATE)
O << '!';
O << MFI->getRegisterName(reg);
}
}
void PTXAsmPrinter::
printCall(const MachineInstr *MI, raw_ostream &O) {
O << "\tcall.uni\t";
// The first two operands are the predicate slot
unsigned Index = 2;
while (!MI->getOperand(Index).isGlobal()) {
if (Index == 2) {
O << "(";
} else {
O << ", ";
}
printOperand(MI, Index, O);
Index++;
}
if (Index != 2) {
O << "), ";
}
assert(MI->getOperand(Index).isGlobal() &&
"A GlobalAddress must follow the return arguments");
const GlobalValue *Address = MI->getOperand(Index).getGlobal();
O << Address->getName() << ", (";
Index++;
while (Index < MI->getNumOperands()) {
printOperand(MI, Index, O);
if (Index < MI->getNumOperands()-1) {
O << ", ";
}
Index++;
}
O << ")";
}
unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
StringRef DirName) {
// If FE did not provide a file name, then assume stdin.
@ -704,7 +511,8 @@ MCOperand PTXAsmPrinter::GetSymbolRef(const MachineOperand &MO,
return MCOperand::CreateExpr(Expr);
}
bool PTXAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
MCOperand PTXAsmPrinter::lowerOperand(const MachineOperand &MO) {
MCOperand MCOp;
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
const MCExpr *Expr;
const char *RegSymbolName;
@ -742,7 +550,7 @@ bool PTXAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
break;
}
return true;
return MCOp;
}
// Force static initialization.

View File

@ -39,24 +39,11 @@ public:
virtual void EmitFunctionEntryLabel();
virtual void EmitInstruction(const MachineInstr *MI);
void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
const char *Modifier = 0);
void printReturnOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
const char *Modifier = 0);
void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
void printCall(const MachineInstr *MI, raw_ostream &O);
unsigned GetOrCreateSourceID(StringRef FileName,
StringRef DirName);
MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol);
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
// autogen'd.
void printInstruction(const MachineInstr *MI, raw_ostream &OS);
static const char *getRegisterName(unsigned RegNo);
MCOperand lowerOperand(const MachineOperand &MO);
private:
void EmitVariableDeclaration(const GlobalVariable *gv);

View File

@ -92,7 +92,7 @@ SDNode *PTXDAGToDAGISel::SelectBRCOND(SDNode *Node) {
SDValue Chain = Node->getOperand(0);
SDValue Pred = Node->getOperand(1);
SDValue Target = Node->getOperand(2); // branch target
SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32);
SDValue PredOp = CurDAG->getTargetConstant(PTXPredicate::Normal, MVT::i32);
DebugLoc dl = Node->getDebugLoc();
assert(Target.getOpcode() == ISD::BasicBlock);
@ -129,7 +129,7 @@ SDNode *PTXDAGToDAGISel::SelectREADPARAM(SDNode *Node) {
OpCode = PTX::READPARAMF64;
SDValue Pred = CurDAG->getRegister(PTX::NoRegister, MVT::i1);
SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NONE, MVT::i32);
SDValue PredOp = CurDAG->getTargetConstant(PTXPredicate::None, MVT::i32);
DebugLoc dl = Node->getDebugLoc();
SDValue Ops[] = { Index, Pred, PredOp, Chain };
@ -167,7 +167,7 @@ SDNode *PTXDAGToDAGISel::SelectWRITEPARAM(SDNode *Node) {
llvm_unreachable("Invalid type in SelectWRITEPARAM");
SDValue Pred = CurDAG->getRegister(PTX::NoRegister, MVT::i1);
SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NONE, MVT::i32);
SDValue PredOp = CurDAG->getTargetConstant(PTXPredicate::None, MVT::i32);
DebugLoc dl = Node->getDebugLoc();
SDValue Ops[] = { Value, Pred, PredOp, Chain };

View File

@ -167,7 +167,7 @@ DefinesPredicate(MachineInstr *MI,
return false;
Pred.push_back(MO);
Pred.push_back(MachineOperand::CreateImm(PTX::PRED_NONE));
Pred.push_back(MachineOperand::CreateImm(PTXPredicate::None));
return true;
}
@ -283,7 +283,7 @@ InsertBranch(MachineBasicBlock &MBB,
BuildMI(&MBB, DL, get(PTX::BRAdp))
.addMBB(TBB).addReg(Cond[0].getReg()).addImm(Cond[1].getImm());
BuildMI(&MBB, DL, get(PTX::BRAd))
.addMBB(FBB).addReg(PTX::NoRegister).addImm(PTX::PRED_NONE);
.addMBB(FBB).addReg(PTX::NoRegister).addImm(PTXPredicate::None);
return 2;
} else if (Cond.size()) {
BuildMI(&MBB, DL, get(PTX::BRAdp))
@ -291,7 +291,7 @@ InsertBranch(MachineBasicBlock &MBB,
return 1;
} else {
BuildMI(&MBB, DL, get(PTX::BRAd))
.addMBB(TBB).addReg(PTX::NoRegister).addImm(PTX::PRED_NONE);
.addMBB(TBB).addReg(PTX::NoRegister).addImm(PTXPredicate::None);
return 1;
}
}
@ -319,7 +319,7 @@ MachineSDNode *PTXInstrInfo::
GetPTXMachineNode(SelectionDAG *DAG, unsigned Opcode,
DebugLoc dl, EVT VT, SDValue Op1) {
SDValue predReg = DAG->getRegister(PTX::NoRegister, MVT::i1);
SDValue predOp = DAG->getTargetConstant(PTX::PRED_NONE, MVT::i32);
SDValue predOp = DAG->getTargetConstant(PTXPredicate::None, MVT::i32);
SDValue ops[] = { Op1, predReg, predOp };
return DAG->getMachineNode(Opcode, dl, VT, ops, array_lengthof(ops));
}
@ -328,7 +328,7 @@ MachineSDNode *PTXInstrInfo::
GetPTXMachineNode(SelectionDAG *DAG, unsigned Opcode,
DebugLoc dl, EVT VT, SDValue Op1, SDValue Op2) {
SDValue predReg = DAG->getRegister(PTX::NoRegister, MVT::i1);
SDValue predOp = DAG->getTargetConstant(PTX::PRED_NONE, MVT::i32);
SDValue predOp = DAG->getTargetConstant(PTXPredicate::None, MVT::i32);
SDValue ops[] = { Op1, Op2, predReg, predOp };
return DAG->getMachineNode(Opcode, dl, VT, ops, array_lengthof(ops));
}
@ -336,7 +336,7 @@ GetPTXMachineNode(SelectionDAG *DAG, unsigned Opcode,
void PTXInstrInfo::AddDefaultPredicate(MachineInstr *MI) {
if (MI->findFirstPredOperandIdx() == -1) {
MI->addOperand(MachineOperand::CreateReg(PTX::NoRegister, /*IsDef=*/false));
MI->addOperand(MachineOperand::CreateImm(PTX::PRED_NONE));
MI->addOperand(MachineOperand::CreateImm(PTXPredicate::None));
}
}

View File

@ -26,7 +26,7 @@ def load_global : PatFrag<(ops node:$ptr), (load node:$ptr), [{
const PointerType *PT;
if ((Src = cast<LoadSDNode>(N)->getSrcValue()) &&
(PT = dyn_cast<PointerType>(Src->getType())))
return PT->getAddressSpace() == PTX::GLOBAL;
return PT->getAddressSpace() == PTXStateSpace::Global;
return false;
}]>;
@ -35,7 +35,7 @@ def load_constant : PatFrag<(ops node:$ptr), (load node:$ptr), [{
const PointerType *PT;
if ((Src = cast<LoadSDNode>(N)->getSrcValue()) &&
(PT = dyn_cast<PointerType>(Src->getType())))
return PT->getAddressSpace() == PTX::CONSTANT;
return PT->getAddressSpace() == PTXStateSpace::Constant;
return false;
}]>;
@ -44,7 +44,7 @@ def load_shared : PatFrag<(ops node:$ptr), (load node:$ptr), [{
const PointerType *PT;
if ((Src = cast<LoadSDNode>(N)->getSrcValue()) &&
(PT = dyn_cast<PointerType>(Src->getType())))
return PT->getAddressSpace() == PTX::SHARED;
return PT->getAddressSpace() == PTXStateSpace::Shared;
return false;
}]>;
@ -54,7 +54,7 @@ def store_global
const PointerType *PT;
if ((Src = cast<StoreSDNode>(N)->getSrcValue()) &&
(PT = dyn_cast<PointerType>(Src->getType())))
return PT->getAddressSpace() == PTX::GLOBAL;
return PT->getAddressSpace() == PTXStateSpace::Global;
return false;
}]>;
@ -64,7 +64,7 @@ def store_shared
const PointerType *PT;
if ((Src = cast<StoreSDNode>(N)->getSrcValue()) &&
(PT = dyn_cast<PointerType>(Src->getType())))
return PT->getAddressSpace() == PTX::SHARED;
return PT->getAddressSpace() == PTXStateSpace::Shared;
return false;
}]>;

View File

@ -100,7 +100,7 @@ public:
/// @{
virtual void ChangeSection(const MCSection *Section);
virtual void InitSections() {}
virtual void InitSections() { /* PTX does not use sections */ }
virtual void EmitLabel(MCSymbol *Symbol);
@ -235,7 +235,7 @@ void PTXMCAsmStreamer::ChangeSection(const MCSection *Section) {
void PTXMCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
//assert(getCurrentSection() && "Cannot emit before setting section!");
assert(getCurrentSection() && "Cannot emit before setting section!");
OS << *Symbol << MAI.getLabelSuffix();
EmitEOL();
@ -499,7 +499,7 @@ bool PTXMCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
void PTXMCAsmStreamer::AddEncodingComment(const MCInst &Inst) {}
void PTXMCAsmStreamer::EmitInstruction(const MCInst &Inst) {
//assert(getCurrentSection() && "Cannot emit contents before setting section!");
assert(getCurrentSection() && "Cannot emit contents before setting section!");
// Show the encoding in a comment if we have a code emitter.
if (Emitter)

View File

@ -26,8 +26,7 @@ void llvm::LowerPTXMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const MachineOperand &MO = MI->getOperand(i);
MCOperand MCOp;
if (AP.lowerOperand(MO, MCOp))
OutMI.addOperand(MCOp);
OutMI.addOperand(AP.lowerOperand(MO));
}
}