3232#include "llvm/Analysis/TargetLibraryInfo.h"
3333#include "llvm/Analysis/ValueTracking.h"
3434#include "llvm/Analysis/VectorUtils.h"
35+ #include "llvm/BinaryFormat/Dwarf.h"
3536#include "llvm/CodeGen/ByteProvider.h"
3637#include "llvm/CodeGen/DAGCombine.h"
3738#include "llvm/CodeGen/ISDOpcodes.h"
@@ -14445,29 +14446,35 @@ static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
1444514446 LN0->getBasePtr(), N0.getValueType(),
1444614447 LN0->getMemOperand());
1444714448 Combiner.ExtendSetCCUses(SetCCs, N0, ExtLoad, ExtOpc);
14449+ unsigned Opcode = N->getOpcode();
14450+ bool IsSigned = Opcode == ISD::SIGN_EXTEND ? true : false;
1444814451 // If the load value is used only by N, replace it via CombineTo N.
1444914452 SDValue OldLoadVal(LN0, 0);
14450- SDValue OldSextValue (N, 0);
14453+ SDValue OldExtValue (N, 0);
1445114454 bool NoReplaceTrunc = OldLoadVal.hasOneUse();
1445214455 Combiner.CombineTo(N, ExtLoad);
1445314456
1445414457 // Because we are replacing a load and a sext with a load-sext instruction,
1445514458 // the dbg_value attached to the load will be of a smaller bit width, and we
1445614459 // have to add a DW_OP_LLVM_fragment to the DIExpression.
14457- auto SalvageToOldLoadSize = [&](SDValue From, SDValue To64 ) {
14460+ auto SalvageToOldLoadSize = [&](SDValue From, SDValue To, bool IsSigned ) {
1445814461 for (SDDbgValue *Dbg : DAG.GetDbgValues(From.getNode())) {
14459- unsigned VarBits = From->getValueSizeInBits(0);
14462+ unsigned VarBitsFrom = From->getValueSizeInBits(0);
14463+ unsigned VarBitsTo = To->getValueSizeInBits(0);
1446014464
14461- // Build/append a fragment expression [0, VarBits]
14465+ // Build a convert expression for the s|z extend.
1446214466 const DIExpression *OldE = Dbg->getExpression();
14463- auto NewE = DIExpression::createFragmentExpression(OldE, 0, VarBits);
14464-
14465- // Create a new SDDbgValue that points at the widened node with the
14466- // fragment.
14467- if (!NewE)
14468- continue;
14467+ SmallVector<uint64_t, 8> Ops;
14468+ dwarf::TypeKind TK =
14469+ IsSigned ? dwarf::DW_ATE_signed : dwarf::DW_ATE_unsigned;
14470+ Ops.append({dwarf::DW_OP_LLVM_convert, VarBitsFrom, TK,
14471+ dwarf::DW_OP_LLVM_convert, VarBitsTo, TK});
14472+ auto *NewE = DIExpression::get(OldE->getContext(), Ops);
14473+
14474+ // // Create a new SDDbgValue that points at the widened node with the
14475+ // // fragment.
1446914476 SDDbgValue *NewDV = DAG.getDbgValue(
14470- Dbg->getVariable(), * NewE, To64 .getNode(), To64 .getResNo(),
14477+ Dbg->getVariable(), NewE, To .getNode(), To .getResNo(),
1447114478 Dbg->isIndirect(), Dbg->getDebugLoc(), Dbg->getOrder());
1447214479 DAG.AddDbgValue(NewDV, /*isParametet*/ false);
1447314480 }
@@ -14476,21 +14483,21 @@ static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
1447614483 if (NoReplaceTrunc) {
1447714484 if (LN0->getHasDebugValue()) {
1447814485 DAG.transferDbgValues(OldLoadVal, ExtLoad);
14479- SalvageToOldLoadSize(OldLoadVal, ExtLoad);
14486+ SalvageToOldLoadSize(OldLoadVal, ExtLoad, IsSigned );
1448014487 }
1448114488 if (N->getHasDebugValue())
14482- DAG.transferDbgValues(OldSextValue , ExtLoad);
14489+ DAG.transferDbgValues(OldExtValue , ExtLoad);
1448314490 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
1448414491 Combiner.recursivelyDeleteUnusedNodes(LN0);
1448514492 } else {
1448614493 SDValue Trunc =
1448714494 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), ExtLoad);
1448814495 if (LN0->getHasDebugValue()) {
1448914496 DAG.transferDbgValues(OldLoadVal, Trunc);
14490- SalvageToOldLoadSize(OldLoadVal, Trunc);
14497+ SalvageToOldLoadSize(OldLoadVal, Trunc, IsSigned );
1449114498 }
1449214499 if (N->getHasDebugValue())
14493- DAG.transferDbgValues(OldSextValue , Trunc);
14500+ DAG.transferDbgValues(OldExtValue , Trunc);
1449414501 Combiner.CombineTo(LN0, Trunc, ExtLoad.getValue(1));
1449514502 }
1449614503 return SDValue(N, 0); // Return N so it doesn't get rechecked!
0 commit comments