LLVM  9.0.0svn
Macros | Functions
CallPromotionUtils.cpp File Reference
#include "llvm/Transforms/Utils/CallPromotionUtils.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Include dependency graph for CallPromotionUtils.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "call-promotion-utils"
 

Functions

static void fixupPHINodeForNormalDest (InvokeInst *Invoke, BasicBlock *OrigBlock, BasicBlock *MergeBlock)
 Fix-up phi nodes in an invoke instruction's normal destination. More...
 
static void fixupPHINodeForUnwindDest (InvokeInst *Invoke, BasicBlock *OrigBlock, BasicBlock *ThenBlock, BasicBlock *ElseBlock)
 Fix-up phi nodes in an invoke instruction's unwind destination. More...
 
static void createRetPHINode (Instruction *OrigInst, Instruction *NewInst, BasicBlock *MergeBlock, IRBuilder<> &Builder)
 Create a phi node for the returned value of a call or invoke instruction. More...
 
static void createRetBitCast (CallSite CS, Type *RetTy, CastInst **RetBitCast)
 Cast a call or invoke instruction to the given type. More...
 
static InstructionversionCallSite (CallSite CS, Value *Callee, MDNode *BranchWeights)
 Predicate and clone the given call site. More...
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "call-promotion-utils"

Definition at line 20 of file CallPromotionUtils.cpp.

Function Documentation

◆ createRetBitCast()

static void createRetBitCast ( CallSite  CS,
Type RetTy,
CastInst **  RetBitCast 
)
static

Cast a call or invoke instruction to the given type.

When promoting a call site, the return type of the call site might not match that of the callee. If this is the case, we have to cast the returned value to the correct type. The location of the cast depends on if we have a call or invoke instruction.

For example, if the call instruction below requires a bitcast after promotion:

orig_bb: t0 = call i32 () ...

The bitcast is placed after the call instruction:

orig_bb: ; Uses of the original return value are replaced by uses of the bitcast. t0 = call i32 () t1 = bitcast i32 t0 to ... ...

A similar transformation is performed for invoke instructions. However, since invokes are terminating, a new block is created for the bitcast. For example, if the invoke instruction below requires a bitcast after promotion:

orig_bb: t0 = invoke i32 () to label normal_dst unwind label unwind_dst

The edge between the original block and the invoke's normal destination is split, and the bitcast is placed there:

orig_bb: t0 = invoke i32 () to label split_bb unwind label unwind_dst

split_bb: ; Uses of the original return value are replaced by uses of the bitcast. t1 = bitcast i32 t0 to ... br label normal_dst

Definition at line 161 of file CallPromotionUtils.cpp.

