================ @@ -106,14 +107,71 @@ static void findReturnsToZap(Function &F, } } -static bool runIPSCCP( - Module &M, const DataLayout &DL, FunctionAnalysisManager *FAM, - std::function<const TargetLibraryInfo &(Function &)> GetTLI, - std::function<TargetTransformInfo &(Function &)> GetTTI, - std::function<AssumptionCache &(Function &)> GetAC, - std::function<DominatorTree &(Function &)> GetDT, - std::function<BlockFrequencyInfo &(Function &)> GetBFI, - bool IsFuncSpecEnabled) { +static void createDebugConstantExpression(Module &M, GlobalVariable *GV) { + SmallVector<DIGlobalVariableExpression *, 1> GVEs; + GV->getDebugInfo(GVEs); + if (GVEs.size() != 1) + return; + + DIBuilder DIB(M); + + // Create integer constant expression. + auto createIntegerExpression = [&DIB](const Constant *CV) -> DIExpression * { + const APInt &API = cast<ConstantInt>(CV)->getValue(); + std::optional<uint64_t> InitIntOpt; + if (API.isNonNegative()) + InitIntOpt = API.tryZExtValue(); + else if (auto Temp = API.trySExtValue()) + // Transform a signed optional to unsigned optional. + InitIntOpt = static_cast<uint64_t>(*Temp); + return InitIntOpt ? DIB.createConstantValueExpression(InitIntOpt.value()) + : nullptr; + }; + + const Constant *CV = GV->getInitializer(); + Type *Ty = GV->getValueType(); + if (Ty->isIntegerTy()) { + DIExpression *InitExpr = createIntegerExpression(CV); + if (InitExpr) + GVEs[0]->replaceOperandWith(1, InitExpr); + return; + } + + if (Ty->isFloatTy() || Ty->isDoubleTy()) { + const APFloat &APF = cast<ConstantFP>(CV)->getValueAPF(); + GVEs[0]->replaceOperandWith(1, DIB.createConstantValueExpression( + APF.bitcastToAPInt().getZExtValue())); + return; + } + + if (!Ty->isPointerTy()) + return; + + if (isa<ConstantPointerNull>(CV)) { + GVEs[0]->replaceOperandWith(1, DIB.createConstantValueExpression(0)); + return; + } + if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV); ---------------- dwblaikie wrote:
This is probably problematic too - the initializer part of the `if` isn't part of the boolean condition - so the `CE` would be unconditionally dereferenced in the `CE->getNumOperands()` call - so either the `CE` init should use `cast`, not `dyn_cast` - or the condition needs to be `CE && CE->getNumOperands() ...` (or nested `if`s) https://github.com/llvm/llvm-project/pull/66745 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits