O << '\n';
for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
I != E; ++I) {
- std::string Name = Mang->getMangledName(I);
+ MCSymbol *Name = GetGlobalValueSymbol(I);
const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
- std::string Target = Mang->getMangledName(GV);
+ MCSymbol *Target = GetGlobalValueSymbol(GV);
- if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
- O << "\t.globl\t" << Name << '\n';
- else if (I->hasWeakLinkage())
- O << MAI->getWeakRefDirective() << Name << '\n';
- else if (!I->hasLocalLinkage())
- llvm_unreachable("Invalid alias linkage");
+ if (I->hasExternalLinkage() || !MAI->getWeakRefDirective()) {
+ O << "\t.globl\t";
+ Name->print(O, MAI);
+ O << '\n';
+ } else if (I->hasWeakLinkage()) {
+ O << MAI->getWeakRefDirective();
+ Name->print(O, MAI);
+ O << '\n';
+ } else {
+ assert(!I->hasLocalLinkage() && "Invalid alias linkage");
+ }
printVisibility(Name, I->getVisibility());
- O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
+ O << MAI->getSetDirective() << ' ';
+ Name->print(O, MAI);
+ O << ", ";
+ Target->print(O, MAI);
+ O << '\n';
}
}
}
void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
- // What's my mangled name?
- CurrentFnName = Mang->getMangledName(MF.getFunction());
+ // Get the function symbol.
+ CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
IncrementFunctionNumber();
if (VerboseAsm)
const MCSection *S;
unsigned Alignment;
SmallVector<unsigned, 4> CPEs;
- SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
+ SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
};
}
/// EmitString - Emit a string with quotes and a null terminator.
/// Special characters are emitted properly.
/// \literal (Eg. '\t') \endliteral
-void AsmPrinter::EmitString(const std::string &String) const {
- EmitString(String.c_str(), String.size());
+void AsmPrinter::EmitString(const StringRef String) const {
+ EmitString(String.data(), String.size());
}
void AsmPrinter::EmitString(const char *String, unsigned Size) const {
// Print out the specified constant, without a storage class. Only the
// constants valid in constant expressions can occur here.
void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
- if (CV->isNullValue() || isa<UndefValue>(CV))
+ if (CV->isNullValue() || isa<UndefValue>(CV)) {
O << '0';
- else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
+ return;
+ }
+
+ if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
O << CI->getZExtValue();
- } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
+ return;
+ }
+
+ if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
// This is a constant address for a global variable or function. Use the
// name of the variable or function as the address value.
O << Mang->getMangledName(GV);
- } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
+ return;
+ }
+
+ if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
+ GetBlockAddressSymbol(BA)->print(O, MAI);
+ return;
+ }
+
+ const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
+ if (CE == 0) {
+ llvm_unreachable("Unknown constant value!");
+ O << '0';
+ return;
+ }
+
+ switch (CE->getOpcode()) {
+ case Instruction::ZExt:
+ case Instruction::SExt:
+ case Instruction::FPTrunc:
+ case Instruction::FPExt:
+ case Instruction::UIToFP:
+ case Instruction::SIToFP:
+ case Instruction::FPToUI:
+ case Instruction::FPToSI:
+ default:
+ llvm_unreachable("FIXME: Don't support this constant cast expr");
+ case Instruction::GetElementPtr: {
+ // generate a symbolic expression for the byte address
const TargetData *TD = TM.getTargetData();
- unsigned Opcode = CE->getOpcode();
- switch (Opcode) {
- case Instruction::Trunc:
- case Instruction::ZExt:
- case Instruction::SExt:
- case Instruction::FPTrunc:
- case Instruction::FPExt:
- case Instruction::UIToFP:
- case Instruction::SIToFP:
- case Instruction::FPToUI:
- case Instruction::FPToSI:
- llvm_unreachable("FIXME: Don't support this constant cast expr");
- case Instruction::GetElementPtr: {
- // generate a symbolic expression for the byte address
- const Constant *ptrVal = CE->getOperand(0);
- SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
- if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
- idxVec.size())) {
- // Truncate/sext the offset to the pointer size.
- if (TD->getPointerSizeInBits() != 64) {
- int SExtAmount = 64-TD->getPointerSizeInBits();
- Offset = (Offset << SExtAmount) >> SExtAmount;
- }
-
- if (Offset)
- O << '(';
- EmitConstantValueOnly(ptrVal);
- if (Offset > 0)
- O << ") + " << Offset;
- else if (Offset < 0)
- O << ") - " << -Offset;
- } else {
- EmitConstantValueOnly(ptrVal);
- }
- break;
+ const Constant *ptrVal = CE->getOperand(0);
+ SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
+ int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
+ idxVec.size());
+ if (Offset == 0)
+ return EmitConstantValueOnly(ptrVal);
+
+ // Truncate/sext the offset to the pointer size.
+ if (TD->getPointerSizeInBits() != 64) {
+ int SExtAmount = 64-TD->getPointerSizeInBits();
+ Offset = (Offset << SExtAmount) >> SExtAmount;
}
- case Instruction::BitCast:
- return EmitConstantValueOnly(CE->getOperand(0));
-
- case Instruction::IntToPtr: {
- // Handle casts to pointers by changing them into casts to the appropriate
- // integer type. This promotes constant folding and simplifies this code.
- Constant *Op = CE->getOperand(0);
- Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
- false/*ZExt*/);
+
+ if (Offset)
+ O << '(';
+ EmitConstantValueOnly(ptrVal);
+ if (Offset > 0)
+ O << ") + " << Offset;
+ else
+ O << ") - " << -Offset;
+ return;
+ }
+ case Instruction::BitCast:
+ return EmitConstantValueOnly(CE->getOperand(0));
+
+ case Instruction::IntToPtr: {
+ // Handle casts to pointers by changing them into casts to the appropriate
+ // integer type. This promotes constant folding and simplifies this code.
+ const TargetData *TD = TM.getTargetData();
+ Constant *Op = CE->getOperand(0);
+ Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
+ false/*ZExt*/);
+ return EmitConstantValueOnly(Op);
+ }
+
+ case Instruction::PtrToInt: {
+ // Support only foldable casts to/from pointers that can be eliminated by
+ // changing the pointer to the appropriately sized integer type.
+ Constant *Op = CE->getOperand(0);
+ const Type *Ty = CE->getType();
+ const TargetData *TD = TM.getTargetData();
+
+ // We can emit the pointer value into this slot if the slot is an
+ // integer slot greater or equal to the size of the pointer.
+ if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
return EmitConstantValueOnly(Op);
- }
-
+
+ O << "((";
+ EmitConstantValueOnly(Op);
+ APInt ptrMask =
+ APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
+
+ SmallString<40> S;
+ ptrMask.toStringUnsigned(S);
+ O << ") & " << S.str() << ')';
+ return;
+ }
- case Instruction::PtrToInt: {
- // Support only foldable casts to/from pointers that can be eliminated by
- // changing the pointer to the appropriately sized integer type.
- Constant *Op = CE->getOperand(0);
- const Type *Ty = CE->getType();
-
- // We can emit the pointer value into this slot if the slot is an
- // integer slot greater or equal to the size of the pointer.
- if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
- return EmitConstantValueOnly(Op);
-
- O << "((";
- EmitConstantValueOnly(Op);
- APInt ptrMask =
- APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
+ case Instruction::Trunc:
+ // We emit the value and depend on the assembler to truncate the generated
+ // expression properly. This is important for differences between
+ // blockaddress labels. Since the two labels are in the same function, it
+ // is reasonable to treat their delta as a 32-bit value.
+ return EmitConstantValueOnly(CE->getOperand(0));
- SmallString<40> S;
- ptrMask.toStringUnsigned(S);
- O << ") & " << S.str() << ')';
- break;
- }
+ case Instruction::Add:
+ case Instruction::Sub:
+ case Instruction::And:
+ case Instruction::Or:
+ case Instruction::Xor:
+ O << '(';
+ EmitConstantValueOnly(CE->getOperand(0));
+ O << ')';
+ switch (CE->getOpcode()) {
case Instruction::Add:
+ O << " + ";
+ break;
case Instruction::Sub:
+ O << " - ";
+ break;
case Instruction::And:
+ O << " & ";
+ break;
case Instruction::Or:
+ O << " | ";
+ break;
case Instruction::Xor:
- O << '(';
- EmitConstantValueOnly(CE->getOperand(0));
- O << ')';
- switch (Opcode) {
- case Instruction::Add:
- O << " + ";
- break;
- case Instruction::Sub:
- O << " - ";
- break;
- case Instruction::And:
- O << " & ";
- break;
- case Instruction::Or:
- O << " | ";
- break;
- case Instruction::Xor:
- O << " ^ ";
- break;
- default:
- break;
- }
- O << '(';
- EmitConstantValueOnly(CE->getOperand(1));
- O << ')';
- break;
+ O << " ^ ";
+ break;
default:
- llvm_unreachable("Unsupported operator!");
+ break;
}
- } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
- GetBlockAddressSymbol(BA)->print(O, MAI);
- } else {
- llvm_unreachable("Unknown constant value!");
+ O << '(';
+ EmitConstantValueOnly(CE->getOperand(1));
+ O << ')';
+ break;
}
}
unsigned AddrSpace) {
const TargetData *TD = TM.getTargetData();
unsigned BitWidth = CI->getBitWidth();
- assert(isPowerOf2_32(BitWidth) &&
- "Non-power-of-2-sized integers not handled!");
+ assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
// We don't expect assemblers to support integer data directives
// for more than 64 bits, so we emit the data in at most 64-bit
else
Val = RawData[i];
- if (MAI->getData64bitsDirective(AddrSpace))
+ if (MAI->getData64bitsDirective(AddrSpace)) {
O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
- else if (TD->isBigEndian()) {
- O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
- if (VerboseAsm) {
- O.PadToColumn(MAI->getCommentColumn());
- O << MAI->getCommentString()
- << " most significant half of i64 " << Val;
- }
- O << '\n';
- O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
- if (VerboseAsm) {
- O.PadToColumn(MAI->getCommentColumn());
- O << MAI->getCommentString()
- << " least significant half of i64 " << Val;
- }
- O << '\n';
- } else {
- O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
- if (VerboseAsm) {
- O.PadToColumn(MAI->getCommentColumn());
- O << MAI->getCommentString()
- << " least significant half of i64 " << Val;
- }
- O << '\n';
- O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
- if (VerboseAsm) {
- O.PadToColumn(MAI->getCommentColumn());
- O << MAI->getCommentString()
- << " most significant half of i64 " << Val;
- }
- O << '\n';
+ continue;
+ }
+
+ // Emit two 32-bit chunks, order depends on endianness.
+ unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
+ const char *FirstName = " least", *SecondName = " most";
+ if (TD->isBigEndian()) {
+ std::swap(FirstChunk, SecondChunk);
+ std::swap(FirstName, SecondName);
+ }
+
+ O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
+ if (VerboseAsm) {
+ O.PadToColumn(MAI->getCommentColumn());
+ O << MAI->getCommentString()
+ << FirstName << " significant half of i64 " << Val;
}
+ O << '\n';
+
+ O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
+ if (VerboseAsm) {
+ O.PadToColumn(MAI->getCommentColumn());
+ O << MAI->getCommentString()
+ << SecondName << " significant half of i64 " << Val;
+ }
+ O << '\n';
}
}
if (CV->isNullValue() || isa<UndefValue>(CV)) {
EmitZeros(Size, AddrSpace);
return;
- } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
+ }
+
+ if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
EmitGlobalConstantArray(CVA , AddrSpace);
return;
- } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
+ }
+
+ if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
EmitGlobalConstantStruct(CVS, AddrSpace);
return;
- } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
+ }
+
+ if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
EmitGlobalConstantFP(CFP, AddrSpace);
return;
- } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
+ }
+
+ if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
+ // If we can directly emit an 8-byte constant, do it.
+ if (Size == 8)
+ if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
+ O << Data64Dir << CI->getZExtValue() << '\n';
+ return;
+ }
+
// Small integers are handled below; large integers are handled here.
if (Size > 4) {
EmitGlobalConstantLargeInt(CI, AddrSpace);
return;
}
- } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
+ }
+
+ if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
EmitGlobalConstantVector(CP);
return;
}
unsigned L = DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
CurDLT.Scope);
printLabel(L);
+ O << '\n';
DW->BeginScope(MI, L);
PrevDLT = CurDLT;
}
/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
/// instruction, using the specified assembler variant. Targets should
-/// overried this to format as appropriate.
+/// override this to format as appropriate.
bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
unsigned AsmVariant, const char *ExtraCode) {
// Target doesn't support this yet!
return true;
}
-MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const {
- return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock());
+MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
+ const char *Suffix) const {
+ return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
}
MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
- const BasicBlock *BB) const {
+ const BasicBlock *BB,
+ const char *Suffix) const {
assert(BB->hasName() &&
"Address of anonymous basic block not supported yet!");
// This code must use the function name itself, and not the function number,
// since it must be possible to generate the label name from within other
// functions.
- std::string FuncName = Mang->getMangledName(F);
+ SmallString<60> FnName;
+ Mang->getNameWithPrefix(FnName, F, false);
- SmallString<60> Name;
- raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BA"
- << FuncName.size() << '_' << FuncName << '_'
- << Mang->makeNameProper(BB->getName());
+ // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
+ SmallString<60> NameResult;
+ Mang->getNameWithPrefix(NameResult,
+ StringRef("BA") + Twine((unsigned)FnName.size()) +
+ "_" + FnName.str() + "_" + BB->getName() + Suffix,
+ Mangler::Private);
- return OutContext.GetOrCreateSymbol(Name.str());
+ return OutContext.GetOrCreateSymbol(NameResult.str());
}
MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
return OutContext.GetOrCreateSymbol(Name.str());
}
+/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
+/// value.
+MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
+ SmallString<60> NameStr;
+ Mang->getNameWithPrefix(NameStr, GV, false);
+ return OutContext.GetOrCreateSymbol(NameStr.str());
+}
+
+/// GetPrivateGlobalValueSymbolStub - Return the MCSymbol for a symbol with
+/// global value name as its base, with the specified suffix, and where the
+/// symbol is forced to have private linkage.
+MCSymbol *AsmPrinter::GetPrivateGlobalValueSymbolStub(const GlobalValue *GV,
+ StringRef Suffix) const {
+ SmallString<60> NameStr;
+ Mang->getNameWithPrefix(NameStr, GV, true);
+ NameStr.append(Suffix.begin(), Suffix.end());
+ return OutContext.GetOrCreateSymbol(NameStr.str());
+}
+
+/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
+/// ExternalSymbol.
+MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
+ SmallString<60> NameStr;
+ Mang->getNameWithPrefix(NameStr, Sym);
+ return OutContext.GetOrCreateSymbol(NameStr.str());
+}
+
/// EmitBasicBlockStart - This method prints the label for the specified
/// MachineBasicBlock, an alignment (if present) and a comment describing
}
}
-void AsmPrinter::printVisibility(const std::string& Name,
+void AsmPrinter::printVisibility(const MCSymbol *Sym,
unsigned Visibility) const {
if (Visibility == GlobalValue::HiddenVisibility) {
- if (const char *Directive = MAI->getHiddenDirective())
- O << Directive << Name << '\n';
+ if (const char *Directive = MAI->getHiddenDirective()) {
+ O << Directive;
+ Sym->print(O, MAI);
+ O << '\n';
+ }
} else if (Visibility == GlobalValue::ProtectedVisibility) {
- if (const char *Directive = MAI->getProtectedDirective())
- O << Directive << Name << '\n';
+ if (const char *Directive = MAI->getProtectedDirective()) {
+ O << Directive;
+ Sym->print(O, MAI);
+ O << '\n';
+ }
}
}
// Print source line info.
O.PadToColumn(MAI->getCommentColumn());
- O << MAI->getCommentString() << " SrcLine ";
- if (DLT.Scope) {
- DICompileUnit CU(DLT.Scope);
- if (!CU.isNull())
- O << CU.getFilename() << " ";
- }
- O << DLT.Line;
+ O << MAI->getCommentString() << ' ';
+ DIScope Scope(DLT.Scope);
+ // Omit the directory, because it's likely to be long and uninteresting.
+ if (!Scope.isNull())
+ O << Scope.getFilename();
+ else
+ O << "<unknown>";
+ O << ':' << DLT.Line;
if (DLT.Col != 0)
- O << ":" << DLT.Col;
+ O << ':' << DLT.Col;
Newline = true;
}
+ // Check for spills and reloads
+ int FI;
+
+ const MachineFrameInfo *FrameInfo =
+ MI.getParent()->getParent()->getFrameInfo();
+
+ // We assume a single instruction only has a spill or reload, not
+ // both.
+ const MachineMemOperand *MMO;
+ if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
+ if (FrameInfo->isSpillSlotObjectIndex(FI)) {
+ MMO = *MI.memoperands_begin();
+ if (Newline) O << '\n';
+ O.PadToColumn(MAI->getCommentColumn());
+ O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
+ Newline = true;
+ }
+ }
+ else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
+ if (FrameInfo->isSpillSlotObjectIndex(FI)) {
+ if (Newline) O << '\n';
+ O.PadToColumn(MAI->getCommentColumn());
+ O << MAI->getCommentString() << ' '
+ << MMO->getSize() << "-byte Folded Reload";
+ Newline = true;
+ }
+ }
+ else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
+ if (FrameInfo->isSpillSlotObjectIndex(FI)) {
+ MMO = *MI.memoperands_begin();
+ if (Newline) O << '\n';
+ O.PadToColumn(MAI->getCommentColumn());
+ O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
+ Newline = true;
+ }
+ }
+ else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
+ if (FrameInfo->isSpillSlotObjectIndex(FI)) {
+ if (Newline) O << '\n';
+ O.PadToColumn(MAI->getCommentColumn());
+ O << MAI->getCommentString() << ' '
+ << MMO->getSize() << "-byte Folded Spill";
+ Newline = true;
+ }
+ }
+
+ // Check for spill-induced copies
+ unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
+ if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
+ SrcSubIdx, DstSubIdx)) {
+ if (MI.getAsmPrinterFlag(ReloadReuse)) {
+ if (Newline) O << '\n';
+ O.PadToColumn(MAI->getCommentColumn());
+ O << MAI->getCommentString() << " Reload Reuse";
+ }
+ }
}
/// PrintChildLoopComment - Print comments about child loops within