LLVM  5.0.0
Classes | Macros | Functions | Variables
CodeGenPrepare.cpp File Reference
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/CodeGen/Analysis.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/Statepoint.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/ValueMap.h"
#include "llvm/Pass.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/TargetSubtargetInfo.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/BuildLibCalls.h"
#include "llvm/Transforms/Utils/BypassSlowDivision.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
Include dependency graph for CodeGenPrepare.cpp:

Go to the source code of this file.

Classes

class  MemCmpExpansion
 

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 (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")
 
 STATISTIC (NumMemCmpCalls,"Number of memcmp calls")
 
 STATISTIC (NumMemCmpNotConstant,"Number of memcmp calls without constant size")
 
 STATISTIC (NumMemCmpGreaterThanMax,"Number of memcmp calls with size greater than max size")
 
 STATISTIC (NumMemCmpInlined,"Number of inlined memcmp calls")
 
 INITIALIZE_PASS_BEGIN (CodeGenPrepare, DEBUG_TYPE,"Optimize for code generation", false, false) INITIALIZE_PASS_END(CodeGenPrepare
 
static BasicBlock * findIBRPredecessor (BasicBlock *BB, SmallVectorImpl< BasicBlock * > &OtherPreds)
 
static void computeBaseDerivedRelocateMap (const SmallVectorImpl< GCRelocateInst * > &AllRelocateCalls, DenseMap< GCRelocateInst *, SmallVector< GCRelocateInst *, 2 >> &RelocateInstMap)
 
static bool getGEPSmallConstantIntOffsetV (GetElementPtrInst *GEP, SmallVectorImpl< Value * > &OffsetV)
 
static bool simplifyRelocatesOffABase (GCRelocateInst *RelocatedBase, const SmallVectorImpl< GCRelocateInst * > &Targets)
 
static bool SinkCast (CastInst *CI)
 SinkCast - Sink the specified cast instruction into its user blocks. More...
 
static bool OptimizeNoopCopyExpression (CastInst *CI, const TargetLowering &TLI, const DataLayout &DL)
 If the specified cast instruction is a noop copy (e.g. More...
 
static bool CombineUAddWithOverflow (CmpInst *CI)
 Try to combine CI into a call to the llvm.uadd.with.overflow intrinsic if possible. More...
 
static bool SinkCmpExpression (CmpInst *CI, const TargetLowering *TLI)
 Sink the given CmpInst into user blocks to reduce the number of virtual registers that must be created and coalesced. More...
 
static bool OptimizeCmpExpression (CmpInst *CI, const TargetLowering *TLI)
 
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. More...
 
static bool isExtractBitsCandidateUse (Instruction *User)
 Check if the candidates could be combined with a shift instruction, which includes: More...
 
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. More...
 
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. More...
 
static bool despeculateCountZeros (IntrinsicInst *CountZeros, const TargetLowering *TLI, const DataLayout *DL, bool &ModifiedDT)
 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. More...
 
static bool expandMemCmp (CallInst *CI, const TargetTransformInfo *TTI, const TargetLowering *TLI, const DataLayout *DL)
 We want to transform: call = call signext i32 (i8* %0, i8* %1, i64 15) To: loadbb: %0 = bitcast i32* buffer2 to i8* %1 = bitcast i32* buffer1 to i8* %2 = bitcast i8* %1 to i64* %3 = bitcast i8* %0 to i64* %4 = load i64, i64* %2 %5 = load i64, i64* %3 %6 = call i64 .bswap.i64(i64 %4) %7 = call i64 .bswap.i64(i64 %5) %8 = sub i64 %6, %7 %9 = icmp ne i64 %8, 0 br i1 %9, label res_block, label loadbb1 res_block: ; preds = loadbb2, loadbb1, loadbb phi.src1 = phi i64 [ %6, loadbb ], [ %22, loadbb1 ], [ %36, loadbb2 ] phi.src2 = phi i64 [ %7, loadbb ], [ %23, loadbb1 ], [ %37, loadbb2 ] %10 = icmp ult i64 phi.src1, phi.src2 %11 = select i1 %10, i32 -1, i32 1 br label endblock loadbb1: ; preds = loadbb %12 = bitcast i32* buffer2 to i8* %13 = bitcast i32* buffer1 to i8* %14 = bitcast i8* %13 to i32* %15 = bitcast i8* %12 to i32* %16 = getelementptr i32, i32* %14, i32 2 %17 = getelementptr i32, i32* %15, i32 2 %18 = load i32, i32* %16 %19 = load i32, i32* %17 %20 = call i32 .bswap.i32(i32 %18) %21 = call i32 .bswap.i32(i32 %19) %22 = zext i32 %20 to i64 %23 = zext i32 %21 to i64 %24 = sub i64 %22, %23 %25 = icmp ne i64 %24, 0 br i1 %25, label res_block, label loadbb2 loadbb2: ; preds = loadbb1 %26 = bitcast i32* buffer2 to i8* %27 = bitcast i32* buffer1 to i8* %28 = bitcast i8* %27 to i16* %29 = bitcast i8* %26 to i16* %30 = getelementptr i16, i16* %28, i16 6 %31 = getelementptr i16, i16* %29, i16 6 %32 = load i16, i16* %30 %33 = load i16, i16* %31 %34 = call i16 .bswap.i16(i16 %32) %35 = call i16 .bswap.i16(i16 %33) %36 = zext i16 %34 to i64 %37 = zext i16 %35 to i64 %38 = sub i64 %36, %37 %39 = icmp ne i64 %38, 0 br i1 %39, label res_block, label loadbb3 loadbb3: ; preds = loadbb2 %40 = bitcast i32* buffer2 to i8* %41 = bitcast i32* buffer1 to i8* %42 = getelementptr i8, i8* %41, i8 14 %43 = getelementptr i8, i8* %40, i8 14 %44 = load i8, i8* %42 %45 = load i8, i8* %43 %46 = zext i8 %44 to i32 %47 = zext i8 %45 to i32 %48 = sub i32 %46, %47 br label endblock endblock: ; preds = res_block, loadbb3 phi.res = phi i32 [ %48, loadbb3 ], [ %11, res_block ] ret i32 phi.res. More...
 
static bool IsNonLocalValue (Value *V, BasicBlock *BB)
 Return true if the specified values are defined in a different basic block than BB. More...
 
static bool hasSameExtUse (Value *Val, const TargetLowering &TLI)
 Check if all the uses of Val are equivalent (or free) zero or sign extensions. More...
 
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. More...
 
static bool isFormingBranchFromSelectProfitable (const TargetTransformInfo *TTI, const TargetLowering *TLI, SelectInst *SI)
 Returns true if a SelectInst should be turned into an explicit branch. More...
 
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. More...
 
static bool isBroadcastShuffle (ShuffleVectorInst *SVI)
 
static bool splitMergedValStore (StoreInst &SI, const DataLayout &DL, const TargetLowering &TLI)
 For the instruction sequence of store below, F and I values are bundled together as an i64 value before being stored into memory. More...
 
static bool makeBitReverse (Instruction &I, const DataLayout &DL, const TargetLowering &TLI)
 Given an OR instruction, check to see if this is a bitreverse idiom. More...
 
static void scaleWeights (uint64_t &NewTrue, uint64_t &NewFalse)
 Scale down both weights to fit into uint32_t. More...
 

Variables

static cl::opt< bool > DisableBranchOpts ("disable-cgp-branch-opts", cl::Hidden, cl::init(false), cl::desc("Disable branch optimizations in CodeGenPrepare"))
 
static cl::opt< bool > DisableGCOpts ("disable-cgp-gc-opts", cl::Hidden, cl::init(false), cl::desc("Disable GC optimizations in CodeGenPrepare"))
 
static cl::opt< bool > DisableSelectToBranch ("disable-cgp-select2branch", cl::Hidden, cl::init(false), cl::desc("Disable select to branch conversion."))
 
static cl::opt< bool > AddrSinkUsingGEPs ("addr-sink-using-gep", cl::Hidden, cl::init(true), cl::desc("Address sinking in CGP using GEPs."))
 
static cl::opt< bool > EnableAndCmpSinking ("enable-andcmp-sinking", cl::Hidden, cl::init(true), cl::desc("Enable sinkinig and/cmp into branches."))
 
static cl::opt< bool > DisableStoreExtract ("disable-cgp-store-extract", cl::Hidden, cl::init(false), cl::desc("Disable store(extract) optimizations in CodeGenPrepare"))
 
static cl::opt< bool > StressStoreExtract ("stress-cgp-store-extract", cl::Hidden, cl::init(false), cl::desc("Stress test store(extract) optimizations in CodeGenPrepare"))
 
static cl::opt< bool > DisableExtLdPromotion ("disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in ""CodeGenPrepare"))
 
static cl::opt< bool > StressExtLdPromotion ("stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) ""optimization in CodeGenPrepare"))
 
static cl::opt< bool > DisablePreheaderProtect ("disable-preheader-prot", cl::Hidden, cl::init(false), cl::desc("Disable protection against removing loop preheaders"))
 
static cl::opt< bool > ProfileGuidedSectionPrefix ("profile-guided-section-prefix", cl::Hidden, cl::init(true), cl::ZeroOrMore, cl::desc("Use profile info to add section prefix for hot/cold functions"))
 
static cl::opt< unsigned > FreqRatioToSkipMerge ("cgp-freq-ratio-to-skip-merge", cl::Hidden, cl::init(2), cl::desc("Skip merging empty blocks if (frequency of empty block) / ""(frequency of destination block) is greater than this ratio"))
 
static cl::opt< bool > ForceSplitStore ("force-split-store", cl::Hidden, cl::init(false), cl::desc("Force store splitting no matter what the target query says."))
 
static cl::opt< bool > EnableTypePromotionMerge ("cgp-type-promotion-merge", cl::Hidden, cl::desc("Enable merging of redundant sexts when one is dominating"" the other."), cl::init(true))
 
static cl::opt< unsigned > MemCmpNumLoadsPerBlock ("memcmp-num-loads-per-block", cl::Hidden, cl::init(1), cl::desc("The number of loads per basic block for inline expansion of ""memcmp that is only being compared against zero."))
 
 DEBUG_TYPE
 
Optimize for code generation
 
Optimize for code false
 

Macro Definition Documentation

#define DEBUG_TYPE   "codegenprepare"

Definition at line 67 of file CodeGenPrepare.cpp.

Function Documentation

static bool CombineUAddWithOverflow ( CmpInst *  CI)
static
static void computeBaseDerivedRelocateMap ( const SmallVectorImpl< GCRelocateInst * > &  AllRelocateCalls,
DenseMap< GCRelocateInst *, SmallVector< GCRelocateInst *, 2 >> &  RelocateInstMap 
)
static
static bool despeculateCountZeros ( IntrinsicInst *  CountZeros,
const TargetLowering *  TLI,
const DataLayout *  DL,
bool &  ModifiedDT 
)
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 .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 .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 1582 of file CodeGenPrepare.cpp.

References llvm::PHINode::addIncoming(), llvm::Instruction::eraseFromParent(), llvm::BasicBlock::front(), llvm::Value::getContext(), llvm::Instruction::getDebugLoc(), llvm::IntrinsicInst::getIntrinsicID(), llvm::DataLayout::getLargestLegalIntTypeSizeInBits(), llvm::Constant::getNullValue(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::Type::getPrimitiveSizeInBits(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), llvm::TargetLoweringBase::isCheapToSpeculateCtlz(), llvm::TargetLoweringBase::isCheapToSpeculateCttz(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_One(), llvm::PatternMatch::match(), llvm::Value::replaceAllUsesWith(), llvm::CallInst::setArgOperand(), llvm::IRBuilderBase::SetInsertPoint(), llvm::BasicBlock::splitBasicBlock(), and Zero.

static bool expandMemCmp ( CallInst *  CI,
const TargetTransformInfo *  TTI,
const TargetLowering *  TLI,
const DataLayout *  DL 
)
static

We want to transform: call = call signext i32 (i8* %0, i8* %1, i64 15) To: loadbb: %0 = bitcast i32* buffer2 to i8* %1 = bitcast i32* buffer1 to i8* %2 = bitcast i8* %1 to i64* %3 = bitcast i8* %0 to i64* %4 = load i64, i64* %2 %5 = load i64, i64* %3 %6 = call i64 .bswap.i64(i64 %4) %7 = call i64 .bswap.i64(i64 %5) %8 = sub i64 %6, %7 %9 = icmp ne i64 %8, 0 br i1 %9, label res_block, label loadbb1 res_block: ; preds = loadbb2, loadbb1, loadbb phi.src1 = phi i64 [ %6, loadbb ], [ %22, loadbb1 ], [ %36, loadbb2 ] phi.src2 = phi i64 [ %7, loadbb ], [ %23, loadbb1 ], [ %37, loadbb2 ] %10 = icmp ult i64 phi.src1, phi.src2 %11 = select i1 %10, i32 -1, i32 1 br label endblock loadbb1: ; preds = loadbb %12 = bitcast i32* buffer2 to i8* %13 = bitcast i32* buffer1 to i8* %14 = bitcast i8* %13 to i32* %15 = bitcast i8* %12 to i32* %16 = getelementptr i32, i32* %14, i32 2 %17 = getelementptr i32, i32* %15, i32 2 %18 = load i32, i32* %16 %19 = load i32, i32* %17 %20 = call i32 .bswap.i32(i32 %18) %21 = call i32 .bswap.i32(i32 %19) %22 = zext i32 %20 to i64 %23 = zext i32 %21 to i64 %24 = sub i64 %22, %23 %25 = icmp ne i64 %24, 0 br i1 %25, label res_block, label loadbb2 loadbb2: ; preds = loadbb1 %26 = bitcast i32* buffer2 to i8* %27 = bitcast i32* buffer1 to i8* %28 = bitcast i8* %27 to i16* %29 = bitcast i8* %26 to i16* %30 = getelementptr i16, i16* %28, i16 6 %31 = getelementptr i16, i16* %29, i16 6 %32 = load i16, i16* %30 %33 = load i16, i16* %31 %34 = call i16 .bswap.i16(i16 %32) %35 = call i16 .bswap.i16(i16 %33) %36 = zext i16 %34 to i64 %37 = zext i16 %35 to i64 %38 = sub i64 %36, %37 %39 = icmp ne i64 %38, 0 br i1 %39, label res_block, label loadbb3 loadbb3: ; preds = loadbb2 %40 = bitcast i32* buffer2 to i8* %41 = bitcast i32* buffer1 to i8* %42 = getelementptr i8, i8* %41, i8 14 %43 = getelementptr i8, i8* %40, i8 14 %44 = load i8, i8* %42 %45 = load i8, i8* %43 %46 = zext i8 %44 to i32 %47 = zext i8 %45 to i32 %48 = sub i32 %46, %47 br label endblock endblock: ; preds = res_block, loadbb3 phi.res = phi i32 [ %48, loadbb3 ], [ %11, res_block ] ret i32 phi.res.

Definition at line 2241 of file CodeGenPrepare.cpp.

References llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::TargetTransformInfo::expandMemCmp(), llvm::CallInst::getArgOperand(), llvm::Instruction::getFunction(), llvm::TargetLoweringBase::getMaxExpandSizeMemcmp(), MemCmpExpansion::getMemCmpExpansion(), llvm::ConstantInt::getZExtValue(), MemCmpNumLoadsPerBlock, llvm::Function::optForMinSize(), llvm::Function::optForSize(), and llvm::Value::replaceAllUsesWith().

static BasicBlock* findIBRPredecessor ( BasicBlock *  BB,
SmallVectorImpl< BasicBlock * > &  OtherPreds 
)
static
static bool getGEPSmallConstantIntOffsetV ( GetElementPtrInst *  GEP,
SmallVectorImpl< Value * > &  OffsetV 
)
static
static 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 5315 of file CodeGenPrepare.cpp.

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

static bool hasSameExtUse ( Value *  Val,
const TargetLowering &  TLI 
)
static
INITIALIZE_PASS_BEGIN ( CodeGenPrepare  ,
DEBUG_TYPE  ,
"Optimize for code generation"  ,
false  ,
false   
)
static bool isBroadcastShuffle ( ShuffleVectorInst *  SVI)
static
static 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 1382 of file CodeGenPrepare.cpp.

References llvm::Instruction::getOpcode(), and llvm::User::getOperand().

Referenced by OptimizeExtractBits().

static bool isFormingBranchFromSelectProfitable ( const TargetTransformInfo *  TTI,
const TargetLowering *  TLI,
SelectInst *  SI 
)
static
static 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 4241 of file CodeGenPrepare.cpp.

References llvm::Instruction::getParent().

static bool makeBitReverse ( Instruction &  I,
const DataLayout &  DL,
const TargetLowering &  TLI 
)
static
static bool OptimizeCmpExpression ( CmpInst *  CI,
const TargetLowering *  TLI 
)
static

Definition at line 1290 of file CodeGenPrepare.cpp.

References CombineUAddWithOverflow(), and SinkCmpExpression().

static 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 recoginze 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 1485 of file CodeGenPrepare.cpp.

References assert(), E, llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::BasicBlock::getFirstInsertionPt(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::Value::getType(), llvm::TargetLoweringBase::getValueType(), isExtractBitsCandidateUse(), llvm::TargetLoweringBase::isTypeLegal(), SinkShiftAndTruncate(), llvm::Value::use_empty(), llvm::Value::user_begin(), and llvm::Value::user_end().

static 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 1141 of file CodeGenPrepare.cpp.

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

static void scaleWeights ( uint64_t &  NewTrue,
uint64_t &  NewFalse 
)
static

Scale down both weights to fit into uint32_t.

Definition at line 6299 of file CodeGenPrepare.cpp.

static bool simplifyRelocatesOffABase ( GCRelocateInst *  RelocatedBase,
const SmallVectorImpl< GCRelocateInst * > &  Targets 
)
static
static bool sinkAndCmp0Expression ( Instruction *  AndI,
const TargetLowering &  TLI,
SetOfInstrs &  InsertedInsts 
)
static

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.

Return true if any changes are made.

Definition at line 1305 of file CodeGenPrepare.cpp.

References assert(), llvm::BinaryOperator::Create(), llvm::dbgs(), DEBUG, llvm::Value::dump(), llvm::dyn_cast(), E, llvm::Instruction::eraseFromParent(), llvm::Instruction::getDebugLoc(), llvm::User::getOperand(), llvm::Instruction::getParent(), getParent(), llvm::Value::hasOneUse(), llvm::TargetLoweringBase::isMaskAndCmp0FoldingBeneficial(), llvm::Instruction::setDebugLoc(), llvm::Value::user_begin(), llvm::Value::user_end(), and llvm::Value::users().

static bool SinkCast ( CastInst *  CI)
static
static bool SinkCmpExpression ( CmpInst *  CI,
const TargetLowering *  TLI 
)
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 1233 of file CodeGenPrepare.cpp.

References assert(), llvm::CmpInst::Create(), E, llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::Instruction::getDebugLoc(), llvm::BasicBlock::getFirstInsertionPt(), llvm::CmpInst::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::CmpInst::getPredicate(), llvm::Instruction::setDebugLoc(), llvm::Value::use_empty(), llvm::Value::user_begin(), llvm::Value::user_end(), and llvm::TargetLoweringBase::useSoftFloat().

Referenced by OptimizeCmpExpression().

static 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 5262 of file CodeGenPrepare.cpp.

References llvm::dyn_cast(), llvm::TargetTransformInfo::getUserCost(), llvm::Value::hasOneUse(), and llvm::isSafeToSpeculativelyExecute().

Referenced by isFormingBranchFromSelectProfitable().

static bool SinkShiftAndTruncate ( BinaryOperator *  ShiftI,
Instruction *  User,
ConstantInt *  CI,
DenseMap< BasicBlock *, BinaryOperator * > &  InsertedShifts,
const TargetLowering &  TLI,
const DataLayout &  DL 
)
static
static bool splitMergedValStore ( StoreInst &  SI,
const DataLayout &  DL,
const TargetLowering &  TLI 
)
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 efficent 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 5997 of file CodeGenPrepare.cpp.

References llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), ForceSplitStore, llvm::StoreInst::getAlignment(), llvm::Value::getContext(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::StoreInst::getPointerAddressSpace(), llvm::Type::getPointerTo(), llvm::Value::getType(), llvm::DataLayout::getTypeSizeInBits(), llvm::DataLayout::getTypeStoreSizeInBits(), llvm::StoreInst::getValueOperand(), llvm::Type::isIntegerTy(), llvm::TargetLoweringBase::isMultiStoresCheaperThanBitsMerge(), llvm::PatternMatch::m_c_Or(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_SpecificInt(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_ZExt(), llvm::PatternMatch::match(), llvm::IRBuilderBase::SetInsertPoint(), and llvm::Upper.

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 ( NumExtsMoved  ,
"Number of ext instructions combined with loads"  [s|z] 
)
STATISTIC ( NumExtUses  ,
"Number of uses of ext instructions optimized"  [s|z] 
)
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"   
)
STATISTIC ( NumMemCmpCalls  ,
"Number of memcmp calls"   
)
STATISTIC ( NumMemCmpNotConstant  ,
"Number of memcmp calls without constant size"   
)
STATISTIC ( NumMemCmpGreaterThanMax  ,
"Number of memcmp calls with size greater than max size"   
)
STATISTIC ( NumMemCmpInlined  ,
"Number of inlined memcmp calls"   
)

Variable Documentation

cl::opt<bool> AddrSinkUsingGEPs("addr-sink-using-gep", cl::Hidden, cl::init(true), cl::desc("Address sinking in CGP using GEPs."))
static
DEBUG_TYPE

Definition at line 275 of file CodeGenPrepare.cpp.

cl::opt<bool> DisableBranchOpts("disable-cgp-branch-opts", cl::Hidden, cl::init(false), cl::desc("Disable branch optimizations in CodeGenPrepare"))
static
cl::opt<bool> DisableExtLdPromotion("disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in ""CodeGenPrepare"))
static
cl::opt<bool> DisableGCOpts("disable-cgp-gc-opts", cl::Hidden, cl::init(false), cl::desc("Disable GC optimizations in CodeGenPrepare"))
static
cl::opt<bool> DisablePreheaderProtect("disable-preheader-prot", cl::Hidden, cl::init(false), cl::desc("Disable protection against removing loop preheaders"))
static
cl::opt<bool> DisableSelectToBranch("disable-cgp-select2branch", cl::Hidden, cl::init(false), cl::desc("Disable select to branch conversion."))
static
cl::opt<bool> DisableStoreExtract("disable-cgp-store-extract", cl::Hidden, cl::init(false), cl::desc("Disable store(extract) optimizations in CodeGenPrepare"))
static
cl::opt<bool> EnableAndCmpSinking("enable-andcmp-sinking", cl::Hidden, cl::init(true), cl::desc("Enable sinkinig and/cmp into branches."))
static
cl::opt<bool> EnableTypePromotionMerge("cgp-type-promotion-merge", cl::Hidden, cl::desc("Enable merging of redundant sexts when one is dominating"" the other."), cl::init(true))
static
Optimize for code false

Definition at line 275 of file CodeGenPrepare.cpp.

cl::opt<bool> ForceSplitStore("force-split-store", cl::Hidden, cl::init(false), cl::desc("Force store splitting no matter what the target query says."))
static

Referenced by splitMergedValStore().

cl::opt<unsigned> FreqRatioToSkipMerge("cgp-freq-ratio-to-skip-merge", cl::Hidden, cl::init(2), cl::desc("Skip merging empty blocks if (frequency of empty block) / ""(frequency of destination block) is greater than this ratio"))
static
Optimize for code generation

Definition at line 275 of file CodeGenPrepare.cpp.

cl::opt<unsigned> MemCmpNumLoadsPerBlock("memcmp-num-loads-per-block", cl::Hidden, cl::init(1), cl::desc("The number of loads per basic block for inline expansion of ""memcmp that is only being compared against zero."))
static

Referenced by expandMemCmp().

cl::opt<bool> ProfileGuidedSectionPrefix("profile-guided-section-prefix", cl::Hidden, cl::init(true), cl::ZeroOrMore, cl::desc("Use profile info to add section prefix for hot/cold functions"))
static
cl::opt<bool> StressExtLdPromotion("stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) ""optimization in CodeGenPrepare"))
static
cl::opt<bool> StressStoreExtract("stress-cgp-store-extract", cl::Hidden, cl::init(false), cl::desc("Stress test store(extract) optimizations in CodeGenPrepare"))
static