File tree Expand file tree Collapse file tree 4 files changed +43
-22
lines changed Expand file tree Collapse file tree 4 files changed +43
-22
lines changed Original file line number Diff line number Diff line change @@ -149,6 +149,13 @@ class Instruction : public User,
149
149
// / it takes constant time.
150
150
bool comesBefore (const Instruction *Other) const ;
151
151
152
+ // / Get the first insertion point at which the result of this instruction
153
+ // / is defined. This is *not* the directly following instruction in a number
154
+ // / of cases, e.g. phi nodes or terminators that return values. This function
155
+ // / may return null if the insertion after the definition is not possible,
156
+ // / e.g. due to a catchswitch terminator.
157
+ Instruction *getInsertionPointAfterDef ();
158
+
152
159
// ===--------------------------------------------------------------------===//
153
160
// Subclass classification.
154
161
// ===--------------------------------------------------------------------===//
Original file line number Diff line number Diff line change @@ -116,6 +116,32 @@ bool Instruction::comesBefore(const Instruction *Other) const {
116
116
return Order < Other->Order ;
117
117
}
118
118
119
+ Instruction *Instruction::getInsertionPointAfterDef () {
120
+ assert (!getType ()->isVoidTy () && " Instruction must define result" );
121
+ BasicBlock *InsertBB;
122
+ BasicBlock::iterator InsertPt;
123
+ if (auto *PN = dyn_cast<PHINode>(this )) {
124
+ InsertBB = PN->getParent ();
125
+ InsertPt = InsertBB->getFirstInsertionPt ();
126
+ } else if (auto *II = dyn_cast<InvokeInst>(this )) {
127
+ InsertBB = II->getNormalDest ();
128
+ InsertPt = InsertBB->getFirstInsertionPt ();
129
+ } else if (auto *CB = dyn_cast<CallBrInst>(this )) {
130
+ InsertBB = CB->getDefaultDest ();
131
+ InsertPt = InsertBB->getFirstInsertionPt ();
132
+ } else {
133
+ assert (!isTerminator () && " Only invoke/callbr terminators return value" );
134
+ InsertBB = getParent ();
135
+ InsertPt = std::next (getIterator ());
136
+ }
137
+
138
+ // catchswitch blocks don't have any legal insertion point (because they
139
+ // are both an exception pad and a terminator).
140
+ if (InsertPt == InsertBB->end ())
141
+ return nullptr ;
142
+ return &*InsertPt;
143
+ }
144
+
119
145
bool Instruction::isOnlyUserOfAnyOperand () {
120
146
return any_of (operands (), [](Value *V) { return V->hasOneUser (); });
121
147
}
Original file line number Diff line number Diff line change @@ -2633,16 +2633,13 @@ void coro::salvageDebugInfo(
2633
2633
// dbg.value or dbg.addr since they do not have the same function wide
2634
2634
// guarantees that dbg.declare does.
2635
2635
if (!isa<DbgValueInst>(DVI) && !isa<DbgAddrIntrinsic>(DVI)) {
2636
- if (auto *II = dyn_cast<InvokeInst>(Storage))
2637
- DVI->moveBefore (II->getNormalDest ()->getFirstNonPHI ());
2638
- else if (auto *CBI = dyn_cast<CallBrInst>(Storage))
2639
- DVI->moveBefore (CBI->getDefaultDest ()->getFirstNonPHI ());
2640
- else if (auto *InsertPt = dyn_cast<Instruction>(Storage)) {
2641
- assert (!InsertPt->isTerminator () &&
2642
- " Unimaged terminator that could return a storage." );
2643
- DVI->moveAfter (InsertPt);
2644
- } else if (isa<Argument>(Storage))
2645
- DVI->moveAfter (F->getEntryBlock ().getFirstNonPHI ());
2636
+ Instruction *InsertPt = nullptr ;
2637
+ if (auto *I = dyn_cast<Instruction>(Storage))
2638
+ InsertPt = I->getInsertionPointAfterDef ();
2639
+ else if (isa<Argument>(Storage))
2640
+ InsertPt = &*F->getEntryBlock ().begin ();
2641
+ if (InsertPt)
2642
+ DVI->moveBefore (InsertPt);
2646
2643
}
2647
2644
}
2648
2645
Original file line number Diff line number Diff line change @@ -3451,18 +3451,9 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
3451
3451
NV = NC = CastInst::CreateBitOrPointerCast (NC, OldRetTy);
3452
3452
NC->setDebugLoc (Caller->getDebugLoc ());
3453
3453
3454
- // If this is an invoke/callbr instruction, we should insert it after the
3455
- // first non-phi instruction in the normal successor block.
3456
- if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
3457
- BasicBlock::iterator I = II->getNormalDest ()->getFirstInsertionPt ();
3458
- InsertNewInstBefore (NC, *I);
3459
- } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(Caller)) {
3460
- BasicBlock::iterator I = CBI->getDefaultDest ()->getFirstInsertionPt ();
3461
- InsertNewInstBefore (NC, *I);
3462
- } else {
3463
- // Otherwise, it's a call, just insert cast right after the call.
3464
- InsertNewInstBefore (NC, *Caller);
3465
- }
3454
+ Instruction *InsertPt = NewCall->getInsertionPointAfterDef ();
3455
+ assert (InsertPt && " No place to insert cast" );
3456
+ InsertNewInstBefore (NC, *InsertPt);
3466
3457
Worklist.pushUsersToWorkList (*Caller);
3467
3458
} else {
3468
3459
NV = UndefValue::get (Caller->getType ());
You can’t perform that action at this time.
0 commit comments