llvm.org GIT mirror llvm / 52492ac
Change constantexpr global variable initializers to convert the constants to MCExpr then emit them through MCStreamer with EmitValue. I think all global variable initializers are now going through mcstreamer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94293 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
4 changed file(s) with 87 addition(s) and 136 deletion(s). Raw diff Collapse all Expand all
346346 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
347347
348348 protected:
349 /// EmitConstantValueOnly - Print out the specified constant, without a
350 /// storage class. Only constants of first-class type are allowed here.
351 void EmitConstantValueOnly(const Constant *CV);
352
353349 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
354350
355351 /// processDebugLoc - Processes the debug information of each machine
2525 #include "llvm/CodeGen/MachineModuleInfo.h"
2626 #include "llvm/Analysis/DebugInfo.h"
2727 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCExpr.h"
2829 #include "llvm/MC/MCInst.h"
2930 #include "llvm/MC/MCSection.h"
3031 #include "llvm/MC/MCStreamer.h"
757758 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
758759 }
759760
760 // Print out the specified constant, without a storage class. Only the
761 // constants valid in constant expressions can occur here.
762 void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
763 if (CV->isNullValue() || isa(CV)) {
764 O << '0';
765 return;
766 }
767
768 if (const ConstantInt *CI = dyn_cast(CV)) {
769 O << CI->getZExtValue();
770 return;
771 }
772
773 if (const GlobalValue *GV = dyn_cast(CV)) {
774 // This is a constant address for a global variable or function. Use the
775 // name of the variable or function as the address value.
776 O << *GetGlobalValueSymbol(GV);
777 return;
778 }
779
780 if (const BlockAddress *BA = dyn_cast(CV)) {
781 O << *GetBlockAddressSymbol(BA);
782 return;
783 }
761 /// LowerConstant - Lower the specified LLVM Constant to an MCExpr.
762 ///
763 static const MCExpr *LowerConstant(const Constant *CV, AsmPrinter &AP) {
764 MCContext &Ctx = AP.OutContext;
765
766 if (CV->isNullValue() || isa(CV))
767 return MCConstantExpr::Create(0, Ctx);
768
769 if (const ConstantInt *CI = dyn_cast(CV))
770 return MCConstantExpr::Create(CI->getZExtValue(), Ctx);
771
772 if (const GlobalValue *GV = dyn_cast(CV))
773 return MCSymbolRefExpr::Create(AP.GetGlobalValueSymbol(GV), Ctx);
774 if (const BlockAddress *BA = dyn_cast(CV))
775 return MCSymbolRefExpr::Create(AP.GetBlockAddressSymbol(BA), Ctx);
784776
785777 const ConstantExpr *CE = dyn_cast(CV);
786778 if (CE == 0) {
787 llvm_unreachable("Unknown constant value!");
788 O << '0';
789 return;
779 llvm_unreachable("Unknown constant value to lower!");
780 return MCConstantExpr::Create(0, Ctx);
790781 }
791782
792783 switch (CE->getOpcode()) {
798789 case Instruction::SIToFP:
799790 case Instruction::FPToUI:
800791 case Instruction::FPToSI:
801 default:
802 llvm_unreachable("FIXME: Don't support this constant cast expr");
792 default: llvm_unreachable("FIXME: Don't support this constant cast expr");
803793 case Instruction::GetElementPtr: {
804 // generate a symbolic expression for the byte address
805 const TargetData *TD = TM.getTargetData();
806 const Constant *ptrVal = CE->getOperand(0);
807 SmallVector idxVec(CE->op_begin()+1, CE->op_end());
808 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
809 idxVec.size());
794 const TargetData &TD = *AP.TM.getTargetData();
795 // Generate a symbolic expression for the byte address
796 const Constant *PtrVal = CE->getOperand(0);
797 SmallVector IdxVec(CE->op_begin()+1, CE->op_end());
798 int64_t Offset = TD.getIndexedOffset(PtrVal->getType(), &IdxVec[0],
799 IdxVec.size());
800
801 const MCExpr *Base = LowerConstant(CE->getOperand(0), AP);
810802 if (Offset == 0)
811 return EmitConstantValueOnly(ptrVal);
803 return Base;
812804
813805 // Truncate/sext the offset to the pointer size.
814 if (TD->getPointerSizeInBits() != 64) {
815 int SExtAmount = 64-TD->getPointerSizeInBits();
806 if (TD.getPointerSizeInBits() != 64) {
807 int SExtAmount = 64-TD.getPointerSizeInBits();
816808 Offset = (Offset << SExtAmount) >> SExtAmount;
817809 }
818810
819 if (Offset)
820 O << '(';
821 EmitConstantValueOnly(ptrVal);
822 if (Offset > 0)
823 O << ") + " << Offset;
824 else
825 O << ") - " << -Offset;
826 return;
827 }
828 case Instruction::BitCast:
829 return EmitConstantValueOnly(CE->getOperand(0));
830
831 case Instruction::IntToPtr: {
832 // Handle casts to pointers by changing them into casts to the appropriate
833 // integer type. This promotes constant folding and simplifies this code.
834 const TargetData *TD = TM.getTargetData();
835 Constant *Op = CE->getOperand(0);
836 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
837 false/*ZExt*/);
838 return EmitConstantValueOnly(Op);
839 }
840
841 case Instruction::PtrToInt: {
842 // Support only foldable casts to/from pointers that can be eliminated by
843 // changing the pointer to the appropriately sized integer type.
844 Constant *Op = CE->getOperand(0);
845 const Type *Ty = CE->getType();
846 const TargetData *TD = TM.getTargetData();
847
848 // We can emit the pointer value into this slot if the slot is an
849 // integer slot greater or equal to the size of the pointer.
850 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
851 return EmitConstantValueOnly(Op);
852
853 O << "((";
854 EmitConstantValueOnly(Op);
855 APInt ptrMask =
856 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
857
858 SmallString<40> S;
859 ptrMask.toStringUnsigned(S);
860 O << ") & " << S.str() << ')';
861 return;
811 return MCBinaryExpr::CreateAdd(Base, MCConstantExpr::Create(Offset, Ctx),
812 Ctx);
862813 }
863814
864815 case Instruction::Trunc:
866817 // expression properly. This is important for differences between
867818 // blockaddress labels. Since the two labels are in the same function, it
868819 // is reasonable to treat their delta as a 32-bit value.
869 return EmitConstantValueOnly(CE->getOperand(0));
820 // FALL THROUGH.
821 case Instruction::BitCast:
822 return LowerConstant(CE->getOperand(0), AP);
823
824 case Instruction::IntToPtr: {
825 const TargetData &TD = *AP.TM.getTargetData();
826 // Handle casts to pointers by changing them into casts to the appropriate
827 // integer type. This promotes constant folding and simplifies this code.
828 Constant *Op = CE->getOperand(0);
829 Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()),
830 false/*ZExt*/);
831 return LowerConstant(Op, AP);
832 }
833
834 case Instruction::PtrToInt: {
835 const TargetData &TD = *AP.TM.getTargetData();
836 // Support only foldable casts to/from pointers that can be eliminated by
837 // changing the pointer to the appropriately sized integer type.
838 Constant *Op = CE->getOperand(0);
839 const Type *Ty = CE->getType();
840
841 const MCExpr *OpExpr = LowerConstant(Op, AP);
842
843 // We can emit the pointer value into this slot if the slot is an
844 // integer slot equal to the size of the pointer.
845 if (TD.getTypeAllocSize(Ty) == TD.getTypeAllocSize(Op->getType()))
846 return OpExpr;
847
848 // Otherwise the pointer is smaller than the resultant integer, mask off
849 // the high bits so we are sure to get a proper truncation if the input is
850 // a constant expr.
851 unsigned InBits = TD.getTypeAllocSizeInBits(Op->getType());
852 const MCExpr *MaskExpr = MCConstantExpr::Create(~0ULL >> (64-InBits), Ctx);
853 return MCBinaryExpr::CreateAnd(OpExpr, MaskExpr, Ctx);
854 }
870855
871856 case Instruction::Add:
872857 case Instruction::Sub:
873858 case Instruction::And:
874859 case Instruction::Or:
875 case Instruction::Xor:
876 O << '(';
877 EmitConstantValueOnly(CE->getOperand(0));
878 O << ')';
860 case Instruction::Xor: {
861 const MCExpr *LHS = LowerConstant(CE->getOperand(0), AP);
862 const MCExpr *RHS = LowerConstant(CE->getOperand(1), AP);
879863 switch (CE->getOpcode()) {
880 case Instruction::Add:
881 O << " + ";
882 break;
883 case Instruction::Sub:
884 O << " - ";
885 break;
886 case Instruction::And:
887 O << " & ";
888 break;
889 case Instruction::Or:
890 O << " | ";
891 break;
892 case Instruction::Xor:
893 O << " ^ ";
894 break;
895 default:
896 break;
897 }
898 O << '(';
899 EmitConstantValueOnly(CE->getOperand(1));
900 O << ')';
901 break;
864 default: llvm_unreachable("Unknown binary operator constant cast expr");
865 case Instruction::Add: return MCBinaryExpr::CreateAdd(LHS, RHS, Ctx);
866 case Instruction::Sub: return MCBinaryExpr::CreateSub(LHS, RHS, Ctx);
867 case Instruction::And: return MCBinaryExpr::CreateAnd(LHS, RHS, Ctx);
868 case Instruction::Or: return MCBinaryExpr::CreateOr (LHS, RHS, Ctx);
869 case Instruction::Xor: return MCBinaryExpr::CreateXor(LHS, RHS, Ctx);
870 }
871 }
902872 }
903873 }
904874
10821052 return;
10831053 }
10841054
1085 // Otherwise, it must be a ConstantExpr. Emit the data directive, then emit
1086 // the expression value.
1087 switch (TM.getTargetData()->getTypeAllocSize(CV->getType())) {
1088 case 0: return;
1089 case 1: O << MAI->getData8bitsDirective(AddrSpace); break;
1090 case 2: O << MAI->getData16bitsDirective(AddrSpace); break;
1091 case 4: O << MAI->getData32bitsDirective(AddrSpace); break;
1092 case 8:
1093 if (const char *Dir = MAI->getData64bitsDirective(AddrSpace)) {
1094 O << Dir;
1095 break;
1096 }
1097 // FALL THROUGH.
1098 default:
1099 llvm_unreachable("Target cannot handle given data directive width!");
1100 return;
1101 }
1102
1103 EmitConstantValueOnly(CV);
1104 O << '\n';
1055 // Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
1056 // thread the streamer with EmitValue.
1057 OutStreamer.EmitValue(LowerConstant(CV, *this),
1058 TM.getTargetData()->getTypeAllocSize(CV->getType()),
1059 AddrSpace);
11051060 }
11061061
11071062 void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
16701625 return GMP;
16711626 }
16721627
1673 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
1674 llvm_unreachable(0);
1628 llvm_report_error("no GCMetadataPrinter registered for GC: " + Twine(Name));
1629 return 0;
16751630 }
16761631
16771632 /// EmitComments - Pretty-print comments for instructions
None ; RUN: llc < %s | grep { - 92}
0 ; RUN: llc < %s | grep p-92
11 ; PR3481
22 ; The offset should print as -92, not +17179869092
33
22
33 ; CHECK: .globl r
44 ; CHECK: r:
5 ; CHECK: .quad ((r) & 4294967295)
5 ; CHECK: .quad r&4294967295
66
77 @r = global %union.x { i64 ptrtoint (%union.x* @r to i64) }, align 4