2012-09-05 02:22:17 +08: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-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Utils/BypassSlowDivision.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2016-10-29 05:43:54 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2012-09-05 02:22:17 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "bypass-slow-division"
|
|
|
|
|
2012-09-10 19:52:08 +08:00
|
|
|
namespace {
|
2012-09-05 02:22:17 +08: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 17:49:53 +08:00
|
|
|
}
|
2012-09-05 02:22:17 +08:00
|
|
|
|
2012-09-10 19:52:08 +08:00
|
|
|
namespace llvm {
|
2012-09-05 02:22:17 +08: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 13:29:35 +08:00
|
|
|
return DivOpInfo(false, nullptr, nullptr);
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static DivOpInfo getTombstoneKey() {
|
2014-04-25 13:29:35 +08:00
|
|
|
return DivOpInfo(true, nullptr, nullptr);
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(const DivOpInfo &Val) {
|
|
|
|
return (unsigned)(reinterpret_cast<uintptr_t>(Val.Dividend) ^
|
|
|
|
reinterpret_cast<uintptr_t>(Val.Divisor)) ^
|
2012-09-05 04:48:24 +08:00
|
|
|
(unsigned)Val.SignedOp;
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef DenseMap<DivOpInfo, DivPhiNodes> DivCacheTy;
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2012-09-05 02:22:17 +08: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.
|
2016-01-05 07:18:58 +08:00
|
|
|
static bool insertFastDiv(Instruction *I, IntegerType *BypassType,
|
|
|
|
bool UseDivOp, bool UseSignedOp,
|
2012-09-05 04:48:24 +08:00
|
|
|
DivCacheTy &PerBBDivCache) {
|
2016-01-05 07:18:58 +08:00
|
|
|
Function *F = I->getParent()->getParent();
|
2012-09-05 02:22:17 +08:00
|
|
|
// Get instruction operands
|
2016-01-05 07:18:58 +08:00
|
|
|
Value *Dividend = I->getOperand(0);
|
|
|
|
Value *Divisor = I->getOperand(1);
|
2012-09-05 02:22:17 +08:00
|
|
|
|
2016-11-16 08:44:43 +08:00
|
|
|
if (isa<ConstantInt>(Divisor)) {
|
|
|
|
// Division by a constant should have been been solved and replaced earlier
|
|
|
|
// in the pipeline.
|
2012-09-05 04:48:24 +08:00
|
|
|
return false;
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 08:44:47 +08:00
|
|
|
// If the numerator is a constant, bail if it doesn't fit into BypassType.
|
|
|
|
if (ConstantInt *ConstDividend = dyn_cast<ConstantInt>(Dividend))
|
|
|
|
if (ConstDividend->getValue().getActiveBits() > BypassType->getBitWidth())
|
|
|
|
return false;
|
|
|
|
|
2012-09-05 02:22:17 +08:00
|
|
|
// Basic Block is split before divide
|
2016-01-05 07:18:58 +08:00
|
|
|
BasicBlock *MainBB = &*I->getParent();
|
|
|
|
BasicBlock *SuccessorBB = MainBB->splitBasicBlock(I);
|
2012-09-05 02:22:17 +08:00
|
|
|
|
|
|
|
// Add new basic block for slow divide operation
|
2016-01-05 07:18:58 +08:00
|
|
|
BasicBlock *SlowBB =
|
|
|
|
BasicBlock::Create(F->getContext(), "", MainBB->getParent(), SuccessorBB);
|
2012-09-05 02:22:17 +08:00
|
|
|
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
|
2016-01-05 07:18:58 +08:00
|
|
|
BasicBlock *FastBB =
|
|
|
|
BasicBlock::Create(F->getContext(), "", MainBB->getParent(), SuccessorBB);
|
2012-09-05 02:22:17 +08:00
|
|
|
FastBB->moveBefore(SlowBB);
|
|
|
|
IRBuilder<> FastBuilder(FastBB, FastBB->begin());
|
2012-09-05 04:48:24 +08:00
|
|
|
Value *ShortDivisorV = FastBuilder.CreateCast(Instruction::Trunc, Divisor,
|
|
|
|
BypassType);
|
|
|
|
Value *ShortDividendV = FastBuilder.CreateCast(Instruction::Trunc, Dividend,
|
|
|
|
BypassType);
|
2012-09-05 02:22:17 +08:00
|
|
|
|
|
|
|
// udiv/urem because optimization only handles positive numbers
|
2016-10-29 05:43:51 +08:00
|
|
|
Value *ShortQuotientV = FastBuilder.CreateUDiv(ShortDividendV, ShortDivisorV);
|
2012-09-05 02:22:17 +08:00
|
|
|
Value *ShortRemainderV = FastBuilder.CreateURem(ShortDividendV,
|
|
|
|
ShortDivisorV);
|
|
|
|
Value *FastQuotientV = FastBuilder.CreateCast(Instruction::ZExt,
|
2012-09-05 04:48:24 +08:00
|
|
|
ShortQuotientV,
|
|
|
|
Dividend->getType());
|
2012-09-05 02:22:17 +08: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());
|
2016-01-05 07:18:58 +08:00
|
|
|
PHINode *QuoPhi = SuccessorBuilder.CreatePHI(I->getType(), 2);
|
2012-09-05 02:22:17 +08:00
|
|
|
QuoPhi->addIncoming(SlowQuotientV, SlowBB);
|
|
|
|
QuoPhi->addIncoming(FastQuotientV, FastBB);
|
2016-01-05 07:18:58 +08:00
|
|
|
PHINode *RemPhi = SuccessorBuilder.CreatePHI(I->getType(), 2);
|
2012-09-05 02:22:17 +08:00
|
|
|
RemPhi->addIncoming(SlowRemainderV, SlowBB);
|
|
|
|
RemPhi->addIncoming(FastRemainderV, FastBB);
|
|
|
|
|
2016-01-05 07:18:58 +08:00
|
|
|
// Replace I with appropriate phi node
|
2012-09-05 04:48:24 +08:00
|
|
|
if (UseDivOp)
|
2016-01-05 07:18:58 +08:00
|
|
|
I->replaceAllUsesWith(QuoPhi);
|
2012-09-05 04:48:24 +08:00
|
|
|
else
|
2016-01-05 07:18:58 +08:00
|
|
|
I->replaceAllUsesWith(RemPhi);
|
|
|
|
I->eraseFromParent();
|
2012-09-05 02:22:17 +08:00
|
|
|
|
|
|
|
// Combine operands into a single value with OR for value testing below
|
|
|
|
MainBB->getInstList().back().eraseFromParent();
|
|
|
|
IRBuilder<> MainBuilder(MainBB, MainBB->end());
|
2016-11-16 08:44:47 +08:00
|
|
|
|
|
|
|
// We should have bailed out above if the divisor is a constant, but the
|
|
|
|
// dividend may still be a constant. Set OrV to our non-constant operands
|
|
|
|
// OR'ed together.
|
|
|
|
assert(!isa<ConstantInt>(Divisor));
|
|
|
|
|
|
|
|
Value *OrV;
|
|
|
|
if (!isa<ConstantInt>(Dividend))
|
|
|
|
OrV = MainBuilder.CreateOr(Dividend, Divisor);
|
|
|
|
else
|
|
|
|
OrV = Divisor;
|
2012-09-05 02:22:17 +08:00
|
|
|
|
|
|
|
// 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-05 02:13:57 +08:00
|
|
|
Value *ZeroV = ConstantInt::getSigned(Dividend->getType(), 0);
|
2012-09-05 02:22:17 +08:00
|
|
|
Value *CmpV = MainBuilder.CreateICmpEQ(AndV, ZeroV);
|
|
|
|
MainBuilder.CreateCondBr(CmpV, FastBB, SlowBB);
|
|
|
|
|
|
|
|
// 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-05 04:48:24 +08:00
|
|
|
return true;
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-05 07:18:58 +08:00
|
|
|
// reuseOrInsertFastDiv - Reuses previously computed dividend or remainder from
|
|
|
|
// the current BB if operands and operation are identical. Otherwise calls
|
|
|
|
// insertFastDiv to perform the optimization and caches the resulting dividend
|
|
|
|
// and remainder.
|
|
|
|
static bool reuseOrInsertFastDiv(Instruction *I, IntegerType *BypassType,
|
|
|
|
bool UseDivOp, bool UseSignedOp,
|
2012-09-05 04:48:24 +08:00
|
|
|
DivCacheTy &PerBBDivCache) {
|
2012-09-05 02:22:17 +08:00
|
|
|
// Get instruction operands
|
2016-01-05 07:18:58 +08:00
|
|
|
DivOpInfo Key(UseSignedOp, I->getOperand(0), I->getOperand(1));
|
2012-09-05 07:11:11 +08:00
|
|
|
DivCacheTy::iterator CacheI = PerBBDivCache.find(Key);
|
2012-09-05 02:22:17 +08:00
|
|
|
|
|
|
|
if (CacheI == PerBBDivCache.end()) {
|
|
|
|
// If previous instance does not exist, insert fast div
|
2016-01-05 07:18:58 +08:00
|
|
|
return insertFastDiv(I, BypassType, UseDivOp, UseSignedOp, PerBBDivCache);
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace operation value with previously generated phi node
|
2012-09-05 07:11:11 +08:00
|
|
|
DivPhiNodes &Value = CacheI->second;
|
2012-09-05 02:22:17 +08:00
|
|
|
if (UseDivOp) {
|
|
|
|
// Replace all uses of div instruction with quotient phi node
|
2016-01-05 07:18:58 +08:00
|
|
|
I->replaceAllUsesWith(Value.Quotient);
|
2012-09-05 02:22:17 +08:00
|
|
|
} else {
|
|
|
|
// Replace all uses of rem instruction with remainder phi node
|
2016-01-05 07:18:58 +08:00
|
|
|
I->replaceAllUsesWith(Value.Remainder);
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove redundant operation
|
2016-01-05 07:18:58 +08:00
|
|
|
I->eraseFromParent();
|
2012-09-05 04:48:24 +08:00
|
|
|
return true;
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-05 07:18:58 +08:00
|
|
|
// bypassSlowDivision - This optimization identifies DIV instructions in a BB
|
|
|
|
// that can be profitably bypassed and carried out with a shorter, faster
|
|
|
|
// divide.
|
|
|
|
bool llvm::bypassSlowDivision(
|
|
|
|
BasicBlock *BB, const DenseMap<unsigned int, unsigned int> &BypassWidths) {
|
2012-09-05 02:22:17 +08:00
|
|
|
DivCacheTy DivCache;
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
2016-01-05 07:18:58 +08:00
|
|
|
Instruction* Next = &*BB->begin();
|
|
|
|
while (Next != nullptr) {
|
|
|
|
// We may add instructions immediately after I, but we want to skip over
|
|
|
|
// them.
|
|
|
|
Instruction* I = Next;
|
|
|
|
Next = Next->getNextNode();
|
2012-09-05 02:22:17 +08:00
|
|
|
|
|
|
|
// Get instruction details
|
2016-01-05 07:18:58 +08:00
|
|
|
unsigned Opcode = I->getOpcode();
|
2012-09-05 02:22:17 +08:00
|
|
|
bool UseDivOp = Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
|
|
|
|
bool UseRemOp = Opcode == Instruction::SRem || Opcode == Instruction::URem;
|
2012-09-05 04:48:24 +08:00
|
|
|
bool UseSignedOp = Opcode == Instruction::SDiv ||
|
|
|
|
Opcode == Instruction::SRem;
|
2012-09-05 02:22:17 +08:00
|
|
|
|
|
|
|
// Only optimize div or rem ops
|
2012-09-05 04:48:24 +08:00
|
|
|
if (!UseDivOp && !UseRemOp)
|
2012-09-05 02:22:17 +08:00
|
|
|
continue;
|
2012-09-05 04:48:24 +08:00
|
|
|
|
2012-10-04 00:11:44 +08:00
|
|
|
// Skip division on vector types, only optimize integer instructions
|
2016-01-05 07:18:58 +08:00
|
|
|
if (!I->getType()->isIntegerTy())
|
2012-10-04 00:11:44 +08:00
|
|
|
continue;
|
|
|
|
|
2012-10-05 05:33:40 +08:00
|
|
|
// Get bitwidth of div/rem instruction
|
2016-01-05 07:18:58 +08:00
|
|
|
IntegerType *T = cast<IntegerType>(I->getType());
|
2013-03-05 02:13:57 +08:00
|
|
|
unsigned int bitwidth = T->getBitWidth();
|
2012-10-04 00:11:44 +08:00
|
|
|
|
2012-10-05 05:33:40 +08:00
|
|
|
// Continue if bitwidth is not bypassed
|
|
|
|
DenseMap<unsigned int, unsigned int>::const_iterator BI = BypassWidths.find(bitwidth);
|
|
|
|
if (BI == BypassWidths.end())
|
2012-09-05 02:22:17 +08:00
|
|
|
continue;
|
|
|
|
|
2012-10-05 05:33:40 +08:00
|
|
|
// Get type for div/rem instruction with bypass bitwidth
|
2016-01-05 07:18:58 +08:00
|
|
|
IntegerType *BT = IntegerType::get(I->getContext(), BI->second);
|
2012-10-04 00:11:44 +08:00
|
|
|
|
2016-01-05 07:18:58 +08:00
|
|
|
MadeChange |= reuseOrInsertFastDiv(I, BT, UseDivOp, UseSignedOp, DivCache);
|
2012-09-05 02:22:17 +08:00
|
|
|
}
|
|
|
|
|
2016-10-29 05:43:54 +08:00
|
|
|
// Above we eagerly create divs and rems, as pairs, so that we can efficiently
|
|
|
|
// create divrem machine instructions. Now erase any unused divs / rems so we
|
|
|
|
// don't leave extra instructions sitting around.
|
|
|
|
for (auto &KV : DivCache)
|
|
|
|
for (Instruction *Phi : {KV.second.Quotient, KV.second.Remainder})
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Phi);
|
|
|
|
|
2012-09-05 02:22:17 +08:00
|
|
|
return MadeChange;
|
|
|
|
}
|