LLVM  12.0.0git
Public Member Functions | Protected Attributes | List of all members
llvm::CombinerHelper Class Reference

#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"

Collaboration diagram for llvm::CombinerHelper:
Collaboration graph
[legend]

Public Member Functions

 CombinerHelper (GISelChangeObserver &Observer, MachineIRBuilder &B, GISelKnownBits *KB=nullptr, MachineDominatorTree *MDT=nullptr, const LegalizerInfo *LI=nullptr)
 
GISelKnownBitsgetKnownBits () const
 
void replaceRegWith (MachineRegisterInfo &MRI, Register FromReg, Register ToReg) const
 MachineRegisterInfo::replaceRegWith() and inform the observer of the changes. More...
 
void replaceRegOpWith (MachineRegisterInfo &MRI, MachineOperand &FromRegOp, Register ToReg) const
 Replace a single register operand with a new register and inform the observer of the changes. More...
 
bool tryCombineCopy (MachineInstr &MI)
 If MI is COPY, try to combine it. More...
 
bool matchCombineCopy (MachineInstr &MI)
 
void applyCombineCopy (MachineInstr &MI)
 
bool isPredecessor (const MachineInstr &DefMI, const MachineInstr &UseMI)
 Returns true if DefMI precedes UseMI or they are the same instruction. More...
 
bool dominates (const MachineInstr &DefMI, const MachineInstr &UseMI)
 Returns true if DefMI dominates UseMI. More...
 
bool tryCombineExtendingLoads (MachineInstr &MI)
 If MI is extend that consumes the result of a load, try to combine it. More...
 
bool matchCombineExtendingLoads (MachineInstr &MI, PreferredTuple &MatchInfo)
 
void applyCombineExtendingLoads (MachineInstr &MI, PreferredTuple &MatchInfo)
 
bool tryCombineIndexedLoadStore (MachineInstr &MI)
 Combine MI into a pre-indexed or post-indexed load/store operation if legal and the surrounding code makes it useful. More...
 
bool matchCombineIndexedLoadStore (MachineInstr &MI, IndexedLoadStoreMatchInfo &MatchInfo)
 
void applyCombineIndexedLoadStore (MachineInstr &MI, IndexedLoadStoreMatchInfo &MatchInfo)
 
bool matchSextAlreadyExtended (MachineInstr &MI)
 
bool applySextAlreadyExtended (MachineInstr &MI)
 
bool matchElideBrByInvertingCond (MachineInstr &MI)
 
void applyElideBrByInvertingCond (MachineInstr &MI)
 
bool tryElideBrByInvertingCond (MachineInstr &MI)
 
bool tryCombineConcatVectors (MachineInstr &MI)
 If MI is G_CONCAT_VECTORS, try to combine it. More...
 
bool matchCombineConcatVectors (MachineInstr &MI, bool &IsUndef, SmallVectorImpl< Register > &Ops)
 Check if the G_CONCAT_VECTORS MI is undef or if it can be flattened into a build_vector. More...
 
void applyCombineConcatVectors (MachineInstr &MI, bool IsUndef, const ArrayRef< Register > Ops)
 Replace MI with a flattened build_vector with Ops or an implicit_def if IsUndef is true. More...
 
bool tryCombineShuffleVector (MachineInstr &MI)
 Try to combine G_SHUFFLE_VECTOR into G_CONCAT_VECTORS. More...
 
bool matchCombineShuffleVector (MachineInstr &MI, SmallVectorImpl< Register > &Ops)
 Check if the G_SHUFFLE_VECTOR MI can be replaced by a concat_vectors. More...
 
void applyCombineShuffleVector (MachineInstr &MI, const ArrayRef< Register > Ops)
 Replace MI with a concat_vectors with Ops. More...
 
bool tryCombineMemCpyFamily (MachineInstr &MI, unsigned MaxLen=0)
 Optimize memcpy intrinsics et al, e.g. More...
 
bool matchPtrAddImmedChain (MachineInstr &MI, PtrAddChain &MatchInfo)
 
