llvm.org GIT mirror llvm / 718cb66
Add lengthof and endof templates that hide a lot of sizeof computations. Patch by Sterling Stein! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41758 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 12 years ago
16 changed file(s) with 68 addition(s) and 47 deletion(s). Raw diff Collapse all Expand all
1818
1919 #include
2020 #include // for std::pair
21 #include // for std::size_t
2122 #include "llvm/ADT/iterator"
2223
2324 namespace llvm {
198199 return tier(f, s);
199200 }
200201
202 //===----------------------------------------------------------------------===//
203 // Extra additions to arrays
204 //===----------------------------------------------------------------------===//
205
206 /// Find where an array ends (for ending iterators)
207 /// This returns a pointer to the byte immediately
208 /// after the end of an array.
209 template
210 inline T *array_endof(T (&x)[N]) {
211 return x+N;
212 }
213
214 /// Find the length of an array.
215 template
216 inline size_t array_lengthof(T (&x)[N]) {
217 return N;
218 }
219
201220 } // End llvm namespace
202221
203222 #endif
2424 #include "llvm/CodeGen/SelectionDAGNodes.h"
2525 #include "llvm/CodeGen/RuntimeLibcalls.h"
2626 #include "llvm/ADT/APFloat.h"
27 #include "llvm/ADT/STLExtras.h"
2728 #include
2829 #include
2930
153154 }
154155 void setTypeAction(MVT::ValueType VT, LegalizeAction Action) {
155156 assert(!MVT::isExtendedVT(VT));
156 assert(unsigned(VT >> 4) <
157 sizeof(ValueTypeActions)/sizeof(ValueTypeActions[0]));
157 assert(unsigned(VT >> 4) < array_lengthof(ValueTypeActions));
158158 ValueTypeActions[VT>>4] |= Action << ((VT*2) & 31);
159159 }
160160 };
710710 /// with the specified type and indicate what to do about it.
711711 void setOperationAction(unsigned Op, MVT::ValueType VT,
712712 LegalizeAction Action) {
713 assert(VT < 32 && Op < sizeof(OpActions)/sizeof(OpActions[0]) &&
713 assert(VT < 32 && Op < array_lengthof(OpActions) &&
714714 "Table isn't big enough!");
715715 OpActions[Op] &= ~(uint64_t(3UL) << VT*2);
716716 OpActions[Op] |= (uint64_t)Action << VT*2;
720720 /// work with the with specified type and indicate what to do about it.
721721 void setLoadXAction(unsigned ExtType, MVT::ValueType VT,
722722 LegalizeAction Action) {
723 assert(VT < 32 && ExtType < sizeof(LoadXActions)/sizeof(LoadXActions[0]) &&
723 assert(VT < 32 && ExtType < array_lengthof(LoadXActions) &&
724724 "Table isn't big enough!");
725725 LoadXActions[ExtType] &= ~(uint64_t(3UL) << VT*2);
726726 LoadXActions[ExtType] |= (uint64_t)Action << VT*2;
741741 void setIndexedLoadAction(unsigned IdxMode, MVT::ValueType VT,
742742 LegalizeAction Action) {
743743 assert(VT < 32 && IdxMode <
744 sizeof(IndexedModeActions[0]) / sizeof(IndexedModeActions[0][0]) &&
744 array_lengthof(IndexedModeActions[0]) &&
745745 "Table isn't big enough!");
746746 IndexedModeActions[0][IdxMode] &= ~(uint64_t(3UL) << VT*2);
747747 IndexedModeActions[0][IdxMode] |= (uint64_t)Action << VT*2;
754754 void setIndexedStoreAction(unsigned IdxMode, MVT::ValueType VT,
755755 LegalizeAction Action) {
756756 assert(VT < 32 && IdxMode <
757 sizeof(IndexedModeActions[1]) / sizeof(IndexedModeActions[1][0]) &&
757 array_lengthof(IndexedModeActions[1]) &&
758758 "Table isn't big enough!");
759759 IndexedModeActions[1][IdxMode] &= ~(uint64_t(3UL) << VT*2);
760760 IndexedModeActions[1][IdxMode] |= (uint64_t)Action << VT*2;
2626 #include "llvm/ADT/SmallVector.h"
2727 #include "llvm/ADT/StringMap.h"
2828 #include "llvm/ADT/BitVector.h"
29 #include "llvm/ADT/STLExtras.h"
2930 #include "llvm/Support/Compiler.h"
3031 #include "llvm/Support/GetElementPtrTypeIterator.h"
3132 #include "llvm/Support/ManagedStatic.h"
889890 if (!Initialized) {
890891 NoMemoryTable->insert(NoMemoryTable->end(),
891892 DoesntAccessMemoryFns,
892 DoesntAccessMemoryFns+
893 sizeof(DoesntAccessMemoryFns)/sizeof(DoesntAccessMemoryFns[0]));
893 array_endof(DoesntAccessMemoryFns));
894894
895895 OnlyReadsMemoryTable->insert(OnlyReadsMemoryTable->end(),
896 OnlyReadsMemoryFns,
897 OnlyReadsMemoryFns+
898 sizeof(OnlyReadsMemoryFns)/sizeof(OnlyReadsMemoryFns[0]));
896 OnlyReadsMemoryFns,
897 array_endof(OnlyReadsMemoryFns));
899898
900899 // Sort the table the first time through.
901900 std::sort(NoMemoryTable->begin(), NoMemoryTable->end(), StringCompare());
1919 #include "llvm/Support/Streams.h"
2020 #include "llvm/Target/TargetData.h"
2121 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/STLExtras.h"
2223 using namespace llvm;
2324
2425 template
420421 CI->getOperand(2),
421422 CI->getOperand(3)
422423 };
423 return new CallInst(F, Args, Args+sizeof(Args)/sizeof(Args[0]), CI->getName(), CI);
424 return new CallInst(F, Args, array_endof(Args), CI->getName(), CI);
424425 }
425426
426427 /// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
586587 CI->getOperand(3),
587588 CI->getOperand(4)
588589 };
589 return new CallInst(F, Args, Args+sizeof(Args)/sizeof(Args[0]), CI->getName(), CI);
590 return new CallInst(F, Args, array_endof(Args), CI->getName(), CI);
590591 }
591592
592593
1717 #include "llvm/DerivedTypes.h"
1818 #include "llvm/CodeGen/SelectionDAG.h"
1919 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/STLExtras.h"
2021 #include "llvm/Support/MathExtras.h"
2122 using namespace llvm;
2223
144145 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType());
145146 ShiftAmtHandling = Undefined;
146147 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
147 memset(TargetDAGCombineArray, 0,
148 sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0]));
148 memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
149149 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
150150 allowUnalignedMemoryAccesses = false;
151151 UseUnderscoreSetJmp = false;
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "Unix.h"
15 #include "llvm/ADT/STLExtras.h"
1516 #include
1617 #include
1718 #if HAVE_EXECINFO_H
3940 const int IntSigs[] = {
4041 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
4142 };
42 const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
43 const int *IntSigsEnd = array_endof(IntSigs);
4344
4445 // KillSigs - Signals that are synchronous with the program that will cause it
4546 // to die.
4950 , SIGEMT
5051 #endif
5152 };
52 const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
53 const int *KillSigsEnd = array_endof(KillSigs);
5354
5455 #ifdef HAVE_BACKTRACE
5556 void* StackTrace[256];
6768 void PrintStackTrace() {
6869 #ifdef HAVE_BACKTRACE
6970 // Use backtrace() to output a backtrace on Linux systems with glibc.
70 int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
71 int depth = backtrace(StackTrace, array_lengthof(StackTrace));
7172
7273 // Create a one-way unix pipe. The backtracing process writes to PipeFDs[1],
7374 // the c++filt process reads from PipeFDs[0].
1616 #include "ARMAddressingModes.h"
1717 #include "ARMGenInstrInfo.inc"
1818 #include "ARMMachineFunctionInfo.h"
19 #include "llvm/ADT/STLExtras.h"
1920 #include "llvm/CodeGen/LiveVariables.h"
2021 #include "llvm/CodeGen/MachineInstrBuilder.h"
2122 #include "llvm/CodeGen/MachineJumpTableInfo.h"
2728 cl::desc("Enable ARM 2-addr to 3-addr conv"));
2829
2930 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
30 : TargetInstrInfo(ARMInsts, sizeof(ARMInsts)/sizeof(ARMInsts[0])),
31 : TargetInstrInfo(ARMInsts, array_lengthof(ARMInsts)),
3132 RI(*this, STI) {
3233 }
3334
1313 #include "Alpha.h"
1414 #include "AlphaInstrInfo.h"
1515 #include "AlphaGenInstrInfo.inc"
16 #include "llvm/ADT/STLExtras.h"
1617 #include "llvm/CodeGen/MachineInstrBuilder.h"
1718 using namespace llvm;
1819
1920 AlphaInstrInfo::AlphaInstrInfo()
20 : TargetInstrInfo(AlphaInsts, sizeof(AlphaInsts)/sizeof(AlphaInsts[0])),
21 : TargetInstrInfo(AlphaInsts, array_lengthof(AlphaInsts)),
2122 RI(*this) { }
2223
2324
1212
1313 #include "Mips.h"
1414 #include "MipsInstrInfo.h"
15 #include "llvm/ADT/STLExtras.h"
1516 #include "llvm/CodeGen/MachineInstrBuilder.h"
1617 #include "MipsGenInstrInfo.inc"
1718
1920
2021 // TODO: Add the subtarget support on this constructor
2122 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
22 : TargetInstrInfo(MipsInsts, sizeof(MipsInsts)/sizeof(MipsInsts[0])),
23 : TargetInstrInfo(MipsInsts, array_lengthof(MipsInsts)),
2324 TM(tm), RI(*this) {}
2425
2526 static bool isZeroImm(const MachineOperand &op) {
1515 #include "PPCPredicates.h"
1616 #include "PPCTargetMachine.h"
1717 #include "PPCPerfectShuffle.h"
18 #include "llvm/ADT/STLExtras.h"
1819 #include "llvm/ADT/VectorExtras.h"
1920 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
2021 #include "llvm/CodeGen/CallingConvLower.h"
12631264 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
12641265 };
12651266
1266 const unsigned Num_GPR_Regs = sizeof(GPR_32)/sizeof(GPR_32[0]);
1267 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
12671268 const unsigned Num_FPR_Regs = isMachoABI ? 13 : 8;
1268 const unsigned Num_VR_Regs = sizeof( VR)/sizeof( VR[0]);
1269 const unsigned Num_VR_Regs = array_lengthof( VR);
12691270
12701271 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
12711272
15821583 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
15831584 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
15841585 };
1585 const unsigned NumGPRs = sizeof(GPR_32)/sizeof(GPR_32[0]);
1586 const unsigned NumGPRs = array_lengthof(GPR_32);
15861587 const unsigned NumFPRs = isMachoABI ? 13 : 8;
1587 const unsigned NumVRs = sizeof( VR)/sizeof( VR[0]);
1588 const unsigned NumVRs = array_lengthof( VR);
15881589
15891590 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
15901591
23982399 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
23992400 };
24002401
2401 for (unsigned idx = 0; idx < sizeof(SplatCsts)/sizeof(SplatCsts[0]); ++idx){
2402 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
24022403 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
24032404 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
24042405 int i = SplatCsts[idx];
1414 #include "PPCPredicates.h"
1515 #include "PPCGenInstrInfo.inc"
1616 #include "PPCTargetMachine.h"
17 #include "llvm/ADT/STLExtras.h"
1718 #include "llvm/CodeGen/MachineInstrBuilder.h"
1819 using namespace llvm;
1920
2021 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
21 : TargetInstrInfo(PPCInsts, sizeof(PPCInsts)/sizeof(PPCInsts[0])), TM(tm),
22 : TargetInstrInfo(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
2223 RI(*TM.getSubtargetImpl(), *this) {}
2324
2425 /// getPointerRegClass - Return the register class to use to hold pointers.
1212
1313 #include "SparcInstrInfo.h"
1414 #include "Sparc.h"
15 #include "llvm/ADT/STLExtras.h"
1516 #include "llvm/CodeGen/MachineInstrBuilder.h"
1617 #include "SparcGenInstrInfo.inc"
1718 using namespace llvm;
1819
1920 SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
20 : TargetInstrInfo(SparcInsts, sizeof(SparcInsts)/sizeof(SparcInsts[0])),
21 : TargetInstrInfo(SparcInsts, array_lengthof(SparcInsts)),
2122 RI(ST, *this) {
2223 }
2324
298298 return -1;
299299 }
300300
301 #define ARRAY_SIZE(TABLE) \
302 (sizeof(TABLE)/sizeof(TABLE[0]))
303
304301 #ifdef NDEBUG
305302 #define ASSERT_SORTED(TABLE)
306303 #else
307304 #define ASSERT_SORTED(TABLE) \
308305 { static bool TABLE##Checked = false; \
309306 if (!TABLE##Checked) { \
310 assert(TableIsSorted(TABLE, ARRAY_SIZE(TABLE)) && \
307 assert(TableIsSorted(TABLE, array_lengthof(TABLE)) && \
311308 "All lookup tables must be sorted for efficient access!"); \
312309 TABLE##Checked = true; \
313310 } \
486483
487484 static unsigned getConcreteOpcode(unsigned Opcode) {
488485 ASSERT_SORTED(OpcodeTable);
489 int Opc = Lookup(OpcodeTable, ARRAY_SIZE(OpcodeTable), Opcode);
486 int Opc = Lookup(OpcodeTable, array_lengthof(OpcodeTable), Opcode);
490487 assert(Opc != -1 && "FP Stack instruction not in OpcodeTable!");
491488 return Opc;
492489 }
534531 RegMap[Stack[--StackTop]] = ~0; // Update state
535532
536533 // Check to see if there is a popping version of this instruction...
537 int Opcode = Lookup(PopTable, ARRAY_SIZE(PopTable), I->getOpcode());
534 int Opcode = Lookup(PopTable, array_lengthof(PopTable), I->getOpcode());
538535 if (Opcode != -1) {
539536 I->setInstrDescriptor(TII->get(Opcode));
540537 if (Opcode == X86::UCOM_FPPr)
829826 InstTable = ReverseSTiTable;
830827 }
831828
832 int Opcode = Lookup(InstTable, ARRAY_SIZE(ForwardST0Table), MI->getOpcode());
829 int Opcode = Lookup(InstTable, array_lengthof(ForwardST0Table),
830 MI->getOpcode());
833831 assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
834832
835833 // NotTOS - The register which is not on the top of stack...
1616 #include "X86InstrBuilder.h"
1717 #include "X86Subtarget.h"
1818 #include "X86TargetMachine.h"
19 #include "llvm/ADT/STLExtras.h"
1920 #include "llvm/CodeGen/MachineInstrBuilder.h"
2021 #include "llvm/CodeGen/LiveVariables.h"
2122 #include "llvm/CodeGen/SSARegMap.h"
2223 using namespace llvm;
2324
2425 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
25 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
26 : TargetInstrInfo(X86Insts, array_lengthof(X86Insts)),
2627 TM(tm), RI(tm, *this) {
2728 }
2829
391391 return NULL;
392392 }
393393
394 #define ARRAY_SIZE(TABLE) \
395 (sizeof(TABLE)/sizeof(TABLE[0]))
396
397394 #ifdef NDEBUG
398395 #define ASSERT_SORTED(TABLE)
399396 #else
400397 #define ASSERT_SORTED(TABLE) \
401398 { static bool TABLE##Checked = false; \
402399 if (!TABLE##Checked) { \
403 assert(TableIsSorted(TABLE, ARRAY_SIZE(TABLE)) && \
400 assert(TableIsSorted(TABLE, array_lengthof(TABLE)) && \
404401 "All lookup tables must be sorted for efficient access!"); \
405402 TABLE##Checked = true; \
406403 } \
589586 };
590587 ASSERT_SORTED(OpcodeTable);
591588 OpcodeTablePtr = OpcodeTable;
592 OpcodeTableSize = ARRAY_SIZE(OpcodeTable);
589 OpcodeTableSize = array_lengthof(OpcodeTable);
593590 isTwoAddrFold = true;
594591 } else if (i == 0) { // If operand 0
595592 if (MI->getOpcode() == X86::MOV16r0)
674671
675672 ASSERT_SORTED(OpcodeTable);
676673 OpcodeTablePtr = OpcodeTable;
677 OpcodeTableSize = ARRAY_SIZE(OpcodeTable);
674 OpcodeTableSize = array_lengthof(OpcodeTable);
678675 } else if (i == 1) {
679676 static const TableEntry OpcodeTable[] = {
680677 { X86::CMP16rr, X86::CMP16rm },
783780
784781 ASSERT_SORTED(OpcodeTable);
785782 OpcodeTablePtr = OpcodeTable;
786 OpcodeTableSize = ARRAY_SIZE(OpcodeTable);
783 OpcodeTableSize = array_lengthof(OpcodeTable);
787784 } else if (i == 2) {
788785 static const TableEntry OpcodeTable[] = {
789786 { X86::ADC32rr, X86::ADC32rm },
978975
979976 ASSERT_SORTED(OpcodeTable);
980977 OpcodeTablePtr = OpcodeTable;
981 OpcodeTableSize = ARRAY_SIZE(OpcodeTable);
978 OpcodeTableSize = array_lengthof(OpcodeTable);
982979 }
983980
984981 // If table selected...
1313 #include "llvm/Support/Mangler.h"
1414 #include "llvm/DerivedTypes.h"
1515 #include "llvm/Module.h"
16 #include "llvm/ADT/STLExtras.h"
1617 #include "llvm/ADT/StringExtras.h"
1718 using namespace llvm;
1819
184185 Mangler::Mangler(Module &M, const char *prefix)
185186 : Prefix(prefix), UseQuotes(false), PreserveAsmNames(false),
186187 Count(0), TypeCounter(0) {
187 std::fill(AcceptableChars,
188 AcceptableChars+sizeof(AcceptableChars)/sizeof(AcceptableChars[0]),
189 0);
188 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
190189
191190 // Letters and numbers are acceptable.
192191 for (unsigned char X = 'a'; X <= 'z'; ++X)