LLVM 24.0.0git
CodeGenPrepare.cpp File Reference
#include "llvm/CodeGen/CodeGenPrepare.h"
#include "CodeGenOptions.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/FloatingPointPredicateUtils.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/CodeGen/Analysis.h"
#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
#include "llvm/CodeGen/ISDOpcodes.h"
#include "llvm/CodeGen/SelectionDAGNodes.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/CodeGenTypes/MachineValueType.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/CycleInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IntrinsicsAArch64.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ProfDataUtils.h"
#include "llvm/IR/Statepoint.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/ValueMap.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/BlockFrequency.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/BypassSlowDivision.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
#include "llvm/Transforms/Utils/SizeOpts.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iterator>
#include <limits>
#include <memory>
#include <optional>
#include <utility>
#include <vector>

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "codegenprepare"

Functions

 STATISTIC (NumBlocksElim, "Number of blocks eliminated")
 STATISTIC (NumPHIsElim, "Number of trivial PHIs eliminated")
 STATISTIC (NumGEPsElim, "Number of GEPs converted to casts")
 STATISTIC (NumCmpUses, "Number of uses of Cmp expressions replaced with uses of " "sunken Cmps")
 STATISTIC (NumCastUses, "Number of uses of Cast expressions replaced with uses " "of sunken Casts")
 STATISTIC (NumMemoryInsts, "Number of memory instructions whose address " "computations were sunk")
 STATISTIC (NumMemoryInstsPhiCreated, "Number of phis created when address " "computations were sunk to memory instructions")
 STATISTIC (NumMemoryInstsSelectCreated, "Number of select created when address " "computations were sunk to memory instructions")
 STATISTIC (NumExtsMoved, "Number of [s|z]ext instructions combined with loads")
 STATISTIC (NumExtUses, "Number of uses of [s|z]ext instructions optimized")
 STATISTIC (NumAndsAdded, "Number of and mask instructions added to form ext loads")
 STATISTIC (NumAndUses, "Number of uses of and mask instructions optimized")
 STATISTIC (NumRetsDup, "Number of return instructions duplicated")
 STATISTIC (NumDbgValueMoved, "Number of debug value instructions moved")
 STATISTIC (NumSelectsExpanded, "Number of selects turned into branches")
 STATISTIC (NumStoreExtractExposed, "Number of store(extractelement) exposed")
 INITIALIZE_PASS_BEGIN (CodeGenPrepareLegacyPass, DEBUG_TYPE, "Optimize for code generation", false, false) INITIALIZE_PASS_END(CodeGenPrepareLegacyPass
static void replaceAllUsesWith (Value *Old, Value *New, SmallPtrSet< BasicBlock *, 32 > &FreshBBs, bool IsHuge)
 Replace all old uses with new ones, and push the updated BBs into FreshBBs.
static void computeBaseDerivedRelocateMap (const SmallVectorImpl< GCRelocateInst * > &AllRelocateCalls, MapVector< GCRelocateInst *, SmallVector< GCRelocateInst *, 0 > > &RelocateInstMap)
static bool getGEPSmallConstantIntOffsetV (GetElementPtrInst *GEP, SmallVectorImpl< Value * > &OffsetV)
static bool simplifyRelocatesOffABase (GCRelocateInst *RelocatedBase, const SmallVectorImpl< GCRelocateInst * > &Targets)
static bool SinkCast (CastInst *CI)
 Sink the specified cast instruction into its user blocks.
static bool optimizeBitCast (BitCastInst *BCI, const TargetLowering &TLI, const DataLayout &DL)
 Hoists bitcasts to the source block to reduce register pressure.
static bool OptimizeNoopCopyExpression (CastInst *CI, const TargetLowering &TLI, const DataLayout &DL)
 If the specified cast instruction is a noop copy (e.g.
static bool matchIncrement (const Instruction *IVInc, Instruction *&LHS, Constant *&Step)
static std::optional< std::pair< Instruction *, Constant * > > getIVIncrement (const PHINode *PN, const LoopInfo *LI)
 If given PN is an inductive variable with value IVInc coming from the backedge, and on each iteration it gets increased by Step, return pair <IVInc, Step>.
static bool isIVIncrement (const Value *V, const LoopInfo *LI)
static bool matchUAddWithOverflowConstantEdgeCases (CmpInst *Cmp, BinaryOperator *&Add)
 Match special-case patterns that check for unsigned add overflow.
static bool sinkCmpExpression (CmpInst *Cmp, const TargetLowering &TLI, const DataLayout &DL)
 Sink the given CmpInst into user blocks to reduce the number of virtual registers that must be created and coalesced.
static bool foldICmpWithDominatingICmp (CmpInst *Cmp, const TargetLowering &TLI, bool EnableICmpEqToICmpSt)
 For pattern like:
static bool swapICmpOperandsToExposeCSEOpportunities (CmpInst *Cmp)
 Many architectures use the same instruction for both subtract and cmp.
static bool foldFCmpToFPClassTest (CmpInst *Cmp, const TargetLowering &TLI, const DataLayout &DL)
static bool isRemOfLoopIncrementWithLoopInvariant (Instruction *Rem, const LoopInfo *LI, Value *&RemAmtOut, Value *&AddInstOut, Value *&AddOffsetOut, PHINode *&LoopIncrPNOut)
static bool foldURemOfLoopIncrement (Instruction *Rem, const DataLayout *DL, const LoopInfo *LI, SmallPtrSet< BasicBlock *, 32 > &FreshBBs, bool IsHuge)
static bool sinkAndCmp0Expression (Instruction *AndI, const TargetLowering &TLI, SetOfInstrs &InsertedInsts)
 Duplicate and sink the given 'and' instruction into user blocks where it is used in a compare to allow isel to generate better code for targets where this operation can be combined.
static bool isExtractBitsCandidateUse (Instruction *User)
 Check if the candidates could be combined with a shift instruction, which includes:
static bool SinkShiftAndTruncate (BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI, DenseMap< BasicBlock *, BinaryOperator * > &InsertedShifts, const TargetLowering &TLI, const DataLayout &DL)
 Sink both shift and truncate instruction to the use of truncate's BB.
static bool OptimizeExtractBits (BinaryOperator *ShiftI, ConstantInt *CI, const TargetLowering &TLI, const DataLayout &DL)
 Sink the shift right instruction into user blocks if the uses could potentially be combined with this shift instruction and generate BitExtract instruction.
static bool despeculateCountZeros (IntrinsicInst *CountZeros, DomTreeUpdater *DTU, LoopInfo *LI, const TargetLowering *TLI, const DataLayout *DL, ModifyDT &ModifiedDT, SmallPtrSet< BasicBlock *, 32 > &FreshBBs, bool IsHugeFunc)
 If counting leading or trailing zeros is an expensive operation and a zero input is defined, add a check for zero to avoid calling the intrinsic.
static bool isIntrinsicOrLFToBeTailCalled (const TargetLibraryInfo *TLInfo, const CallInst *CI)
static bool MightBeFoldableInst (Instruction *I)
 This is a little filter, which returns true if an addressing computation involving I might be folded into a load/store accessing it.
static bool isPromotedInstructionLegal (const TargetLowering &TLI, const DataLayout &DL, Value *Val)
 Check whether or not Val is a legal instruction for TLI.
static bool IsOperandAMemoryOperand (CallInst *CI, InlineAsm *IA, Value *OpVal, const TargetLowering &TLI, const TargetRegisterInfo &TRI)
 Check to see if all uses of OpVal by the specified inline asm call are due to memory operands.
static bool FindAllMemoryUses (Instruction *I, SmallVectorImpl< std::pair< Use *, Type * > > &MemoryUses, SmallPtrSetImpl< Instruction * > &ConsideredInsts, const TargetLowering &TLI, const TargetRegisterInfo &TRI, bool OptSize, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI, unsigned &SeenInsts, unsigned MaxUsersToScan)
 Recursively walk all the uses of I until we find a memory use.
static bool FindAllMemoryUses (Instruction *I, SmallVectorImpl< std::pair< Use *, Type * > > &MemoryUses, const TargetLowering &TLI, const TargetRegisterInfo &TRI, bool OptSize, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI, unsigned MaxUsersToScan)
static bool IsNonLocalValue (Value *V, BasicBlock *BB)
 Return true if the specified values are defined in a different basic block than BB.
static BasicBlock::iterator findInsertPos (Value *Addr, Instruction *MemoryInst, Value *SunkAddr)
static bool matchOverflowPattern (Instruction *&I, ExtractValueInst *&MulExtract, ExtractValueInst *&OverflowExtract)
static bool hasSameExtUse (Value *Val, const TargetLowering &TLI)
 Check if all the uses of Val are equivalent (or free) zero or sign extensions.
static bool sinkSelectOperand (const TargetTransformInfo *TTI, Value *V)
 Check if V (an operand of a select instruction) is an expensive instruction that is only used once.
static bool isFormingBranchFromSelectProfitable (const TargetTransformInfo *TTI, const TargetLowering *TLI, SelectInst *SI)
 Returns true if a SelectInst should be turned into an explicit branch.
static Value * getTrueOrFalseValue (SelectInst *SI, bool isTrue, const SmallPtrSet< const Instruction *, 2 > &Selects)
 If isTrue is true, return the true value of SI, otherwise return false value of SI.
static bool splitMergedValStore (StoreInst &SI, const DataLayout &DL, const TargetLowering &TLI, bool ForceSplitStore)
 For the instruction sequence of store below, F and I values are bundled together as an i64 value before being stored into memory.
static bool GEPSequentialConstIndexed (GetElementPtrInst *GEP)
static bool tryUnmergingGEPsAcrossIndirectBr (GetElementPtrInst *GEPI, const TargetTransformInfo *TTI)
static bool optimizeBranch (CondBrInst *Branch, const TargetLowering &TLI, SmallPtrSet< BasicBlock *, 32 > &FreshBBs, bool IsHugeFunc)
static void DbgInserterHelper (DbgVariableRecord *DVR, BasicBlock::iterator VI)

Variables

 DEBUG_TYPE
Optimize for code generation
Optimize for code false

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "codegenprepare"

Definition at line 114 of file CodeGenPrepare.cpp.

Function Documentation

◆ computeBaseDerivedRelocateMap()

◆ DbgInserterHelper()

◆ despeculateCountZeros()

bool despeculateCountZeros ( IntrinsicInst * CountZeros,
DomTreeUpdater * DTU,
LoopInfo * LI,
const TargetLowering * TLI,
const DataLayout * DL,
ModifyDT & ModifiedDT,
SmallPtrSet< BasicBlock *, 32 > & FreshBBs,
bool IsHugeFunc )
static

If counting leading or trailing zeros is an expensive operation and a zero input is defined, add a check for zero to avoid calling the intrinsic.

We want to transform: z = call i64 @llvm.cttz.i64(i64 A, i1 false)

into: entry: cmpz = icmp eq i64 A, 0 br i1 cmpz, label cond.end, label cond.false cond.false: z = call i64 @llvm.cttz.i64(i64 A, i1 true) br label cond.end cond.end: ctz = phi i64 [ 64, entry ], [ z, cond.false ]

If the transform is performed, return true and set ModifiedDT to true.

Definition at line 2489 of file CodeGenPrepare.cpp.

References llvm::PHINode::addIncoming(), llvm::GenericDomTreeUpdater< DerivedT, DomTreeT, PostDomTreeT >::applyUpdates(), llvm::BasicBlock::begin(), llvm::BitWidth, DL, llvm::Instruction::eraseFromParent(), llvm::Instruction::getDebugLoc(), llvm::IntrinsicInst::getIntrinsicID(), llvm::Constant::getNullValue(), llvm::User::getOperand(), llvm::User::getOperandUse(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), llvm::DominatorTreeBase< BasicBlock, false >::Insert, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::TargetLoweringBase::isCheapToSpeculateCtlz(), llvm::TargetLoweringBase::isCheapToSpeculateCttz(), llvm::isGuaranteedNotToBeUndefOrPoison(), llvm::isKnownNonZero(), llvm::PatternMatch::m_One(), llvm::PatternMatch::match(), replaceAllUsesWith(), llvm::CallBase::setArgOperand(), and llvm::SplitBlock().

◆ FindAllMemoryUses() [1/2]

bool FindAllMemoryUses ( Instruction * I,
SmallVectorImpl< std::pair< Use *, Type * > > & MemoryUses,
const TargetLowering & TLI,
const TargetRegisterInfo & TRI,
bool OptSize,
ProfileSummaryInfo * PSI,
BlockFrequencyInfo * BFI,
unsigned MaxUsersToScan )
static

Definition at line 5546 of file CodeGenPrepare.cpp.

References FindAllMemoryUses(), I, and TRI.

◆ FindAllMemoryUses() [2/2]

bool FindAllMemoryUses ( Instruction * I,
SmallVectorImpl< std::pair< Use *, Type * > > & MemoryUses,
SmallPtrSetImpl< Instruction * > & ConsideredInsts,
const TargetLowering & TLI,
const TargetRegisterInfo & TRI,
bool OptSize,
ProfileSummaryInfo * PSI,
BlockFrequencyInfo * BFI,
unsigned & SeenInsts,
unsigned MaxUsersToScan )
static

◆ findInsertPos()

◆ foldFCmpToFPClassTest()

◆ foldICmpWithDominatingICmp()

bool foldICmpWithDominatingICmp ( CmpInst * Cmp,
const TargetLowering & TLI,
bool EnableICmpEqToICmpSt )
static

For pattern like:

DomCond = icmp sgt/slt CmpOp0, CmpOp1 (might not be in DomBB) ... DomBB: ... br DomCond, TrueBB, CmpBB CmpBB: (with DomBB being the single predecessor) ... Cmp = icmp eq CmpOp0, CmpOp1 ...

It would use two comparison on targets that lowering of icmp sgt/slt is different from lowering of icmp eq (PowerPC). This function try to convert 'Cmp = icmp eq CmpOp0, CmpOp1' to ' Cmp = icmp slt/sgt CmpOp0, CmpOp1'. After that, DomCond and Cmp can use the same comparison so reduce one comparison.

Return true if any changes are made.

Definition at line 1878 of file CodeGenPrepare.cpp.

References llvm::cast(), llvm::dyn_cast(), getCondition(), llvm::BasicBlock::getSinglePredecessor(), llvm::CmpInst::getSwappedPredicate(), llvm::BasicBlock::getTerminator(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLT, llvm::isa(), llvm::TargetLoweringBase::isEqualityCmpFoldedWithSignedCmp(), llvm_unreachable, llvm::PatternMatch::m_Br(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().

◆ foldURemOfLoopIncrement()

◆ GEPSequentialConstIndexed()

bool GEPSequentialConstIndexed ( GetElementPtrInst * GEP)
static

Definition at line 8551 of file CodeGenPrepare.cpp.

References GEP, llvm::gep_type_begin(), I, and llvm::isa().

Referenced by tryUnmergingGEPsAcrossIndirectBr().

◆ getGEPSmallConstantIntOffsetV()

bool getGEPSmallConstantIntOffsetV ( GetElementPtrInst * GEP,
SmallVectorImpl< Value * > & OffsetV )
static

◆ getIVIncrement()

std::optional< std::pair< Instruction *, Constant * > > getIVIncrement ( const PHINode * PN,
const LoopInfo * LI )
static

If given PN is an inductive variable with value IVInc coming from the backedge, and on each iteration it gets increased by Step, return pair <IVInc, Step>.

Otherwise, return std::nullopt.

Definition at line 1467 of file CodeGenPrepare.cpp.

References llvm::dyn_cast(), llvm::PHINode::getIncomingValueForBlock(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), LHS, and matchIncrement().

Referenced by isIVIncrement(), and isRemOfLoopIncrementWithLoopInvariant().

◆ getTrueOrFalseValue()

Value * getTrueOrFalseValue ( SelectInst * SI,
bool isTrue,
const SmallPtrSet< const Instruction *, 2 > & Selects )
static

If isTrue is true, return the true value of SI, otherwise return false value of SI.

If the true/false value of SI is defined by any select instructions in Selects, look through the defining select instruction until the true/false value is not defined in Selects.

Definition at line 7526 of file CodeGenPrepare.cpp.

References assert(), llvm::SmallPtrSetImpl< PtrType >::count(), and llvm::dyn_cast().

◆ hasSameExtUse()

bool hasSameExtUse ( Value * Val,
const TargetLowering & TLI )
static

◆ INITIALIZE_PASS_BEGIN()

INITIALIZE_PASS_BEGIN ( CodeGenPrepareLegacyPass ,
DEBUG_TYPE ,
"Optimize for code generation" ,
false ,
false  )

◆ isExtractBitsCandidateUse()

bool isExtractBitsCandidateUse ( Instruction * User)
static

Check if the candidates could be combined with a shift instruction, which includes:

  1. Truncate instruction
  2. And instruction and the imm is a mask of the low bits: imm & (imm+1) == 0

Definition at line 2277 of file CodeGenPrepare.cpp.

References llvm::cast(), llvm::User::getOperand(), and llvm::isa().

Referenced by OptimizeExtractBits().

◆ isFormingBranchFromSelectProfitable()

bool isFormingBranchFromSelectProfitable ( const TargetTransformInfo * TTI,
const TargetLowering * TLI,
SelectInst * SI )
static

◆ isIntrinsicOrLFToBeTailCalled()

◆ isIVIncrement()

bool isIVIncrement ( const Value * V,
const LoopInfo * LI )
static

Definition at line 1482 of file CodeGenPrepare.cpp.

References llvm::dyn_cast(), getIVIncrement(), I, LHS, and matchIncrement().

◆ IsNonLocalValue()

bool IsNonLocalValue ( Value * V,
BasicBlock * BB )
static

Return true if the specified values are defined in a different basic block than BB.

Definition at line 5692 of file CodeGenPrepare.cpp.

References llvm::dyn_cast(), and I.

◆ IsOperandAMemoryOperand()

bool IsOperandAMemoryOperand ( CallInst * CI,
InlineAsm * IA,
Value * OpVal,
const TargetLowering & TLI,
const TargetRegisterInfo & TRI )
static

Check to see if all uses of OpVal by the specified inline asm call are due to memory operands.

If so, return true, otherwise return false.

Definition at line 5435 of file CodeGenPrepare.cpp.

References llvm::TargetLowering::C_Memory, llvm::TargetLowering::ComputeConstraintToUse(), F, llvm::Instruction::getFunction(), llvm::TargetLowering::ParseConstraints(), and TRI.

Referenced by FindAllMemoryUses().

◆ isPromotedInstructionLegal()

bool isPromotedInstructionLegal ( const TargetLowering & TLI,
const DataLayout & DL,
Value * Val )
static

Check whether or not Val is a legal instruction for TLI.

Note
Val is assumed to be the product of some type promotion. Therefore if Val has an undefined state in TLI, this is assumed to be legal, as the non-promoted value would have had the same state.

Definition at line 4600 of file CodeGenPrepare.cpp.

References DL, llvm::dyn_cast(), llvm::Instruction::getOpcode(), llvm::Value::getType(), llvm::TargetLoweringBase::getValueType(), llvm::TargetLoweringBase::InstructionOpcodeToISD(), and llvm::TargetLoweringBase::isOperationLegalOrCustom().

◆ isRemOfLoopIncrementWithLoopInvariant()

◆ matchIncrement()

◆ matchOverflowPattern()

bool matchOverflowPattern ( Instruction *& I,
ExtractValueInst *& MulExtract,
ExtractValueInst *& OverflowExtract )
static

Definition at line 6313 of file CodeGenPrepare.cpp.

References llvm::dyn_cast(), and I.

◆ matchUAddWithOverflowConstantEdgeCases()

◆ MightBeFoldableInst()

bool MightBeFoldableInst ( Instruction * I)
static

This is a little filter, which returns true if an addressing computation involving I might be folded into a load/store accessing it.

This doesn't need to be perfect, but needs to accept at least the set of instructions that MatchOperationAddr can.

Definition at line 4569 of file CodeGenPrepare.cpp.

References I, and llvm::isa().

Referenced by FindAllMemoryUses().

◆ optimizeBitCast()

◆ optimizeBranch()

◆ OptimizeExtractBits()

bool OptimizeExtractBits ( BinaryOperator * ShiftI,
ConstantInt * CI,
const TargetLowering & TLI,
const DataLayout & DL )
static

Sink the shift right instruction into user blocks if the uses could potentially be combined with this shift instruction and generate BitExtract instruction.

It will only be applied if the architecture supports BitExtract instruction. Here is an example: BB1: x.extract.shift = lshr i64 arg1, 32 BB2: x.extract.trunc = trunc i64 x.extract.shift to i16 ==>

BB2: x.extract.shift.1 = lshr i64 arg1, 32 x.extract.trunc = trunc i64 x.extract.shift.1 to i16

CodeGen will recognize the pattern in BB2 and generate BitExtract instruction. Return true if any changes are made.

Only insert instructions in each block once.

Definition at line 2386 of file CodeGenPrepare.cpp.

References assert(), llvm::cast(), DL, E(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::Instruction::getDebugLoc(), llvm::BasicBlock::getFirstInsertionPt(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::Value::getType(), llvm::TargetLoweringBase::getValueType(), llvm::Instruction::insertBefore(), llvm::isa(), isExtractBitsCandidateUse(), llvm::TargetLoweringBase::isTypeLegal(), llvm::salvageDebugInfo(), llvm::Instruction::setDebugLoc(), SinkShiftAndTruncate(), llvm::Value::use_empty(), llvm::Instruction::user_begin(), and llvm::Instruction::user_end().

◆ OptimizeNoopCopyExpression()

bool OptimizeNoopCopyExpression ( CastInst * CI,
const TargetLowering & TLI,
const DataLayout & DL )
static

If the specified cast instruction is a noop copy (e.g.

it's casting from one pointer type to another, i32->i8 on PPC), sink it into user blocks to reduce the number of virtual registers that must be created and coalesced.

Return true if any changes are made.

Definition at line 1405 of file CodeGenPrepare.cpp.

References llvm::EVT::bitsLT(), DL, llvm::dyn_cast(), llvm::Value::getContext(), llvm::User::getOperand(), llvm::Value::getType(), llvm::TargetLoweringBase::getTypeAction(), llvm::TargetLoweringBase::getTypeToTransformTo(), llvm::TargetLoweringBase::getValueType(), llvm::TargetLoweringBase::isFreeAddrSpaceCast(), llvm::EVT::isInteger(), SinkCast(), and llvm::TargetLoweringBase::TypePromoteInteger.

◆ replaceAllUsesWith()

void replaceAllUsesWith ( Value * Old,
Value * New,
SmallPtrSet< BasicBlock *, 32 > & FreshBBs,
bool IsHuge )
static

◆ simplifyRelocatesOffABase()

◆ sinkAndCmp0Expression()

◆ SinkCast()

◆ sinkCmpExpression()

bool sinkCmpExpression ( CmpInst * Cmp,
const TargetLowering & TLI,
const DataLayout & DL )
static

Sink the given CmpInst into user blocks to reduce the number of virtual registers that must be created and coalesced.

This is a clear win except on targets with multiple condition code registers (PowerPC), where it might lose; some adjustment may be wanted there.

Return true if any changes are made.

Definition at line 1786 of file CodeGenPrepare.cpp.

References llvm::any_of(), assert(), llvm::cast(), llvm::CmpInst::Create(), DL, E(), llvm::BasicBlock::end(), llvm::EVT::getEVT(), llvm::BasicBlock::getFirstInsertionPt(), llvm::TargetLoweringBase::hasMultipleConditionRegisters(), llvm::Instruction::insertBefore(), llvm::isa(), llvm::Instruction::setDebugLoc(), and llvm::TargetLoweringBase::useSoftFloat().

◆ sinkSelectOperand()

bool sinkSelectOperand ( const TargetTransformInfo * TTI,
Value * V )
static

Check if V (an operand of a select instruction) is an expensive instruction that is only used once.

Definition at line 7472 of file CodeGenPrepare.cpp.

References llvm::dyn_cast(), I, and llvm::isSafeToSpeculativelyExecute().

Referenced by isFormingBranchFromSelectProfitable().

◆ SinkShiftAndTruncate()

◆ splitMergedValStore()

bool splitMergedValStore ( StoreInst & SI,
const DataLayout & DL,
const TargetLowering & TLI,
bool ForceSplitStore )
static

For the instruction sequence of store below, F and I values are bundled together as an i64 value before being stored into memory.

Sometimes it is more efficient to generate separate stores for F and I, which can remove the bitwise instructions or sink them to colder places.

(store (or (zext (bitcast F to i32) to i64), (shl (zext I to i64), 32)), addr) --> (store F, addr) and (store I, addr+4)

Similarly, splitting for other merged store can also be beneficial, like: For pair of {i32, i32}, i64 store --> two i32 stores. For pair of {i32, i16}, i64 store --> two i32 stores. For pair of {i16, i16}, i32 store --> two i16 stores. For pair of {i16, i8}, i32 store --> two i16 stores. For pair of {i8, i8}, i16 store --> two i8 stores.

We allow each target to determine specifically which kind of splitting is supported.

The store patterns are commonly seen from the simple code snippet below if only std::make_pair(...) is sroa transformed before inlined into hoo. void goo(const std::pair<int, float> &); hoo() { ... goo(std::make_pair(tmp, ftmp)); ... }

Although we already have similar splitting in DAG Combine, we duplicate it in CodeGenPrepare to catch the case in which pattern is across multiple BBs. The logic in DAG Combine is kept to catch case generated during code expansion.

Definition at line 8453 of file CodeGenPrepare.cpp.

References llvm::commonAlignment(), DL, llvm::dyn_cast(), llvm::EVT::getEVT(), llvm::Type::getInt32Ty(), llvm::Type::getIntNTy(), llvm::Value::getType(), llvm::Type::isIntegerTy(), llvm::TargetLoweringBase::isMultiStoresCheaperThanBitsMerge(), llvm::Type::isScalableTy(), LValue, llvm::PatternMatch::m_c_Or(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_SpecificInt(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_ZExt(), llvm::PatternMatch::match(), and llvm::Upper.

◆ STATISTIC() [1/16]

STATISTIC ( NumAndsAdded ,
"Number of and mask instructions added to form ext loads"  )

◆ STATISTIC() [2/16]

STATISTIC ( NumAndUses ,
"Number of uses of and mask instructions optimized"  )

◆ STATISTIC() [3/16]

STATISTIC ( NumBlocksElim ,
"Number of blocks eliminated"  )

◆ STATISTIC() [4/16]

STATISTIC ( NumCastUses ,
"Number of uses of Cast expressions replaced with uses " "of sunken Casts"  )

◆ STATISTIC() [5/16]

STATISTIC ( NumCmpUses ,
"Number of uses of Cmp expressions replaced with uses of " "sunken Cmps"  )

◆ STATISTIC() [6/16]

STATISTIC ( NumDbgValueMoved ,
"Number of debug value instructions moved"  )

◆ STATISTIC() [7/16]

STATISTIC ( NumExtsMoved ,
"Number of ext instructions combined with loads" [s|z] )

◆ STATISTIC() [8/16]

STATISTIC ( NumExtUses ,
"Number of uses of ext instructions optimized" [s|z] )

◆ STATISTIC() [9/16]

STATISTIC ( NumGEPsElim ,
"Number of GEPs converted to casts"  )

◆ STATISTIC() [10/16]

STATISTIC ( NumMemoryInsts ,
"Number of memory instructions whose address " "computations were sunk"  )

◆ STATISTIC() [11/16]

STATISTIC ( NumMemoryInstsPhiCreated ,
"Number of phis created when address " "computations were sunk to memory instructions"  )

◆ STATISTIC() [12/16]

STATISTIC ( NumMemoryInstsSelectCreated ,
"Number of select created when address " "computations were sunk to memory instructions"  )

◆ STATISTIC() [13/16]

STATISTIC ( NumPHIsElim ,
"Number of trivial PHIs eliminated"  )

◆ STATISTIC() [14/16]

STATISTIC ( NumRetsDup ,
"Number of return instructions duplicated"  )

◆ STATISTIC() [15/16]

STATISTIC ( NumSelectsExpanded ,
"Number of selects turned into branches"  )

◆ STATISTIC() [16/16]

STATISTIC ( NumStoreExtractExposed ,
"Number of store(extractelement) exposed"  )

◆ swapICmpOperandsToExposeCSEOpportunities()

bool swapICmpOperandsToExposeCSEOpportunities ( CmpInst * Cmp)
static

Many architectures use the same instruction for both subtract and cmp.

Try to swap cmp operands to match subtract operations to allow for CSE.

Definition at line 1947 of file CodeGenPrepare.cpp.

References llvm::Value::getType(), llvm::isa(), llvm::Type::isIntegerTy(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Sub(), llvm::PatternMatch::match(), and llvm::Value::users().

◆ tryUnmergingGEPsAcrossIndirectBr()

Variable Documentation

◆ DEBUG_TYPE

DEBUG_TYPE

Definition at line 402 of file CodeGenPrepare.cpp.

◆ false

Optimize for code false

Definition at line 403 of file CodeGenPrepare.cpp.

◆ generation

Optimize for code generation

Definition at line 403 of file CodeGenPrepare.cpp.

Referenced by StackNode::childGeneration().