TargetTransformInfo.h revision 06f5ebc5a1604b01689cf2d482dd05f956538af6
158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//===- llvm/Analysis/TargetTransformInfo.h ----------------------*- C++ -*-===//
258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// License. See LICENSE.TXT for details.
758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//===----------------------------------------------------------------------===//
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This pass exposes codegen information to IR-level passes. Every
1158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// transformation that uses codegen information is broken into three parts:
1258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// 1. The IR-level analysis pass.
1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// 2. The IR-level transformation interface which provides the needed
1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//    information.
1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// 3. Codegen-level implementation which uses target-specific hooks.
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// This file defines #2, which is the interface that IR-level transformations
1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// use for querying the codegen.
1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//===----------------------------------------------------------------------===//
2158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFO_H
2358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#define LLVM_ANALYSIS_TARGETTRANSFORMINFO_H
2458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/IR/Intrinsics.h"
2658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/Pass.h"
2758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/Support/DataTypes.h"
2858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace llvm {
3058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class GlobalValue;
3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class Type;
3358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class User;
3458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class Value;
3558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// TargetTransformInfo - This pass provides access to the codegen
3758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// interfaces that are needed for IR-level transformations.
3858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class TargetTransformInfo {
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)protected:
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief The TTI instance one level down the stack.
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
4258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// This is used to implement the default behavior all of the methods which
4358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// is to delegate up through the stack of TTIs until one can answer the
4458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// query.
4558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  TargetTransformInfo *PrevTTI;
4658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief The top of the stack of TTI analyses available.
4858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// This is a convenience routine maintained as TTI analyses become available
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// that complements the PrevTTI delegation chain. When one part of an
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// analysis pass wants to query another part of the analysis pass it can use
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// this to start back at the top of the stack.
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TargetTransformInfo *TopTTI;
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// All pass subclasses must in their initializePass routine call
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// pushTTIStack with themselves to update the pointers tracking the previous
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// TTI instance in the analysis group's stack, and the top of the analysis
5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// group's stack.
5958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void pushTTIStack(Pass *P);
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// All pass subclasses must in their finalizePass routine call popTTIStack
6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// to update the pointers tracking the previous TTI instance in the analysis
6358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// group's stack, and the top of the analysis group's stack.
6458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void popTTIStack();
6558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// All pass subclasses must call TargetTransformInfo::getAnalysisUsage.
6758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
6858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)public:
7058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// This class is intended to be subclassed by real implementations.
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual ~TargetTransformInfo() = 0;
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \name Generic Target Information
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// @{
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Underlying constants for 'cost' values in this interface.
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// Many APIs in this interface return a cost. This enum defines the
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// fundamental values that should be used to interpret (and produce) those
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// costs. The costs are returned as an unsigned rather than a member of this
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// enumeration because it is expected that the cost of one IR instruction
8258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// may have a multiplicative factor to it or otherwise won't fit directly
8358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// into the enum. Moreover, it is common to sum or average costs which works
8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// better as simple integral values. Thus this enum only provides constants.
8558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// Note that these costs should usually reflect the intersection of code-size
8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// cost and execution cost. A free instruction is typically one that folds
8858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// into another instruction. For example, reg-to-reg moves can often be
8958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// skipped by renaming the registers in the CPU, but they still are encoded
9058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// and thus wouldn't be considered 'free' here.
9158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  enum TargetCostConstants {
9258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    TCC_Free = 0,       ///< Expected to fold away in lowering.
9358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    TCC_Basic = 1,      ///< The cost of a typical 'add' instruction.
9458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    TCC_Expensive = 4   ///< The cost of a 'div' instruction on x86.
9558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  };
9658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
9758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Estimate the cost of a specific operation when lowered.
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
9958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// Note that this is designed to work on an arbitrary synthetic opcode, and
10058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// thus work for hypothetical queries before an instruction has even been
10158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// formed. However, this does *not* work for GEPs, and must not be called
10258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// for a GEP instruction. Instead, use the dedicated getGEPCost interface as
10358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// analyzing a GEP's cost required more information.
10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// Typically only the result type is required, and the operand type can be
10658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// omitted. However, if the opcode is one of the cast instructions, the
10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// operand type is required.
10858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
10958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// The returned cost is defined in terms of \c TargetCostConstants, see its
11058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// comments for a detailed explanation of the cost values.
11158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual unsigned getOperationCost(unsigned Opcode, Type *Ty,
11258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                    Type *OpTy = 0) const;
11358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
11458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Estimate the cost of a GEP operation when lowered.
11558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
11658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// The contract for this function is the same as \c getOperationCost except
11758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// that it supports an interface that provides extra information specific to
11858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// the GEP operation.
11958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual unsigned getGEPCost(const Value *Ptr,
12058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                              ArrayRef<const Value *> Operands) const;
12158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
12258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Estimate the cost of a function call when lowered.
12358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
12458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// The contract for this is the same as \c getOperationCost except that it
12558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// supports an interface that provides extra information specific to call
12658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// instructions.
12758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// This is the most basic query for estimating call cost: it only knows the
12958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// function type and (potentially) the number of arguments at the call site.
13058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// The latter is only interesting for varargs function types.
13158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual unsigned getCallCost(FunctionType *FTy, int NumArgs = -1) const;
13258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Estimate the cost of calling a specific function when lowered.
13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// This overload adds the ability to reason about the particular function
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// being called in the event it is a library call with special lowering.
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual unsigned getCallCost(const Function *F, int NumArgs = -1) const;
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Estimate the cost of calling a specific function when lowered.
14058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
14158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// This overload allows specifying a set of candidate argument values.
14258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual unsigned getCallCost(const Function *F,
14358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                               ArrayRef<const Value *> Arguments) const;
14458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
14558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Estimate the cost of an intrinsic when lowered.
14658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
14758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// Mirrors the \c getCallCost method but uses an intrinsic identifier.
14858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
14958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                    ArrayRef<Type *> ParamTys) const;
15058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
15158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Estimate the cost of an intrinsic when lowered.
15258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
15358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// Mirrors the \c getCallCost method but uses an intrinsic identifier.
15458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
15558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                    ArrayRef<const Value *> Arguments) const;
15658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// \brief Estimate the cost of a given IR user when lowered.
1585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ///
1595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// This can estimate the cost of either a ConstantExpr or Instruction when
1605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// lowered. It has two primary advantages over the \c getOperationCost and
1615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// \c getGEPCost above, and one significant disadvantage: it can only be
1625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// used when the IR construct has already been formed.
1635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ///
1645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// The advantages are that it can inspect the SSA use graph to reason more
1655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// accurately about the cost. For example, all-constant-GEPs can often be
1665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// folded into a load or other instruction, but if they are used in some
1675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// other context they may not be folded. This routine can distinguish such
1685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// cases.
1695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ///
17058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// The returned cost is defined in terms of \c TargetCostConstants, see its
17158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// comments for a detailed explanation of the cost values.
17258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual unsigned getUserCost(const User *U) const;
17358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
17458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Test whether calls to a function lower to actual program function
17558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// calls.
17658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
17758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// The idea is to test whether the program is likely to require a 'call'
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// instruction or equivalent in order to call the given function.
17958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
18058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// FIXME: It's not clear that this is a good or useful query API. Client's
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// should probably move to simpler cost metrics using the above.
18258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// Alternatively, we could split the cost interface into distinct code-size
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// and execution-speed costs. This would allow modelling the core of this
18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// query more accurately as the a call is a single small instruction, but
18558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// incurs significant execution cost.
18658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual bool isLoweredToCall(const Function *F) const;
18758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
18858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// @}
18958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
19058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \name Scalar Target Information
19158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// @{
19258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
19358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Flags indicating the kind of support for population count.
19458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ///
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// Compared to the SW implementation, HW support is supposed to
19658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// significantly boost the performance when the population is dense, and it
19758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// may or may not degrade performance if the population is sparse. A HW
1985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// support is considered as "Fast" if it can outperform, or is on a par
1995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// with, SW implementation when the population is sparse; otherwise, it is
2005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// considered as "Slow".
2015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum PopcntSupportKind {
2025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    PSK_Software,
2035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    PSK_SlowHardware,
2045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    PSK_FastHardware
2055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  };
2065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// isLegalAddImmediate - Return true if the specified immediate is legal
2085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// add immediate, that is the target has add instructions which can add
2095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// a register with the immediate without having to materialize the
2105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// immediate into a register.
2115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual bool isLegalAddImmediate(int64_t Imm) const;
2125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// isLegalICmpImmediate - Return true if the specified immediate is legal
21458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// icmp immediate, that is the target has icmp instructions which can compare
21558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// a register against the immediate without having to materialize the
21658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// immediate into a register.
21758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual bool isLegalICmpImmediate(int64_t Imm) const;
21858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
21958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// isLegalAddressingMode - Return true if the addressing mode represented by
22058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// AM is legal for this target, for a load/store of the specified type.
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// The type may be VoidTy, in which case only return true if the addressing
22258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// mode is legal for a load/store of any legal type.
22358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// TODO: Handle pre/postinc as well.
22458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
22558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                     int64_t BaseOffset, bool HasBaseReg,
22658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                     int64_t Scale) const;
22758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
22858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// \brief Return the cost of the scaling factor used in the addressing
2295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// mode represented by AM for this target, for a load/store
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// of the specified type.
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// If the AM is supported, the return value must be >= 0.
232116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// If the AM is not supported, it returns a negative value.
2335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  /// TODO: Handle pre/postinc as well.
234  virtual int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
235                                   int64_t BaseOffset, bool HasBaseReg,
236                                   int64_t Scale) const;
237
238  /// isTruncateFree - Return true if it's free to truncate a value of
239  /// type Ty1 to type Ty2. e.g. On x86 it's free to truncate a i32 value in
240  /// register EAX to i16 by referencing its sub-register AX.
241  virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
242
243  /// Is this type legal.
244  virtual bool isTypeLegal(Type *Ty) const;
245
246  /// getJumpBufAlignment - returns the target's jmp_buf alignment in bytes
247  virtual unsigned getJumpBufAlignment() const;
248
249  /// getJumpBufSize - returns the target's jmp_buf size in bytes.
250  virtual unsigned getJumpBufSize() const;
251
252  /// shouldBuildLookupTables - Return true if switches should be turned into
253  /// lookup tables for the target.
254  virtual bool shouldBuildLookupTables() const;
255
256  /// getPopcntSupport - Return hardware support for population count.
257  virtual PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const;
258
259  /// getIntImmCost - Return the expected cost of materializing the given
260  /// integer immediate of the specified type.
261  virtual unsigned getIntImmCost(const APInt &Imm, Type *Ty) const;
262
263  /// @}
264
265  /// \name Vector Target Information
266  /// @{
267
268  /// \brief The various kinds of shuffle patterns for vector queries.
269  enum ShuffleKind {
270    SK_Broadcast,       ///< Broadcast element 0 to all other elements.
271    SK_Reverse,         ///< Reverse the order of the vector.
272    SK_InsertSubvector, ///< InsertSubvector. Index indicates start offset.
273    SK_ExtractSubvector ///< ExtractSubvector Index indicates start offset.
274  };
275
276  /// \brief Additonal information about an operand's possible values.
277  enum OperandValueKind {
278    OK_AnyValue,            // Operand can have any value.
279    OK_UniformValue,        // Operand is uniform (splat of a value).
280    OK_UniformConstantValue // Operand is uniform constant.
281  };
282
283  /// \return The number of scalar or vector registers that the target has.
284  /// If 'Vectors' is true, it returns the number of vector registers. If it is
285  /// set to false, it returns the number of scalar registers.
286  virtual unsigned getNumberOfRegisters(bool Vector) const;
287
288  /// \return The width of the largest scalar or vector register type.
289  virtual unsigned getRegisterBitWidth(bool Vector) const;
290
291  /// \return The maximum unroll factor that the vectorizer should try to
292  /// perform for this target. This number depends on the level of parallelism
293  /// and the number of execution units in the CPU.
294  virtual unsigned getMaximumUnrollFactor() const;
295
296  /// \return The expected cost of arithmetic ops, such as mul, xor, fsub, etc.
297  virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
298                                  OperandValueKind Opd1Info = OK_AnyValue,
299                                  OperandValueKind Opd2Info = OK_AnyValue) const;
300
301  /// \return The cost of a shuffle instruction of kind Kind and of type Tp.
302  /// The index and subtype parameters are used by the subvector insertion and
303  /// extraction shuffle kinds.
304  virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, int Index = 0,
305                                  Type *SubTp = 0) const;
306
307  /// \return The expected cost of cast instructions, such as bitcast, trunc,
308  /// zext, etc.
309  virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
310                                    Type *Src) const;
311
312  /// \return The expected cost of control-flow related instructions such as
313  /// Phi, Ret, Br.
314  virtual unsigned getCFInstrCost(unsigned Opcode) const;
315
316  /// \returns The expected cost of compare and select instructions.
317  virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
318                                      Type *CondTy = 0) const;
319
320  /// \return The expected cost of vector Insert and Extract.
321  /// Use -1 to indicate that there is no information on the index value.
322  virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
323                                      unsigned Index = -1) const;
324
325  /// \return The cost of Load and Store instructions.
326  virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
327                                   unsigned Alignment,
328                                   unsigned AddressSpace) const;
329
330  /// \returns The cost of Intrinsic instructions.
331  virtual unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
332                                         ArrayRef<Type *> Tys) const;
333
334  /// \returns The number of pieces into which the provided type must be
335  /// split during legalization. Zero is returned when the answer is unknown.
336  virtual unsigned getNumberOfParts(Type *Tp) const;
337
338  /// \returns The cost of the address computation. For most targets this can be
339  /// merged into the instruction indexing mode. Some targets might want to
340  /// distinguish between address computation for memory operations on vector
341  /// types and scalar types. Such targets should override this function.
342  virtual unsigned getAddressComputationCost(Type *Ty) const;
343
344  /// @}
345
346  /// Analysis group identification.
347  static char ID;
348};
349
350/// \brief Create the base case instance of a pass in the TTI analysis group.
351///
352/// This class provides the base case for the stack of TTI analyzes. It doesn't
353/// delegate to anything and uses the STTI and VTTI objects passed in to
354/// satisfy the queries.
355ImmutablePass *createNoTargetTransformInfoPass();
356
357} // End llvm namespace
358
359#endif
360