1 | //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements the BasicBlock class for the IR library. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "llvm/IR/BasicBlock.h" |
14 | #include "SymbolTableListTraitsImpl.h" |
15 | #include "llvm/ADT/STLExtras.h" |
16 | #include "llvm/ADT/Statistic.h" |
17 | #include "llvm/IR/CFG.h" |
18 | #include "llvm/IR/Constants.h" |
19 | #include "llvm/IR/DebugProgramInstruction.h" |
20 | #include "llvm/IR/Instructions.h" |
21 | #include "llvm/IR/IntrinsicInst.h" |
22 | #include "llvm/IR/LLVMContext.h" |
23 | #include "llvm/IR/Type.h" |
24 | #include "llvm/Support/CommandLine.h" |
25 | |
26 | #include "LLVMContextImpl.h" |
27 | |
28 | using namespace llvm; |
29 | |
30 | #define DEBUG_TYPE "ir" |
31 | STATISTIC(NumInstrRenumberings, "Number of renumberings across all blocks" ); |
32 | |
33 | cl::opt<bool> UseNewDbgInfoFormat( |
34 | "experimental-debuginfo-iterators" , |
35 | cl::desc("Enable communicating debuginfo positions through iterators, " |
36 | "eliminating intrinsics. Has no effect if " |
37 | "--preserve-input-debuginfo-format=true." ), |
38 | cl::init(Val: true)); |
39 | cl::opt<cl::boolOrDefault> PreserveInputDbgFormat( |
40 | "preserve-input-debuginfo-format" , cl::Hidden, |
41 | cl::desc("When set to true, IR files will be processed and printed in " |
42 | "their current debug info format, regardless of default behaviour " |
43 | "or other flags passed. Has no effect if input IR does not " |
44 | "contain debug records or intrinsics. Ignored in llvm-link, " |
45 | "llvm-lto, and llvm-lto2." )); |
46 | |
47 | bool WriteNewDbgInfoFormatToBitcode /*set default value in cl::init() below*/; |
48 | cl::opt<bool, true> WriteNewDbgInfoFormatToBitcode2( |
49 | "write-experimental-debuginfo-iterators-to-bitcode" , cl::Hidden, |
50 | cl::location(L&: WriteNewDbgInfoFormatToBitcode), cl::init(Val: true)); |
51 | |
52 | DbgMarker *BasicBlock::createMarker(Instruction *I) { |
53 | assert(IsNewDbgInfoFormat && |
54 | "Tried to create a marker in a non new debug-info block!" ); |
55 | if (I->DebugMarker) |
56 | return I->DebugMarker; |
57 | DbgMarker *Marker = new DbgMarker(); |
58 | Marker->MarkedInstr = I; |
59 | I->DebugMarker = Marker; |
60 | return Marker; |
61 | } |
62 | |
63 | DbgMarker *BasicBlock::createMarker(InstListType::iterator It) { |
64 | assert(IsNewDbgInfoFormat && |
65 | "Tried to create a marker in a non new debug-info block!" ); |
66 | if (It != end()) |
67 | return createMarker(I: &*It); |
68 | DbgMarker *DM = getTrailingDbgRecords(); |
69 | if (DM) |
70 | return DM; |
71 | DM = new DbgMarker(); |
72 | setTrailingDbgRecords(DM); |
73 | return DM; |
74 | } |
75 | |
76 | void BasicBlock::convertToNewDbgValues() { |
77 | IsNewDbgInfoFormat = true; |
78 | |
79 | // Iterate over all instructions in the instruction list, collecting debug |
80 | // info intrinsics and converting them to DbgRecords. Once we find a "real" |
81 | // instruction, attach all those DbgRecords to a DbgMarker in that |
82 | // instruction. |
83 | SmallVector<DbgRecord *, 4> DbgVarRecs; |
84 | for (Instruction &I : make_early_inc_range(Range&: InstList)) { |
85 | assert(!I.DebugMarker && "DebugMarker already set on old-format instrs?" ); |
86 | if (DbgVariableIntrinsic *DVI = dyn_cast<DbgVariableIntrinsic>(Val: &I)) { |
87 | // Convert this dbg.value to a DbgVariableRecord. |
88 | DbgVariableRecord *Value = new DbgVariableRecord(DVI); |
89 | DbgVarRecs.push_back(Elt: Value); |
90 | DVI->eraseFromParent(); |
91 | continue; |
92 | } |
93 | |
94 | if (DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(Val: &I)) { |
95 | DbgVarRecs.push_back( |
96 | Elt: new DbgLabelRecord(DLI->getLabel(), DLI->getDebugLoc())); |
97 | DLI->eraseFromParent(); |
98 | continue; |
99 | } |
100 | |
101 | if (DbgVarRecs.empty()) |
102 | continue; |
103 | |
104 | // Create a marker to store DbgRecords in. |
105 | createMarker(I: &I); |
106 | DbgMarker *Marker = I.DebugMarker; |
107 | |
108 | for (DbgRecord *DVR : DbgVarRecs) |
109 | Marker->insertDbgRecord(New: DVR, InsertAtHead: false); |
110 | |
111 | DbgVarRecs.clear(); |
112 | } |
113 | } |
114 | |
115 | void BasicBlock::convertFromNewDbgValues() { |
116 | invalidateOrders(); |
117 | IsNewDbgInfoFormat = false; |
118 | |
119 | // Iterate over the block, finding instructions annotated with DbgMarkers. |
120 | // Convert any attached DbgRecords to debug intrinsics and insert ahead of the |
121 | // instruction. |
122 | for (auto &Inst : *this) { |
123 | if (!Inst.DebugMarker) |
124 | continue; |
125 | |
126 | DbgMarker &Marker = *Inst.DebugMarker; |
127 | for (DbgRecord &DR : Marker.getDbgRecordRange()) |
128 | InstList.insert(where: Inst.getIterator(), |
129 | New: DR.createDebugIntrinsic(M: getModule(), InsertBefore: nullptr)); |
130 | |
131 | Marker.eraseFromParent(); |
132 | } |
133 | |
134 | // Assume no trailing DbgRecords: we could technically create them at the end |
135 | // of the block, after a terminator, but this would be non-cannonical and |
136 | // indicates that something else is broken somewhere. |
137 | assert(!getTrailingDbgRecords()); |
138 | } |
139 | |
140 | #ifndef NDEBUG |
141 | void BasicBlock::dumpDbgValues() const { |
142 | for (auto &Inst : *this) { |
143 | if (!Inst.DebugMarker) |
144 | continue; |
145 | |
146 | dbgs() << "@ " << Inst.DebugMarker << " " ; |
147 | Inst.DebugMarker->dump(); |
148 | }; |
149 | } |
150 | #endif |
151 | |
152 | void BasicBlock::setIsNewDbgInfoFormat(bool NewFlag) { |
153 | if (NewFlag && !IsNewDbgInfoFormat) |
154 | convertToNewDbgValues(); |
155 | else if (!NewFlag && IsNewDbgInfoFormat) |
156 | convertFromNewDbgValues(); |
157 | } |
158 | void BasicBlock::setNewDbgInfoFormatFlag(bool NewFlag) { |
159 | IsNewDbgInfoFormat = NewFlag; |
160 | } |
161 | |
162 | ValueSymbolTable *BasicBlock::getValueSymbolTable() { |
163 | if (Function *F = getParent()) |
164 | return F->getValueSymbolTable(); |
165 | return nullptr; |
166 | } |
167 | |
168 | LLVMContext &BasicBlock::getContext() const { |
169 | return getType()->getContext(); |
170 | } |
171 | |
172 | template <> void llvm::invalidateParentIListOrdering(BasicBlock *BB) { |
173 | BB->invalidateOrders(); |
174 | } |
175 | |
176 | // Explicit instantiation of SymbolTableListTraits since some of the methods |
177 | // are not in the public header file... |
178 | template class llvm::SymbolTableListTraits<Instruction, |
179 | ilist_iterator_bits<true>>; |
180 | |
181 | BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent, |
182 | BasicBlock *InsertBefore) |
183 | : Value(Type::getLabelTy(C), Value::BasicBlockVal), |
184 | IsNewDbgInfoFormat(false), Parent(nullptr) { |
185 | |
186 | if (NewParent) |
187 | insertInto(Parent: NewParent, InsertBefore); |
188 | else |
189 | assert(!InsertBefore && |
190 | "Cannot insert block before another block with no function!" ); |
191 | |
192 | setName(Name); |
193 | if (NewParent) |
194 | setIsNewDbgInfoFormat(NewParent->IsNewDbgInfoFormat); |
195 | } |
196 | |
197 | void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) { |
198 | assert(NewParent && "Expected a parent" ); |
199 | assert(!Parent && "Already has a parent" ); |
200 | |
201 | if (InsertBefore) |
202 | NewParent->insert(Position: InsertBefore->getIterator(), BB: this); |
203 | else |
204 | NewParent->insert(Position: NewParent->end(), BB: this); |
205 | |
206 | setIsNewDbgInfoFormat(NewParent->IsNewDbgInfoFormat); |
207 | } |
208 | |
209 | BasicBlock::~BasicBlock() { |
210 | validateInstrOrdering(); |
211 | |
212 | // If the address of the block is taken and it is being deleted (e.g. because |
213 | // it is dead), this means that there is either a dangling constant expr |
214 | // hanging off the block, or an undefined use of the block (source code |
215 | // expecting the address of a label to keep the block alive even though there |
216 | // is no indirect branch). Handle these cases by zapping the BlockAddress |
217 | // nodes. There are no other possible uses at this point. |
218 | if (hasAddressTaken()) { |
219 | assert(!use_empty() && "There should be at least one blockaddress!" ); |
220 | Constant *Replacement = |
221 | ConstantInt::get(Ty: llvm::Type::getInt32Ty(C&: getContext()), V: 1); |
222 | while (!use_empty()) { |
223 | BlockAddress *BA = cast<BlockAddress>(Val: user_back()); |
224 | BA->replaceAllUsesWith(V: ConstantExpr::getIntToPtr(C: Replacement, |
225 | Ty: BA->getType())); |
226 | BA->destroyConstant(); |
227 | } |
228 | } |
229 | |
230 | assert(getParent() == nullptr && "BasicBlock still linked into the program!" ); |
231 | dropAllReferences(); |
232 | for (auto &Inst : *this) { |
233 | if (!Inst.DebugMarker) |
234 | continue; |
235 | Inst.DebugMarker->eraseFromParent(); |
236 | } |
237 | InstList.clear(); |
238 | } |
239 | |
240 | void BasicBlock::setParent(Function *parent) { |
241 | // Set Parent=parent, updating instruction symtab entries as appropriate. |
242 | InstList.setSymTabObject(Dest: &Parent, Src: parent); |
243 | } |
244 | |
245 | iterator_range<filter_iterator<BasicBlock::const_iterator, |
246 | std::function<bool(const Instruction &)>>> |
247 | BasicBlock::instructionsWithoutDebug(bool SkipPseudoOp) const { |
248 | std::function<bool(const Instruction &)> Fn = [=](const Instruction &I) { |
249 | return !isa<DbgInfoIntrinsic>(Val: I) && |
250 | !(SkipPseudoOp && isa<PseudoProbeInst>(Val: I)); |
251 | }; |
252 | return make_filter_range(Range: *this, Pred: Fn); |
253 | } |
254 | |
255 | iterator_range< |
256 | filter_iterator<BasicBlock::iterator, std::function<bool(Instruction &)>>> |
257 | BasicBlock::instructionsWithoutDebug(bool SkipPseudoOp) { |
258 | std::function<bool(Instruction &)> Fn = [=](Instruction &I) { |
259 | return !isa<DbgInfoIntrinsic>(Val: I) && |
260 | !(SkipPseudoOp && isa<PseudoProbeInst>(Val: I)); |
261 | }; |
262 | return make_filter_range(Range&: *this, Pred: Fn); |
263 | } |
264 | |
265 | filter_iterator<BasicBlock::const_iterator, |
266 | std::function<bool(const Instruction &)>>::difference_type |
267 | BasicBlock::sizeWithoutDebug() const { |
268 | return std::distance(first: instructionsWithoutDebug().begin(), |
269 | last: instructionsWithoutDebug().end()); |
270 | } |
271 | |
272 | void BasicBlock::removeFromParent() { |
273 | getParent()->getBasicBlockList().remove(IT: getIterator()); |
274 | } |
275 | |
276 | iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() { |
277 | return getParent()->getBasicBlockList().erase(where: getIterator()); |
278 | } |
279 | |
280 | void BasicBlock::moveBefore(SymbolTableList<BasicBlock>::iterator MovePos) { |
281 | getParent()->splice(ToIt: MovePos, FromF: getParent(), FromIt: getIterator()); |
282 | } |
283 | |
284 | void BasicBlock::moveAfter(BasicBlock *MovePos) { |
285 | MovePos->getParent()->splice(ToIt: ++MovePos->getIterator(), FromF: getParent(), |
286 | FromIt: getIterator()); |
287 | } |
288 | |
289 | const Module *BasicBlock::getModule() const { |
290 | return getParent()->getParent(); |
291 | } |
292 | |
293 | const CallInst *BasicBlock::getTerminatingMustTailCall() const { |
294 | if (InstList.empty()) |
295 | return nullptr; |
296 | const ReturnInst *RI = dyn_cast<ReturnInst>(Val: &InstList.back()); |
297 | if (!RI || RI == &InstList.front()) |
298 | return nullptr; |
299 | |
300 | const Instruction *Prev = RI->getPrevNode(); |
301 | if (!Prev) |
302 | return nullptr; |
303 | |
304 | if (Value *RV = RI->getReturnValue()) { |
305 | if (RV != Prev) |
306 | return nullptr; |
307 | |
308 | // Look through the optional bitcast. |
309 | if (auto *BI = dyn_cast<BitCastInst>(Val: Prev)) { |
310 | RV = BI->getOperand(i_nocapture: 0); |
311 | Prev = BI->getPrevNode(); |
312 | if (!Prev || RV != Prev) |
313 | return nullptr; |
314 | } |
315 | } |
316 | |
317 | if (auto *CI = dyn_cast<CallInst>(Val: Prev)) { |
318 | if (CI->isMustTailCall()) |
319 | return CI; |
320 | } |
321 | return nullptr; |
322 | } |
323 | |
324 | const CallInst *BasicBlock::getTerminatingDeoptimizeCall() const { |
325 | if (InstList.empty()) |
326 | return nullptr; |
327 | auto *RI = dyn_cast<ReturnInst>(Val: &InstList.back()); |
328 | if (!RI || RI == &InstList.front()) |
329 | return nullptr; |
330 | |
331 | if (auto *CI = dyn_cast_or_null<CallInst>(Val: RI->getPrevNode())) |
332 | if (Function *F = CI->getCalledFunction()) |
333 | if (F->getIntrinsicID() == Intrinsic::experimental_deoptimize) |
334 | return CI; |
335 | |
336 | return nullptr; |
337 | } |
338 | |
339 | const CallInst *BasicBlock::getPostdominatingDeoptimizeCall() const { |
340 | const BasicBlock* BB = this; |
341 | SmallPtrSet<const BasicBlock *, 8> Visited; |
342 | Visited.insert(Ptr: BB); |
343 | while (auto *Succ = BB->getUniqueSuccessor()) { |
344 | if (!Visited.insert(Ptr: Succ).second) |
345 | return nullptr; |
346 | BB = Succ; |
347 | } |
348 | return BB->getTerminatingDeoptimizeCall(); |
349 | } |
350 | |
351 | const Instruction *BasicBlock::getFirstMayFaultInst() const { |
352 | if (InstList.empty()) |
353 | return nullptr; |
354 | for (const Instruction &I : *this) |
355 | if (isa<LoadInst>(Val: I) || isa<StoreInst>(Val: I) || isa<CallBase>(Val: I)) |
356 | return &I; |
357 | return nullptr; |
358 | } |
359 | |
360 | const Instruction* BasicBlock::getFirstNonPHI() const { |
361 | for (const Instruction &I : *this) |
362 | if (!isa<PHINode>(Val: I)) |
363 | return &I; |
364 | return nullptr; |
365 | } |
366 | |
367 | BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const { |
368 | const Instruction *I = getFirstNonPHI(); |
369 | if (!I) |
370 | return end(); |
371 | BasicBlock::const_iterator It = I->getIterator(); |
372 | // Set the head-inclusive bit to indicate that this iterator includes |
373 | // any debug-info at the start of the block. This is a no-op unless the |
374 | // appropriate CMake flag is set. |
375 | It.setHeadBit(true); |
376 | return It; |
377 | } |
378 | |
379 | const Instruction *BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp) const { |
380 | for (const Instruction &I : *this) { |
381 | if (isa<PHINode>(Val: I) || isa<DbgInfoIntrinsic>(Val: I)) |
382 | continue; |
383 | |
384 | if (SkipPseudoOp && isa<PseudoProbeInst>(Val: I)) |
385 | continue; |
386 | |
387 | return &I; |
388 | } |
389 | return nullptr; |
390 | } |
391 | |
392 | const Instruction * |
393 | BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const { |
394 | for (const Instruction &I : *this) { |
395 | if (isa<PHINode>(Val: I) || isa<DbgInfoIntrinsic>(Val: I)) |
396 | continue; |
397 | |
398 | if (I.isLifetimeStartOrEnd()) |
399 | continue; |
400 | |
401 | if (SkipPseudoOp && isa<PseudoProbeInst>(Val: I)) |
402 | continue; |
403 | |
404 | return &I; |
405 | } |
406 | return nullptr; |
407 | } |
408 | |
409 | BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const { |
410 | const Instruction *FirstNonPHI = getFirstNonPHI(); |
411 | if (!FirstNonPHI) |
412 | return end(); |
413 | |
414 | const_iterator InsertPt = FirstNonPHI->getIterator(); |
415 | if (InsertPt->isEHPad()) ++InsertPt; |
416 | // Set the head-inclusive bit to indicate that this iterator includes |
417 | // any debug-info at the start of the block. This is a no-op unless the |
418 | // appropriate CMake flag is set. |
419 | InsertPt.setHeadBit(true); |
420 | return InsertPt; |
421 | } |
422 | |
423 | BasicBlock::const_iterator BasicBlock::getFirstNonPHIOrDbgOrAlloca() const { |
424 | const Instruction *FirstNonPHI = getFirstNonPHI(); |
425 | if (!FirstNonPHI) |
426 | return end(); |
427 | |
428 | const_iterator InsertPt = FirstNonPHI->getIterator(); |
429 | if (InsertPt->isEHPad()) |
430 | ++InsertPt; |
431 | |
432 | if (isEntryBlock()) { |
433 | const_iterator End = end(); |
434 | while (InsertPt != End && |
435 | (isa<AllocaInst>(Val: *InsertPt) || isa<DbgInfoIntrinsic>(Val: *InsertPt) || |
436 | isa<PseudoProbeInst>(Val: *InsertPt))) { |
437 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Val: &*InsertPt)) { |
438 | if (!AI->isStaticAlloca()) |
439 | break; |
440 | } |
441 | ++InsertPt; |
442 | } |
443 | } |
444 | return InsertPt; |
445 | } |
446 | |
447 | void BasicBlock::dropAllReferences() { |
448 | for (Instruction &I : *this) |
449 | I.dropAllReferences(); |
450 | } |
451 | |
452 | const BasicBlock *BasicBlock::getSinglePredecessor() const { |
453 | const_pred_iterator PI = pred_begin(BB: this), E = pred_end(BB: this); |
454 | if (PI == E) return nullptr; // No preds. |
455 | const BasicBlock *ThePred = *PI; |
456 | ++PI; |
457 | return (PI == E) ? ThePred : nullptr /*multiple preds*/; |
458 | } |
459 | |
460 | const BasicBlock *BasicBlock::getUniquePredecessor() const { |
461 | const_pred_iterator PI = pred_begin(BB: this), E = pred_end(BB: this); |
462 | if (PI == E) return nullptr; // No preds. |
463 | const BasicBlock *PredBB = *PI; |
464 | ++PI; |
465 | for (;PI != E; ++PI) { |
466 | if (*PI != PredBB) |
467 | return nullptr; |
468 | // The same predecessor appears multiple times in the predecessor list. |
469 | // This is OK. |
470 | } |
471 | return PredBB; |
472 | } |
473 | |
474 | bool BasicBlock::hasNPredecessors(unsigned N) const { |
475 | return hasNItems(Begin: pred_begin(BB: this), End: pred_end(BB: this), N); |
476 | } |
477 | |
478 | bool BasicBlock::hasNPredecessorsOrMore(unsigned N) const { |
479 | return hasNItemsOrMore(Begin: pred_begin(BB: this), End: pred_end(BB: this), N); |
480 | } |
481 | |
482 | const BasicBlock *BasicBlock::getSingleSuccessor() const { |
483 | const_succ_iterator SI = succ_begin(BB: this), E = succ_end(BB: this); |
484 | if (SI == E) return nullptr; // no successors |
485 | const BasicBlock *TheSucc = *SI; |
486 | ++SI; |
487 | return (SI == E) ? TheSucc : nullptr /* multiple successors */; |
488 | } |
489 | |
490 | const BasicBlock *BasicBlock::getUniqueSuccessor() const { |
491 | const_succ_iterator SI = succ_begin(BB: this), E = succ_end(BB: this); |
492 | if (SI == E) return nullptr; // No successors |
493 | const BasicBlock *SuccBB = *SI; |
494 | ++SI; |
495 | for (;SI != E; ++SI) { |
496 | if (*SI != SuccBB) |
497 | return nullptr; |
498 | // The same successor appears multiple times in the successor list. |
499 | // This is OK. |
500 | } |
501 | return SuccBB; |
502 | } |
503 | |
504 | iterator_range<BasicBlock::phi_iterator> BasicBlock::phis() { |
505 | PHINode *P = empty() ? nullptr : dyn_cast<PHINode>(Val: &*begin()); |
506 | return make_range<phi_iterator>(x: P, y: nullptr); |
507 | } |
508 | |
509 | void BasicBlock::removePredecessor(BasicBlock *Pred, |
510 | bool KeepOneInputPHIs) { |
511 | // Use hasNUsesOrMore to bound the cost of this assertion for complex CFGs. |
512 | assert((hasNUsesOrMore(16) || llvm::is_contained(predecessors(this), Pred)) && |
513 | "Pred is not a predecessor!" ); |
514 | |
515 | // Return early if there are no PHI nodes to update. |
516 | if (empty() || !isa<PHINode>(Val: begin())) |
517 | return; |
518 | |
519 | unsigned NumPreds = cast<PHINode>(Val&: front()).getNumIncomingValues(); |
520 | for (PHINode &Phi : make_early_inc_range(Range: phis())) { |
521 | Phi.removeIncomingValue(BB: Pred, DeletePHIIfEmpty: !KeepOneInputPHIs); |
522 | if (KeepOneInputPHIs) |
523 | continue; |
524 | |
525 | // If we have a single predecessor, removeIncomingValue may have erased the |
526 | // PHI node itself. |
527 | if (NumPreds == 1) |
528 | continue; |
529 | |
530 | // Try to replace the PHI node with a constant value. |
531 | if (Value *PhiConstant = Phi.hasConstantValue()) { |
532 | Phi.replaceAllUsesWith(V: PhiConstant); |
533 | Phi.eraseFromParent(); |
534 | } |
535 | } |
536 | } |
537 | |
538 | bool BasicBlock::canSplitPredecessors() const { |
539 | const Instruction *FirstNonPHI = getFirstNonPHI(); |
540 | if (isa<LandingPadInst>(Val: FirstNonPHI)) |
541 | return true; |
542 | // This is perhaps a little conservative because constructs like |
543 | // CleanupBlockInst are pretty easy to split. However, SplitBlockPredecessors |
544 | // cannot handle such things just yet. |
545 | if (FirstNonPHI->isEHPad()) |
546 | return false; |
547 | return true; |
548 | } |
549 | |
550 | bool BasicBlock::isLegalToHoistInto() const { |
551 | auto *Term = getTerminator(); |
552 | // No terminator means the block is under construction. |
553 | if (!Term) |
554 | return true; |
555 | |
556 | // If the block has no successors, there can be no instructions to hoist. |
557 | assert(Term->getNumSuccessors() > 0); |
558 | |
559 | // Instructions should not be hoisted across special terminators, which may |
560 | // have side effects or return values. |
561 | return !Term->isSpecialTerminator(); |
562 | } |
563 | |
564 | bool BasicBlock::isEntryBlock() const { |
565 | const Function *F = getParent(); |
566 | assert(F && "Block must have a parent function to use this API" ); |
567 | return this == &F->getEntryBlock(); |
568 | } |
569 | |
570 | BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName, |
571 | bool Before) { |
572 | if (Before) |
573 | return splitBasicBlockBefore(I, BBName); |
574 | |
575 | assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!" ); |
576 | assert(I != InstList.end() && |
577 | "Trying to get me to create degenerate basic block!" ); |
578 | |
579 | BasicBlock *New = BasicBlock::Create(Context&: getContext(), Name: BBName, Parent: getParent(), |
580 | InsertBefore: this->getNextNode()); |
581 | |
582 | // Save DebugLoc of split point before invalidating iterator. |
583 | DebugLoc Loc = I->getStableDebugLoc(); |
584 | // Move all of the specified instructions from the original basic block into |
585 | // the new basic block. |
586 | New->splice(ToIt: New->end(), FromBB: this, FromBeginIt: I, FromEndIt: end()); |
587 | |
588 | // Add a branch instruction to the newly formed basic block. |
589 | BranchInst *BI = BranchInst::Create(IfTrue: New, InsertAtEnd: this); |
590 | BI->setDebugLoc(Loc); |
591 | |
592 | // Now we must loop through all of the successors of the New block (which |
593 | // _were_ the successors of the 'this' block), and update any PHI nodes in |
594 | // successors. If there were PHI nodes in the successors, then they need to |
595 | // know that incoming branches will be from New, not from Old (this). |
596 | // |
597 | New->replaceSuccessorsPhiUsesWith(Old: this, New); |
598 | return New; |
599 | } |
600 | |
601 | BasicBlock *BasicBlock::splitBasicBlockBefore(iterator I, const Twine &BBName) { |
602 | assert(getTerminator() && |
603 | "Can't use splitBasicBlockBefore on degenerate BB!" ); |
604 | assert(I != InstList.end() && |
605 | "Trying to get me to create degenerate basic block!" ); |
606 | |
607 | assert((!isa<PHINode>(*I) || getSinglePredecessor()) && |
608 | "cannot split on multi incoming phis" ); |
609 | |
610 | BasicBlock *New = BasicBlock::Create(Context&: getContext(), Name: BBName, Parent: getParent(), InsertBefore: this); |
611 | // Save DebugLoc of split point before invalidating iterator. |
612 | DebugLoc Loc = I->getDebugLoc(); |
613 | // Move all of the specified instructions from the original basic block into |
614 | // the new basic block. |
615 | New->splice(ToIt: New->end(), FromBB: this, FromBeginIt: begin(), FromEndIt: I); |
616 | |
617 | // Loop through all of the predecessors of the 'this' block (which will be the |
618 | // predecessors of the New block), replace the specified successor 'this' |
619 | // block to point at the New block and update any PHI nodes in 'this' block. |
620 | // If there were PHI nodes in 'this' block, the PHI nodes are updated |
621 | // to reflect that the incoming branches will be from the New block and not |
622 | // from predecessors of the 'this' block. |
623 | // Save predecessors to separate vector before modifying them. |
624 | SmallVector<BasicBlock *, 4> Predecessors; |
625 | for (BasicBlock *Pred : predecessors(BB: this)) |
626 | Predecessors.push_back(Elt: Pred); |
627 | for (BasicBlock *Pred : Predecessors) { |
628 | Instruction *TI = Pred->getTerminator(); |
629 | TI->replaceSuccessorWith(OldBB: this, NewBB: New); |
630 | this->replacePhiUsesWith(Old: Pred, New); |
631 | } |
632 | // Add a branch instruction from "New" to "this" Block. |
633 | BranchInst *BI = BranchInst::Create(IfTrue: this, InsertAtEnd: New); |
634 | BI->setDebugLoc(Loc); |
635 | |
636 | return New; |
637 | } |
638 | |
639 | BasicBlock::iterator BasicBlock::erase(BasicBlock::iterator FromIt, |
640 | BasicBlock::iterator ToIt) { |
641 | for (Instruction &I : make_early_inc_range(Range: make_range(x: FromIt, y: ToIt))) |
642 | I.eraseFromParent(); |
643 | return ToIt; |
644 | } |
645 | |
646 | void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) { |
647 | // N.B. This might not be a complete BasicBlock, so don't assume |
648 | // that it ends with a non-phi instruction. |
649 | for (Instruction &I : *this) { |
650 | PHINode *PN = dyn_cast<PHINode>(Val: &I); |
651 | if (!PN) |
652 | break; |
653 | PN->replaceIncomingBlockWith(Old, New); |
654 | } |
655 | } |
656 | |
657 | void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *Old, |
658 | BasicBlock *New) { |
659 | Instruction *TI = getTerminator(); |
660 | if (!TI) |
661 | // Cope with being called on a BasicBlock that doesn't have a terminator |
662 | // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. |
663 | return; |
664 | for (BasicBlock *Succ : successors(I: TI)) |
665 | Succ->replacePhiUsesWith(Old, New); |
666 | } |
667 | |
668 | void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { |
669 | this->replaceSuccessorsPhiUsesWith(Old: this, New); |
670 | } |
671 | |
672 | bool BasicBlock::isLandingPad() const { |
673 | return isa<LandingPadInst>(Val: getFirstNonPHI()); |
674 | } |
675 | |
676 | const LandingPadInst *BasicBlock::getLandingPadInst() const { |
677 | return dyn_cast<LandingPadInst>(Val: getFirstNonPHI()); |
678 | } |
679 | |
680 | std::optional<uint64_t> BasicBlock::() const { |
681 | const Instruction *TI = getTerminator(); |
682 | if (MDNode * = |
683 | TI->getMetadata(KindID: LLVMContext::MD_irr_loop)) { |
684 | MDString *MDName = cast<MDString>(Val: MDIrrLoopHeader->getOperand(I: 0)); |
685 | if (MDName->getString().equals(RHS: "loop_header_weight" )) { |
686 | auto *CI = mdconst::extract<ConstantInt>(MD: MDIrrLoopHeader->getOperand(I: 1)); |
687 | return std::optional<uint64_t>(CI->getValue().getZExtValue()); |
688 | } |
689 | } |
690 | return std::nullopt; |
691 | } |
692 | |
693 | BasicBlock::iterator llvm::skipDebugIntrinsics(BasicBlock::iterator It) { |
694 | while (isa<DbgInfoIntrinsic>(Val: It)) |
695 | ++It; |
696 | return It; |
697 | } |
698 | |
699 | void BasicBlock::renumberInstructions() { |
700 | unsigned Order = 0; |
701 | for (Instruction &I : *this) |
702 | I.Order = Order++; |
703 | |
704 | // Set the bit to indicate that the instruction order valid and cached. |
705 | BasicBlockBits Bits = getBasicBlockBits(); |
706 | Bits.InstrOrderValid = true; |
707 | setBasicBlockBits(Bits); |
708 | |
709 | NumInstrRenumberings++; |
710 | } |
711 | |
712 | void BasicBlock::flushTerminatorDbgRecords() { |
713 | // If we erase the terminator in a block, any DbgRecords will sink and "fall |
714 | // off the end", existing after any terminator that gets inserted. With |
715 | // dbg.value intrinsics we would just insert the terminator at end() and |
716 | // the dbg.values would come before the terminator. With DbgRecords, we must |
717 | // do this manually. |
718 | // To get out of this unfortunate form, whenever we insert a terminator, |
719 | // check whether there's anything trailing at the end and move those |
720 | // DbgRecords in front of the terminator. |
721 | |
722 | // Do nothing if we're not in new debug-info format. |
723 | if (!IsNewDbgInfoFormat) |
724 | return; |
725 | |
726 | // If there's no terminator, there's nothing to do. |
727 | Instruction *Term = getTerminator(); |
728 | if (!Term) |
729 | return; |
730 | |
731 | // Are there any dangling DbgRecords? |
732 | DbgMarker *TrailingDbgRecords = getTrailingDbgRecords(); |
733 | if (!TrailingDbgRecords) |
734 | return; |
735 | |
736 | // Transfer DbgRecords from the trailing position onto the terminator. |
737 | createMarker(I: Term); |
738 | Term->DebugMarker->absorbDebugValues(Src&: *TrailingDbgRecords, InsertAtHead: false); |
739 | TrailingDbgRecords->eraseFromParent(); |
740 | deleteTrailingDbgRecords(); |
741 | } |
742 | |
743 | void BasicBlock::spliceDebugInfoEmptyBlock(BasicBlock::iterator Dest, |
744 | BasicBlock *Src, |
745 | BasicBlock::iterator First, |
746 | BasicBlock::iterator Last) { |
747 | // Imagine the folowing: |
748 | // |
749 | // bb1: |
750 | // dbg.value(... |
751 | // ret i32 0 |
752 | // |
753 | // If an optimisation pass attempts to splice the contents of the block from |
754 | // BB1->begin() to BB1->getTerminator(), then the dbg.value will be |
755 | // transferred to the destination. |
756 | // However, in the "new" DbgRecord format for debug-info, that range is empty: |
757 | // begin() returns an iterator to the terminator, as there will only be a |
758 | // single instruction in the block. We must piece together from the bits set |
759 | // in the iterators whether there was the intention to transfer any debug |
760 | // info. |
761 | |
762 | // If we're not in "new" debug-info format, do nothing. |
763 | if (!IsNewDbgInfoFormat) |
764 | return; |
765 | |
766 | assert(First == Last); |
767 | bool InsertAtHead = Dest.getHeadBit(); |
768 | bool ReadFromHead = First.getHeadBit(); |
769 | |
770 | // If the source block is completely empty, including no terminator, then |
771 | // transfer any trailing DbgRecords that are still hanging around. This can |
772 | // occur when a block is optimised away and the terminator has been moved |
773 | // somewhere else. |
774 | if (Src->empty()) { |
775 | DbgMarker *SrcTrailingDbgRecords = Src->getTrailingDbgRecords(); |
776 | if (!SrcTrailingDbgRecords) |
777 | return; |
778 | |
779 | Dest->adoptDbgRecords(BB: Src, It: Src->end(), InsertAtHead); |
780 | // adoptDbgRecords should have released the trailing DbgRecords. |
781 | assert(!Src->getTrailingDbgRecords()); |
782 | return; |
783 | } |
784 | |
785 | // There are instructions in this block; if the First iterator was |
786 | // with begin() / getFirstInsertionPt() then the caller intended debug-info |
787 | // at the start of the block to be transferred. Return otherwise. |
788 | if (Src->empty() || First != Src->begin() || !ReadFromHead) |
789 | return; |
790 | |
791 | // Is there actually anything to transfer? |
792 | if (!First->hasDbgRecords()) |
793 | return; |
794 | |
795 | createMarker(It: Dest)->absorbDebugValues(Src&: *First->DebugMarker, InsertAtHead); |
796 | |
797 | return; |
798 | } |
799 | |
800 | void BasicBlock::spliceDebugInfo(BasicBlock::iterator Dest, BasicBlock *Src, |
801 | BasicBlock::iterator First, |
802 | BasicBlock::iterator Last) { |
803 | /* Do a quick normalisation before calling the real splice implementation. We |
804 | might be operating on a degenerate basic block that has no instructions |
805 | in it, a legitimate transient state. In that case, Dest will be end() and |
806 | any DbgRecords temporarily stored in the TrailingDbgRecords map in |
807 | LLVMContext. We might illustrate it thus: |
808 | |
809 | Dest |
810 | | |
811 | this-block: ~~~~~~~~ |
812 | Src-block: ++++B---B---B---B:::C |
813 | | | |
814 | First Last |
815 | |
816 | However: does the caller expect the "~" DbgRecords to end up before or |
817 | after the spliced segment? This is communciated in the "Head" bit of Dest, |
818 | which signals whether the caller called begin() or end() on this block. |
819 | |
820 | If the head bit is set, then all is well, we leave DbgRecords trailing just |
821 | like how dbg.value instructions would trail after instructions spliced to |
822 | the beginning of this block. |
823 | |
824 | If the head bit isn't set, then try to jam the "~" DbgRecords onto the |
825 | front of the First instruction, then splice like normal, which joins the |
826 | "~" DbgRecords with the "+" DbgRecords. However if the "+" DbgRecords are |
827 | supposed to be left behind in Src, then: |
828 | * detach the "+" DbgRecords, |
829 | * move the "~" DbgRecords onto First, |
830 | * splice like normal, |
831 | * replace the "+" DbgRecords onto the Last position. |
832 | Complicated, but gets the job done. */ |
833 | |
834 | // If we're inserting at end(), and not in front of dangling DbgRecords, then |
835 | // move the DbgRecords onto "First". They'll then be moved naturally in the |
836 | // splice process. |
837 | DbgMarker *MoreDanglingDbgRecords = nullptr; |
838 | DbgMarker *OurTrailingDbgRecords = getTrailingDbgRecords(); |
839 | if (Dest == end() && !Dest.getHeadBit() && OurTrailingDbgRecords) { |
840 | // Are the "+" DbgRecords not supposed to move? If so, detach them |
841 | // temporarily. |
842 | if (!First.getHeadBit() && First->hasDbgRecords()) { |
843 | MoreDanglingDbgRecords = Src->getMarker(It: First); |
844 | MoreDanglingDbgRecords->removeFromParent(); |
845 | } |
846 | |
847 | if (First->hasDbgRecords()) { |
848 | // Place them at the front, it would look like this: |
849 | // Dest |
850 | // | |
851 | // this-block: |
852 | // Src-block: ~~~~~~~~++++B---B---B---B:::C |
853 | // | | |
854 | // First Last |
855 | First->adoptDbgRecords(BB: this, It: end(), InsertAtHead: true); |
856 | } else { |
857 | // No current marker, create one and absorb in. (FIXME: we can avoid an |
858 | // allocation in the future). |
859 | DbgMarker *CurMarker = Src->createMarker(I: &*First); |
860 | CurMarker->absorbDebugValues(Src&: *OurTrailingDbgRecords, InsertAtHead: false); |
861 | OurTrailingDbgRecords->eraseFromParent(); |
862 | } |
863 | deleteTrailingDbgRecords(); |
864 | First.setHeadBit(true); |
865 | } |
866 | |
867 | // Call the main debug-info-splicing implementation. |
868 | spliceDebugInfoImpl(ToIt: Dest, FromBB: Src, FromBeginIt: First, FromEndIt: Last); |
869 | |
870 | // Do we have some "+" DbgRecords hanging around that weren't supposed to |
871 | // move, and we detached to make things easier? |
872 | if (!MoreDanglingDbgRecords) |
873 | return; |
874 | |
875 | // FIXME: we could avoid an allocation here sometimes. (adoptDbgRecords |
876 | // requires an iterator). |
877 | DbgMarker *LastMarker = Src->createMarker(It: Last); |
878 | LastMarker->absorbDebugValues(Src&: *MoreDanglingDbgRecords, InsertAtHead: true); |
879 | MoreDanglingDbgRecords->eraseFromParent(); |
880 | } |
881 | |
882 | void BasicBlock::spliceDebugInfoImpl(BasicBlock::iterator Dest, BasicBlock *Src, |
883 | BasicBlock::iterator First, |
884 | BasicBlock::iterator Last) { |
885 | // Find out where to _place_ these dbg.values; if InsertAtHead is specified, |
886 | // this will be at the start of Dest's debug value range, otherwise this is |
887 | // just Dest's marker. |
888 | bool InsertAtHead = Dest.getHeadBit(); |
889 | bool ReadFromHead = First.getHeadBit(); |
890 | // Use this flag to signal the abnormal case, where we don't want to copy the |
891 | // DbgRecords ahead of the "Last" position. |
892 | bool ReadFromTail = !Last.getTailBit(); |
893 | bool LastIsEnd = (Last == Src->end()); |
894 | |
895 | /* |
896 | Here's an illustration of what we're about to do. We have two blocks, this |
897 | and Src, and two segments of list. Each instruction is marked by a capital |
898 | while potential DbgRecord debug-info is marked out by "-" characters and a |
899 | few other special characters (+:=) where I want to highlight what's going |
900 | on. |
901 | |
902 | Dest |
903 | | |
904 | this-block: A----A----A ====A----A----A----A---A---A |
905 | Src-block ++++B---B---B---B:::C |
906 | | | |
907 | First Last |
908 | |
909 | The splice method is going to take all the instructions from First up to |
910 | (but not including) Last and insert them in _front_ of Dest, forming one |
911 | long list. All the DbgRecords attached to instructions _between_ First and |
912 | Last need no maintenence. However, we have to do special things with the |
913 | DbgRecords marked with the +:= characters. We only have three positions: |
914 | should the "+" DbgRecords be transferred, and if so to where? Do we move the |
915 | ":" DbgRecords? Would they go in front of the "=" DbgRecords, or should the |
916 | "=" DbgRecords go before "+" DbgRecords? |
917 | |
918 | We're told which way it should be by the bits carried in the iterators. The |
919 | "Head" bit indicates whether the specified position is supposed to be at the |
920 | front of the attached DbgRecords (true) or not (false). The Tail bit is true |
921 | on the other end of a range: is the range intended to include DbgRecords up |
922 | to the end (false) or not (true). |
923 | |
924 | FIXME: the tail bit doesn't need to be distinct from the head bit, we could |
925 | combine them. |
926 | |
927 | Here are some examples of different configurations: |
928 | |
929 | Dest.Head = true, First.Head = true, Last.Tail = false |
930 | |
931 | this-block: A----A----A++++B---B---B---B:::====A----A----A----A---A---A |
932 | | | |
933 | First Dest |
934 | |
935 | Wheras if we didn't want to read from the Src list, |
936 | |
937 | Dest.Head = true, First.Head = false, Last.Tail = false |
938 | |
939 | this-block: A----A----AB---B---B---B:::====A----A----A----A---A---A |
940 | | | |
941 | First Dest |
942 | |
943 | Or if we didn't want to insert at the head of Dest: |
944 | |
945 | Dest.Head = false, First.Head = false, Last.Tail = false |
946 | |
947 | this-block: A----A----A====B---B---B---B:::A----A----A----A---A---A |
948 | | | |
949 | First Dest |
950 | |
951 | Tests for these various configurations can be found in the unit test file |
952 | BasicBlockDbgInfoTest.cpp. |
953 | |
954 | */ |
955 | |
956 | // Detach the marker at Dest -- this lets us move the "====" DbgRecords |
957 | // around. |
958 | DbgMarker *DestMarker = nullptr; |
959 | if (Dest != end()) { |
960 | if ((DestMarker = getMarker(It: Dest))) |
961 | DestMarker->removeFromParent(); |
962 | } |
963 | |
964 | // If we're moving the tail range of DbgRecords (":::"), absorb them into the |
965 | // front of the DbgRecords at Dest. |
966 | if (ReadFromTail && Src->getMarker(It: Last)) { |
967 | DbgMarker *FromLast = Src->getMarker(It: Last); |
968 | if (LastIsEnd) { |
969 | Dest->adoptDbgRecords(BB: Src, It: Last, InsertAtHead: true); |
970 | // adoptDbgRecords will release any trailers. |
971 | assert(!Src->getTrailingDbgRecords()); |
972 | } else { |
973 | // FIXME: can we use adoptDbgRecords here to reduce allocations? |
974 | DbgMarker *OntoDest = createMarker(It: Dest); |
975 | OntoDest->absorbDebugValues(Src&: *FromLast, InsertAtHead: true); |
976 | } |
977 | } |
978 | |
979 | // If we're _not_ reading from the head of First, i.e. the "++++" DbgRecords, |
980 | // move their markers onto Last. They remain in the Src block. No action |
981 | // needed. |
982 | if (!ReadFromHead && First->hasDbgRecords()) { |
983 | if (Last != Src->end()) { |
984 | Last->adoptDbgRecords(BB: Src, It: First, InsertAtHead: true); |
985 | } else { |
986 | DbgMarker *OntoLast = Src->createMarker(It: Last); |
987 | DbgMarker *FromFirst = Src->createMarker(It: First); |
988 | // Always insert at front of Last. |
989 | OntoLast->absorbDebugValues(Src&: *FromFirst, InsertAtHead: true); |
990 | } |
991 | } |
992 | |
993 | // Finally, do something with the "====" DbgRecords we detached. |
994 | if (DestMarker) { |
995 | if (InsertAtHead) { |
996 | // Insert them at the end of the DbgRecords at Dest. The "::::" DbgRecords |
997 | // might be in front of them. |
998 | DbgMarker *NewDestMarker = createMarker(It: Dest); |
999 | NewDestMarker->absorbDebugValues(Src&: *DestMarker, InsertAtHead: false); |
1000 | } else { |
1001 | // Insert them right at the start of the range we moved, ahead of First |
1002 | // and the "++++" DbgRecords. |
1003 | DbgMarker *FirstMarker = createMarker(It: First); |
1004 | FirstMarker->absorbDebugValues(Src&: *DestMarker, InsertAtHead: true); |
1005 | } |
1006 | DestMarker->eraseFromParent(); |
1007 | } else if (Dest == end() && !InsertAtHead) { |
1008 | // In the rare circumstance where we insert at end(), and we did not |
1009 | // generate the iterator with begin() / getFirstInsertionPt(), it means |
1010 | // any trailing debug-info at the end of the block would "normally" have |
1011 | // been pushed in front of "First". Move it there now. |
1012 | DbgMarker *FirstMarker = getMarker(It: First); |
1013 | DbgMarker *TrailingDbgRecords = getTrailingDbgRecords(); |
1014 | if (TrailingDbgRecords) { |
1015 | FirstMarker->absorbDebugValues(Src&: *TrailingDbgRecords, InsertAtHead: true); |
1016 | TrailingDbgRecords->eraseFromParent(); |
1017 | deleteTrailingDbgRecords(); |
1018 | } |
1019 | } |
1020 | } |
1021 | |
1022 | void BasicBlock::splice(iterator Dest, BasicBlock *Src, iterator First, |
1023 | iterator Last) { |
1024 | assert(Src->IsNewDbgInfoFormat == IsNewDbgInfoFormat); |
1025 | |
1026 | #ifdef EXPENSIVE_CHECKS |
1027 | // Check that First is before Last. |
1028 | auto FromBBEnd = Src->end(); |
1029 | for (auto It = First; It != Last; ++It) |
1030 | assert(It != FromBBEnd && "FromBeginIt not before FromEndIt!" ); |
1031 | #endif // EXPENSIVE_CHECKS |
1032 | |
1033 | // Lots of horrible special casing for empty transfers: the dbg.values between |
1034 | // two positions could be spliced in dbg.value mode. |
1035 | if (First == Last) { |
1036 | spliceDebugInfoEmptyBlock(Dest, Src, First, Last); |
1037 | return; |
1038 | } |
1039 | |
1040 | // Handle non-instr debug-info specific juggling. |
1041 | if (IsNewDbgInfoFormat) |
1042 | spliceDebugInfo(Dest, Src, First, Last); |
1043 | |
1044 | // And move the instructions. |
1045 | getInstList().splice(where: Dest, L2&: Src->getInstList(), first: First, last: Last); |
1046 | |
1047 | flushTerminatorDbgRecords(); |
1048 | } |
1049 | |
1050 | void BasicBlock::insertDbgRecordAfter(DbgRecord *DR, Instruction *I) { |
1051 | assert(IsNewDbgInfoFormat); |
1052 | assert(I->getParent() == this); |
1053 | |
1054 | iterator NextIt = std::next(x: I->getIterator()); |
1055 | DbgMarker *NextMarker = createMarker(It: NextIt); |
1056 | NextMarker->insertDbgRecord(New: DR, InsertAtHead: true); |
1057 | } |
1058 | |
1059 | void BasicBlock::insertDbgRecordBefore(DbgRecord *DR, |
1060 | InstListType::iterator Where) { |
1061 | assert(Where == end() || Where->getParent() == this); |
1062 | bool InsertAtHead = Where.getHeadBit(); |
1063 | DbgMarker *M = createMarker(It: Where); |
1064 | M->insertDbgRecord(New: DR, InsertAtHead); |
1065 | } |
1066 | |
1067 | DbgMarker *BasicBlock::getNextMarker(Instruction *I) { |
1068 | return getMarker(It: std::next(x: I->getIterator())); |
1069 | } |
1070 | |
1071 | DbgMarker *BasicBlock::getMarker(InstListType::iterator It) { |
1072 | if (It == end()) { |
1073 | DbgMarker *DM = getTrailingDbgRecords(); |
1074 | return DM; |
1075 | } |
1076 | return It->DebugMarker; |
1077 | } |
1078 | |
1079 | void BasicBlock::reinsertInstInDbgRecords( |
1080 | Instruction *I, std::optional<DbgRecord::self_iterator> Pos) { |
1081 | // "I" was originally removed from a position where it was |
1082 | // immediately in front of Pos. Any DbgRecords on that position then "fell |
1083 | // down" onto Pos. "I" has been re-inserted at the front of that wedge of |
1084 | // DbgRecords, shuffle them around to represent the original positioning. To |
1085 | // illustrate: |
1086 | // |
1087 | // Instructions: I1---I---I0 |
1088 | // DbgRecords: DDD DDD |
1089 | // |
1090 | // Instruction "I" removed, |
1091 | // |
1092 | // Instructions: I1------I0 |
1093 | // DbgRecords: DDDDDD |
1094 | // ^Pos |
1095 | // |
1096 | // Instruction "I" re-inserted (now): |
1097 | // |
1098 | // Instructions: I1---I------I0 |
1099 | // DbgRecords: DDDDDD |
1100 | // ^Pos |
1101 | // |
1102 | // After this method completes: |
1103 | // |
1104 | // Instructions: I1---I---I0 |
1105 | // DbgRecords: DDD DDD |
1106 | |
1107 | // This happens if there were no DbgRecords on I0. Are there now DbgRecords |
1108 | // there? |
1109 | if (!Pos) { |
1110 | DbgMarker *NextMarker = getNextMarker(I); |
1111 | if (!NextMarker) |
1112 | return; |
1113 | if (NextMarker->StoredDbgRecords.empty()) |
1114 | return; |
1115 | // There are DbgMarkers there now -- they fell down from "I". |
1116 | DbgMarker *ThisMarker = createMarker(I); |
1117 | ThisMarker->absorbDebugValues(Src&: *NextMarker, InsertAtHead: false); |
1118 | return; |
1119 | } |
1120 | |
1121 | // Is there even a range of DbgRecords to move? |
1122 | DbgMarker *DM = (*Pos)->getMarker(); |
1123 | auto Range = make_range(x: DM->StoredDbgRecords.begin(), y: (*Pos)); |
1124 | if (Range.begin() == Range.end()) |
1125 | return; |
1126 | |
1127 | // Otherwise: splice. |
1128 | DbgMarker *ThisMarker = createMarker(I); |
1129 | assert(ThisMarker->StoredDbgRecords.empty()); |
1130 | ThisMarker->absorbDebugValues(Range, Src&: *DM, InsertAtHead: true); |
1131 | } |
1132 | |
1133 | #ifndef NDEBUG |
1134 | /// In asserts builds, this checks the numbering. In non-asserts builds, it |
1135 | /// is defined as a no-op inline function in BasicBlock.h. |
1136 | void BasicBlock::validateInstrOrdering() const { |
1137 | if (!isInstrOrderValid()) |
1138 | return; |
1139 | const Instruction *Prev = nullptr; |
1140 | for (const Instruction &I : *this) { |
1141 | assert((!Prev || Prev->comesBefore(&I)) && |
1142 | "cached instruction ordering is incorrect" ); |
1143 | Prev = &I; |
1144 | } |
1145 | } |
1146 | #endif |
1147 | |
1148 | void BasicBlock::setTrailingDbgRecords(DbgMarker *foo) { |
1149 | getContext().pImpl->setTrailingDbgRecords(B: this, M: foo); |
1150 | } |
1151 | |
1152 | DbgMarker *BasicBlock::getTrailingDbgRecords() { |
1153 | return getContext().pImpl->getTrailingDbgRecords(B: this); |
1154 | } |
1155 | |
1156 | void BasicBlock::deleteTrailingDbgRecords() { |
1157 | getContext().pImpl->deleteTrailingDbgRecords(B: this); |
1158 | } |
1159 | |