[llvm] r258391 - Change ConstantFoldInstOperands to take Instruction instead of opcode and type. NFC.

Daniel Berlin via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 25 16:49:38 PST 2016


Hi Manuel,

Things like NewGVN don't always have instruction *'s to hand to
ConstantFoldInstOperands.

For example, we create an expression that stores operands and opcodes and
such (like GVN does!)

Before, we could pass the opcode and operands, and that worked great.

Now you've made it take the Instruction.

I don't see how this makes anything related to opaque pointers easier,
maybe you could enlighten me?

Additionally, i can't see how use cases like mine are supposed to function
- i have what was an instruction, but is now an operation and some operands.
I want to see if it will fold to a constant.

What API am i expected to use to do so?

Note also that the InstOperands API was *explicitly* designed to not
require an instruction, to support the above use case. This was the
difference between ConstantFoldInst and ConstantFoldInstOperands.  It
happens most users can pass it instructions, but that doesn't , IMHO make a
lot of sense.

I want to request that you provide some way to not require the instruction,
as the current API does (and many users use!), so that folks can use this
without creating fake instructions to do so.



On Wed, Jan 20, 2016 at 10:33 PM, Manuel Jacob via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Author: mjacob
> Date: Thu Jan 21 00:33:22 2016
> New Revision: 258391
>
> URL: http://llvm.org/viewvc/llvm-project?rev=258391&view=rev
> Log:
> Change ConstantFoldInstOperands to take Instruction instead of opcode and
> type.  NFC.
>
> Summary:
> The previous form, taking opcode and type, is moved to an internal
> helper and the new form, taking an instruction, is a wrapper around this
> helper.
>
> Although this is a slight cleanup on its own, the main motivation is to
> refactor the constant folding API to ease migration to opaque pointers.
> This will be follow-up work.
>
> Reviewers: eddyb
>
> Subscribers: dblaikie, llvm-commits
>
> Differential Revision: http://reviews.llvm.org/D16383
>
> Modified:
>     llvm/trunk/include/llvm/Analysis/ConstantFolding.h
>     llvm/trunk/lib/Analysis/ConstantFolding.cpp
>     llvm/trunk/lib/Analysis/InlineCost.cpp
>     llvm/trunk/lib/Analysis/InstructionSimplify.cpp
>     llvm/trunk/lib/Analysis/ScalarEvolution.cpp
>     llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
>
> Modified: llvm/trunk/include/llvm/Analysis/ConstantFolding.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/ConstantFolding.h?rev=258391&r1=258390&r2=258391&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/Analysis/ConstantFolding.h (original)
> +++ llvm/trunk/include/llvm/Analysis/ConstantFolding.h Thu Jan 21 00:33:22
> 2016
> @@ -51,8 +51,7 @@ ConstantFoldConstantExpression(const Con
>  /// fold instructions like loads and stores, which have no constant
> expression
>  /// form.
>  ///
> -Constant *ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
> -                                   ArrayRef<Constant *> Ops,
> +Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *>
> Ops,
>                                     const DataLayout &DL,
>                                     const TargetLibraryInfo *TLI =
> nullptr);
>
>
> Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=258391&r1=258390&r2=258391&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
> +++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Thu Jan 21 00:33:22 2016
> @@ -881,6 +881,56 @@ static Constant *SymbolicallyEvaluateGEP
>    return C;
>  }
>
> +/// Attempt to constant fold an instruction with the
> +/// specified opcode and operands.  If successful, the constant result is
> +/// returned, if not, null is returned.  Note that this function can fail
> when
> +/// attempting to fold instructions like loads and stores, which have no
> +/// constant expression form.
> +///
> +/// TODO: This function neither utilizes nor preserves
> nsw/nuw/inbounds/etc
> +/// information, due to only being passed an opcode and operands. Constant
> +/// folding using this function strips this information.
> +///
> +static Constant *ConstantFoldInstOperandsImpl(unsigned Opcode, Type
> *DestTy,
> +                                              ArrayRef<Constant *> Ops,
> +                                              const DataLayout &DL,
> +                                              const TargetLibraryInfo
> *TLI) {
> +  // Handle easy binops first.
> +  if (Instruction::isBinaryOp(Opcode))
> +    return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
> +
> +  if (Instruction::isCast(Opcode))
> +    return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
> +
> +  switch (Opcode) {
> +  default: return nullptr;
> +  case Instruction::ICmp:
> +  case Instruction::FCmp: llvm_unreachable("Invalid for compares");
> +  case Instruction::Call:
> +    if (Function *F = dyn_cast<Function>(Ops.back()))
> +      if (canConstantFoldCallTo(F))
> +        return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
> +    return nullptr;
> +  case Instruction::Select:
> +    return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
> +  case Instruction::ExtractElement:
> +    return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
> +  case Instruction::InsertElement:
> +    return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
> +  case Instruction::ShuffleVector:
> +    return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
> +  case Instruction::GetElementPtr: {
> +    Type *SrcTy = nullptr;
> +    if (Constant *C = CastGEPIndices(SrcTy, Ops, DestTy, DL, TLI))
> +      return C;
> +    if (Constant *C = SymbolicallyEvaluateGEP(SrcTy, Ops, DestTy, DL,
> TLI))
> +      return C;
> +
> +    return ConstantExpr::getGetElementPtr(SrcTy, Ops[0], Ops.slice(1));
> +  }
> +  }
> +}
> +
>
>
>
>  //===----------------------------------------------------------------------===//
> @@ -925,7 +975,7 @@ Constant *llvm::ConstantFoldInstruction(
>    }
>
>    // Scan the operand list, checking to see if they are all constants, if
> so,
> -  // hand off to ConstantFoldInstOperands.
> +  // hand off to ConstantFoldInstOperandsImpl.
>    SmallVector<Constant*, 8> Ops;
>    for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
> {
>      Constant *Op = dyn_cast<Constant>(*i);
> @@ -959,7 +1009,7 @@ Constant *llvm::ConstantFoldInstruction(
>                                      EVI->getIndices());
>    }
>
> -  return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, DL,
> TLI);
> +  return ConstantFoldInstOperandsImpl(I->getOpcode(), I->getType(), Ops,
> DL, TLI);
>  }
>
>  static Constant *
> @@ -982,7 +1032,7 @@ ConstantFoldConstantExpressionImpl(const
>    if (CE->isCompare())
>      return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0],
> Ops[1],
>                                             DL, TLI);
> -  return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), Ops,
> DL, TLI);
> +  return ConstantFoldInstOperandsImpl(CE->getOpcode(), CE->getType(),
> Ops, DL, TLI);
>  }
>
>  /// Attempt to fold the constant expression
> @@ -995,54 +1045,12 @@ Constant *llvm::ConstantFoldConstantExpr
>    return ConstantFoldConstantExpressionImpl(CE, DL, TLI, FoldedOps);
>  }
>
> -/// Attempt to constant fold an instruction with the
> -/// specified opcode and operands.  If successful, the constant result is
> -/// returned, if not, null is returned.  Note that this function can fail
> when
> -/// attempting to fold instructions like loads and stores, which have no
> -/// constant expression form.
> -///
> -/// TODO: This function neither utilizes nor preserves
> nsw/nuw/inbounds/etc
> -/// information, due to only being passed an opcode and operands. Constant
> -/// folding using this function strips this information.
> -///
> -Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
> +Constant *llvm::ConstantFoldInstOperands(Instruction *I,
>                                           ArrayRef<Constant *> Ops,
>                                           const DataLayout &DL,
>                                           const TargetLibraryInfo *TLI) {
> -  // Handle easy binops first.
> -  if (Instruction::isBinaryOp(Opcode))
> -    return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
> -
> -  if (Instruction::isCast(Opcode))
> -    return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
> -
> -  switch (Opcode) {
> -  default: return nullptr;
> -  case Instruction::ICmp:
> -  case Instruction::FCmp: llvm_unreachable("Invalid for compares");
> -  case Instruction::Call:
> -    if (Function *F = dyn_cast<Function>(Ops.back()))
> -      if (canConstantFoldCallTo(F))
> -        return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
> -    return nullptr;
> -  case Instruction::Select:
> -    return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
> -  case Instruction::ExtractElement:
> -    return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
> -  case Instruction::InsertElement:
> -    return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
> -  case Instruction::ShuffleVector:
> -    return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
> -  case Instruction::GetElementPtr: {
> -    Type *SrcTy = nullptr;
> -    if (Constant *C = CastGEPIndices(SrcTy, Ops, DestTy, DL, TLI))
> -      return C;
> -    if (Constant *C = SymbolicallyEvaluateGEP(SrcTy, Ops, DestTy, DL,
> TLI))
> -      return C;
> -
> -    return ConstantExpr::getGetElementPtr(SrcTy, Ops[0], Ops.slice(1));
> -  }
> -  }
> +  return ConstantFoldInstOperandsImpl(I->getOpcode(), I->getType(), Ops,
> DL,
> +                                      TLI);
>  }
>
>  /// Attempt to constant fold a compare
>
> Modified: llvm/trunk/lib/Analysis/InlineCost.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InlineCost.cpp?rev=258391&r1=258390&r2=258391&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Analysis/InlineCost.cpp (original)
> +++ llvm/trunk/lib/Analysis/InlineCost.cpp Thu Jan 21 00:33:22 2016
> @@ -533,8 +533,7 @@ bool CallAnalyzer::visitUnaryInstruction
>      COp = SimplifiedValues.lookup(Operand);
>    if (COp) {
>      const DataLayout &DL = F.getParent()->getDataLayout();
> -    if (Constant *C = ConstantFoldInstOperands(I.getOpcode(), I.getType(),
> -                                               COp, DL)) {
> +    if (Constant *C = ConstantFoldInstOperands(&I, COp, DL)) {
>        SimplifiedValues[&I] = C;
>        return true;
>      }
>
> Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=258391&r1=258390&r2=258391&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
> +++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Thu Jan 21 00:33:22
> 2016
> @@ -3261,8 +3261,7 @@ static const Value *SimplifyWithOpReplac
>          if (!LI->isVolatile())
>            return ConstantFoldLoadFromConstPtr(ConstOps[0], Q.DL);
>
> -      return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
> ConstOps,
> -                                      Q.DL, Q.TLI);
> +      return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI);
>      }
>    }
>
>
> Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=258391&r1=258390&r2=258391&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
> +++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Thu Jan 21 00:33:22 2016
> @@ -5917,8 +5917,7 @@ static Constant *EvaluateExpression(Valu
>      if (!LI->isVolatile())
>        return ConstantFoldLoadFromConstPtr(Operands[0], DL);
>    }
> -  return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Operands,
> DL,
> -                                  TLI);
> +  return ConstantFoldInstOperands(I, Operands, DL, TLI);
>  }
>
>
> @@ -6306,8 +6305,7 @@ const SCEV *ScalarEvolution::computeSCEV
>              if (!LI->isVolatile())
>                C = ConstantFoldLoadFromConstPtr(Operands[0], DL);
>            } else
> -            C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
> Operands,
> -                                         DL, &TLI);
> +            C = ConstantFoldInstOperands(I, Operands, DL, &TLI);
>            if (!C) return V;
>            return getSCEV(C);
>          }
>
> Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=258391&r1=258390&r2=258391&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
> +++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Thu Jan 21 00:33:22
> 2016
> @@ -3989,7 +3989,7 @@ ConstantFold(Instruction *I, const DataL
>                                             COps[1], DL);
>    }
>
> -  return ConstantFoldInstOperands(I->getOpcode(), I->getType(), COps, DL);
> +  return ConstantFoldInstOperands(I, COps, DL);
>  }
>
>  /// Try to determine the resulting constant values in phi nodes
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160125/8fd54d63/attachment-0001.html>


More information about the llvm-commits mailing list