diff --git a/llvm/include/llvm/Analysis/ConstantFolding.h b/llvm/include/llvm/Analysis/ConstantFolding.h --- a/llvm/include/llvm/Analysis/ConstantFolding.h +++ b/llvm/include/llvm/Analysis/ConstantFolding.h @@ -46,9 +46,9 @@ Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI = nullptr); -/// ConstantFoldConstant - Attempt to fold the constant using the -/// specified DataLayout. -/// If successful, the constant result is returned, if not, null is returned. +/// ConstantFoldConstant - Fold the constant using the specified DataLayout. +/// This function always returns a non-null constant: Either the folding result, +/// or the original constant if further folding is not possible. Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI = nullptr); diff --git a/llvm/include/llvm/Analysis/TargetFolder.h b/llvm/include/llvm/Analysis/TargetFolder.h --- a/llvm/include/llvm/Analysis/TargetFolder.h +++ b/llvm/include/llvm/Analysis/TargetFolder.h @@ -34,9 +34,7 @@ /// Fold - Fold the constant using target specific information. Constant *Fold(Constant *C) const { - if (Constant *CF = ConstantFoldConstant(C, DL)) - return CF; - return C; + return ConstantFoldConstant(C, DL); } virtual void anchor(); diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -801,10 +801,7 @@ Constant *C = ConstantExpr::getGetElementPtr( SrcElemTy, Ops[0], NewIdxs, /*InBounds=*/false, InRangeIndex); - if (Constant *Folded = ConstantFoldConstant(C, DL, TLI)) - C = Folded; - - return C; + return ConstantFoldConstant(C, DL, TLI); } /// Strip the pointer casts, but preserve the address space information. @@ -865,9 +862,7 @@ Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType()); Res = ConstantExpr::getSub(Res, CE->getOperand(1)); Res = ConstantExpr::getIntToPtr(Res, ResTy); - if (auto *FoldedRes = ConstantFoldConstant(Res, DL, TLI)) - Res = FoldedRes; - return Res; + return ConstantFoldConstant(Res, DL, TLI); } } return nullptr; @@ -1087,23 +1082,19 @@ const TargetLibraryInfo *TLI, SmallDenseMap &FoldedOps) { if (!isa(C) && !isa(C)) - return nullptr; + return const_cast(C); SmallVector Ops; - for (const Use &NewU : C->operands()) { - auto *NewC = cast(&NewU); + for (const Use &OldU : C->operands()) { + Constant *OldC = cast(&OldU); + Constant *NewC = OldC; // Recursively fold the ConstantExpr's operands. If we have already folded // a ConstantExpr, we don't have to process it again. - if (isa(NewC) || isa(NewC)) { - auto It = FoldedOps.find(NewC); + if (isa(OldC) || isa(OldC)) { + auto It = FoldedOps.find(OldC); if (It == FoldedOps.end()) { - if (auto *FoldedC = - ConstantFoldConstantImpl(NewC, DL, TLI, FoldedOps)) { - FoldedOps.insert({NewC, FoldedC}); - NewC = FoldedC; - } else { - FoldedOps.insert({NewC, NewC}); - } + NewC = ConstantFoldConstantImpl(OldC, DL, TLI, FoldedOps); + FoldedOps.insert({OldC, NewC}); } else { NewC = It->second; } @@ -1144,8 +1135,7 @@ if (!C) return nullptr; // Fold the PHI's operands. - if (auto *FoldedC = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps)) - C = FoldedC; + C = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps); // If the incoming value is a different constant to // the one we saw previously, then give up. if (CommonValue && C != CommonValue) @@ -1167,9 +1157,7 @@ for (const Use &OpU : I->operands()) { auto *Op = cast(&OpU); // Fold the Instruction's operands. - if (auto *FoldedOp = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps)) - Op = FoldedOp; - + Op = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps); Ops.push_back(Op); } diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4167,9 +4167,7 @@ auto *CE = ConstantExpr::getGetElementPtr(SrcTy, cast(Ops[0]), Ops.slice(1)); - if (auto *CEFolded = ConstantFoldConstant(CE, Q.DL)) - return CEFolded; - return CE; + return ConstantFoldConstant(CE, Q.DL); } Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef Ops, diff --git a/llvm/lib/Analysis/Lint.cpp b/llvm/lib/Analysis/Lint.cpp --- a/llvm/lib/Analysis/Lint.cpp +++ b/llvm/lib/Analysis/Lint.cpp @@ -735,9 +735,9 @@ if (Value *W = SimplifyInstruction(Inst, {*DL, TLI, DT, AC})) return findValueImpl(W, OffsetOk, Visited); } else if (auto *C = dyn_cast(V)) { - if (Value *W = ConstantFoldConstant(C, *DL, TLI)) - if (W && W != V) - return findValueImpl(W, OffsetOk, Visited); + Value *W = ConstantFoldConstant(C, *DL, TLI); + if (W != V) + return findValueImpl(W, OffsetOk, Visited); } return V; diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2249,23 +2249,22 @@ } switch (CE->getOpcode()) { - default: + default: { // If the code isn't optimized, there may be outstanding folding // opportunities. Attempt to fold the expression using DataLayout as a // last resort before giving up. - if (Constant *C = ConstantFoldConstant(CE, getDataLayout())) - if (C != CE) - return lowerConstant(C); + Constant *C = ConstantFoldConstant(CE, getDataLayout()); + if (C != CE) + return lowerConstant(C); // Otherwise report the problem to the user. - { - std::string S; - raw_string_ostream OS(S); - OS << "Unsupported expression in static initializer: "; - CE->printAsOperand(OS, /*PrintType=*/false, - !MF ? nullptr : MF->getFunction().getParent()); - report_fatal_error(OS.str()); - } + std::string S; + raw_string_ostream OS(S); + OS << "Unsupported expression in static initializer: "; + CE->printAsOperand(OS, /*PrintType=*/false, + !MF ? nullptr : MF->getFunction().getParent()); + report_fatal_error(OS.str()); + } case Instruction::GetElementPtr: { // Generate a symbolic expression for the byte address APInt OffsetAI(getDataLayout().getPointerTypeSizeInBits(CE->getType()), 0); @@ -2790,7 +2789,7 @@ // to emit the value in chunks. Try to constant fold the value and emit it // that way. Constant *New = ConstantFoldConstant(CE, DL); - if (New && New != CE) + if (New != CE) return emitGlobalConstantImpl(DL, New, AP); } } diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp --- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -1815,7 +1815,7 @@ aggBuffer->addBytes(ptr, 4, Bytes); break; } else if (const auto *Cexpr = dyn_cast(CPV)) { - if (const auto *constInt = dyn_cast_or_null( + if (const auto *constInt = dyn_cast( ConstantFoldConstant(Cexpr, DL))) { int int32 = (int)(constInt->getZExtValue()); ConvertIntToBytes<>(ptr, int32); @@ -1837,7 +1837,7 @@ aggBuffer->addBytes(ptr, 8, Bytes); break; } else if (const ConstantExpr *Cexpr = dyn_cast(CPV)) { - if (const auto *constInt = dyn_cast_or_null( + if (const auto *constInt = dyn_cast( ConstantFoldConstant(Cexpr, DL))) { long long int64 = (long long)(constInt->getZExtValue()); ConvertIntToBytes<>(ptr, int64); @@ -1993,23 +1993,22 @@ } switch (CE->getOpcode()) { - default: + default: { // If the code isn't optimized, there may be outstanding folding // opportunities. Attempt to fold the expression using DataLayout as a // last resort before giving up. - if (Constant *C = ConstantFoldConstant(CE, getDataLayout())) - if (C && C != CE) - return lowerConstantForGV(C, ProcessingGeneric); + Constant *C = ConstantFoldConstant(CE, getDataLayout()); + if (C != CE) + return lowerConstantForGV(C, ProcessingGeneric); // Otherwise report the problem to the user. - { - std::string S; - raw_string_ostream OS(S); - OS << "Unsupported expression in static initializer: "; - CE->printAsOperand(OS, /*PrintType=*/false, - !MF ? nullptr : MF->getFunction().getParent()); - report_fatal_error(OS.str()); - } + std::string S; + raw_string_ostream OS(S); + OS << "Unsupported expression in static initializer: "; + CE->printAsOperand(OS, /*PrintType=*/false, + !MF ? nullptr : MF->getFunction().getParent()); + report_fatal_error(OS.str()); + } case Instruction::AddrSpaceCast: { // Strip the addrspacecast and pass along the operand diff --git a/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp --- a/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp +++ b/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp @@ -291,9 +291,7 @@ if (auto *C = dyn_cast(V)) { C = ConstantExpr::getIntegerCast(C, Ty, false); // If we got a constantexpr back, try to simplify it with DL info. - if (Constant *FoldedC = ConstantFoldConstant(C, DL, &TLI)) - C = FoldedC; - return C; + return ConstantFoldConstant(C, DL, &TLI); } auto *I = cast(V); diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -2385,7 +2385,7 @@ // for that optional parameter, since we don't have a Function to // provide GetTLI anyway. Constant *New = ConstantFoldConstant(C, DL, /*TLI*/ nullptr); - if (New && New != C) + if (New != C) GV->setInitializer(New); } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -164,9 +164,7 @@ if (Constant *C = dyn_cast(V)) { C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); // If we got a constantexpr back, try to simplify it with DL info. - if (Constant *FoldedC = ConstantFoldConstant(C, DL, &TLI)) - C = FoldedC; - return C; + return ConstantFoldConstant(C, DL, &TLI); } // Otherwise, it must be an instruction. diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -616,10 +616,9 @@ else V = IC.Builder.CreateLShr(C, NumBits); // If we got a constantexpr back, try to simplify it with TD info. + // TODO: This is dubious, IRBuilder should already do this. if (auto *C = dyn_cast(V)) - if (auto *FoldedC = - ConstantFoldConstant(C, DL, &IC.getTargetLibraryInfo())) - V = FoldedC; + V = ConstantFoldConstant(C, DL, &IC.getTargetLibraryInfo()); return V; } diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -3626,8 +3626,6 @@ Constant *&FoldRes = FoldedConstants[C]; if (!FoldRes) FoldRes = ConstantFoldConstant(C, DL, TLI); - if (!FoldRes) - FoldRes = C; if (FoldRes != C) { LLVM_DEBUG(dbgs() << "IC: ConstFold operand of: " << *Inst diff --git a/llvm/lib/Transforms/Utils/Evaluator.cpp b/llvm/lib/Transforms/Utils/Evaluator.cpp --- a/llvm/lib/Transforms/Utils/Evaluator.cpp +++ b/llvm/lib/Transforms/Utils/Evaluator.cpp @@ -196,8 +196,7 @@ Constant *const IdxList[] = {IdxZero, IdxZero}; Ptr = ConstantExpr::getGetElementPtr(Ty, Ptr, IdxList); - if (auto *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI)) - Ptr = FoldedPtr; + Ptr = ConstantFoldConstant(Ptr, DL, TLI); } return Val; } @@ -339,7 +338,8 @@ return false; // no volatile/atomic accesses. } Constant *Ptr = getVal(SI->getOperand(1)); - if (auto *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI)) { + Constant *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI); + if (Ptr != FoldedPtr) { LLVM_DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr); Ptr = FoldedPtr; LLVM_DEBUG(dbgs() << "; To: " << *Ptr << "\n"); @@ -448,7 +448,8 @@ } Constant *Ptr = getVal(LI->getOperand(0)); - if (auto *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI)) { + Constant *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI); + if (Ptr != FoldedPtr) { Ptr = FoldedPtr; LLVM_DEBUG(dbgs() << "Found a constant pointer expression, constant " "folding: " @@ -648,9 +649,7 @@ } if (!CurInst->use_empty()) { - if (auto *FoldedInstResult = ConstantFoldConstant(InstResult, DL, TLI)) - InstResult = FoldedInstResult; - + InstResult = ConstantFoldConstant(InstResult, DL, TLI); setVal(&*CurInst, InstResult); } diff --git a/llvm/lib/Transforms/Utils/VNCoercion.cpp b/llvm/lib/Transforms/Utils/VNCoercion.cpp --- a/llvm/lib/Transforms/Utils/VNCoercion.cpp +++ b/llvm/lib/Transforms/Utils/VNCoercion.cpp @@ -54,8 +54,7 @@ assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) && "precondition violation - materialization can't fail"); if (auto *C = dyn_cast(StoredVal)) - if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL)) - StoredVal = FoldedStoredVal; + StoredVal = ConstantFoldConstant(C, DL); // If this is already the right type, just return it. Type *StoredValTy = StoredVal->getType(); @@ -88,8 +87,7 @@ } if (auto *C = dyn_cast(StoredVal)) - if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL)) - StoredVal = FoldedStoredVal; + StoredVal = ConstantFoldConstant(C, DL); return StoredVal; } @@ -134,8 +132,7 @@ } if (auto *C = dyn_cast(StoredVal)) - if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL)) - StoredVal = FoldedStoredVal; + StoredVal = ConstantFoldConstant(C, DL); return StoredVal; }