bool applyPtrAddImmedChain (MachineInstr &MI, PtrAddChain &MatchInfo)
 
bool matchCombineMulToShl (MachineInstr &MI, unsigned &ShiftVal)
 Transform a multiply by a power-of-2 value to a left shift. More...
 
bool applyCombineMulToShl (MachineInstr &MI, unsigned &ShiftVal)
 
bool matchCombineShiftToUnmerge (MachineInstr &MI, unsigned TargetShiftSize, unsigned &ShiftVal)
 Reduce a shift by a constant to an unmerge and a shift on a half sized type. More...
 
bool applyCombineShiftToUnmerge (MachineInstr &MI, const unsigned &ShiftVal)
 
bool tryCombineShiftToUnmerge (MachineInstr &MI, unsigned TargetShiftAmount)
 
bool matchAnyExplicitUseIsUndef (MachineInstr &MI)
 Return true if any explicit use operand on MI is defined by a G_IMPLICIT_DEF. More...
 
bool matchAllExplicitUsesAreUndef (MachineInstr &MI)
 Return true if all register explicit use operands on MI are defined by a G_IMPLICIT_DEF. More...
 
bool matchUndefShuffleVectorMask (MachineInstr &MI)
 Return true if a G_SHUFFLE_VECTOR instruction MI has an undef mask. More...
 
bool matchUndefStore (MachineInstr &MI)
 Return true if a G_STORE instruction MI is storing an undef value. More...
 
bool replaceInstWithFConstant (MachineInstr &MI, double C)
 Replace an instruction with a G_FCONSTANT with value C. More...
 
bool replaceInstWithConstant (MachineInstr &MI, int64_t C)
 Replace an instruction with a G_CONSTANT with value C. More...
 
bool replaceInstWithUndef (MachineInstr &MI)
 Replace an instruction with a G_IMPLICIT_DEF. More...
 
bool replaceSingleDefInstWithOperand (MachineInstr &MI, unsigned OpIdx)
 Delete MI and replace all of its uses with its OpIdx-th operand. More...
 
bool matchEqualDefs (const MachineOperand &MOP1, const MachineOperand &MOP2)
 Return true if MOP1 and MOP2 are register operands are defined by equivalent instructions. More...
 
bool matchConstantOp (const MachineOperand &MOP, int64_t C)
 Return true if MOP is defined by a G_CONSTANT with a value equal to C. More...
 
bool matchSelectSameVal (MachineInstr &MI)
 Optimize (cond ? x : x) -> x. More...
 
bool matchBinOpSameVal (MachineInstr &MI)
 Optimize (x op x) -> x. More...
 
bool matchOperandIsZero (MachineInstr &MI, unsigned OpIdx)
 Check if operand OpIdx is zero. More...
 
bool eraseInst (MachineInstr &MI)
 Erase MI. More...
 
bool matchSimplifyAddToSub (MachineInstr &MI, std::tuple< Register, Register > &MatchInfo)
 Return true if MI is a G_ADD which can be simplified to a G_SUB. More...
 
bool applySimplifyAddToSub (MachineInstr &MI, std::tuple< Register, Register > &MatchInfo)
 
bool tryCombine (MachineInstr &MI)
 Try to transform MI by using all of the above combine functions. More...
 

Protected Attributes

MachineIRBuilderBuilder
 
MachineRegisterInfoMRI
 
GISelChangeObserverObserver
 
GISelKnownBitsKB
 
MachineDominatorTreeMDT
 
const LegalizerInfoLI
 

Detailed Description

Definition at line 53 of file CombinerHelper.h.

Constructor & Destructor Documentation

◆ CombinerHelper()

CombinerHelper::CombinerHelper ( GISelChangeObserver Observer,
MachineIRBuilder B,
GISelKnownBits KB = nullptr,
MachineDominatorTree MDT = nullptr,
const LegalizerInfo LI = nullptr 
)

Definition at line 38 of file CombinerHelper.cpp.

