Index: include/llvm/Analysis/ConstantFolding.h =================================================================== --- include/llvm/Analysis/ConstantFolding.h +++ include/llvm/Analysis/ConstantFolding.h @@ -35,8 +35,11 @@ /// Note that this fails if not all of the operands are constant. Otherwise, /// this function can only fail when attempting to fold instructions like loads /// and stores, which have no constant expression form. +/// Only attempt to constant-fold ConstantExpr operands if FoldOperands +/// is set to true; set this to false if they are already known to be folded. Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL, - const TargetLibraryInfo *TLI = nullptr); + const TargetLibraryInfo *TLI = nullptr, + bool FoldOperands = true); /// ConstantFoldConstantExpression - Attempt to fold the constant expression /// using the specified DataLayout. If successful, the constant result is Index: lib/Analysis/ConstantFolding.cpp =================================================================== --- lib/Analysis/ConstantFolding.cpp +++ lib/Analysis/ConstantFolding.cpp @@ -949,7 +949,8 @@ //===----------------------------------------------------------------------===// Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL, - const TargetLibraryInfo *TLI) { + const TargetLibraryInfo *TLI, + bool FoldOperands) { // Handle PHI nodes quickly here... if (PHINode *PN = dyn_cast(I)) { Constant *CommonValue = nullptr; @@ -966,7 +967,8 @@ if (!C) return nullptr; // Fold the PHI's operands. - if (ConstantExpr *NewC = dyn_cast(C)) + ConstantExpr *NewC = dyn_cast(C); + if (NewC && FoldOperands) C = ConstantFoldConstantExpression(NewC, DL, TLI); // If the incoming value is a different constant to // the one we saw previously, then give up. @@ -989,7 +991,8 @@ for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) { Constant *Op = cast(*i); // Fold the Instruction's operands. - if (ConstantExpr *NewCE = dyn_cast(Op)) + ConstantExpr *NewCE = dyn_cast(Op); + if (NewCE && FoldOperands) Op = ConstantFoldConstantExpression(NewCE, DL, TLI); Ops.push_back(Op); Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2937,18 +2937,6 @@ continue; } - // ConstantProp instruction if trivially constant. - if (!Inst->use_empty() && - (Inst->getNumOperands() == 0 || isa(Inst->getOperand(0)))) - if (Constant *C = ConstantFoldInstruction(Inst, DL, TLI)) { - DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: " - << *Inst << '\n'); - Inst->replaceAllUsesWith(C); - ++NumConstProp; - Inst->eraseFromParent(); - continue; - } - // See if we can constant fold its operands. for (User::op_iterator i = Inst->op_begin(), e = Inst->op_end(); i != e; ++i) { @@ -2968,6 +2956,18 @@ } } + // ConstantProp instruction if trivially constant. + if (!Inst->use_empty() && + (Inst->getNumOperands() == 0 || isa(Inst->getOperand(0)))) + if (Constant *C = ConstantFoldInstruction(Inst, DL, TLI, false )) { + DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: " + << *Inst << '\n'); + Inst->replaceAllUsesWith(C); + ++NumConstProp; + Inst->eraseFromParent(); + continue; + } + InstrsForInstCombineWorklist.push_back(Inst); }