From: Matt Arsenault Date: Tue, 16 Dec 2014 19:16:17 +0000 (+0000) Subject: NVPTX: Remove duplicate of AsmPrinter::lowerConstant X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=ec6fabb9d325c43826f8bbacee3911a0f66d5543;p=oota-llvm.git NVPTX: Remove duplicate of AsmPrinter::lowerConstant git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224355 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/lib/Target/NVPTX/NVPTXAsmPrinter.cpp index fe8452e094e..abe117d0e5b 100644 --- a/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ b/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -108,160 +108,6 @@ void VisitGlobalVariableForEmission( } } -// @TODO: This is a copy from AsmPrinter.cpp. The function is static, so we -// cannot just link to the existing version. -/// LowerConstant - Lower the specified LLVM Constant to an MCExpr. -/// -using namespace nvptx; -const MCExpr *nvptx::LowerConstant(const Constant *CV, AsmPrinter &AP) { - MCContext &Ctx = AP.OutContext; - - if (CV->isNullValue() || isa(CV)) - return MCConstantExpr::Create(0, Ctx); - - if (const ConstantInt *CI = dyn_cast(CV)) - return MCConstantExpr::Create(CI->getZExtValue(), Ctx); - - if (const GlobalValue *GV = dyn_cast(CV)) - return MCSymbolRefExpr::Create(AP.getSymbol(GV), Ctx); - - if (const BlockAddress *BA = dyn_cast(CV)) - return MCSymbolRefExpr::Create(AP.GetBlockAddressSymbol(BA), Ctx); - - const ConstantExpr *CE = dyn_cast(CV); - if (!CE) - llvm_unreachable("Unknown constant value to lower!"); - - switch (CE->getOpcode()) { - default: - // If the code isn't optimized, there may be outstanding folding - // opportunities. Attempt to fold the expression using DataLayout as a - // last resort before giving up. - if (Constant *C = ConstantFoldConstantExpression( - CE, AP.TM.getSubtargetImpl()->getDataLayout())) - if (C != CE) - return LowerConstant(C, AP); - - // Otherwise report the problem to the user. - { - std::string S; - raw_string_ostream OS(S); - OS << "Unsupported expression in static initializer: "; - CE->printAsOperand(OS, /*PrintType=*/ false, - !AP.MF ? nullptr : AP.MF->getFunction()->getParent()); - report_fatal_error(OS.str()); - } - case Instruction::AddrSpaceCast: { - // Strip any addrspace(1)->addrspace(0) addrspace casts. These will be - // handled by the generic() logic in the MCExpr printer - PointerType *DstTy = cast(CE->getType()); - PointerType *SrcTy = cast(CE->getOperand(0)->getType()); - if (SrcTy->getAddressSpace() == 1 && DstTy->getAddressSpace() == 0) { - return LowerConstant(cast(CE->getOperand(0)), AP); - } - std::string S; - raw_string_ostream OS(S); - OS << "Unsupported expression in static initializer: "; - CE->printAsOperand(OS, /*PrintType=*/ false, - !AP.MF ? nullptr : AP.MF->getFunction()->getParent()); - report_fatal_error(OS.str()); - } - case Instruction::GetElementPtr: { - const DataLayout &TD = *AP.TM.getSubtargetImpl()->getDataLayout(); - // Generate a symbolic expression for the byte address - APInt OffsetAI(TD.getPointerSizeInBits(), 0); - cast(CE)->accumulateConstantOffset(TD, OffsetAI); - - const MCExpr *Base = LowerConstant(CE->getOperand(0), AP); - if (!OffsetAI) - return Base; - - int64_t Offset = OffsetAI.getSExtValue(); - return MCBinaryExpr::CreateAdd(Base, MCConstantExpr::Create(Offset, Ctx), - Ctx); - } - - 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. - // FALL THROUGH. - case Instruction::BitCast: - return LowerConstant(CE->getOperand(0), AP); - - case Instruction::IntToPtr: { - const DataLayout &TD = *AP.TM.getSubtargetImpl()->getDataLayout(); - // 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*/); - return LowerConstant(Op, AP); - } - - case Instruction::PtrToInt: { - const DataLayout &TD = *AP.TM.getSubtargetImpl()->getDataLayout(); - // 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); - Type *Ty = CE->getType(); - - const MCExpr *OpExpr = LowerConstant(Op, AP); - - // We can emit the pointer value into this slot if the slot is an - // integer slot equal to the size of the pointer. - if (TD.getTypeAllocSize(Ty) == TD.getTypeAllocSize(Op->getType())) - return OpExpr; - - // Otherwise the pointer is smaller than the resultant integer, mask off - // the high bits so we are sure to get a proper truncation if the input is - // a constant expr. - unsigned InBits = TD.getTypeAllocSizeInBits(Op->getType()); - const MCExpr *MaskExpr = - MCConstantExpr::Create(~0ULL >> (64 - InBits), Ctx); - return MCBinaryExpr::CreateAnd(OpExpr, MaskExpr, Ctx); - } - - // The MC library also has a right-shift operator, but it isn't consistently - // signed or unsigned between different targets. - case Instruction::Add: - case Instruction::Sub: - case Instruction::Mul: - case Instruction::SDiv: - case Instruction::SRem: - case Instruction::Shl: - case Instruction::And: - case Instruction::Or: - case Instruction::Xor: { - const MCExpr *LHS = LowerConstant(CE->getOperand(0), AP); - const MCExpr *RHS = LowerConstant(CE->getOperand(1), AP); - switch (CE->getOpcode()) { - default: - llvm_unreachable("Unknown binary operator constant cast expr"); - case Instruction::Add: - return MCBinaryExpr::CreateAdd(LHS, RHS, Ctx); - case Instruction::Sub: - return MCBinaryExpr::CreateSub(LHS, RHS, Ctx); - case Instruction::Mul: - return MCBinaryExpr::CreateMul(LHS, RHS, Ctx); - case Instruction::SDiv: - return MCBinaryExpr::CreateDiv(LHS, RHS, Ctx); - case Instruction::SRem: - return MCBinaryExpr::CreateMod(LHS, RHS, Ctx); - case Instruction::Shl: - return MCBinaryExpr::CreateShl(LHS, RHS, Ctx); - case Instruction::And: - return MCBinaryExpr::CreateAnd(LHS, RHS, Ctx); - case Instruction::Or: - return MCBinaryExpr::CreateOr(LHS, RHS, Ctx); - case Instruction::Xor: - return MCBinaryExpr::CreateXor(LHS, RHS, Ctx); - } - } - } -} - void NVPTXAsmPrinter::emitLineNumberAsDotLoc(const MachineInstr &MI) { if (!EmitLineNumbers) return; @@ -1895,7 +1741,7 @@ void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) { } return; } else { - O << *LowerConstant(CPV, *this); + O << *lowerConstant(CPV); return; } } diff --git a/lib/Target/NVPTX/NVPTXAsmPrinter.h b/lib/Target/NVPTX/NVPTXAsmPrinter.h index 83fa5d3e09a..c11b5793b22 100644 --- a/lib/Target/NVPTX/NVPTXAsmPrinter.h +++ b/lib/Target/NVPTX/NVPTXAsmPrinter.h @@ -39,13 +39,6 @@ // A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer // (subclass of MCStreamer). -// This is defined in AsmPrinter.cpp. -// Used to process the constant expressions in initializers. -namespace nvptx { -const llvm::MCExpr * -LowerConstant(const llvm::Constant *CV, llvm::AsmPrinter &AP); -} - namespace llvm { class LineReader { @@ -167,7 +160,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter { O << *Name; } } else if (const ConstantExpr *Cexpr = dyn_cast(v)) { - O << *nvptx::LowerConstant(Cexpr, AP); + O << *AP.lowerConstant(Cexpr); } else llvm_unreachable("symbol type unknown"); nSym++;