Mercurial > hg > CbC > CbC_llvm
comparison lib/IR/BasicBlock.cpp @ 147:c2174574ed3a
LLVM 10
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 14 Aug 2019 16:55:33 +0900 |
parents | 3a76565eade5 |
children |
comparison
equal
deleted
inserted
replaced
134:3a76565eade5 | 147:c2174574ed3a |
---|---|
1 //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===// | 1 //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 // | 4 // See https://llvm.org/LICENSE.txt for license information. |
5 // This file is distributed under the University of Illinois Open Source | 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 // License. See LICENSE.TXT for details. | |
7 // | 6 // |
8 //===----------------------------------------------------------------------===// | 7 //===----------------------------------------------------------------------===// |
9 // | 8 // |
10 // This file implements the BasicBlock class for the IR library. | 9 // This file implements the BasicBlock class for the IR library. |
11 // | 10 // |
88 void BasicBlock::setParent(Function *parent) { | 87 void BasicBlock::setParent(Function *parent) { |
89 // Set Parent=parent, updating instruction symtab entries as appropriate. | 88 // Set Parent=parent, updating instruction symtab entries as appropriate. |
90 InstList.setSymTabObject(&Parent, parent); | 89 InstList.setSymTabObject(&Parent, parent); |
91 } | 90 } |
92 | 91 |
92 iterator_range<filter_iterator<BasicBlock::const_iterator, | |
93 std::function<bool(const Instruction &)>>> | |
94 BasicBlock::instructionsWithoutDebug() const { | |
95 std::function<bool(const Instruction &)> Fn = [](const Instruction &I) { | |
96 return !isa<DbgInfoIntrinsic>(I); | |
97 }; | |
98 return make_filter_range(*this, Fn); | |
99 } | |
100 | |
101 iterator_range<filter_iterator<BasicBlock::iterator, | |
102 std::function<bool(Instruction &)>>> | |
103 BasicBlock::instructionsWithoutDebug() { | |
104 std::function<bool(Instruction &)> Fn = [](Instruction &I) { | |
105 return !isa<DbgInfoIntrinsic>(I); | |
106 }; | |
107 return make_filter_range(*this, Fn); | |
108 } | |
109 | |
93 void BasicBlock::removeFromParent() { | 110 void BasicBlock::removeFromParent() { |
94 getParent()->getBasicBlockList().remove(getIterator()); | 111 getParent()->getBasicBlockList().remove(getIterator()); |
95 } | 112 } |
96 | 113 |
97 iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() { | 114 iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() { |
115 | 132 |
116 const Module *BasicBlock::getModule() const { | 133 const Module *BasicBlock::getModule() const { |
117 return getParent()->getParent(); | 134 return getParent()->getParent(); |
118 } | 135 } |
119 | 136 |
120 const TerminatorInst *BasicBlock::getTerminator() const { | 137 const Instruction *BasicBlock::getTerminator() const { |
121 if (InstList.empty()) return nullptr; | 138 if (InstList.empty() || !InstList.back().isTerminator()) |
122 return dyn_cast<TerminatorInst>(&InstList.back()); | 139 return nullptr; |
140 return &InstList.back(); | |
123 } | 141 } |
124 | 142 |
125 const CallInst *BasicBlock::getTerminatingMustTailCall() const { | 143 const CallInst *BasicBlock::getTerminatingMustTailCall() const { |
126 if (InstList.empty()) | 144 if (InstList.empty()) |
127 return nullptr; | 145 return nullptr; |
185 const Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() const { | 203 const Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() const { |
186 for (const Instruction &I : *this) { | 204 for (const Instruction &I : *this) { |
187 if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I)) | 205 if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I)) |
188 continue; | 206 continue; |
189 | 207 |
190 if (auto *II = dyn_cast<IntrinsicInst>(&I)) | 208 if (I.isLifetimeStartOrEnd()) |
191 if (II->getIntrinsicID() == Intrinsic::lifetime_start || | 209 continue; |
192 II->getIntrinsicID() == Intrinsic::lifetime_end) | |
193 continue; | |
194 | 210 |
195 return &I; | 211 return &I; |
196 } | 212 } |
197 return nullptr; | 213 return nullptr; |
198 } | 214 } |
239 // This is OK. | 255 // This is OK. |
240 } | 256 } |
241 return PredBB; | 257 return PredBB; |
242 } | 258 } |
243 | 259 |
260 bool BasicBlock::hasNPredecessors(unsigned N) const { | |
261 return hasNItems(pred_begin(this), pred_end(this), N); | |
262 } | |
263 | |
264 bool BasicBlock::hasNPredecessorsOrMore(unsigned N) const { | |
265 return hasNItemsOrMore(pred_begin(this), pred_end(this), N); | |
266 } | |
267 | |
244 const BasicBlock *BasicBlock::getSingleSuccessor() const { | 268 const BasicBlock *BasicBlock::getSingleSuccessor() const { |
245 succ_const_iterator SI = succ_begin(this), E = succ_end(this); | 269 succ_const_iterator SI = succ_begin(this), E = succ_end(this); |
246 if (SI == E) return nullptr; // no successors | 270 if (SI == E) return nullptr; // no successors |
247 const BasicBlock *TheSucc = *SI; | 271 const BasicBlock *TheSucc = *SI; |
248 ++SI; | 272 ++SI; |
273 /// actually not used to update the Predecessor list, but is actually used to | 297 /// actually not used to update the Predecessor list, but is actually used to |
274 /// update the PHI nodes that reside in the block. Note that this should be | 298 /// update the PHI nodes that reside in the block. Note that this should be |
275 /// called while the predecessor still refers to this block. | 299 /// called while the predecessor still refers to this block. |
276 /// | 300 /// |
277 void BasicBlock::removePredecessor(BasicBlock *Pred, | 301 void BasicBlock::removePredecessor(BasicBlock *Pred, |
278 bool DontDeleteUselessPHIs) { | 302 bool KeepOneInputPHIs) { |
279 assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs. | 303 assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs. |
280 find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) && | 304 find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) && |
281 "removePredecessor: BB is not a predecessor!"); | 305 "removePredecessor: BB is not a predecessor!"); |
282 | 306 |
283 if (InstList.empty()) return; | 307 if (InstList.empty()) return; |
304 // Disable PHI elimination! | 328 // Disable PHI elimination! |
305 if (this == Other) max_idx = 3; | 329 if (this == Other) max_idx = 3; |
306 } | 330 } |
307 | 331 |
308 // <= Two predecessors BEFORE I remove one? | 332 // <= Two predecessors BEFORE I remove one? |
309 if (max_idx <= 2 && !DontDeleteUselessPHIs) { | 333 if (max_idx <= 2 && !KeepOneInputPHIs) { |
310 // Yup, loop through and nuke the PHI nodes | 334 // Yup, loop through and nuke the PHI nodes |
311 while (PHINode *PN = dyn_cast<PHINode>(&front())) { | 335 while (PHINode *PN = dyn_cast<PHINode>(&front())) { |
312 // Remove the predecessor first. | 336 // Remove the predecessor first. |
313 PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs); | 337 PN->removeIncomingValue(Pred, !KeepOneInputPHIs); |
314 | 338 |
315 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value | 339 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value |
316 if (max_idx == 2) { | 340 if (max_idx == 2) { |
317 if (PN->getIncomingValue(0) != PN) | 341 if (PN->getIncomingValue(0) != PN) |
318 PN->replaceAllUsesWith(PN->getIncomingValue(0)); | 342 PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
333 ++II; | 357 ++II; |
334 PN->removeIncomingValue(Pred, false); | 358 PN->removeIncomingValue(Pred, false); |
335 // If all incoming values to the Phi are the same, we can replace the Phi | 359 // If all incoming values to the Phi are the same, we can replace the Phi |
336 // with that value. | 360 // with that value. |
337 Value* PNV = nullptr; | 361 Value* PNV = nullptr; |
338 if (!DontDeleteUselessPHIs && (PNV = PN->hasConstantValue())) | 362 if (!KeepOneInputPHIs && (PNV = PN->hasConstantValue())) |
339 if (PNV != PN) { | 363 if (PNV != PN) { |
340 PN->replaceAllUsesWith(PNV); | 364 PN->replaceAllUsesWith(PNV); |
341 PN->eraseFromParent(); | 365 PN->eraseFromParent(); |
342 } | 366 } |
343 } | 367 } |
364 | 388 |
365 // If the block has no successors, there can be no instructions to hoist. | 389 // If the block has no successors, there can be no instructions to hoist. |
366 assert(Term->getNumSuccessors() > 0); | 390 assert(Term->getNumSuccessors() > 0); |
367 | 391 |
368 // Instructions should not be hoisted across exception handling boundaries. | 392 // Instructions should not be hoisted across exception handling boundaries. |
369 return !Term->isExceptional(); | 393 return !Term->isExceptionalTerminator(); |
370 } | 394 } |
371 | 395 |
372 /// This splits a basic block into two at the specified | 396 /// This splits a basic block into two at the specified |
373 /// instruction. Note that all instructions BEFORE the specified iterator stay | 397 /// instruction. Note that all instructions BEFORE the specified iterator stay |
374 /// as part of the original basic block, an unconditional branch is added to | 398 /// as part of the original basic block, an unconditional branch is added to |
399 BI->setDebugLoc(Loc); | 423 BI->setDebugLoc(Loc); |
400 | 424 |
401 // Now we must loop through all of the successors of the New block (which | 425 // Now we must loop through all of the successors of the New block (which |
402 // _were_ the successors of the 'this' block), and update any PHI nodes in | 426 // _were_ the successors of the 'this' block), and update any PHI nodes in |
403 // successors. If there were PHI nodes in the successors, then they need to | 427 // successors. If there were PHI nodes in the successors, then they need to |
404 // know that incoming branches will be from New, not from Old. | 428 // know that incoming branches will be from New, not from Old (this). |
405 // | 429 // |
406 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) { | 430 New->replaceSuccessorsPhiUsesWith(this, New); |
407 // Loop over any phi nodes in the basic block, updating the BB field of | |
408 // incoming values... | |
409 BasicBlock *Successor = *I; | |
410 for (auto &PN : Successor->phis()) { | |
411 int Idx = PN.getBasicBlockIndex(this); | |
412 while (Idx != -1) { | |
413 PN.setIncomingBlock((unsigned)Idx, New); | |
414 Idx = PN.getBasicBlockIndex(this); | |
415 } | |
416 } | |
417 } | |
418 return New; | 431 return New; |
419 } | 432 } |
420 | 433 |
421 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { | 434 void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) { |
422 TerminatorInst *TI = getTerminator(); | 435 // N.B. This might not be a complete BasicBlock, so don't assume |
436 // that it ends with a non-phi instruction. | |
437 for (iterator II = begin(), IE = end(); II != IE; ++II) { | |
438 PHINode *PN = dyn_cast<PHINode>(II); | |
439 if (!PN) | |
440 break; | |
441 PN->replaceIncomingBlockWith(Old, New); | |
442 } | |
443 } | |
444 | |
445 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *Old, | |
446 BasicBlock *New) { | |
447 Instruction *TI = getTerminator(); | |
423 if (!TI) | 448 if (!TI) |
424 // Cope with being called on a BasicBlock that doesn't have a terminator | 449 // Cope with being called on a BasicBlock that doesn't have a terminator |
425 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. | 450 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. |
426 return; | 451 return; |
427 for (BasicBlock *Succ : TI->successors()) { | 452 llvm::for_each(successors(TI), [Old, New](BasicBlock *Succ) { |
428 // N.B. Succ might not be a complete BasicBlock, so don't assume | 453 Succ->replacePhiUsesWith(Old, New); |
429 // that it ends with a non-phi instruction. | 454 }); |
430 for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) { | 455 } |
431 PHINode *PN = dyn_cast<PHINode>(II); | 456 |
432 if (!PN) | 457 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { |
433 break; | 458 this->replaceSuccessorsPhiUsesWith(this, New); |
434 int i; | |
435 while ((i = PN->getBasicBlockIndex(this)) >= 0) | |
436 PN->setIncomingBlock(i, New); | |
437 } | |
438 } | |
439 } | 459 } |
440 | 460 |
441 /// Return true if this basic block is a landing pad. I.e., it's | 461 /// Return true if this basic block is a landing pad. I.e., it's |
442 /// the destination of the 'unwind' edge of an invoke instruction. | 462 /// the destination of the 'unwind' edge of an invoke instruction. |
443 bool BasicBlock::isLandingPad() const { | 463 bool BasicBlock::isLandingPad() const { |
448 const LandingPadInst *BasicBlock::getLandingPadInst() const { | 468 const LandingPadInst *BasicBlock::getLandingPadInst() const { |
449 return dyn_cast<LandingPadInst>(getFirstNonPHI()); | 469 return dyn_cast<LandingPadInst>(getFirstNonPHI()); |
450 } | 470 } |
451 | 471 |
452 Optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const { | 472 Optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const { |
453 const TerminatorInst *TI = getTerminator(); | 473 const Instruction *TI = getTerminator(); |
454 if (MDNode *MDIrrLoopHeader = | 474 if (MDNode *MDIrrLoopHeader = |
455 TI->getMetadata(LLVMContext::MD_irr_loop)) { | 475 TI->getMetadata(LLVMContext::MD_irr_loop)) { |
456 MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0)); | 476 MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0)); |
457 if (MDName->getString().equals("loop_header_weight")) { | 477 if (MDName->getString().equals("loop_header_weight")) { |
458 auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1)); | 478 auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1)); |
459 return Optional<uint64_t>(CI->getValue().getZExtValue()); | 479 return Optional<uint64_t>(CI->getValue().getZExtValue()); |
460 } | 480 } |
461 } | 481 } |
462 return Optional<uint64_t>(); | 482 return Optional<uint64_t>(); |
463 } | 483 } |
484 | |
485 BasicBlock::iterator llvm::skipDebugIntrinsics(BasicBlock::iterator It) { | |
486 while (isa<DbgInfoIntrinsic>(It)) | |
487 ++It; | |
488 return It; | |
489 } |