26 #define DEBUG_TYPE "integer-division"
42 assert(BitWidth == 32 &&
"Unexpected bit width");
68 if (
Instruction *URemInst = dyn_cast<Instruction>(URem))
93 if (
Instruction *UDiv = dyn_cast<Instruction>(Quotient))
111 if (BitWidth == 64) {
114 assert(BitWidth == 32 &&
"Unexpected bit width");
142 if (
Instruction *UDiv = dyn_cast<Instruction>(Q_Mag))
166 if (BitWidth == 64) {
172 assert(BitWidth == 32 &&
"Unexpected bit width");
219 SpecialCases->
setName(
Twine(SpecialCases->getName(),
"_udiv-special-cases"));
223 "udiv-loop-exit",
F, End);
225 "udiv-do-while",
F, End);
227 "udiv-preheader",
F, End);
377 assert((Rem->
getOpcode() == Instruction::SRem ||
378 Rem->
getOpcode() == Instruction::URem) &&
379 "Trying to expand remainder from a non-remainder function");
389 if (RemTyBitWidth != 32 && RemTyBitWidth != 64)
393 if (Rem->
getOpcode() == Instruction::SRem) {
421 assert(UDiv->getOpcode() == Instruction::UDiv &&
"Non-udiv in expansion?");
437 assert((Div->
getOpcode() == Instruction::SDiv ||
438 Div->
getOpcode() == Instruction::UDiv) &&
439 "Trying to expand division from a non-division function");
449 if (DivTyBitWidth != 32 && DivTyBitWidth != 64)
453 if (Div->
getOpcode() == Instruction::SDiv) {
490 assert((Rem->
getOpcode() == Instruction::SRem ||
491 Rem->
getOpcode() == Instruction::URem) &&
492 "Trying to expand remainder from a non-remainder function");
500 if (RemTyBitWidth > 32)
503 if (RemTyBitWidth == 32)
516 if (Rem->
getOpcode() == Instruction::SRem) {
519 ExtRem = Builder.
CreateSRem(ExtDividend, ExtDivisor);
523 ExtRem = Builder.
CreateURem(ExtDividend, ExtDivisor);
540 assert((Rem->
getOpcode() == Instruction::SRem ||
541 Rem->
getOpcode() == Instruction::URem) &&
542 "Trying to expand remainder from a non-remainder function");
550 if (RemTyBitWidth > 64)
553 if (RemTyBitWidth == 64)
566 if (Rem->
getOpcode() == Instruction::SRem) {
569 ExtRem = Builder.
CreateSRem(ExtDividend, ExtDivisor);
573 ExtRem = Builder.
CreateURem(ExtDividend, ExtDivisor);
591 assert((Div->
getOpcode() == Instruction::SDiv ||
592 Div->
getOpcode() == Instruction::UDiv) &&
593 "Trying to expand division from a non-division function");
601 if (DivTyBitWidth > 32)
604 if (DivTyBitWidth == 32)
617 if (Div->
getOpcode() == Instruction::SDiv) {
620 ExtDiv = Builder.
CreateSDiv(ExtDividend, ExtDivisor);
624 ExtDiv = Builder.
CreateUDiv(ExtDividend, ExtDivisor);
641 assert((Div->
getOpcode() == Instruction::SDiv ||
642 Div->
getOpcode() == Instruction::UDiv) &&
643 "Trying to expand division from a non-division function");
651 if (DivTyBitWidth > 64)
654 if (DivTyBitWidth == 64)
667 if (Div->
getOpcode() == Instruction::SDiv) {
670 ExtDiv = Builder.
CreateSDiv(ExtDividend, ExtDivisor);
674 ExtDiv = Builder.
CreateUDiv(ExtDividend, ExtDivisor);
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
BasicBlock::iterator GetInsertPoint() const
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
void dropAllReferences()
Drop all references to operands.
Value * CreateSRem(Value *LHS, Value *RHS, const Twine &Name="")
const Function * getParent() const
Return the enclosing method, or null if none.
bool expandRemainderUpTo64Bits(BinaryOperator *Rem)
Generate code to calculate the remainder of two integers, replacing Rem with the generated code...
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
iterator begin()
Instruction iterator methods.
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
void setName(const Twine &Name)
Change the name of the value.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
bool expandRemainderUpTo32Bits(BinaryOperator *Rem)
Generate code to calculate the remainder of two integers, replacing Rem with the generated code...
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool expandRemainder(BinaryOperator *Rem)
Generate code to calculate the remainder of two integers, replacing Rem with the generated code...
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
static Value * generateSignedDivisionCode(Value *Dividend, Value *Divisor, IRBuilder<> &Builder)
Generate code to divide two signed integers.
bool expandDivisionUpTo32Bits(BinaryOperator *Div)
Generate code to divide two integers, replacing Div with the generated code.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
ConstantInt * getTrue()
Get the constant value for i1 true.
bool isVectorTy() const
isVectorTy - True if this is an instance of VectorType.
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
bool expandDivision(BinaryOperator *Div)
Generate code to divide two integers, replacing Div with the generated code.
bool expandDivisionUpTo64Bits(BinaryOperator *Div)
Generate code to divide two integers, replacing Div with the generated code.
Value * getOperand(unsigned i) const
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Class to represent integer types.
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
LLVMContext & getContext() const
CallInst * CreateCall(Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="")
static Value * generatedUnsignedRemainderCode(Value *Dividend, Value *Divisor, IRBuilder<> &Builder)
Generate code to compute the remainder of two unsigned integers.
BinaryOps getOpcode() const
unsigned getIntegerBitWidth() const
This is the shared class of boolean and integer constants.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Type * getType() const
All values are typed, get the type of this value.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
BasicBlock * GetInsertBlock() const
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateSDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Value * CreateUDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="")
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static Value * generateUnsignedDivisionCode(Value *Dividend, Value *Divisor, IRBuilder<> &Builder)
Generates code to divide two unsigned scalar 32-bit or 64-bit integers.
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Module * getParent()
Get the module that this global value is contained inside of...
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
LLVM Value Representation.
static Value * generateSignedRemainderCode(Value *Dividend, Value *Divisor, IRBuilder<> &Builder)
Generate code to compute the remainder of two signed integers.
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")