LLVM API Documentation

Defines | Enumerations | Functions | Variables
InstructionCombining.cpp File Reference
#include "llvm/Transforms/Scalar.h"
#include "InstCombine.h"
#include "llvm-c/Initialization.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Analysis/AssumptionTracker.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
#include <climits>
Include dependency graph for InstructionCombining.cpp:

Go to the source code of this file.


#define DEBUG_TYPE   "instcombine"


enum  Personality_Type { Unknown_Personality, GNU_Ada_Personality, GNU_CXX_Personality, GNU_ObjC_Personality }


 STATISTIC (NumCombined,"Number of insts combined")
 STATISTIC (NumConstProp,"Number of constant folds")
 STATISTIC (NumDeadInst,"Number of dead inst eliminated")
 STATISTIC (NumSunkInst,"Number of instructions sunk")
 STATISTIC (NumExpand,"Number of expansions")
 STATISTIC (NumFactor,"Number of factorizations")
 STATISTIC (NumReassoc,"Number of reassociations")
void LLVMInitializeInstCombine (LLVMPassRegistryRef R)
 INITIALIZE_PASS_BEGIN (InstCombiner,"instcombine","Combine redundant instructions", false, false) INITIALIZE_PASS_END(InstCombiner
static bool MaintainNoSignedWrap (BinaryOperator &I, Value *B, Value *C)
static void ClearSubclassDataAfterReassociation (BinaryOperator &I)
static bool LeftDistributesOverRight (Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
static bool RightDistributesOverLeft (Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
static ValuegetIdentityValue (Instruction::BinaryOps OpCode, Value *V)
static Instruction::BinaryOps getBinOpsForFactorization (Instruction::BinaryOps TopLevelOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS)
static ValuetryFactorization (InstCombiner::BuilderTy *Builder, const DataLayout *DL, BinaryOperator &I, Instruction::BinaryOps InnerOpcode, Value *A, Value *B, Value *C, Value *D)
static ValueFoldOperationIntoSelectOperand (Instruction &I, Value *SO, InstCombiner *IC)
static bool shouldMergeGEPs (GEPOperator &GEP, GEPOperator &Src)
static ValueCreateBinOpAsGiven (BinaryOperator &Inst, Value *LHS, Value *RHS, InstCombiner::BuilderTy *B)
 Creates node of binary operation with the same attributes as the specified one but with other operands.
static bool isAllocSiteRemovable (Instruction *AI, SmallVectorImpl< WeakVH > &Users, const TargetLibraryInfo *TLI)
static InstructiontryToMoveFreeBeforeNullTest (CallInst &FI)
 Move the call to free before a NULL test.
static Personality_Type RecognizePersonality (Value *Pers)
static bool isCatchAll (Personality_Type Personality, Constant *TypeInfo)
 isCatchAll - Return 'true' if the given typeinfo will match anything.
static bool shorter_filter (const Value *LHS, const Value *RHS)
static bool TryToSinkInstruction (Instruction *I, BasicBlock *DestBlock)
static bool AddReachableCodeToWorklist (BasicBlock *BB, SmallPtrSetImpl< BasicBlock * > &Visited, InstCombiner &IC, const DataLayout *DL, const TargetLibraryInfo *TLI)


Combine redundant instructions
Combine redundant false

Define Documentation

#define DEBUG_TYPE   "instcombine"

Definition at line 65 of file InstructionCombining.cpp.

Enumeration Type Documentation


Definition at line 2289 of file InstructionCombining.cpp.

Function Documentation

static bool AddReachableCodeToWorklist ( BasicBlock BB,
SmallPtrSetImpl< BasicBlock * > &  Visited,
InstCombiner IC,
const DataLayout DL,
const TargetLibraryInfo TLI 
) [static]

AddReachableCodeToWorklist - Walk the function in depth-first order, adding all reachable code to the worklist.

This has a couple of tricks to make the code faster and more powerful. In particular, we constant fold and DCE instructions as we go, to avoid adding them to the worklist (this significantly speeds up instcombine on code where many instructions are dead or constant). Additionally, if we find a branch whose condition is a known constant, we only visit the reachable successors.

Definition at line 2673 of file InstructionCombining.cpp.

References llvm::InstCombineWorklist::AddInitialGroup(), llvm::BasicBlock::begin(), llvm::WinEH::CE, llvm::ConstantFoldConstantExpression(), llvm::ConstantFoldInstruction(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::SmallVectorBase::empty(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::SelectInst::getCondition(), llvm::BranchInst::getCondition(), llvm::TerminatorInst::getNumSuccessors(), llvm::User::getOperand(), llvm::TerminatorInst::getSuccessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::BranchInst::isConditional(), llvm::isInstructionTriviallyDead(), llvm::User::op_begin(), llvm::User::op_end(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::Value::replaceAllUsesWith(), llvm::SmallVectorTemplateCommon< T >::size(), llvm::Value::use_empty(), and llvm::InstCombiner::Worklist.

Referenced by llvm::InstCombiner::DoOneIteration().

static void ClearSubclassDataAfterReassociation ( BinaryOperator I) [static]

Conservatively clears subclassOptionalData after a reassociation or commutation. We preserve fast-math flags when applicable as they can be preserved.

Definition at line 174 of file InstructionCombining.cpp.

References llvm::Value::clearSubclassOptionalData(), llvm::dyn_cast(), llvm::Instruction::getFastMathFlags(), I, and llvm::Instruction::setFastMathFlags().

static Value* CreateBinOpAsGiven ( BinaryOperator Inst,
Value LHS,
Value RHS,
InstCombiner::BuilderTy B 
) [static]

Creates node of binary operation with the same attributes as the specified one but with other operands.

Definition at line 1219 of file InstructionCombining.cpp.

References llvm::IRBuilder< preserveNames, T, Inserter >::CreateBinOp(), llvm::BinaryOperator::getOpcode(), llvm::BinaryOperator::hasNoSignedWrap(), llvm::BinaryOperator::hasNoUnsignedWrap(), and llvm::BinaryOperator::isExact().

static Value* FoldOperationIntoSelectOperand ( Instruction I,
Value SO,
InstCombiner IC 
) [static]
static Instruction::BinaryOps getBinOpsForFactorization ( Instruction::BinaryOps  TopLevelOpcode,
BinaryOperator Op,
Value *&  LHS,
Value *&  RHS 
) [static]

This function factors binary ops which can be combined using distributive laws. This function tries to transform 'Op' based TopLevelOpcode to enable factorization e.g for ADD(SHL(X , 2), MUL(X, 5)), When this function called with TopLevelOpcode == Instruction::Add and Op = SHL(X, 2), transforms SHL(X, 2) to MUL(X, 4) i.e. returns Instruction::Mul with LHS set to 'X' and RHS to 4.

Definition at line 443 of file InstructionCombining.cpp.

References llvm::Add, llvm::ConstantInt::get(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::ConstantExpr::getShl(), llvm::Value::getType(), and llvm::Sub.

static Value* getIdentityValue ( Instruction::BinaryOps  OpCode,
Value V 
) [static]

This function returns identity value for given opcode, which can be used to factor patterns like (X * 2) + X ==> (X * 2) + (X * 1) ==> X * (2 + 1).

Definition at line 424 of file InstructionCombining.cpp.

References llvm::ConstantInt::get(), and llvm::Value::getType().

"instcombine"  ,
"Combine redundant instructions ,
false  ,
static bool isAllocSiteRemovable ( Instruction AI,
SmallVectorImpl< WeakVH > &  Users,
const TargetLibraryInfo TLI 
) [static]
static bool isCatchAll ( Personality_Type  Personality,
Constant TypeInfo 
) [static]

isCatchAll - Return 'true' if the given typeinfo will match anything.

Definition at line 2311 of file InstructionCombining.cpp.

References GNU_Ada_Personality, GNU_CXX_Personality, GNU_ObjC_Personality, llvm::Constant::isNullValue(), llvm_unreachable, and Unknown_Personality.

Referenced by llvm::InstCombiner::visitLandingPadInst().

LeftDistributesOverRight - Whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)".

Definition at line 354 of file InstructionCombining.cpp.

References llvm::Add, llvm::APIntOps::And(), llvm::APIntOps::Or(), llvm::Sub, and llvm::APIntOps::Xor().

Referenced by RightDistributesOverLeft(), and tryFactorization().

static bool MaintainNoSignedWrap ( BinaryOperator I,
Value B,
Value C 
) [static]
static Personality_Type RecognizePersonality ( Value Pers) [static]

RecognizePersonality - See if the given exception handling personality function is one that we understand. If so, return a description of it; otherwise return Unknown_Personality.

Definition at line 2299 of file InstructionCombining.cpp.

References llvm::StringSwitch< T, R >::Case(), llvm::StringSwitch< T, R >::Default(), llvm::dyn_cast(), F(), llvm::Value::getName(), GNU_Ada_Personality, GNU_CXX_Personality, GNU_ObjC_Personality, llvm::Value::stripPointerCasts(), and Unknown_Personality.

Referenced by llvm::InstCombiner::visitLandingPadInst().

RightDistributesOverLeft - Whether "(X LOp Y) ROp Z" is always equal to "(X ROp Z) LOp (Y ROp Z)".

Definition at line 393 of file InstructionCombining.cpp.

References llvm::APIntOps::And(), llvm::Instruction::isCommutative(), LeftDistributesOverRight(), llvm::LShr, llvm::APIntOps::Or(), and llvm::APIntOps::Xor().

Referenced by tryFactorization().

static bool shorter_filter ( const Value LHS,
const Value RHS 
) [static]
static bool shouldMergeGEPs ( GEPOperator GEP,
GEPOperator Src 
) [static]
STATISTIC ( NumCombined  ,
"Number of insts combined"   
STATISTIC ( NumConstProp  ,
"Number of constant folds"   
STATISTIC ( NumDeadInst  ,
"Number of dead inst eliminated"   
STATISTIC ( NumSunkInst  ,
"Number of instructions sunk"   
STATISTIC ( NumExpand  ,
"Number of expansions"   
STATISTIC ( NumFactor  ,
"Number of factorizations"   
STATISTIC ( NumReassoc  ,
"Number of reassociations"   
static Value* tryFactorization ( InstCombiner::BuilderTy Builder,
const DataLayout DL,
BinaryOperator I,
Instruction::BinaryOps  InnerOpcode,
Value A,
Value B,
Value C,
Value D 
) [static]
static Instruction* tryToMoveFreeBeforeNullTest ( CallInst FI) [static]

Move the call to free before a NULL test.

Check if this free is accessed after its argument has been test against NULL (property 0). If yes, it is legal to move this call in its predecessor block.

The move is performed only if the block containing the call to free will be removed, i.e.: 1. it has only one predecessor P, and P has two successors 2. it contains the call and an unconditional branch 3. its successor is the same as its predecessor's successor

The profitability is out-of concern here and this function should be called only if the caller knows this transformation would be profitable (e.g., for code size).

Definition at line 1937 of file InstructionCombining.cpp.

References llvm::CallInst::getArgOperand(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::BasicBlock::getTerminator(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::PatternMatch::m_Br(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_UnconditionalBr(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), llvm::Instruction::moveBefore(), and llvm::BasicBlock::size().

Referenced by llvm::InstCombiner::visitFree().

static bool TryToSinkInstruction ( Instruction I,
BasicBlock DestBlock 
) [static]

TryToSinkInstruction - Try to move the specified instruction from its current block into the beginning of DestBlock, which can only happen if it's safe to move the instruction past all of the instructions between it and the end of its block.

Definition at line 2635 of file InstructionCombining.cpp.

References llvm::BasicBlock::end(), llvm::Function::getEntryBlock(), llvm::BasicBlock::getFirstInsertionPt(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::Value::hasOneUse(), I, llvm::Instruction::mayHaveSideEffects(), llvm::Instruction::mayReadFromMemory(), and llvm::Instruction::moveBefore().

Referenced by llvm::InstCombiner::DoOneIteration().

Variable Documentation

Combine redundant false

Definition at line 90 of file InstructionCombining.cpp.

Definition at line 90 of file InstructionCombining.cpp.

Combine redundant instructions

Definition at line 90 of file InstructionCombining.cpp.