OLD | NEW |
1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// | 1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file implements the Cfg class, including constant pool | 10 // This file implements the Cfg class, including constant pool |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 // Reset each variable's live range. | 287 // Reset each variable's live range. |
288 for (Variable *Var : Variables) | 288 for (Variable *Var : Variables) |
289 Var->resetLiveRange(); | 289 Var->resetLiveRange(); |
290 } | 290 } |
291 // Make a final pass over each node to delete dead instructions, | 291 // Make a final pass over each node to delete dead instructions, |
292 // collect the first and last instruction numbers, and add live | 292 // collect the first and last instruction numbers, and add live |
293 // range segments for that node. | 293 // range segments for that node. |
294 for (CfgNode *Node : Nodes) { | 294 for (CfgNode *Node : Nodes) { |
295 InstNumberT FirstInstNum = Inst::NumberSentinel; | 295 InstNumberT FirstInstNum = Inst::NumberSentinel; |
296 InstNumberT LastInstNum = Inst::NumberSentinel; | 296 InstNumberT LastInstNum = Inst::NumberSentinel; |
297 for (auto I = Node->getPhis().begin(), E = Node->getPhis().end(); I != E; | 297 for (Inst &I : Node->getPhis()) { |
298 ++I) { | 298 I.deleteIfDead(); |
299 I->deleteIfDead(); | 299 if (Mode == Liveness_Intervals && !I.isDeleted()) { |
300 if (Mode == Liveness_Intervals && !I->isDeleted()) { | |
301 if (FirstInstNum == Inst::NumberSentinel) | 300 if (FirstInstNum == Inst::NumberSentinel) |
302 FirstInstNum = I->getNumber(); | 301 FirstInstNum = I.getNumber(); |
303 assert(I->getNumber() > LastInstNum); | 302 assert(I.getNumber() > LastInstNum); |
304 LastInstNum = I->getNumber(); | 303 LastInstNum = I.getNumber(); |
305 } | 304 } |
306 } | 305 } |
307 for (auto I = Node->getInsts().begin(), E = Node->getInsts().end(); I != E; | 306 for (Inst &I : Node->getInsts()) { |
308 ++I) { | 307 I.deleteIfDead(); |
309 I->deleteIfDead(); | 308 if (Mode == Liveness_Intervals && !I.isDeleted()) { |
310 if (Mode == Liveness_Intervals && !I->isDeleted()) { | |
311 if (FirstInstNum == Inst::NumberSentinel) | 309 if (FirstInstNum == Inst::NumberSentinel) |
312 FirstInstNum = I->getNumber(); | 310 FirstInstNum = I.getNumber(); |
313 assert(I->getNumber() > LastInstNum); | 311 assert(I.getNumber() > LastInstNum); |
314 LastInstNum = I->getNumber(); | 312 LastInstNum = I.getNumber(); |
315 } | 313 } |
316 } | 314 } |
317 if (Mode == Liveness_Intervals) { | 315 if (Mode == Liveness_Intervals) { |
318 // Special treatment for live in-args. Their liveness needs to | 316 // Special treatment for live in-args. Their liveness needs to |
319 // extend beyond the beginning of the function, otherwise an arg | 317 // extend beyond the beginning of the function, otherwise an arg |
320 // whose only use is in the first instruction will end up having | 318 // whose only use is in the first instruction will end up having |
321 // the trivial live range [2,2) and will *not* interfere with | 319 // the trivial live range [2,2) and will *not* interfere with |
322 // other arguments. So if the first instruction of the method | 320 // other arguments. So if the first instruction of the method |
323 // is "r=arg1+arg2", both args may be assigned the same | 321 // is "r=arg1+arg2", both args may be assigned the same |
324 // register. This is accomplished by extending the entry | 322 // register. This is accomplished by extending the entry |
325 // block's instruction range from [2,n) to [1,n) which will | 323 // block's instruction range from [2,n) to [1,n) which will |
326 // transform the problematic [2,2) live ranges into [1,2). | 324 // transform the problematic [2,2) live ranges into [1,2). |
327 if (FirstInstNum == Inst::NumberInitial) | 325 if (FirstInstNum == Inst::NumberInitial) |
328 FirstInstNum = Inst::NumberExtended; | 326 FirstInstNum = Inst::NumberExtended; |
329 Node->livenessAddIntervals(getLiveness(), FirstInstNum, LastInstNum); | 327 Node->livenessAddIntervals(getLiveness(), FirstInstNum, LastInstNum); |
330 } | 328 } |
331 } | 329 } |
332 } | 330 } |
333 | 331 |
334 // Traverse every Variable of every Inst and verify that it | 332 // Traverse every Variable of every Inst and verify that it |
335 // appears within the Variable's computed live range. | 333 // appears within the Variable's computed live range. |
336 bool Cfg::validateLiveness() const { | 334 bool Cfg::validateLiveness() const { |
337 TimerMarker T(TimerStack::TT_validateLiveness, this); | 335 TimerMarker T(TimerStack::TT_validateLiveness, this); |
338 bool Valid = true; | 336 bool Valid = true; |
339 Ostream &Str = Ctx->getStrDump(); | 337 Ostream &Str = Ctx->getStrDump(); |
340 for (CfgNode *Node : Nodes) { | 338 for (CfgNode *Node : Nodes) { |
341 Inst *FirstInst = nullptr; | 339 Inst *FirstInst = nullptr; |
342 for (auto Inst = Node->getInsts().begin(), E = Node->getInsts().end(); | 340 for (Inst &Inst : Node->getInsts()) { |
343 Inst != E; ++Inst) { | 341 if (Inst.isDeleted()) |
344 if (Inst->isDeleted()) | |
345 continue; | 342 continue; |
346 if (FirstInst == nullptr) | 343 if (FirstInst == nullptr) |
347 FirstInst = Inst; | 344 FirstInst = &Inst; |
348 InstNumberT InstNumber = Inst->getNumber(); | 345 InstNumberT InstNumber = Inst.getNumber(); |
349 if (Variable *Dest = Inst->getDest()) { | 346 if (Variable *Dest = Inst.getDest()) { |
350 if (!Dest->getIgnoreLiveness()) { | 347 if (!Dest->getIgnoreLiveness()) { |
351 bool Invalid = false; | 348 bool Invalid = false; |
352 const bool IsDest = true; | 349 const bool IsDest = true; |
353 if (!Dest->getLiveRange().containsValue(InstNumber, IsDest)) | 350 if (!Dest->getLiveRange().containsValue(InstNumber, IsDest)) |
354 Invalid = true; | 351 Invalid = true; |
355 // Check that this instruction actually *begins* Dest's live | 352 // Check that this instruction actually *begins* Dest's live |
356 // range, by checking that Dest is not live in the previous | 353 // range, by checking that Dest is not live in the previous |
357 // instruction. As a special exception, we don't check this | 354 // instruction. As a special exception, we don't check this |
358 // for the first instruction of the block, because a Phi | 355 // for the first instruction of the block, because a Phi |
359 // temporary may be live at the end of the previous block, | 356 // temporary may be live at the end of the previous block, |
360 // and if it is also assigned in the first instruction of | 357 // and if it is also assigned in the first instruction of |
361 // this block, the adjacent live ranges get merged. | 358 // this block, the adjacent live ranges get merged. |
362 if (static_cast<class Inst *>(Inst) != FirstInst && | 359 if (static_cast<class Inst *>(&Inst) != FirstInst && |
363 !Inst->isDestNonKillable() && | 360 !Inst.isDestNonKillable() && |
364 Dest->getLiveRange().containsValue(InstNumber - 1, IsDest)) | 361 Dest->getLiveRange().containsValue(InstNumber - 1, IsDest)) |
365 Invalid = true; | 362 Invalid = true; |
366 if (Invalid) { | 363 if (Invalid) { |
367 Valid = false; | 364 Valid = false; |
368 Str << "Liveness error: inst " << Inst->getNumber() << " dest "; | 365 Str << "Liveness error: inst " << Inst.getNumber() << " dest "; |
369 Dest->dump(this); | 366 Dest->dump(this); |
370 Str << " live range " << Dest->getLiveRange() << "\n"; | 367 Str << " live range " << Dest->getLiveRange() << "\n"; |
371 } | 368 } |
372 } | 369 } |
373 } | 370 } |
374 for (SizeT I = 0; I < Inst->getSrcSize(); ++I) { | 371 for (SizeT I = 0; I < Inst.getSrcSize(); ++I) { |
375 Operand *Src = Inst->getSrc(I); | 372 Operand *Src = Inst.getSrc(I); |
376 SizeT NumVars = Src->getNumVars(); | 373 SizeT NumVars = Src->getNumVars(); |
377 for (SizeT J = 0; J < NumVars; ++J) { | 374 for (SizeT J = 0; J < NumVars; ++J) { |
378 const Variable *Var = Src->getVar(J); | 375 const Variable *Var = Src->getVar(J); |
379 const bool IsDest = false; | 376 const bool IsDest = false; |
380 if (!Var->getIgnoreLiveness() && | 377 if (!Var->getIgnoreLiveness() && |
381 !Var->getLiveRange().containsValue(InstNumber, IsDest)) { | 378 !Var->getLiveRange().containsValue(InstNumber, IsDest)) { |
382 Valid = false; | 379 Valid = false; |
383 Str << "Liveness error: inst " << Inst->getNumber() << " var "; | 380 Str << "Liveness error: inst " << Inst.getNumber() << " var "; |
384 Var->dump(this); | 381 Var->dump(this); |
385 Str << " live range " << Var->getLiveRange() << "\n"; | 382 Str << " live range " << Var->getLiveRange() << "\n"; |
386 } | 383 } |
387 } | 384 } |
388 } | 385 } |
389 } | 386 } |
390 } | 387 } |
391 return Valid; | 388 return Valid; |
392 } | 389 } |
393 | 390 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 } | 518 } |
522 } | 519 } |
523 // Print each basic block | 520 // Print each basic block |
524 for (CfgNode *Node : Nodes) | 521 for (CfgNode *Node : Nodes) |
525 Node->dump(this); | 522 Node->dump(this); |
526 if (getContext()->isVerbose(IceV_Instructions)) | 523 if (getContext()->isVerbose(IceV_Instructions)) |
527 Str << "}\n"; | 524 Str << "}\n"; |
528 } | 525 } |
529 | 526 |
530 } // end of namespace Ice | 527 } // end of namespace Ice |
OLD | NEW |