Member Function Documentation

◆ applyCombineConcatVectors()

void CombinerHelper::applyCombineConcatVectors ( MachineInstr MI,
bool  IsUndef,
const ArrayRef< Register Ops 
)

◆ applyCombineCopy()

void CombinerHelper::applyCombineCopy ( MachineInstr MI)

◆ applyCombineExtendingLoads()

void CombinerHelper::applyCombineExtendingLoads ( MachineInstr MI,
PreferredTuple MatchInfo 
)

◆ applyCombineIndexedLoadStore()

void CombinerHelper::applyCombineIndexedLoadStore ( MachineInstr MI,
IndexedLoadStoreMatchInfo MatchInfo 
)

◆ applyCombineMulToShl()

bool CombinerHelper::applyCombineMulToShl ( MachineInstr MI,
unsigned ShiftVal 
)

◆ applyCombineShiftToUnmerge()

bool CombinerHelper::applyCombineShiftToUnmerge ( MachineInstr MI,
const unsigned ShiftVal 
)

◆ applyCombineShuffleVector()

void CombinerHelper::applyCombineShuffleVector ( MachineInstr MI,
const ArrayRef< Register Ops 
)

◆ applyElideBrByInvertingCond()

void CombinerHelper::applyElideBrByInvertingCond ( MachineInstr MI)

◆ applyPtrAddImmedChain()

bool CombinerHelper::applyPtrAddImmedChain ( MachineInstr MI,
PtrAddChain MatchInfo 
)

◆ applySextAlreadyExtended()

bool CombinerHelper::applySextAlreadyExtended ( MachineInstr MI)

◆ applySimplifyAddToSub()

bool CombinerHelper::applySimplifyAddToSub ( MachineInstr MI,
std::tuple< Register, Register > &  MatchInfo 
)

◆ dominates()

bool CombinerHelper::dominates ( const MachineInstr DefMI,
const MachineInstr UseMI 
)

Returns true if DefMI dominates UseMI.

By definition an instruction dominates itself.

If we haven't been provided with a MachineDominatorTree during construction, this function returns a conservative result that tracks just a single basic block.

Definition at line 567 of file CombinerHelper.cpp.

References assert(), llvm::MachineDominatorTree::dominates(), llvm::MachineInstr::getParent(), llvm::MachineInstr::isDebugInstr(), isPredecessor(), and MDT.

Referenced by applySextAlreadyExtended().

◆ eraseInst()

bool CombinerHelper::eraseInst ( MachineInstr MI)

Erase MI.

Definition at line 1558 of file CombinerHelper.cpp.

References llvm::MachineInstr::eraseFromParent().

◆ getKnownBits()

GISelKnownBits* llvm::CombinerHelper::getKnownBits ( ) const
inline

◆ isPredecessor()

bool CombinerHelper::isPredecessor ( const MachineInstr DefMI,
const MachineInstr UseMI 
)

Returns true if DefMI precedes UseMI or they are the same instruction.

Both must be in the same basic block.

Definition at line 551 of file CombinerHelper.cpp.

References assert(), llvm::MachineBasicBlock::begin(), llvm::MachineInstr::getParent(), I, llvm::MachineInstr::isDebugInstr(), llvm_unreachable, and UseMI.

Referenced by dominates().

◆ matchAllExplicitUsesAreUndef()

bool CombinerHelper::matchAllExplicitUsesAreUndef ( MachineInstr MI)

Return true if all register explicit use operands on MI are defined by a G_IMPLICIT_DEF.

Definition at line 1539 of file CombinerHelper.cpp.

References llvm::all_of(), llvm::MachineInstr::explicit_uses(), llvm::getOpcodeDef(), and MRI.

◆ matchAnyExplicitUseIsUndef()

bool CombinerHelper::matchAnyExplicitUseIsUndef ( MachineInstr MI)

Return true if any explicit use operand on MI is defined by a G_IMPLICIT_DEF.

Definition at line 1532 of file CombinerHelper.cpp.

