llvm.org GIT mirror llvm / 6a256cc
Untabify tabs in stuff I've recently added. Check in my register allocator state-saving code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8698 91177308-0d34-0410-b5e6-96231b3b80d8 Brian Gaeke 17 years ago
4 changed file(s) with 312 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
1919 #include "llvm/Function.h"
2020 #include "llvm/Type.h"
2121 #include "llvm/iOther.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Constants.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "llvm/Module.h"
2226 #include "Support/STLExtras.h"
2327 #include "Support/SetOperations.h"
2428 #include "Support/CommandLine.h"
11361140 if (HMI->first) {
11371141 LiveRange *L = HMI->second; // get the LiveRange
11381142 if (L && L->hasSuggestedColor ())
1139 L->setSuggestedColorUsable
1140 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1141 && L->isCallInterference ()));
1143 L->setSuggestedColorUsable
1144 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1145 && L->isCallInterference ()));
11421146 }
11431147 } // for all LR's in hash map
11441148 }
11691173 }
11701174 }
11711175 } // for all LR's in hash map
1176 }
1177
1178
1179 namespace {
1180 /// AllocInfo - Structure representing one instruction's
1181 /// operand's-worth of register allocation state. We create tables
1182 /// made out of these data structures to generate mapping information
1183 /// for this register allocator. (FIXME: This might move to a header
1184 /// file at some point.)
1185 ///
1186 struct AllocInfo {
1187 unsigned Instruction;
1188 unsigned Operand;
1189 unsigned AllocState;
1190 int Placement;
1191 AllocInfo (unsigned Instruction_, unsigned Operand_,
1192 unsigned AllocState_, int Placement_) :
1193 Instruction (Instruction_), Operand (Operand_),
1194 AllocState (AllocState_), Placement (Placement_) { }
1195 /// getConstantType - Return a StructType representing an AllocInfo
1196 /// object.
1197 ///
1198 static StructType *getConstantType () {
1199 std::vector TV;
1200 TV.push_back (Type::UIntTy);
1201 TV.push_back (Type::UIntTy);
1202 TV.push_back (Type::UIntTy);
1203 TV.push_back (Type::IntTy);
1204 return StructType::get (TV);
1205 }
1206 /// toConstant - Convert this AllocInfo into an LLVM Constant of type
1207 /// getConstantType(), and return the Constant.
1208 ///
1209 Constant *toConstant () const {
1210 StructType *ST = getConstantType ();
1211 std::vector CV;
1212 CV.push_back (ConstantUInt::get (Type::UIntTy, Instruction));
1213 CV.push_back (ConstantUInt::get (Type::UIntTy, Operand));
1214 CV.push_back (ConstantUInt::get (Type::UIntTy, AllocState));
1215 CV.push_back (ConstantSInt::get (Type::IntTy, Placement));
1216 return ConstantStruct::get (ST, CV);
1217 }
1218 };
1219 }
1220
1221 void PhyRegAlloc::saveState ()
1222 {
1223 std::vector state;
1224 unsigned Insn = 0;
1225 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
1226 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II != IE; ++II)
1227 for (unsigned i = 0; i < (*II)->getNumOperands (); ++i) {
1228 const Value *V = (*II)->getOperand (i);
1229 // Don't worry about it unless it's something whose reg. we'll need.
1230 if (!isa (V) && !isa (V))
1231 continue;
1232 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
1233 static const unsigned NotAllocated = 0, Allocated = 1, Spilled = 2;
1234 unsigned AllocState = NotAllocated;
1235 int Placement = -1;
1236 if ((HMI != HMIEnd) && HMI->second) {
1237 LiveRange *L = HMI->second;
1238 assert ((L->hasColor () || L->isMarkedForSpill ())
1239 && "Live range exists but not colored or spilled");
1240 if (L->hasColor()) {
1241 AllocState = Allocated;
1242 Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
1243 L->getColor ());
1244 } else if (L->isMarkedForSpill ()) {
1245 AllocState = Spilled;
1246 assert (L->hasSpillOffset ()
1247 && "Live range marked for spill but has no spill offset");
1248 Placement = L->getSpillOffFromFP ();
1249 }
1250 }
1251 state.push_back (AllocInfo (Insn, i, AllocState,
1252 Placement).toConstant ());
1253 }
1254 // Convert state into an LLVM ConstantArray, and put it in a
1255 // ConstantStruct (named S) along with its size.
1256 unsigned Size = state.size ();
1257 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
1258 std::vector TV;
1259 TV.push_back (Type::UIntTy);
1260 TV.push_back (AT);
1261 StructType *ST = StructType::get (TV);
1262 std::vector CV;
1263 CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
1264 CV.push_back (ConstantArray::get (AT, state));
1265 Constant *S = ConstantStruct::get (ST, CV);
1266 // Save S in the map containing register allocator state for this module.
1267 FnAllocState[Fn] = S;
1268 }
1269
1270
1271 bool PhyRegAlloc::doFinalization (Module &M) {
1272 if (!SaveRegAllocState)
1273 return false; // Nothing to do here, unless we're saving state.
1274
1275 // Convert FnAllocState to a single Constant array and add it
1276 // to the Module.
1277 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
1278 std::vector TV;
1279 TV.push_back (Type::UIntTy);
1280 TV.push_back (AT);
1281 PointerType *PT = PointerType::get (StructType::get (TV));
1282
1283 std::vector allstate;
1284 for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
1285 Function *F = I;
1286 if (FnAllocState.find (F) == FnAllocState.end ()) {
1287 allstate.push_back (ConstantPointerNull::get (PT));
1288 } else {
1289 GlobalVariable *GV =
1290 new GlobalVariable (FnAllocState[F]->getType (), true,
1291 GlobalValue::InternalLinkage, FnAllocState[F],
1292 F->getName () + ".regAllocState", &M);
1293 // Have: { uint, [Size x { uint, uint, uint, int }] } *
1294 // Cast it to: { uint, [0 x { uint, uint, uint, int }] } *
1295 Constant *CE = ConstantExpr::getCast (ConstantPointerRef::get (GV), PT);
1296 allstate.push_back (CE);
1297 }
1298 }
1299
1300 unsigned Size = allstate.size ();
1301 // Final structure type is:
1302 // { uint, [Size x { uint, [0 x { uint, uint, uint, int }] } *] }
1303 std::vector TV2;
1304 TV2.push_back (Type::UIntTy);
1305 ArrayType *AT2 = ArrayType::get (PT, Size);
1306 TV2.push_back (AT2);
1307 StructType *ST2 = StructType::get (TV2);
1308 std::vector CV2;
1309 CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
1310 CV2.push_back (ConstantArray::get (AT2, allstate));
1311 new GlobalVariable (ST2, true, GlobalValue::InternalLinkage,
1312 ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
1313 &M);
1314 return false; // No error.
11721315 }
11731316
11741317
12431386 // insert code to copy to the correct register
12441387 colorIncomingArgs();
12451388
1389 // Save register allocation state for this function in a Constant.
1390 if (SaveRegAllocState)
1391 saveState();
1392
12461393 // Now update the machine code with register names and add any
12471394 // additional code inserted by the register allocator to the instruction
12481395 // stream
2828 class MachineInstr;
2929 class LoopInfo;
3030 class RegClass;
31 class Constant;
3132
3233 //----------------------------------------------------------------------------
3334 // Class AddedInstrns:
7677 AddedInstrns AddedInstrAtEntry; // to store instrns added at entry
7778 const LoopInfo *LoopDepthCalc; // to calculate loop depths
7879
80 std::map FnAllocState;
81
7982 PhyRegAlloc(const PhyRegAlloc&); // DO NOT IMPLEMENT
8083 void operator=(const PhyRegAlloc&); // DO NOT IMPLEMENT
8184 public:
8790 /// runOnFunction - Main method called for allocating registers.
8891 ///
8992 virtual bool runOnFunction (Function &F);
93
94 virtual bool doFinalization (Module &M);
9095
9196 virtual void getAnalysisUsage (AnalysisUsage &AU) const;
9297
107112 void addInterferencesForArgs();
108113 void createIGNodeListsAndIGs();
109114 void buildInterferenceGraphs();
115 void saveState();
110116
111117 void setCallInterferences(const MachineInstr *MI,
112118 const ValueSet *LVSetAft);
1919 #include "llvm/Function.h"
2020 #include "llvm/Type.h"
2121 #include "llvm/iOther.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Constants.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "llvm/Module.h"
2226 #include "Support/STLExtras.h"
2327 #include "Support/SetOperations.h"
2428 #include "Support/CommandLine.h"
11361140 if (HMI->first) {
11371141 LiveRange *L = HMI->second; // get the LiveRange
11381142 if (L && L->hasSuggestedColor ())
1139 L->setSuggestedColorUsable
1140 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1141 && L->isCallInterference ()));
1143 L->setSuggestedColorUsable
1144 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1145 && L->isCallInterference ()));
11421146 }
11431147 } // for all LR's in hash map
11441148 }
11691173 }
11701174 }
11711175 } // for all LR's in hash map
1176 }
1177
1178
1179 namespace {
1180 /// AllocInfo - Structure representing one instruction's
1181 /// operand's-worth of register allocation state. We create tables
1182 /// made out of these data structures to generate mapping information
1183 /// for this register allocator. (FIXME: This might move to a header
1184 /// file at some point.)
1185 ///
1186 struct AllocInfo {
1187 unsigned Instruction;
1188 unsigned Operand;
1189 unsigned AllocState;
1190 int Placement;
1191 AllocInfo (unsigned Instruction_, unsigned Operand_,
1192 unsigned AllocState_, int Placement_) :
1193 Instruction (Instruction_), Operand (Operand_),
1194 AllocState (AllocState_), Placement (Placement_) { }
1195 /// getConstantType - Return a StructType representing an AllocInfo
1196 /// object.
1197 ///
1198 static StructType *getConstantType () {
1199 std::vector TV;
1200 TV.push_back (Type::UIntTy);
1201 TV.push_back (Type::UIntTy);
1202 TV.push_back (Type::UIntTy);
1203 TV.push_back (Type::IntTy);
1204 return StructType::get (TV);
1205 }
1206 /// toConstant - Convert this AllocInfo into an LLVM Constant of type
1207 /// getConstantType(), and return the Constant.
1208 ///
1209 Constant *toConstant () const {
1210 StructType *ST = getConstantType ();
1211 std::vector CV;
1212 CV.push_back (ConstantUInt::get (Type::UIntTy, Instruction));
1213 CV.push_back (ConstantUInt::get (Type::UIntTy, Operand));
1214 CV.push_back (ConstantUInt::get (Type::UIntTy, AllocState));
1215 CV.push_back (ConstantSInt::get (Type::IntTy, Placement));
1216 return ConstantStruct::get (ST, CV);
1217 }
1218 };
1219 }
1220
1221 void PhyRegAlloc::saveState ()
1222 {
1223 std::vector state;
1224 unsigned Insn = 0;
1225 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
1226 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II != IE; ++II)
1227 for (unsigned i = 0; i < (*II)->getNumOperands (); ++i) {
1228 const Value *V = (*II)->getOperand (i);
1229 // Don't worry about it unless it's something whose reg. we'll need.
1230 if (!isa (V) && !isa (V))
1231 continue;
1232 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
1233 static const unsigned NotAllocated = 0, Allocated = 1, Spilled = 2;
1234 unsigned AllocState = NotAllocated;
1235 int Placement = -1;
1236 if ((HMI != HMIEnd) && HMI->second) {
1237 LiveRange *L = HMI->second;
1238 assert ((L->hasColor () || L->isMarkedForSpill ())
1239 && "Live range exists but not colored or spilled");
1240 if (L->hasColor()) {
1241 AllocState = Allocated;
1242 Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
1243 L->getColor ());
1244 } else if (L->isMarkedForSpill ()) {
1245 AllocState = Spilled;
1246 assert (L->hasSpillOffset ()
1247 && "Live range marked for spill but has no spill offset");
1248 Placement = L->getSpillOffFromFP ();
1249 }
1250 }
1251 state.push_back (AllocInfo (Insn, i, AllocState,
1252 Placement).toConstant ());
1253 }
1254 // Convert state into an LLVM ConstantArray, and put it in a
1255 // ConstantStruct (named S) along with its size.
1256 unsigned Size = state.size ();
1257 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
1258 std::vector TV;
1259 TV.push_back (Type::UIntTy);
1260 TV.push_back (AT);
1261 StructType *ST = StructType::get (TV);
1262 std::vector CV;
1263 CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
1264 CV.push_back (ConstantArray::get (AT, state));
1265 Constant *S = ConstantStruct::get (ST, CV);
1266 // Save S in the map containing register allocator state for this module.
1267 FnAllocState[Fn] = S;
1268 }
1269
1270
1271 bool PhyRegAlloc::doFinalization (Module &M) {
1272 if (!SaveRegAllocState)
1273 return false; // Nothing to do here, unless we're saving state.
1274
1275 // Convert FnAllocState to a single Constant array and add it
1276 // to the Module.
1277 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
1278 std::vector TV;
1279 TV.push_back (Type::UIntTy);
1280 TV.push_back (AT);
1281 PointerType *PT = PointerType::get (StructType::get (TV));
1282
1283 std::vector allstate;
1284 for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
1285 Function *F = I;
1286 if (FnAllocState.find (F) == FnAllocState.end ()) {
1287 allstate.push_back (ConstantPointerNull::get (PT));
1288 } else {
1289 GlobalVariable *GV =
1290 new GlobalVariable (FnAllocState[F]->getType (), true,
1291 GlobalValue::InternalLinkage, FnAllocState[F],
1292 F->getName () + ".regAllocState", &M);
1293 // Have: { uint, [Size x { uint, uint, uint, int }] } *
1294 // Cast it to: { uint, [0 x { uint, uint, uint, int }] } *
1295 Constant *CE = ConstantExpr::getCast (ConstantPointerRef::get (GV), PT);
1296 allstate.push_back (CE);
1297 }
1298 }
1299
1300 unsigned Size = allstate.size ();
1301 // Final structure type is:
1302 // { uint, [Size x { uint, [0 x { uint, uint, uint, int }] } *] }
1303 std::vector TV2;
1304 TV2.push_back (Type::UIntTy);
1305 ArrayType *AT2 = ArrayType::get (PT, Size);
1306 TV2.push_back (AT2);
1307 StructType *ST2 = StructType::get (TV2);
1308 std::vector CV2;
1309 CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
1310 CV2.push_back (ConstantArray::get (AT2, allstate));
1311 new GlobalVariable (ST2, true, GlobalValue::InternalLinkage,
1312 ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
1313 &M);
1314 return false; // No error.
11721315 }
11731316
11741317
12431386 // insert code to copy to the correct register
12441387 colorIncomingArgs();
12451388
1389 // Save register allocation state for this function in a Constant.
1390 if (SaveRegAllocState)
1391 saveState();
1392
12461393 // Now update the machine code with register names and add any
12471394 // additional code inserted by the register allocator to the instruction
12481395 // stream
2828 class MachineInstr;
2929 class LoopInfo;
3030 class RegClass;
31 class Constant;
3132
3233 //----------------------------------------------------------------------------
3334 // Class AddedInstrns:
7677 AddedInstrns AddedInstrAtEntry; // to store instrns added at entry
7778 const LoopInfo *LoopDepthCalc; // to calculate loop depths
7879
80 std::map FnAllocState;
81
7982 PhyRegAlloc(const PhyRegAlloc&); // DO NOT IMPLEMENT
8083 void operator=(const PhyRegAlloc&); // DO NOT IMPLEMENT
8184 public:
8790 /// runOnFunction - Main method called for allocating registers.
8891 ///
8992 virtual bool runOnFunction (Function &F);
93
94 virtual bool doFinalization (Module &M);
9095
9196 virtual void getAnalysisUsage (AnalysisUsage &AU) const;
9297
107112 void addInterferencesForArgs();
108113 void createIGNodeListsAndIGs();
109114 void buildInterferenceGraphs();
115 void saveState();
110116
111117 void setCallInterferences(const MachineInstr *MI,
112118 const ValueSet *LVSetAft);