@@ -459,6 +459,7 @@ class CodeGenPrepare : public FunctionPass {
459
459
bool optimizeExtractElementInst (Instruction *Inst);
460
460
bool dupRetToEnableTailCallOpts (BasicBlock *BB, ModifyDT &ModifiedDT);
461
461
bool fixupDbgValue (Instruction *I);
462
+ bool fixupDPValue (DPValue &I);
462
463
bool placeDbgValues (Function &F);
463
464
bool placePseudoProbes (Function &F);
464
465
bool canFormExtLd (const SmallVectorImpl<Instruction *> &MovedExts,
@@ -1753,8 +1754,8 @@ static bool sinkCmpExpression(CmpInst *Cmp, const TargetLowering &TLI) {
1753
1754
BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt ();
1754
1755
assert (InsertPt != UserBB->end ());
1755
1756
InsertedCmp = CmpInst::Create (Cmp->getOpcode (), Cmp->getPredicate (),
1756
- Cmp->getOperand (0 ), Cmp->getOperand (1 ), " " ,
1757
- &* InsertPt);
1757
+ Cmp->getOperand (0 ), Cmp->getOperand (1 ), " " );
1758
+ InsertedCmp-> insertBefore (*UserBB, InsertPt);
1758
1759
// Propagate the debug info.
1759
1760
InsertedCmp->setDebugLoc (Cmp->getDebugLoc ());
1760
1761
}
@@ -2066,10 +2067,13 @@ SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI,
2066
2067
// Sink the trunc
2067
2068
BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt ();
2068
2069
TruncInsertPt++;
2070
+ // It will go ahead of any debug-info.
2071
+ TruncInsertPt.setHeadBit (true );
2069
2072
assert (TruncInsertPt != TruncUserBB->end ());
2070
2073
2071
2074
InsertedTrunc = CastInst::Create (TruncI->getOpcode (), InsertedShift,
2072
- TruncI->getType (), " " , &*TruncInsertPt);
2075
+ TruncI->getType (), " " );
2076
+ InsertedTrunc->insertBefore (*TruncUserBB, TruncInsertPt);
2073
2077
InsertedTrunc->setDebugLoc (TruncI->getDebugLoc ());
2074
2078
2075
2079
MadeChange = true ;
@@ -2234,7 +2238,9 @@ static bool despeculateCountZeros(IntrinsicInst *CountZeros,
2234
2238
// Create another block after the count zero intrinsic. A PHI will be added
2235
2239
// in this block to select the result of the intrinsic or the bit-width
2236
2240
// constant if the input to the intrinsic is zero.
2237
- BasicBlock::iterator SplitPt = ++(BasicBlock::iterator (CountZeros));
2241
+ BasicBlock::iterator SplitPt = std::next (BasicBlock::iterator (CountZeros));
2242
+ // Any debug-info after CountZeros should not be included.
2243
+ SplitPt.setHeadBit (true );
2238
2244
BasicBlock *EndBlock = CallBlock->splitBasicBlock (SplitPt, " cond.end" );
2239
2245
if (IsHugeFunc)
2240
2246
FreshBBs.insert (EndBlock);
@@ -2829,34 +2835,44 @@ class TypePromotionTransaction {
2829
2835
Instruction *PrevInst;
2830
2836
BasicBlock *BB;
2831
2837
} Point;
2838
+ std::optional<DPValue::self_iterator> BeforeDPValue = std::nullopt;
2832
2839
2833
2840
// / Remember whether or not the instruction had a previous instruction.
2834
2841
bool HasPrevInstruction;
2835
2842
2836
2843
public:
2837
2844
// / Record the position of \p Inst.
2838
2845
InsertionHandler (Instruction *Inst) {
2839
- BasicBlock::iterator It = Inst->getIterator ();
2840
- HasPrevInstruction = (It != (Inst->getParent ()->begin ()));
2841
- if (HasPrevInstruction)
2842
- Point.PrevInst = &*--It;
2843
- else
2844
- Point.BB = Inst->getParent ();
2846
+ HasPrevInstruction = (Inst != &*(Inst->getParent ()->begin ()));
2847
+ BasicBlock *BB = Inst->getParent ();
2848
+
2849
+ // Record where we would have to re-insert the instruction in the sequence
2850
+ // of DPValues, if we ended up reinserting.
2851
+ if (BB->IsNewDbgInfoFormat )
2852
+ BeforeDPValue = Inst->getDbgReinsertionPosition ();
2853
+
2854
+ if (HasPrevInstruction) {
2855
+ Point.PrevInst = &*std::prev (Inst->getIterator ());
2856
+ } else {
2857
+ Point.BB = BB;
2858
+ }
2845
2859
}
2846
2860
2847
2861
// / Insert \p Inst at the recorded position.
2848
2862
void insert (Instruction *Inst) {
2849
2863
if (HasPrevInstruction) {
2850
2864
if (Inst->getParent ())
2851
2865
Inst->removeFromParent ();
2852
- Inst->insertAfter (Point.PrevInst );
2866
+ Inst->insertAfter (&* Point.PrevInst );
2853
2867
} else {
2854
- Instruction * Position = &* Point.BB ->getFirstInsertionPt ();
2868
+ BasicBlock::iterator Position = Point.BB ->getFirstInsertionPt ();
2855
2869
if (Inst->getParent ())
2856
- Inst->moveBefore (Position);
2870
+ Inst->moveBefore (*Point. BB , Position);
2857
2871
else
2858
- Inst->insertBefore (Position);
2872
+ Inst->insertBefore (*Point. BB , Position);
2859
2873
}
2874
+
2875
+ Inst->getParent ()->reinsertInstInDPValues (Inst, BeforeDPValue);
2860
2876
}
2861
2877
};
2862
2878
@@ -3059,6 +3075,8 @@ class TypePromotionTransaction {
3059
3075
SmallVector<InstructionAndIdx, 4 > OriginalUses;
3060
3076
// / Keep track of the debug users.
3061
3077
SmallVector<DbgValueInst *, 1 > DbgValues;
3078
+ // / And non-instruction debug-users too.
3079
+ SmallVector<DPValue *, 1 > DPValues;
3062
3080
3063
3081
// / Keep track of the new value so that we can undo it by replacing
3064
3082
// / instances of the new value with the original value.
@@ -3079,7 +3097,7 @@ class TypePromotionTransaction {
3079
3097
}
3080
3098
// Record the debug uses separately. They are not in the instruction's
3081
3099
// use list, but they are replaced by RAUW.
3082
- findDbgValues (DbgValues, Inst);
3100
+ findDbgValues (DbgValues, Inst, &DPValues );
3083
3101
3084
3102
// Now, we can replace the uses.
3085
3103
Inst->replaceAllUsesWith (New);
@@ -3096,6 +3114,10 @@ class TypePromotionTransaction {
3096
3114
// correctness and utility of debug value instructions.
3097
3115
for (auto *DVI : DbgValues)
3098
3116
DVI->replaceVariableLocationOp (New, Inst);
3117
+ // Similar story with DPValues, the non-instruction representation of
3118
+ // dbg.values.
3119
+ for (DPValue *DPV : DPValues) // tested by transaction-test I'm adding
3120
+ DPV->replaceVariableLocationOp (New, Inst);
3099
3121
}
3100
3122
};
3101
3123
@@ -6749,7 +6771,7 @@ bool CodeGenPrepare::optimizeLoadExt(LoadInst *Load) {
6749
6771
!TLI->isLoadExtLegal (ISD::ZEXTLOAD, LoadResultVT, TruncVT))
6750
6772
return false ;
6751
6773
6752
- IRBuilder<> Builder (Load->getNextNode ());
6774
+ IRBuilder<> Builder (Load->getNextNonDebugInstruction ());
6753
6775
auto *NewAnd = cast<Instruction>(
6754
6776
Builder.CreateAnd (Load, ConstantInt::get (Ctx, DemandBits)));
6755
6777
// Mark this instruction as "inserted by CGP", so that other
@@ -7005,7 +7027,9 @@ bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) {
7005
7027
// Split the select block, according to how many (if any) values go on each
7006
7028
// side.
7007
7029
BasicBlock *StartBlock = SI->getParent ();
7008
- BasicBlock::iterator SplitPt = ++(BasicBlock::iterator (LastSI));
7030
+ BasicBlock::iterator SplitPt = std::next (BasicBlock::iterator (LastSI));
7031
+ // We should split before any debug-info.
7032
+ SplitPt.setHeadBit (true );
7009
7033
7010
7034
IRBuilder<> IB (SI);
7011
7035
auto *CondFr = IB.CreateFreeze (SI->getCondition (), SI->getName () + " .frozen" );
@@ -7017,18 +7041,18 @@ bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) {
7017
7041
BranchInst *FalseBranch = nullptr ;
7018
7042
if (TrueInstrs.size () == 0 ) {
7019
7043
FalseBranch = cast<BranchInst>(SplitBlockAndInsertIfElse (
7020
- CondFr, &* SplitPt, false , nullptr , nullptr , LI));
7044
+ CondFr, SplitPt, false , nullptr , nullptr , LI));
7021
7045
FalseBlock = FalseBranch->getParent ();
7022
7046
EndBlock = cast<BasicBlock>(FalseBranch->getOperand (0 ));
7023
7047
} else if (FalseInstrs.size () == 0 ) {
7024
7048
TrueBranch = cast<BranchInst>(SplitBlockAndInsertIfThen (
7025
- CondFr, &* SplitPt, false , nullptr , nullptr , LI));
7049
+ CondFr, SplitPt, false , nullptr , nullptr , LI));
7026
7050
TrueBlock = TrueBranch->getParent ();
7027
7051
EndBlock = cast<BasicBlock>(TrueBranch->getOperand (0 ));
7028
7052
} else {
7029
7053
Instruction *ThenTerm = nullptr ;
7030
7054
Instruction *ElseTerm = nullptr ;
7031
- SplitBlockAndInsertIfThenElse (CondFr, &* SplitPt, &ThenTerm, &ElseTerm,
7055
+ SplitBlockAndInsertIfThenElse (CondFr, SplitPt, &ThenTerm, &ElseTerm,
7032
7056
nullptr , nullptr , LI);
7033
7057
TrueBranch = cast<BranchInst>(ThenTerm);
7034
7058
FalseBranch = cast<BranchInst>(ElseTerm);
@@ -8095,10 +8119,14 @@ static bool optimizeBranch(BranchInst *Branch, const TargetLowering &TLI,
8095
8119
}
8096
8120
8097
8121
bool CodeGenPrepare::optimizeInst (Instruction *I, ModifyDT &ModifiedDT) {
8122
+ bool AnyChange = false ;
8123
+ for (DPValue &DPV : I->getDbgValueRange ())
8124
+ AnyChange |= fixupDPValue (DPV);
8125
+
8098
8126
// Bail out if we inserted the instruction to prevent optimizations from
8099
8127
// stepping on each other's toes.
8100
8128
if (InsertedInsts.count (I))
8101
- return false ;
8129
+ return AnyChange ;
8102
8130
8103
8131
// TODO: Move into the switch on opcode below here.
8104
8132
if (PHINode *P = dyn_cast<PHINode>(I)) {
@@ -8112,7 +8140,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
8112
8140
++NumPHIsElim;
8113
8141
return true ;
8114
8142
}
8115
- return false ;
8143
+ return AnyChange ;
8116
8144
}
8117
8145
8118
8146
if (CastInst *CI = dyn_cast<CastInst>(I)) {
@@ -8123,7 +8151,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
8123
8151
// the address of globals out of a loop). If this is the case, we don't
8124
8152
// want to forward-subst the cast.
8125
8153
if (isa<Constant>(CI->getOperand (0 )))
8126
- return false ;
8154
+ return AnyChange ;
8127
8155
8128
8156
if (OptimizeNoopCopyExpression (CI, *TLI, *DL))
8129
8157
return true ;
@@ -8149,7 +8177,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
8149
8177
return MadeChange | optimizeExtUses (I);
8150
8178
}
8151
8179
}
8152
- return false ;
8180
+ return AnyChange ;
8153
8181
}
8154
8182
8155
8183
if (auto *Cmp = dyn_cast<CmpInst>(I))
@@ -8244,7 +8272,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
8244
8272
return true ;
8245
8273
}
8246
8274
}
8247
- return false ;
8275
+ return AnyChange ;
8248
8276
}
8249
8277
8250
8278
if (tryToSinkFreeOperands (I))
@@ -8269,7 +8297,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
8269
8297
return optimizeBranch (cast<BranchInst>(I), *TLI, FreshBBs, IsHugeFunc);
8270
8298
}
8271
8299
8272
- return false ;
8300
+ return AnyChange ;
8273
8301
}
8274
8302
8275
8303
// / Given an OR instruction, check to see if this is a bitreverse
@@ -8359,6 +8387,49 @@ bool CodeGenPrepare::fixupDbgValue(Instruction *I) {
8359
8387
return AnyChange;
8360
8388
}
8361
8389
8390
+ // FIXME: should updating debug-info really cause the "changed" flag to fire,
8391
+ // which can cause a function to be reprocessed?
8392
+ bool CodeGenPrepare::fixupDPValue (DPValue &DPV) {
8393
+ if (DPV.Type != DPValue::LocationType::Value)
8394
+ return false ;
8395
+
8396
+ // Does this DPValue refer to a sunk address calculation?
8397
+ bool AnyChange = false ;
8398
+ SmallDenseSet<Value *> LocationOps (DPV.location_ops ().begin (),
8399
+ DPV.location_ops ().end ());
8400
+ for (Value *Location : LocationOps) {
8401
+ WeakTrackingVH SunkAddrVH = SunkAddrs[Location];
8402
+ Value *SunkAddr = SunkAddrVH.pointsToAliveValue () ? SunkAddrVH : nullptr ;
8403
+ if (SunkAddr) {
8404
+ // Point dbg.value at locally computed address, which should give the best
8405
+ // opportunity to be accurately lowered. This update may change the type
8406
+ // of pointer being referred to; however this makes no difference to
8407
+ // debugging information, and we can't generate bitcasts that may affect
8408
+ // codegen.
8409
+ DPV.replaceVariableLocationOp (Location, SunkAddr);
8410
+ AnyChange = true ;
8411
+ }
8412
+ }
8413
+ return AnyChange;
8414
+ }
8415
+
8416
+ static void DbgInserterHelper (DbgValueInst *DVI, Instruction *VI) {
8417
+ DVI->removeFromParent ();
8418
+ if (isa<PHINode>(VI))
8419
+ DVI->insertBefore (&*VI->getParent ()->getFirstInsertionPt ());
8420
+ else
8421
+ DVI->insertAfter (VI);
8422
+ }
8423
+
8424
+ static void DbgInserterHelper (DPValue *DPV, Instruction *VI) {
8425
+ DPV->removeFromParent ();
8426
+ BasicBlock *VIBB = VI->getParent ();
8427
+ if (isa<PHINode>(VI))
8428
+ VIBB->insertDPValueBefore (DPV, VIBB->getFirstInsertionPt ());
8429
+ else
8430
+ VIBB->insertDPValueAfter (DPV, VI);
8431
+ }
8432
+
8362
8433
// A llvm.dbg.value may be using a value before its definition, due to
8363
8434
// optimizations in this pass and others. Scan for such dbg.values, and rescue
8364
8435
// them by moving the dbg.value to immediately after the value definition.
@@ -8368,59 +8439,69 @@ bool CodeGenPrepare::placeDbgValues(Function &F) {
8368
8439
bool MadeChange = false ;
8369
8440
DominatorTree DT (F);
8370
8441
8371
- for (BasicBlock &BB : F) {
8372
- for (Instruction &Insn : llvm::make_early_inc_range (BB)) {
8373
- DbgValueInst *DVI = dyn_cast<DbgValueInst>(&Insn);
8374
- if (!DVI)
8442
+ auto DbgProcessor = [&](auto *DbgItem, Instruction *Position) {
8443
+ SmallVector<Instruction *, 4 > VIs;
8444
+ for (Value *V : DbgItem->location_ops ())
8445
+ if (Instruction *VI = dyn_cast_or_null<Instruction>(V))
8446
+ VIs.push_back (VI);
8447
+
8448
+ // This item may depend on multiple instructions, complicating any
8449
+ // potential sink. This block takes the defensive approach, opting to
8450
+ // "undef" the item if it has more than one instruction and any of them do
8451
+ // not dominate iem.
8452
+ for (Instruction *VI : VIs) {
8453
+ if (VI->isTerminator ())
8375
8454
continue ;
8376
8455
8377
- SmallVector<Instruction *, 4 > VIs;
8378
- for (Value *V : DVI->getValues ())
8379
- if (Instruction *VI = dyn_cast_or_null<Instruction>(V))
8380
- VIs.push_back (VI);
8381
-
8382
- // This DVI may depend on multiple instructions, complicating any
8383
- // potential sink. This block takes the defensive approach, opting to
8384
- // "undef" the DVI if it has more than one instruction and any of them do
8385
- // not dominate DVI.
8386
- for (Instruction *VI : VIs) {
8387
- if (VI->isTerminator ())
8388
- continue ;
8456
+ // If VI is a phi in a block with an EHPad terminator, we can't insert
8457
+ // after it.
8458
+ if (isa<PHINode>(VI) && VI->getParent ()->getTerminator ()->isEHPad ())
8459
+ continue ;
8389
8460
8390
- // If VI is a phi in a block with an EHPad terminator , we can't insert
8391
- // after it .
8392
- if (isa<PHINode> (VI) && VI-> getParent ()-> getTerminator ()-> isEHPad ( ))
8393
- continue ;
8461
+ // If the defining instruction dominates the dbg.value , we do not need
8462
+ // to move the dbg.value .
8463
+ if (DT. dominates (VI, Position ))
8464
+ continue ;
8394
8465
8395
- // If the defining instruction dominates the dbg.value, we do not need
8396
- // to move the dbg.value.
8397
- if (DT.dominates (VI, DVI))
8398
- continue ;
8466
+ // If we depend on multiple instructions and any of them doesn't
8467
+ // dominate this DVI, we probably can't salvage it: moving it to
8468
+ // after any of the instructions could cause us to lose the others.
8469
+ if (VIs.size () > 1 ) {
8470
+ LLVM_DEBUG (
8471
+ dbgs ()
8472
+ << " Unable to find valid location for Debug Value, undefing:\n "
8473
+ << *DbgItem);
8474
+ DbgItem->setKillLocation ();
8475
+ break ;
8476
+ }
8399
8477
8400
- // If we depend on multiple instructions and any of them doesn't
8401
- // dominate this DVI, we probably can't salvage it: moving it to
8402
- // after any of the instructions could cause us to lose the others.
8403
- if (VIs.size () > 1 ) {
8404
- LLVM_DEBUG (
8405
- dbgs ()
8406
- << " Unable to find valid location for Debug Value, undefing:\n "
8407
- << *DVI);
8408
- DVI->setKillLocation ();
8409
- break ;
8410
- }
8478
+ LLVM_DEBUG (dbgs () << " Moving Debug Value before :\n "
8479
+ << *DbgItem << ' ' << *VI);
8480
+ DbgInserterHelper (DbgItem, VI);
8481
+ MadeChange = true ;
8482
+ ++NumDbgValueMoved;
8483
+ }
8484
+ };
8411
8485
8412
- LLVM_DEBUG (dbgs () << " Moving Debug Value before :\n "
8413
- << *DVI << ' ' << *VI);
8414
- DVI->removeFromParent ();
8415
- if (isa<PHINode>(VI))
8416
- DVI->insertBefore (&*VI->getParent ()->getFirstInsertionPt ());
8417
- else
8418
- DVI->insertAfter (VI);
8419
- MadeChange = true ;
8420
- ++NumDbgValueMoved;
8486
+ for (BasicBlock &BB : F) {
8487
+ for (Instruction &Insn : llvm::make_early_inc_range (BB)) {
8488
+ // Process dbg.value intrinsics.
8489
+ DbgValueInst *DVI = dyn_cast<DbgValueInst>(&Insn);
8490
+ if (DVI) {
8491
+ DbgProcessor (DVI, DVI);
8492
+ continue ;
8493
+ }
8494
+
8495
+ // If this isn't a dbg.value, process any attached DPValue records
8496
+ // attached to this instruction.
8497
+ for (DPValue &DPV : llvm::make_early_inc_range (Insn.getDbgValueRange ())) {
8498
+ if (DPV.Type != DPValue::LocationType::Value)
8499
+ continue ;
8500
+ DbgProcessor (&DPV, &Insn);
8421
8501
}
8422
8502
}
8423
8503
}
8504
+
8424
8505
return MadeChange;
8425
8506
}
8426
8507
0 commit comments