References llvm::CastInst::CreateBitOrPointerCast(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getInstruction(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::User::replaceUsesOfWith(), llvm::SplitEdge(), and llvm::Value::users().

Referenced by llvm::promoteCall().

◆ createRetPHINode()

static void createRetPHINode ( Instruction OrigInst,
Instruction NewInst,
BasicBlock MergeBlock,
IRBuilder<> &  Builder 
)
static

Create a phi node for the returned value of a call or invoke instruction.

After versioning a call or invoke instruction that returns a value, we have to merge the value of the original and new instructions. We do this by creating a phi node and replacing uses of the original instruction with this phi node.

For example, if OrigInst is defined in "else_bb" and NewInst is defined in "then_bb", we create the following phi node:

; Uses of the original instruction are replaced by uses of the phi node. t0 = phi i32 [ orig_inst, else_bb ], [ new_inst, then_bb ],

Definition at line 104 of file CallPromotionUtils.cpp.

References llvm::PHINode::addIncoming(), llvm::IRBuilder< T, Inserter >::CreatePHI(), llvm::BasicBlock::front(), llvm::Instruction::getParent(), llvm::Value::getType(), llvm::Type::isVoidTy(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::IRBuilderBase::SetInsertPoint(), llvm::Value::use_empty(), and llvm::Value::users().

Referenced by versionCallSite().

◆ fixupPHINodeForNormalDest()

static void fixupPHINodeForNormalDest ( InvokeInst Invoke,
BasicBlock OrigBlock,
BasicBlock MergeBlock 
)
static

Fix-up phi nodes in an invoke instruction's normal destination.

After versioning an invoke instruction, values coming from the original block will now be coming from the "merge" block. For example, in the code below:

then_bb: t0 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

else_bb: t1 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

merge_bb: t2 = phi i32 [ t0, then_bb ], [ t1, else_bb ] br normal_dst

normal_dst: t3 = phi i32 [ x, orig_bb ], ...

"orig_bb" is no longer a predecessor of "normal_dst", so the phi nodes in "normal_dst" must be fixed to refer to "merge_bb":

normal_dst: t3 = phi i32 [ x, merge_bb ], ...

Definition at line 47 of file CallPromotionUtils.cpp.

References llvm::InvokeInst::getNormalDest(), and llvm::BasicBlock::phis().

Referenced by versionCallSite().

◆ fixupPHINodeForUnwindDest()

static void fixupPHINodeForUnwindDest ( InvokeInst Invoke,
BasicBlock OrigBlock,
BasicBlock ThenBlock,
BasicBlock ElseBlock 
)
static

Fix-up phi nodes in an invoke instruction's unwind destination.

After versioning an invoke instruction, values coming from the original block will now be coming from either the "then" block or the "else" block. For example, in the code below:

then_bb: t0 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

else_bb: t1 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

unwind_dst: t3 = phi i32 [ x, orig_bb ], ...

"orig_bb" is no longer a predecessor of "unwind_dst", so the phi nodes in "unwind_dst" must be fixed to refer to "then_bb" and "else_bb":

unwind_dst: t3 = phi i32 [ x, then_bb ], [ x, else_bb ], ...

Definition at line 78 of file CallPromotionUtils.cpp.

References llvm::InvokeInst::getUnwindDest(), and llvm::BasicBlock::phis().

Referenced by versionCallSite().

◆ versionCallSite()

static Instruction* versionCallSite ( CallSite  CS,
Value Callee,
MDNode BranchWeights 
)
static

Predicate and clone the given call site.

This function creates an if-then-else structure at the location of the call site. The "if" condition compares the call site's called value to the given callee. The original call site is moved into the "else" block, and a clone of the call site is placed in the "then" block. The cloned instruction is returned.

For example, the call instruction below:

orig_bb: t0 = call i32 ptr() ...

Is replace by the following:

orig_bb: cond = icmp eq i32 ()* ptr, br i1 cond, then_bb, else_bb

then_bb: ; The clone of the original call instruction is placed in the "then" ; block. It is not yet promoted. t1 = call i32 ptr() br merge_bb

else_bb: ; The original call instruction is moved to the "else" block. t0 = call i32 ptr() br merge_bb

merge_bb: ; Uses of the original call instruction are replaced by uses of the phi ; node. t2 = phi i32 [ t0, else_bb ], [ t1, then_bb ] ...

A similar transformation is performed for invoke instructions. However, since invokes are terminating, more work is required. For example, the invoke instruction below:

orig_bb: t0 = invoke ptr() to label normal_dst unwind label unwind_dst

Is replace by the following:

orig_bb: cond = icmp eq i32 ()* ptr, br i1 cond, then_bb, else_bb

then_bb: ; The clone of the original invoke instruction is placed in the "then" ; block, and its normal destination is set to the "merge" block. It is ; not yet promoted. t1 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

else_bb: ; The original invoke instruction is moved into the "else" block, and ; its normal destination is set to the "merge" block. t0 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

merge_bb: ; Uses of the original invoke instruction are replaced by uses of the ; phi node, and the merge block branches to the normal destination. t2 = phi i32 [ t0, else_bb ], [ t1, then_bb ] br normal_dst

Definition at line 256 of file CallPromotionUtils.cpp.

References createRetPHINode(), llvm::Instruction::eraseFromParent(), fixupPHINodeForNormalDest(), fixupPHINodeForUnwindDest(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getCalledValue(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getInstruction(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::Value::getType(), llvm::Instruction::insertBefore(), llvm::Value::setName(), and llvm::SplitBlockAndInsertIfThenElse().

Referenced by llvm::promoteCallWithIfThenElse().