References llvm::any_of(), llvm::MachineInstr::explicit_uses(), llvm::getOpcodeDef(), and MRI.

◆ matchBinOpSameVal()

bool CombinerHelper::matchBinOpSameVal ( MachineInstr MI)

◆ matchCombineConcatVectors()

bool CombinerHelper::matchCombineConcatVectors ( MachineInstr MI,
bool IsUndef,
SmallVectorImpl< Register > &  Ops 
)

Check if the G_CONCAT_VECTORS MI is undef or if it can be flattened into a build_vector.

In the first case IsUndef will be true. In the second case Ops will contain the operands needed to produce the flattened build_vector.

Precondition
MI.getOpcode() == G_CONCAT_VECTORS.

Definition at line 101 of file CombinerHelper.cpp.

References assert(), Builder, llvm::MachineIRBuilder::buildUndef(), llvm::tgtok::Def, llvm::LLT::getNumElements(), llvm::MachineInstr::getOpcode(), llvm::MachineInstr::getOperand(), llvm::MachineInstr::getParent(), llvm::MachineOperand::getReg(), llvm::LLT::getScalarType(), llvm::MachineRegisterInfo::getType(), llvm::MachineRegisterInfo::getVRegDef(), MI, MRI, llvm::SmallVectorTemplateBase< T >::push_back(), Reg, llvm::MachineIRBuilder::setInsertPt(), llvm::RegState::Undef, and llvm::MachineInstr::uses().

Referenced by tryCombineConcatVectors().

◆ matchCombineCopy()

bool CombinerHelper::matchCombineCopy ( MachineInstr MI)

◆ matchCombineExtendingLoads()

bool CombinerHelper::matchCombineExtendingLoads ( MachineInstr MI,
PreferredTuple MatchInfo 
)

◆ matchCombineIndexedLoadStore()

bool CombinerHelper::matchCombineIndexedLoadStore ( MachineInstr MI,
IndexedLoadStoreMatchInfo MatchInfo 
)

◆ matchCombineMulToShl()

bool CombinerHelper::matchCombineMulToShl ( MachineInstr MI,
unsigned ShiftVal 
)

◆ matchCombineShiftToUnmerge()

bool CombinerHelper::matchCombineShiftToUnmerge ( MachineInstr MI,
unsigned  TargetShiftSize,
unsigned ShiftVal 
)

Reduce a shift by a constant to an unmerge and a shift on a half sized type.

This will not produce a shift smaller than TargetShiftSize.

Definition at line 1422 of file CombinerHelper.cpp.

References assert(), llvm::getConstantVRegValWithLookThrough(), llvm::MachineInstr::getOpcode(), llvm::MachineInstr::getOperand(), llvm::MachineOperand::getReg(), llvm::LLT::getSizeInBits(), llvm::MachineRegisterInfo::getType(), llvm::LLT::isVector(), MRI, and Size.

Referenced by tryCombineShiftToUnmerge().

◆ matchCombineShuffleVector()

bool CombinerHelper::matchCombineShuffleVector ( MachineInstr MI,
SmallVectorImpl< Register > &  Ops 
)

◆ matchConstantOp()

bool CombinerHelper::matchConstantOp ( const MachineOperand MOP,
int64_t  C 
)

Return true if MOP is defined by a G_CONSTANT with a value equal to C.

Definition at line 1635 of file CombinerHelper.cpp.

References C, llvm::getConstantVRegValWithLookThrough(), llvm::MachineOperand::getReg(), llvm::MachineOperand::isReg(), and MRI.

Referenced by matchOperandIsZero().

◆ matchElideBrByInvertingCond()

bool CombinerHelper::matchElideBrByInvertingCond ( MachineInstr MI)

◆ matchEqualDefs()

bool CombinerHelper::matchEqualDefs ( const MachineOperand MOP1,
const MachineOperand MOP2 
)

◆ matchOperandIsZero()

bool CombinerHelper::matchOperandIsZero ( MachineInstr MI,
unsigned  OpIdx 
)

