From 85c5465e072b3bbebb1f5e112fb2db46f7fba148 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 23 May 2002 15:50:03 +0000 Subject: [PATCH] Convert RegClass::IsColorUsedArr from a dynamically allocated array to a vector. This makes asserting on array bounds easier. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2731 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/RegClass.h | 19 +++++----- include/llvm/Target/TargetRegInfo.h | 3 +- lib/CodeGen/RegAlloc/PhyRegAlloc.cpp | 41 +++++++++------------ lib/CodeGen/RegAlloc/RegClass.cpp | 9 ++--- lib/CodeGen/RegAlloc/RegClass.h | 19 +++++----- lib/Target/SparcV9/RegAlloc/PhyRegAlloc.cpp | 41 +++++++++------------ lib/Target/SparcV9/RegAlloc/RegClass.cpp | 9 ++--- lib/Target/SparcV9/RegAlloc/RegClass.h | 19 +++++----- lib/Target/SparcV9/SparcV9RegClassInfo.cpp | 13 ++++--- lib/Target/SparcV9/SparcV9RegClassInfo.h | 23 ++++++------ 10 files changed, 90 insertions(+), 106 deletions(-) diff --git a/include/llvm/CodeGen/RegClass.h b/include/llvm/CodeGen/RegClass.h index c93d6961e0b..4584a5f7a1e 100644 --- a/include/llvm/CodeGen/RegClass.h +++ b/include/llvm/CodeGen/RegClass.h @@ -42,16 +42,17 @@ class RegClass { // buildInterferenceGraph std::stack IGNodeStack; // the stack used for coloring - const ReservedColorListType *const ReservedColorList; + // ReservedColorList - for passing registers that are pre-allocated and cannot + // be used by the register allocator for this function. // - // for passing registers that are pre-allocated and cannot be used by the - // register allocator for this function. + const ReservedColorListType *const ReservedColorList; - bool *IsColorUsedArr; + // IsColorUsedArr - An array used for coloring each node. This array must be + // of size MRC->getNumOfAllRegs(). Allocated once in the constructor for + // efficiency. // - // An array used for coloring each node. This array must be of size - // MRC->getNumOfAllRegs(). Allocated once in the constructor - // for efficiency. + std::vector IsColorUsedArr; + //--------------------------- private methods ------------------------------ @@ -71,8 +72,6 @@ class RegClass { const MachineRegClassInfo *MRC, const ReservedColorListType *RCL = 0); - ~RegClass() { delete[] IsColorUsedArr; } - inline void createInterferenceGraph() { IG.createGraph(); } inline InterferenceGraph &getIG() { return IG; } @@ -106,7 +105,7 @@ class RegClass { { IG.mergeIGNodesOfLRs(LR1, LR2); } - inline bool * getIsColorUsedArr() { return IsColorUsedArr; } + inline std::vector &getIsColorUsedArr() { return IsColorUsedArr; } inline void printIGNodeList() const { diff --git a/include/llvm/Target/TargetRegInfo.h b/include/llvm/Target/TargetRegInfo.h index 1067a9a18df..99a89feb2e8 100644 --- a/include/llvm/Target/TargetRegInfo.h +++ b/include/llvm/Target/TargetRegInfo.h @@ -51,7 +51,8 @@ public: // This method should find a color which is not used by neighbors // (i.e., a false position in IsColorUsedArr) and - virtual void colorIGNode(IGNode *Node, bool IsColorUsedArr[]) const = 0; + virtual void colorIGNode(IGNode *Node, + std::vector &IsColorUsedArr) const = 0; virtual bool isRegVolatile(int Reg) const = 0; MachineRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR) diff --git a/lib/CodeGen/RegAlloc/PhyRegAlloc.cpp b/lib/CodeGen/RegAlloc/PhyRegAlloc.cpp index bd0db117f0f..6a7031bab59 100644 --- a/lib/CodeGen/RegAlloc/PhyRegAlloc.cpp +++ b/lib/CodeGen/RegAlloc/PhyRegAlloc.cpp @@ -797,7 +797,7 @@ int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, unsigned NumAvailRegs = RC->getNumOfAvailRegs(); - bool *IsColorUsedArr = RC->getIsColorUsedArr(); + std::vector &IsColorUsedArr = RC->getIsColorUsedArr(); for(unsigned i=0; i < NumAvailRegs; i++) // Reset array IsColorUsedArr[i] = false; @@ -822,16 +822,11 @@ int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, setRelRegsUsedByThisInst(RC, MInst); - unsigned c; // find first unused color - for( c=0; c < NumAvailRegs; c++) - if( ! IsColorUsedArr[ c ] ) break; + for(unsigned c=0; c < NumAvailRegs; c++) // find first unused color + if (!IsColorUsedArr[c]) + return MRI.getUnifiedRegNum(RC->getID(), c); - if(c < NumAvailRegs) - return MRI.getUnifiedRegNum(RC->getID(), c); - else - return -1; - - + return -1; } @@ -840,9 +835,9 @@ int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, // by operands of a machine instruction. Returns the unified reg number. //---------------------------------------------------------------------------- int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC, - const MachineInstr *MInst) { + const MachineInstr *MInst) { - bool *IsColorUsedArr = RC->getIsColorUsedArr(); + vector &IsColorUsedArr = RC->getIsColorUsedArr(); unsigned NumAvailRegs = RC->getNumOfAvailRegs(); @@ -851,14 +846,11 @@ int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC, setRelRegsUsedByThisInst(RC, MInst); - unsigned c; // find first unused color - for( c=0; c < RC->getNumOfAvailRegs(); c++) - if( ! IsColorUsedArr[ c ] ) break; - - if(c < NumAvailRegs) - return MRI.getUnifiedRegNum(RC->getID(), c); - else - assert( 0 && "FATAL: No free register could be found in reg class!!"); + for(unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color + if (!IsColorUsedArr[c]) + return MRI.getUnifiedRegNum(RC->getID(), c); + + assert(0 && "FATAL: No free register could be found in reg class!!"); return 0; } @@ -871,7 +863,7 @@ int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC, void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst ) { - bool *IsColorUsedArr = RC->getIsColorUsedArr(); + vector &IsColorUsedArr = RC->getIsColorUsedArr(); for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { @@ -886,7 +878,7 @@ void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) { int Reg; if( (Reg=Op.getAllocatedRegNum()) != -1) { - IsColorUsedArr[ Reg ] = true; + IsColorUsedArr[Reg] = true; } else { // it is possilbe that this operand still is not marked with @@ -895,13 +887,14 @@ void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, LiveRange *LROfVal = LRI.getLiveRangeForValue(Val); if( LROfVal) if( LROfVal->hasColor() ) - IsColorUsedArr[ LROfVal->getColor() ] = true; + IsColorUsedArr[LROfVal->getColor()] = true; } } // if reg classes are the same } else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) { - IsColorUsedArr[ Op.getMachineRegNum() ] = true; + assert((unsigned)Op.getMachineRegNum() < IsColorUsedArr.size()); + IsColorUsedArr[Op.getMachineRegNum()] = true; } } diff --git a/lib/CodeGen/RegAlloc/RegClass.cpp b/lib/CodeGen/RegAlloc/RegClass.cpp index c680bbfc31b..5998f1fd664 100644 --- a/lib/CodeGen/RegAlloc/RegClass.cpp +++ b/lib/CodeGen/RegAlloc/RegClass.cpp @@ -14,7 +14,7 @@ RegClass::RegClass(const Function *M, if( DEBUG_RA) cerr << "Created Reg Class: " << RegClassID << "\n"; - IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ]; + IsColorUsedArr.resize(Mrc->getNumOfAllRegs()); } @@ -200,14 +200,13 @@ void RegClass::colorIGNode(IGNode *const Node) // init all elements of to IsColorUsedAr false; // - for( unsigned i=0; i < MRC->getNumOfAllRegs(); i++) { - IsColorUsedArr[ i ] = false; - } + for (unsigned i=0; i < MRC->getNumOfAllRegs(); i++) + IsColorUsedArr[i] = false; // init all reserved_regs to true - we can't use them // for( unsigned i=0; i < ReservedColorList->size() ; i++) { - IsColorUsedArr[ (*ReservedColorList)[i] ] = true; + IsColorUsedArr[(*ReservedColorList)[i]] = true; } // initialize all colors used by neighbors of this node to true diff --git a/lib/CodeGen/RegAlloc/RegClass.h b/lib/CodeGen/RegAlloc/RegClass.h index c93d6961e0b..4584a5f7a1e 100644 --- a/lib/CodeGen/RegAlloc/RegClass.h +++ b/lib/CodeGen/RegAlloc/RegClass.h @@ -42,16 +42,17 @@ class RegClass { // buildInterferenceGraph std::stack IGNodeStack; // the stack used for coloring - const ReservedColorListType *const ReservedColorList; + // ReservedColorList - for passing registers that are pre-allocated and cannot + // be used by the register allocator for this function. // - // for passing registers that are pre-allocated and cannot be used by the - // register allocator for this function. + const ReservedColorListType *const ReservedColorList; - bool *IsColorUsedArr; + // IsColorUsedArr - An array used for coloring each node. This array must be + // of size MRC->getNumOfAllRegs(). Allocated once in the constructor for + // efficiency. // - // An array used for coloring each node. This array must be of size - // MRC->getNumOfAllRegs(). Allocated once in the constructor - // for efficiency. + std::vector IsColorUsedArr; + //--------------------------- private methods ------------------------------ @@ -71,8 +72,6 @@ class RegClass { const MachineRegClassInfo *MRC, const ReservedColorListType *RCL = 0); - ~RegClass() { delete[] IsColorUsedArr; } - inline void createInterferenceGraph() { IG.createGraph(); } inline InterferenceGraph &getIG() { return IG; } @@ -106,7 +105,7 @@ class RegClass { { IG.mergeIGNodesOfLRs(LR1, LR2); } - inline bool * getIsColorUsedArr() { return IsColorUsedArr; } + inline std::vector &getIsColorUsedArr() { return IsColorUsedArr; } inline void printIGNodeList() const { diff --git a/lib/Target/SparcV9/RegAlloc/PhyRegAlloc.cpp b/lib/Target/SparcV9/RegAlloc/PhyRegAlloc.cpp index bd0db117f0f..6a7031bab59 100644 --- a/lib/Target/SparcV9/RegAlloc/PhyRegAlloc.cpp +++ b/lib/Target/SparcV9/RegAlloc/PhyRegAlloc.cpp @@ -797,7 +797,7 @@ int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, unsigned NumAvailRegs = RC->getNumOfAvailRegs(); - bool *IsColorUsedArr = RC->getIsColorUsedArr(); + std::vector &IsColorUsedArr = RC->getIsColorUsedArr(); for(unsigned i=0; i < NumAvailRegs; i++) // Reset array IsColorUsedArr[i] = false; @@ -822,16 +822,11 @@ int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, setRelRegsUsedByThisInst(RC, MInst); - unsigned c; // find first unused color - for( c=0; c < NumAvailRegs; c++) - if( ! IsColorUsedArr[ c ] ) break; + for(unsigned c=0; c < NumAvailRegs; c++) // find first unused color + if (!IsColorUsedArr[c]) + return MRI.getUnifiedRegNum(RC->getID(), c); - if(c < NumAvailRegs) - return MRI.getUnifiedRegNum(RC->getID(), c); - else - return -1; - - + return -1; } @@ -840,9 +835,9 @@ int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, // by operands of a machine instruction. Returns the unified reg number. //---------------------------------------------------------------------------- int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC, - const MachineInstr *MInst) { + const MachineInstr *MInst) { - bool *IsColorUsedArr = RC->getIsColorUsedArr(); + vector &IsColorUsedArr = RC->getIsColorUsedArr(); unsigned NumAvailRegs = RC->getNumOfAvailRegs(); @@ -851,14 +846,11 @@ int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC, setRelRegsUsedByThisInst(RC, MInst); - unsigned c; // find first unused color - for( c=0; c < RC->getNumOfAvailRegs(); c++) - if( ! IsColorUsedArr[ c ] ) break; - - if(c < NumAvailRegs) - return MRI.getUnifiedRegNum(RC->getID(), c); - else - assert( 0 && "FATAL: No free register could be found in reg class!!"); + for(unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color + if (!IsColorUsedArr[c]) + return MRI.getUnifiedRegNum(RC->getID(), c); + + assert(0 && "FATAL: No free register could be found in reg class!!"); return 0; } @@ -871,7 +863,7 @@ int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC, void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst ) { - bool *IsColorUsedArr = RC->getIsColorUsedArr(); + vector &IsColorUsedArr = RC->getIsColorUsedArr(); for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { @@ -886,7 +878,7 @@ void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) { int Reg; if( (Reg=Op.getAllocatedRegNum()) != -1) { - IsColorUsedArr[ Reg ] = true; + IsColorUsedArr[Reg] = true; } else { // it is possilbe that this operand still is not marked with @@ -895,13 +887,14 @@ void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, LiveRange *LROfVal = LRI.getLiveRangeForValue(Val); if( LROfVal) if( LROfVal->hasColor() ) - IsColorUsedArr[ LROfVal->getColor() ] = true; + IsColorUsedArr[LROfVal->getColor()] = true; } } // if reg classes are the same } else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) { - IsColorUsedArr[ Op.getMachineRegNum() ] = true; + assert((unsigned)Op.getMachineRegNum() < IsColorUsedArr.size()); + IsColorUsedArr[Op.getMachineRegNum()] = true; } } diff --git a/lib/Target/SparcV9/RegAlloc/RegClass.cpp b/lib/Target/SparcV9/RegAlloc/RegClass.cpp index c680bbfc31b..5998f1fd664 100644 --- a/lib/Target/SparcV9/RegAlloc/RegClass.cpp +++ b/lib/Target/SparcV9/RegAlloc/RegClass.cpp @@ -14,7 +14,7 @@ RegClass::RegClass(const Function *M, if( DEBUG_RA) cerr << "Created Reg Class: " << RegClassID << "\n"; - IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ]; + IsColorUsedArr.resize(Mrc->getNumOfAllRegs()); } @@ -200,14 +200,13 @@ void RegClass::colorIGNode(IGNode *const Node) // init all elements of to IsColorUsedAr false; // - for( unsigned i=0; i < MRC->getNumOfAllRegs(); i++) { - IsColorUsedArr[ i ] = false; - } + for (unsigned i=0; i < MRC->getNumOfAllRegs(); i++) + IsColorUsedArr[i] = false; // init all reserved_regs to true - we can't use them // for( unsigned i=0; i < ReservedColorList->size() ; i++) { - IsColorUsedArr[ (*ReservedColorList)[i] ] = true; + IsColorUsedArr[(*ReservedColorList)[i]] = true; } // initialize all colors used by neighbors of this node to true diff --git a/lib/Target/SparcV9/RegAlloc/RegClass.h b/lib/Target/SparcV9/RegAlloc/RegClass.h index c93d6961e0b..4584a5f7a1e 100644 --- a/lib/Target/SparcV9/RegAlloc/RegClass.h +++ b/lib/Target/SparcV9/RegAlloc/RegClass.h @@ -42,16 +42,17 @@ class RegClass { // buildInterferenceGraph std::stack IGNodeStack; // the stack used for coloring - const ReservedColorListType *const ReservedColorList; + // ReservedColorList - for passing registers that are pre-allocated and cannot + // be used by the register allocator for this function. // - // for passing registers that are pre-allocated and cannot be used by the - // register allocator for this function. + const ReservedColorListType *const ReservedColorList; - bool *IsColorUsedArr; + // IsColorUsedArr - An array used for coloring each node. This array must be + // of size MRC->getNumOfAllRegs(). Allocated once in the constructor for + // efficiency. // - // An array used for coloring each node. This array must be of size - // MRC->getNumOfAllRegs(). Allocated once in the constructor - // for efficiency. + std::vector IsColorUsedArr; + //--------------------------- private methods ------------------------------ @@ -71,8 +72,6 @@ class RegClass { const MachineRegClassInfo *MRC, const ReservedColorListType *RCL = 0); - ~RegClass() { delete[] IsColorUsedArr; } - inline void createInterferenceGraph() { IG.createGraph(); } inline InterferenceGraph &getIG() { return IG; } @@ -106,7 +105,7 @@ class RegClass { { IG.mergeIGNodesOfLRs(LR1, LR2); } - inline bool * getIsColorUsedArr() { return IsColorUsedArr; } + inline std::vector &getIsColorUsedArr() { return IsColorUsedArr; } inline void printIGNodeList() const { diff --git a/lib/Target/SparcV9/SparcV9RegClassInfo.cpp b/lib/Target/SparcV9/SparcV9RegClassInfo.cpp index 11a82511587..da40826b3e0 100644 --- a/lib/Target/SparcV9/SparcV9RegClassInfo.cpp +++ b/lib/Target/SparcV9/SparcV9RegClassInfo.cpp @@ -18,7 +18,7 @@ using std::cerr; // If both above fail, spill. // //----------------------------------------------------------------------------- -void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const { +void SparcIntRegClass::colorIGNode(IGNode * Node, vector &IsColorUsedArr) const { LiveRange *LR = Node->getParentLR(); if( DEBUG_RA ) { @@ -30,7 +30,7 @@ void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const { unsigned SugCol = LR->getSuggestedColor(); - if( ! IsColorUsedArr[ SugCol ] ) { + if (!IsColorUsedArr[SugCol]) { if( LR->isSuggestedColorUsable() ) { @@ -71,7 +71,7 @@ void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const { // find first unused color for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) { - if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; } + if(!IsColorUsedArr[c] ) { ColorFound = true; break; } } if( ColorFound) { @@ -130,7 +130,8 @@ void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const { // If a color is still not fond, mark for spilling // //---------------------------------------------------------------------------- -void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const{ +void SparcFloatRegClass::colorIGNode(IGNode * Node, + vector &IsColorUsedArr) const{ LiveRange *LR = Node->getParentLR(); // Mark the second color for double-precision registers: @@ -175,7 +176,7 @@ void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const{ int ColorFound = -1; // have we found a color yet? bool isCallInterf = LR->isCallInterference(); - // if value is a double - search the double only reigon (f32 - f63) + // if value is a double - search the double only region (f32 - f63) // i.e. we try to allocate f32 - f63 first for doubles since singles // cannot go there. By doing that, we provide more space for singles // in f0 - f31 @@ -243,7 +244,7 @@ void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const{ int SparcFloatRegClass::findFloatColor(const LiveRange *LR, unsigned Start, unsigned End, - bool IsColorUsedArr[]) const { + vector &IsColorUsedArr) const { bool ColorFound = false; unsigned c; diff --git a/lib/Target/SparcV9/SparcV9RegClassInfo.h b/lib/Target/SparcV9/SparcV9RegClassInfo.h index ab5b35c8010..f07d0aa5f2b 100644 --- a/lib/Target/SparcV9/SparcV9RegClassInfo.h +++ b/lib/Target/SparcV9/SparcV9RegClassInfo.h @@ -82,7 +82,7 @@ struct SparcIntRegClass : public MachineRegClassInfo { SparcIntRegOrder::NumOfAvailRegs, SparcIntRegOrder::NumOfAllRegs) { } - void colorIGNode(IGNode *Node, bool IsColorUsedArr[]) const; + void colorIGNode(IGNode *Node, std::vector &IsColorUsedArr) const; inline bool isRegVolatile(int Reg) const { return (Reg < (int) SparcIntRegOrder::StartOfNonVolatileRegs); @@ -144,14 +144,14 @@ class SparcFloatRegOrder{ class SparcFloatRegClass : public MachineRegClassInfo { int findFloatColor(const LiveRange *LR, unsigned Start, - unsigned End, bool IsColorUsedArr[]) const; + unsigned End, std::vector &IsColorUsedArr) const; public: SparcFloatRegClass(unsigned ID) : MachineRegClassInfo(ID, SparcFloatRegOrder::NumOfAvailRegs, SparcFloatRegOrder::NumOfAllRegs) {} - void colorIGNode(IGNode *Node, bool IsColorUsedArr[]) const; + void colorIGNode(IGNode *Node, std::vector &IsColorUsedArr) const; // according to Sparc 64 ABI, all %fp regs are volatile inline bool isRegVolatile(int Reg) const { return true; } @@ -192,7 +192,7 @@ struct SparcIntCCRegClass : public MachineRegClassInfo { SparcIntCCRegClass(unsigned ID) : MachineRegClassInfo(ID, 1, 2) { } - inline void colorIGNode(IGNode *Node, bool IsColorUsedArr[]) const { + void colorIGNode(IGNode *Node, std::vector &IsColorUsedArr) const { if (IsColorUsedArr[0]) Node->getParentLR()->markForSpill(); else @@ -231,13 +231,14 @@ struct SparcFloatCCRegClass : public MachineRegClassInfo { SparcFloatCCRegClass(unsigned ID) : MachineRegClassInfo(ID, 4, 4) { } - void colorIGNode(IGNode *Node, bool IsColorUsedArr[]) const { - int c; - for(c=0; c < 4 && IsColorUsedArr[c] ; ++c) ; // find unused color - if (c < 4) - Node->setColor(c); - else - Node->getParentLR()->markForSpill(); + void colorIGNode(IGNode *Node, std::vector &IsColorUsedArr) const { + for(unsigned c = 0; c != 4; ++c) + if (!IsColorUsedArr[c]) { // find unused color + Node->setColor(c); + return; + } + + Node->getParentLR()->markForSpill(); } // according to Sparc 64 ABI, all %fp CC regs are volatile -- 2.34.1