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"
@@ -14457,29 +14458,35 @@ static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
1445714458 LN0->getBasePtr(), N0.getValueType(),
1445814459 LN0->getMemOperand());
1445914460 Combiner.ExtendSetCCUses(SetCCs, N0, ExtLoad, ExtOpc);
14461+ unsigned Opcode = N->getOpcode();
14462+ bool IsSigned = Opcode == ISD::SIGN_EXTEND ? true : false;
1446014463 // If the load value is used only by N, replace it via CombineTo N.
1446114464 SDValue OldLoadVal(LN0, 0);
14462- SDValue OldSextValue (N, 0);
14465+ SDValue OldExtValue (N, 0);
1446314466 bool NoReplaceTrunc = OldLoadVal.hasOneUse();
1446414467 Combiner.CombineTo(N, ExtLoad);
1446514468
1446614469 // Because we are replacing a load and a sext with a load-sext instruction,
1446714470 // the dbg_value attached to the load will be of a smaller bit width, and we
1446814471 // have to add a DW_OP_LLVM_fragment to the DIExpression.
14469- auto SalvageToOldLoadSize = [&](SDValue From, SDValue To64 ) {
14472+ auto SalvageToOldLoadSize = [&](SDValue From, SDValue To, bool IsSigned ) {
1447014473 for (SDDbgValue *Dbg : DAG.GetDbgValues(From.getNode())) {
14471- unsigned VarBits = From->getValueSizeInBits(0);
14474+ unsigned VarBitsFrom = From->getValueSizeInBits(0);
14475+ unsigned VarBitsTo = To->getValueSizeInBits(0);
1447214476
14473- // Build/append a fragment expression [0, VarBits]
14477+ // Build a convert expression for the s|z extend.
1447414478 const DIExpression *OldE = Dbg->getExpression();
14475- auto NewE = DIExpression::createFragmentExpression(OldE, 0, VarBits);
14476-
14477- // Create a new SDDbgValue that points at the widened node with the
14478- // fragment.
14479- if (!NewE)
14480- continue;
14479+ SmallVector<uint64_t, 8> Ops;
14480+ dwarf::TypeKind TK =
14481+ IsSigned ? dwarf::DW_ATE_signed : dwarf::DW_ATE_unsigned;
14482+ Ops.append({dwarf::DW_OP_LLVM_convert, VarBitsFrom, TK,
14483+ dwarf::DW_OP_LLVM_convert, VarBitsTo, TK});
14484+ auto *NewE = DIExpression::get(OldE->getContext(), Ops);
14485+
14486+ // // Create a new SDDbgValue that points at the widened node with the
14487+ // // fragment.
1448114488 SDDbgValue *NewDV = DAG.getDbgValue(
14482- Dbg->getVariable(), * NewE, To64 .getNode(), To64 .getResNo(),
14489+ Dbg->getVariable(), NewE, To .getNode(), To .getResNo(),
1448314490 Dbg->isIndirect(), Dbg->getDebugLoc(), Dbg->getOrder());
1448414491 DAG.AddDbgValue(NewDV, /*isParametet*/ false);
1448514492 }
@@ -14488,21 +14495,21 @@ static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
1448814495 if (NoReplaceTrunc) {
1448914496 if (LN0->getHasDebugValue()) {
1449014497 DAG.transferDbgValues(OldLoadVal, ExtLoad);
14491- SalvageToOldLoadSize(OldLoadVal, ExtLoad);
14498+ SalvageToOldLoadSize(OldLoadVal, ExtLoad, IsSigned );
1449214499 }
1449314500 if (N->getHasDebugValue())
14494- DAG.transferDbgValues(OldSextValue , ExtLoad);
14501+ DAG.transferDbgValues(OldExtValue , ExtLoad);
1449514502 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
1449614503 Combiner.recursivelyDeleteUnusedNodes(LN0);
1449714504 } else {
1449814505 SDValue Trunc =
1449914506 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), ExtLoad);
1450014507 if (LN0->getHasDebugValue()) {
1450114508 DAG.transferDbgValues(OldLoadVal, Trunc);
14502- SalvageToOldLoadSize(OldLoadVal, Trunc);
14509+ SalvageToOldLoadSize(OldLoadVal, Trunc, IsSigned );
1450314510 }
1450414511 if (N->getHasDebugValue())
14505- DAG.transferDbgValues(OldSextValue , Trunc);
14512+ DAG.transferDbgValues(OldExtValue , Trunc);
1450614513 Combiner.CombineTo(LN0, Trunc, ExtLoad.getValue(1));
1450714514 }
1450814515 return SDValue(N, 0); // Return N so it doesn't get rechecked!
0 commit comments