◆ matchPtrAddImmedChain()

bool CombinerHelper::matchPtrAddImmedChain ( MachineInstr MI,
PtrAddChain MatchInfo 
)

◆ matchSelectSameVal()

bool CombinerHelper::matchSelectSameVal ( MachineInstr MI)

◆ matchSextAlreadyExtended()

bool CombinerHelper::matchSextAlreadyExtended ( MachineInstr MI)

◆ matchSimplifyAddToSub()

bool CombinerHelper::matchSimplifyAddToSub ( MachineInstr MI,
std::tuple< Register, Register > &  MatchInfo 
)

◆ matchUndefShuffleVectorMask()

bool CombinerHelper::matchUndefShuffleVectorMask ( MachineInstr MI)

Return true if a G_SHUFFLE_VECTOR instruction MI has an undef mask.

Definition at line 1546 of file CombinerHelper.cpp.

References llvm::all_of(), assert(), llvm::MachineInstr::getOpcode(), llvm::MachineInstr::getOperand(), llvm::MachineOperand::getShuffleMask(), and llvm::BitmaskEnumDetail::Mask().

◆ matchUndefStore()

bool CombinerHelper::matchUndefStore ( MachineInstr MI)

Return true if a G_STORE instruction MI is storing an undef value.

Definition at line 1552 of file CombinerHelper.cpp.

References assert(), llvm::MachineInstr::getOpcode(), llvm::getOpcodeDef(), llvm::MachineInstr::getOperand(), llvm::MachineOperand::getReg(), and MRI.

◆ replaceInstWithConstant()

bool CombinerHelper::replaceInstWithConstant ( MachineInstr MI,
int64_t  C 
)

◆ replaceInstWithFConstant()

bool CombinerHelper::replaceInstWithFConstant ( MachineInstr MI,
double  C 
)

◆ replaceInstWithUndef()

bool CombinerHelper::replaceInstWithUndef ( MachineInstr MI)

◆ replaceRegOpWith()

void CombinerHelper::replaceRegOpWith ( MachineRegisterInfo MRI,
MachineOperand FromRegOp,
Register  ToReg 
) const

Replace a single register operand with a new register and inform the observer of the changes.

Definition at line 59 of file CombinerHelper.cpp.

References assert(), llvm::GISelChangeObserver::changedInstr(), llvm::GISelChangeObserver::changingInstr(), llvm::MachineOperand::getParent(), Observer, and llvm::MachineOperand::setReg().

Referenced by applyCombineExtendingLoads().

◆ replaceRegWith()

void CombinerHelper::replaceRegWith ( MachineRegisterInfo MRI,
Register  FromReg,
Register  ToReg 
) const

◆ replaceSingleDefInstWithOperand()

bool CombinerHelper::replaceSingleDefInstWithOperand ( MachineInstr MI,
unsigned  OpIdx 
)

◆ tryCombine()

bool CombinerHelper::tryCombine ( MachineInstr MI)

Try to transform MI by using all of the above combine functions.

Returns true if changed.

Definition at line 1730 of file CombinerHelper.cpp.

References tryCombineCopy(), tryCombineExtendingLoads(), and tryCombineIndexedLoadStore().

◆ tryCombineConcatVectors()

bool CombinerHelper::tryCombineConcatVectors ( MachineInstr MI)

If MI is G_CONCAT_VECTORS, try to combine it.

Returns true if MI changed. Right now, we support:

  • concat_vector(undef, undef) => undef
  • concat_vector(build_vector(A, B), build_vector(C, D)) => build_vector(A, B, C, D)
Precondition
MI.getOpcode() == G_CONCAT_VECTORS.

Definition at line 91 of file CombinerHelper.cpp.

References applyCombineConcatVectors(), and matchCombineConcatVectors().

◆ tryCombineCopy()

bool CombinerHelper::tryCombineCopy ( MachineInstr MI)

If MI is COPY, try to combine it.

Returns true if MI changed.

Definition at line 70 of file CombinerHelper.cpp.

