2012-09-04 20:22:17 +02:00
|
|
|
//===-- BypassSlowDivision.cpp - Bypass slow division ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains an optimization for div and rem on architectures that
|
|
|
|
// execute short instructions significantly faster than longer instructions.
|
|
|
|
// For example, on Intel Atom 32-bit divides are slow enough that during
|
|
|
|
// runtime it is profitable to check the value of the operands, and if they are
|
|
|
|
// positive and less than 256 use an unsigned 8-bit divide.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Transforms/Utils/BypassSlowDivision.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2012-09-04 20:22:17 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 04:55:47 +02:00
|
|
|
#define DEBUG_TYPE "bypass-slow-division"
|
|
|
|
|
2012-09-10 13:52:08 +02:00
|
|
|
namespace {
|
2012-09-04 20:22:17 +02:00
|
|
|
struct DivOpInfo {
|
|
|
|
bool SignedOp;
|
|
|
|
Value *Dividend;
|
|
|
|
Value *Divisor;
|
|
|
|
|
|
|
|
DivOpInfo(bool InSignedOp, Value *InDividend, Value *InDivisor)
|
|
|
|
: SignedOp(InSignedOp), Dividend(InDividend), Divisor(InDivisor) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DivPhiNodes {
|
|
|
|
PHINode *Quotient;
|
|
|
|
PHINode *Remainder;
|
|
|
|
|
|
|
|
DivPhiNodes(PHINode *InQuotient, PHINode *InRemainder)
|
|
|
|
: Quotient(InQuotient), Remainder(InRemainder) {}
|
|
|
|
};
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2012-09-04 20:22:17 +02:00
|
|
|
|
2012-09-10 13:52:08 +02:00
|
|
|
namespace llvm {
|
2012-09-04 20:22:17 +02:00
|
|
|
template<>
|
|
|
|
struct DenseMapInfo<DivOpInfo> {
|
|
|
|
static bool isEqual(const DivOpInfo &Val1, const DivOpInfo &Val2) {
|
|
|
|
return Val1.SignedOp == Val2.SignedOp &&
|
|
|
|
Val1.Dividend == Val2.Dividend &&
|
|
|
|
Val1.Divisor == Val2.Divisor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DivOpInfo getEmptyKey() {
|
2014-04-25 07:29:35 +02:00
|
|
|
return DivOpInfo(false, nullptr, nullptr);
|
2012-09-04 20:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static DivOpInfo getTombstoneKey() {
|
2014-04-25 07:29:35 +02:00
|
|
|
return DivOpInfo(true, nullptr, nullptr);
|
2012-09-04 20:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(const DivOpInfo &Val) {
|
|
|
|
return (unsigned)(reinterpret_cast<uintptr_t>(Val.Dividend) ^
|
|
|
|
reinterpret_cast<uintptr_t>(Val.Divisor)) ^
|
2012-09-04 22:48:24 +02:00
|
|
|
(unsigned)Val.SignedOp;
|
2012-09-04 20:22:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef DenseMap<DivOpInfo, DivPhiNodes> DivCacheTy;
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2012-09-04 20:22:17 +02:00
|
|
|
|
|
|
|
// insertFastDiv - Substitutes the div/rem instruction with code that checks the
|
|
|
|
// value of the operands and uses a shorter-faster div/rem instruction when
|
|
|
|
// possible and the longer-slower div/rem instruction otherwise.
|
2012-09-04 22:48:24 +02:00
|
|
|
static bool insertFastDiv(Function &F,
|
2012-09-04 20:22:17 +02:00
|
|
|
Function::iterator &I,
|
|
|
|
BasicBlock::iterator &J,
|
|
|
|
IntegerType *BypassType,
|
|
|
|
bool UseDivOp,
|
|
|
|
bool UseSignedOp,
|
2012-09-04 22:48:24 +02:00
|
|
|
DivCacheTy &PerBBDivCache) {
|
2012-09-04 20:22:17 +02:00
|
|
|
// Get instruction operands
|
|
|
|
Instruction *Instr = J;
|
|
|
|
Value *Dividend = Instr->getOperand(0);
|
|
|
|
Value *Divisor = Instr->getOperand(1);
|
|
|
|
|
2012-09-04 22:48:24 +02:00
|
|
|
if (isa<ConstantInt>(Divisor) ||
|
|
|
|
(isa<ConstantInt>(Dividend) && isa<ConstantInt>(Divisor))) {
|
2012-09-04 20:22:17 +02:00
|
|
|
// Operations with immediate values should have
|
|
|
|
// been solved and replaced during compile time.
|
2012-09-04 22:48:24 +02:00
|
|
|
return false;
|
2012-09-04 20:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic Block is split before divide
|
|
|
|
BasicBlock *MainBB = I;
|
|
|
|
BasicBlock *SuccessorBB = I->splitBasicBlock(J);
|
2012-09-04 22:48:24 +02:00
|
|
|
++I; //advance iterator I to successorBB
|
2012-09-04 20:22:17 +02:00
|
|
|
|
|
|
|
// Add new basic block for slow divide operation
|
|
|
|
BasicBlock *SlowBB = BasicBlock::Create(F.getContext(), "",
|
|
|
|
MainBB->getParent(), SuccessorBB);
|
|
|
|
SlowBB->moveBefore(SuccessorBB);
|
|
|
|
IRBuilder<> SlowBuilder(SlowBB, SlowBB->begin());
|
|
|
|
Value *SlowQuotientV;
|
|
|
|
Value *SlowRemainderV;
|
|
|
|
if (UseSignedOp) {
|
|
|
|
SlowQuotientV = SlowBuilder.CreateSDiv(Dividend, Divisor);
|
|
|
|
SlowRemainderV = SlowBuilder.CreateSRem(Dividend, Divisor);
|
|
|
|
} else {
|
|
|
|
SlowQuotientV = SlowBuilder.CreateUDiv(Dividend, Divisor);
|
|
|
|
SlowRemainderV = SlowBuilder.CreateURem(Dividend, Divisor);
|
|
|
|
}
|
|
|
|
SlowBuilder.CreateBr(SuccessorBB);
|
|
|
|
|
|
|
|
// Add new basic block for fast divide operation
|
|
|
|
BasicBlock *FastBB = BasicBlock::Create(F.getContext(), "",
|
|
|
|
MainBB->getParent(), SuccessorBB);
|
|
|
|
FastBB->moveBefore(SlowBB);
|
|
|
|
IRBuilder<> FastBuilder(FastBB, FastBB->begin());
|
2012-09-04 22:48:24 +02:00
|
|
|
Value *ShortDivisorV = FastBuilder.CreateCast(Instruction::Trunc, Divisor,
|
|
|
|
BypassType);
|
|
|
|
Value *ShortDividendV = FastBuilder.CreateCast(Instruction::Trunc, Dividend,
|
|
|
|
BypassType);
|
2012-09-04 20:22:17 +02:00
|
|
|
|
|
|
|
// udiv/urem because optimization only handles positive numbers
|
|
|
|
Value *ShortQuotientV = FastBuilder.CreateExactUDiv(ShortDividendV,
|
2012-09-04 22:48:24 +02:00
|
|
|
ShortDivisorV);
|
2012-09-04 20:22:17 +02:00
|
|
|
Value *ShortRemainderV = FastBuilder.CreateURem(ShortDividendV,
|
|
|
|
ShortDivisorV);
|
|
|
|
Value *FastQuotientV = FastBuilder.CreateCast(Instruction::ZExt,
|
2012-09-04 22:48:24 +02:00
|
|
|
ShortQuotientV,
|
|
|
|
Dividend->getType());
|
2012-09-04 20:22:17 +02:00
|
|
|
Value *FastRemainderV = FastBuilder.CreateCast(Instruction::ZExt,
|
|
|
|
ShortRemainderV,
|
|
|
|
Dividend->getType());
|
|
|
|
FastBuilder.CreateBr(SuccessorBB);
|
|
|
|
|
|
|
|
// Phi nodes for result of div and rem
|
|
|
|
IRBuilder<> SuccessorBuilder(SuccessorBB, SuccessorBB->begin());
|
|
|
|
PHINode *QuoPhi = SuccessorBuilder.CreatePHI(Instr->getType(), 2);
|
|
|
|
QuoPhi->addIncoming(SlowQuotientV, SlowBB);
|
|
|
|
QuoPhi->addIncoming(FastQuotientV, FastBB);
|
|
|
|
PHINode *RemPhi = SuccessorBuilder.CreatePHI(Instr->getType(), 2);
|
|
|
|
RemPhi->addIncoming(SlowRemainderV, SlowBB);
|
|
|
|
RemPhi->addIncoming(FastRemainderV, FastBB);
|
|
|
|
|
|
|
|
// Replace Instr with appropriate phi node
|
2012-09-04 22:48:24 +02:00
|
|
|
if (UseDivOp)
|
2012-09-04 20:22:17 +02:00
|
|
|
Instr->replaceAllUsesWith(QuoPhi);
|
2012-09-04 22:48:24 +02:00
|
|
|
else
|
2012-09-04 20:22:17 +02:00
|
|
|
Instr->replaceAllUsesWith(RemPhi);
|
|
|
|
Instr->eraseFromParent();
|
|
|
|
|
|
|
|
// Combine operands into a single value with OR for value testing below
|
|
|
|
MainBB->getInstList().back().eraseFromParent();
|
|
|
|
IRBuilder<> MainBuilder(MainBB, MainBB->end());
|
|
|
|
Value *OrV = MainBuilder.CreateOr(Dividend, Divisor);
|
|
|
|
|
|
|
|
// BitMask is inverted to check if the operands are
|
|
|
|
// larger than the bypass type
|
|
|
|
uint64_t BitMask = ~BypassType->getBitMask();
|
|
|
|
Value *AndV = MainBuilder.CreateAnd(OrV, BitMask);
|
|
|
|
|
|
|
|
// Compare operand values and branch
|
2013-03-04 19:13:57 +01:00
|
|
|
Value *ZeroV = ConstantInt::getSigned(Dividend->getType(), 0);
|
2012-09-04 20:22:17 +02:00
|
|
|
Value *CmpV = MainBuilder.CreateICmpEQ(AndV, ZeroV);
|
|
|
|
MainBuilder.CreateCondBr(CmpV, FastBB, SlowBB);
|
|
|
|
|
|
|
|
// point iterator J at first instruction of successorBB
|
|
|
|
J = I->begin();
|
|
|
|
|
|
|
|
// Cache phi nodes to be used later in place of other instances
|
|
|
|
// of div or rem with the same sign, dividend, and divisor
|
|
|
|
DivOpInfo Key(UseSignedOp, Dividend, Divisor);
|
|
|
|
DivPhiNodes Value(QuoPhi, RemPhi);
|
|
|
|
PerBBDivCache.insert(std::pair<DivOpInfo, DivPhiNodes>(Key, Value));
|
2012-09-04 22:48:24 +02:00
|
|
|
return true;
|
2012-09-04 20:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// reuseOrInsertFastDiv - Reuses previously computed dividend or remainder if
|
|
|
|
// operands and operation are identical. Otherwise call insertFastDiv to perform
|
|
|
|
// the optimization and cache the resulting dividend and remainder.
|
2012-09-04 22:48:24 +02:00
|
|
|
static bool reuseOrInsertFastDiv(Function &F,
|
2012-09-04 20:22:17 +02:00
|
|
|
Function::iterator &I,
|
|
|
|
BasicBlock::iterator &J,
|
|
|
|
IntegerType *BypassType,
|
|
|
|
bool UseDivOp,
|
|
|
|
bool UseSignedOp,
|
2012-09-04 22:48:24 +02:00
|
|
|
DivCacheTy &PerBBDivCache) {
|
2012-09-04 20:22:17 +02:00
|
|
|
// Get instruction operands
|
|
|
|
Instruction *Instr = J;
|
|
|
|
DivOpInfo Key(UseSignedOp, Instr->getOperand(0), Instr->getOperand(1));
|
2012-09-05 01:11:11 +02:00
|
|
|
DivCacheTy::iterator CacheI = PerBBDivCache.find(Key);
|
2012-09-04 20:22:17 +02:00
|
|
|
|
|
|
|
if (CacheI == PerBBDivCache.end()) {
|
|
|
|
// If previous instance does not exist, insert fast div
|
2012-09-04 22:48:24 +02:00
|
|
|
return insertFastDiv(F, I, J, BypassType, UseDivOp, UseSignedOp,
|
|
|
|
PerBBDivCache);
|
2012-09-04 20:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace operation value with previously generated phi node
|
2012-09-05 01:11:11 +02:00
|
|
|
DivPhiNodes &Value = CacheI->second;
|
2012-09-04 20:22:17 +02:00
|
|
|
if (UseDivOp) {
|
|
|
|
// Replace all uses of div instruction with quotient phi node
|
|
|
|
J->replaceAllUsesWith(Value.Quotient);
|
|
|
|
} else {
|
|
|
|
// Replace all uses of rem instruction with remainder phi node
|
|
|
|
J->replaceAllUsesWith(Value.Remainder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to next operation
|
2012-09-04 22:48:24 +02:00
|
|
|
++J;
|
2012-09-04 20:22:17 +02:00
|
|
|
|
|
|
|
// Remove redundant operation
|
|
|
|
Instr->eraseFromParent();
|
2012-09-04 22:48:24 +02:00
|
|
|
return true;
|
2012-09-04 20:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// bypassSlowDivision - This optimization identifies DIV instructions that can
|
|
|
|
// be profitably bypassed and carried out with a shorter, faster divide.
|
2012-09-10 13:52:08 +02:00
|
|
|
bool llvm::bypassSlowDivision(Function &F,
|
|
|
|
Function::iterator &I,
|
2012-10-04 23:33:40 +02:00
|
|
|
const DenseMap<unsigned int, unsigned int> &BypassWidths) {
|
2012-09-04 20:22:17 +02:00
|
|
|
DivCacheTy DivCache;
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
for (BasicBlock::iterator J = I->begin(); J != I->end(); J++) {
|
|
|
|
|
|
|
|
// Get instruction details
|
|
|
|
unsigned Opcode = J->getOpcode();
|
|
|
|
bool UseDivOp = Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
|
|
|
|
bool UseRemOp = Opcode == Instruction::SRem || Opcode == Instruction::URem;
|
2012-09-04 22:48:24 +02:00
|
|
|
bool UseSignedOp = Opcode == Instruction::SDiv ||
|
|
|
|
Opcode == Instruction::SRem;
|
2012-09-04 20:22:17 +02:00
|
|
|
|
|
|
|
// Only optimize div or rem ops
|
2012-09-04 22:48:24 +02:00
|
|
|
if (!UseDivOp && !UseRemOp)
|
2012-09-04 20:22:17 +02:00
|
|
|
continue;
|
2012-09-04 22:48:24 +02:00
|
|
|
|
2012-10-03 18:11:44 +02:00
|
|
|
// Skip division on vector types, only optimize integer instructions
|
|
|
|
if (!J->getType()->isIntegerTy())
|
|
|
|
continue;
|
|
|
|
|
2012-10-04 23:33:40 +02:00
|
|
|
// Get bitwidth of div/rem instruction
|
2012-10-03 18:11:44 +02:00
|
|
|
IntegerType *T = cast<IntegerType>(J->getType());
|
2013-03-04 19:13:57 +01:00
|
|
|
unsigned int bitwidth = T->getBitWidth();
|
2012-10-03 18:11:44 +02:00
|
|
|
|
2012-10-04 23:33:40 +02:00
|
|
|
// Continue if bitwidth is not bypassed
|
|
|
|
DenseMap<unsigned int, unsigned int>::const_iterator BI = BypassWidths.find(bitwidth);
|
|
|
|
if (BI == BypassWidths.end())
|
2012-09-04 20:22:17 +02:00
|
|
|
continue;
|
|
|
|
|
2012-10-04 23:33:40 +02:00
|
|
|
// Get type for div/rem instruction with bypass bitwidth
|
|
|
|
IntegerType *BT = IntegerType::get(J->getContext(), BI->second);
|
2012-10-03 18:11:44 +02:00
|
|
|
|
|
|
|
MadeChange |= reuseOrInsertFastDiv(F, I, J, BT, UseDivOp,
|
2012-09-04 22:48:24 +02:00
|
|
|
UseSignedOp, DivCache);
|
2012-09-04 20:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|