References applyCombineCopy(), and matchCombineCopy().

Referenced by tryCombine().

◆ tryCombineExtendingLoads()

bool CombinerHelper::tryCombineExtendingLoads ( MachineInstr MI)

If MI is extend that consumes the result of a load, try to combine it.

Returns true if MI changed.

Definition at line 353 of file CombinerHelper.cpp.

References applyCombineExtendingLoads(), and matchCombineExtendingLoads().

Referenced by tryCombine().

◆ tryCombineIndexedLoadStore()

bool CombinerHelper::tryCombineIndexedLoadStore ( MachineInstr MI)

Combine MI into a pre-indexed or post-indexed load/store operation if legal and the surrounding code makes it useful.

Definition at line 726 of file CombinerHelper.cpp.

References applyCombineIndexedLoadStore(), and matchCombineIndexedLoadStore().

Referenced by tryCombine().

◆ tryCombineMemCpyFamily()

bool CombinerHelper::tryCombineMemCpyFamily ( MachineInstr MI,
unsigned  MaxLen = 0 
)

Optimize memcpy intrinsics et al, e.g.

constant len calls. /p MaxLen if non-zero specifies the max length of a mem libcall to inline.

For example (pre-indexed):

$addr = G_PTR_ADD $base, $offset
[...]
$val = G_LOAD $addr
[...]
$whatever = COPY $addr

–>

$val, $addr = G_INDEXED_LOAD $base, $offset, 1 (IsPre)
[...]
$whatever = COPY $addr

or (post-indexed):

G_STORE $val, $base
[...]
$addr = G_PTR_ADD $base, $offset
[...]
$whatever = COPY $addr

–>

$addr = G_INDEXED_STORE $val, $base, $offset
[...]
$whatever = COPY $addr  

Definition at line 1301 of file CombinerHelper.cpp.

References assert(), llvm::MachineInstr::eraseFromParent(), llvm::MachineMemOperand::getBaseAlign(), llvm::getConstantVRegValWithLookThrough(), llvm::MachineInstr::getIntrinsicID(), llvm::MachineInstr::getOpcode(), llvm::MachineInstr::getOperand(), llvm::MachineOperand::getReg(), llvm::MachineMemOperand::isVolatile(), llvm::MachineInstr::memoperands_begin(), llvm::MachineInstr::memoperands_end(), and MRI.

◆ tryCombineShiftToUnmerge()

bool CombinerHelper::tryCombineShiftToUnmerge ( MachineInstr MI,
unsigned  TargetShiftAmount 
)

◆ tryCombineShuffleVector()

bool CombinerHelper::tryCombineShuffleVector ( MachineInstr MI)

Try to combine G_SHUFFLE_VECTOR into G_CONCAT_VECTORS.

Returns true if MI changed.

Precondition
MI.getOpcode() == G_SHUFFLE_VECTOR.

Definition at line 168 of file CombinerHelper.cpp.

References applyCombineShuffleVector(), and matchCombineShuffleVector().

◆ tryElideBrByInvertingCond()

bool CombinerHelper::tryElideBrByInvertingCond ( MachineInstr MI)

Member Data Documentation

◆ Builder

MachineIRBuilder& llvm::CombinerHelper::Builder
protected

◆ KB

GISelKnownBits* llvm::CombinerHelper::KB
protected

Definition at line 58 of file CombinerHelper.h.

Referenced by matchSextAlreadyExtended().

◆ LI

const LegalizerInfo* llvm::CombinerHelper::LI
protected

Definition at line 60 of file CombinerHelper.h.

Referenced by matchCombineExtendingLoads().

◆ MDT

MachineDominatorTree* llvm::CombinerHelper::MDT
protected

Definition at line 59 of file CombinerHelper.h.

Referenced by dominates().

◆ MRI

MachineRegisterInfo& llvm::CombinerHelper::MRI
protected

◆ Observer

GISelChangeObserver& llvm::CombinerHelper::Observer
protected

The documentation for this class was generated from the following files: