<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">With my change CodegenPrepare doesn't set EverMadeChange to TRUE if BypassSlowDivision doesn't do anything. So behavior changes, but I don't see any way to add a test-case here. I should split it into two different commits anyway, I will be more careful next time.<div><br></div><div>- Kuba<br><div><br><div><div>On Sep 4, 2012, at 10:55 PM, Chandler Carruth <<a href="mailto:chandlerc@google.com">chandlerc@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div class="gmail_extra"><div class="gmail_quote">On Tue, Sep 4, 2012 at 4:48 PM, Jakub Staszak <span dir="ltr"><<a href="mailto:kubastaszak@gmail.com" target="_blank" class="cremed">kubastaszak@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: kuba<br>
Date: Tue Sep 4 15:48:24 2012<br>
New Revision: 163164<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=163164&view=rev" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project?rev=163164&view=rev</a><br>
Log:<br>
Return false if BypassSlowDivision doesn't change anything.<br>
Also a few minor changes: </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
- use pre-inc instead of post-inc<br>
- use isa instead of dyn_cast<br>
- 80 col<br>
- trailing spaces<br></blockquote><div><br></div><div>You've mixed these into one commit, so now I'm confused -- does the first one change behavior? It seems like it should, but maybe it was just a pure refactoring? (If it does, test case?)</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Modified:<br>
llvm/trunk/lib/Transforms/Utils/BypassSlowDivision.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/Utils/BypassSlowDivision.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BypassSlowDivision.cpp?rev=163164&r1=163163&r2=163164&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BypassSlowDivision.cpp?rev=163164&r1=163163&r2=163164&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Utils/BypassSlowDivision.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Utils/BypassSlowDivision.cpp Tue Sep 4 15:48:24 2012<br>
@@ -61,7 +61,7 @@<br>
static unsigned getHashValue(const DivOpInfo &Val) {<br>
return (unsigned)(reinterpret_cast<uintptr_t>(Val.Dividend) ^<br>
reinterpret_cast<uintptr_t>(Val.Divisor)) ^<br>
- (unsigned)Val.SignedOp;<br>
+ (unsigned)Val.SignedOp;<br>
}<br>
};<br>
<br>
@@ -71,31 +71,29 @@<br>
// insertFastDiv - Substitutes the div/rem instruction with code that checks the<br>
// value of the operands and uses a shorter-faster div/rem instruction when<br>
// possible and the longer-slower div/rem instruction otherwise.<br>
-static void insertFastDiv(Function &F,<br>
+static bool insertFastDiv(Function &F,<br>
Function::iterator &I,<br>
BasicBlock::iterator &J,<br>
IntegerType *BypassType,<br>
bool UseDivOp,<br>
bool UseSignedOp,<br>
- DivCacheTy &PerBBDivCache)<br>
-{<br>
+ DivCacheTy &PerBBDivCache) {<br>
// Get instruction operands<br>
Instruction *Instr = J;<br>
Value *Dividend = Instr->getOperand(0);<br>
Value *Divisor = Instr->getOperand(1);<br>
<br>
- if (dyn_cast<ConstantInt>(Divisor) != 0 ||<br>
- (dyn_cast<ConstantInt>(Dividend) != 0 &&<br>
- dyn_cast<ConstantInt>(Divisor) != 0)) {<br>
+ if (isa<ConstantInt>(Divisor) ||<br>
+ (isa<ConstantInt>(Dividend) && isa<ConstantInt>(Divisor))) {<br>
// Operations with immediate values should have<br>
// been solved and replaced during compile time.<br>
- return;<br>
+ return false;<br>
}<br>
<br>
// Basic Block is split before divide<br>
BasicBlock *MainBB = I;<br>
BasicBlock *SuccessorBB = I->splitBasicBlock(J);<br>
- I++; //advance iterator I to successorBB<br>
+ ++I; //advance iterator I to successorBB<br>
<br>
// Add new basic block for slow divide operation<br>
BasicBlock *SlowBB = BasicBlock::Create(F.getContext(), "",<br>
@@ -118,17 +116,19 @@<br>
MainBB->getParent(), SuccessorBB);<br>
FastBB->moveBefore(SlowBB);<br>
IRBuilder<> FastBuilder(FastBB, FastBB->begin());<br>
- Value *ShortDivisorV = FastBuilder.CreateCast(Instruction::Trunc, Divisor, BypassType);<br>
- Value *ShortDividendV = FastBuilder.CreateCast(Instruction::Trunc, Dividend, BypassType);<br>
+ Value *ShortDivisorV = FastBuilder.CreateCast(Instruction::Trunc, Divisor,<br>
+ BypassType);<br>
+ Value *ShortDividendV = FastBuilder.CreateCast(Instruction::Trunc, Dividend,<br>
+ BypassType);<br>
<br>
// udiv/urem because optimization only handles positive numbers<br>
Value *ShortQuotientV = FastBuilder.CreateExactUDiv(ShortDividendV,<br>
- ShortDivisorV);<br>
+ ShortDivisorV);<br>
Value *ShortRemainderV = FastBuilder.CreateURem(ShortDividendV,<br>
ShortDivisorV);<br>
Value *FastQuotientV = FastBuilder.CreateCast(Instruction::ZExt,<br>
- ShortQuotientV,<br>
- Dividend->getType());<br>
+ ShortQuotientV,<br>
+ Dividend->getType());<br>
Value *FastRemainderV = FastBuilder.CreateCast(Instruction::ZExt,<br>
ShortRemainderV,<br>
Dividend->getType());<br>
@@ -144,11 +144,10 @@<br>
RemPhi->addIncoming(FastRemainderV, FastBB);<br>
<br>
// Replace Instr with appropriate phi node<br>
- if (UseDivOp) {<br>
+ if (UseDivOp)<br>
Instr->replaceAllUsesWith(QuoPhi);<br>
- } else {<br>
+ else<br>
Instr->replaceAllUsesWith(RemPhi);<br>
- }<br>
Instr->eraseFromParent();<br>
<br>
// Combine operands into a single value with OR for value testing below<br>
@@ -174,19 +173,19 @@<br>
DivOpInfo Key(UseSignedOp, Dividend, Divisor);<br>
DivPhiNodes Value(QuoPhi, RemPhi);<br>
PerBBDivCache.insert(std::pair<DivOpInfo, DivPhiNodes>(Key, Value));<br>
+ return true;<br>
}<br>
<br>
// reuseOrInsertFastDiv - Reuses previously computed dividend or remainder if<br>
// operands and operation are identical. Otherwise call insertFastDiv to perform<br>
// the optimization and cache the resulting dividend and remainder.<br>
-static void reuseOrInsertFastDiv(Function &F,<br>
+static bool reuseOrInsertFastDiv(Function &F,<br>
Function::iterator &I,<br>
BasicBlock::iterator &J,<br>
IntegerType *BypassType,<br>
bool UseDivOp,<br>
bool UseSignedOp,<br>
- DivCacheTy &PerBBDivCache)<br>
-{<br>
+ DivCacheTy &PerBBDivCache) {<br>
// Get instruction operands<br>
Instruction *Instr = J;<br>
DivOpInfo Key(UseSignedOp, Instr->getOperand(0), Instr->getOperand(1));<br>
@@ -194,8 +193,8 @@<br>
<br>
if (CacheI == PerBBDivCache.end()) {<br>
// If previous instance does not exist, insert fast div<br>
- insertFastDiv(F, I, J, BypassType, UseDivOp, UseSignedOp, PerBBDivCache);<br>
- return;<br>
+ return insertFastDiv(F, I, J, BypassType, UseDivOp, UseSignedOp,<br>
+ PerBBDivCache);<br>
}<br>
<br>
// Replace operation value with previously generated phi node<br>
@@ -209,18 +208,18 @@<br>
}<br>
<br>
// Advance to next operation<br>
- J++;<br>
+ ++J;<br>
<br>
// Remove redundant operation<br>
Instr->eraseFromParent();<br>
+ return true;<br>
}<br>
<br>
// bypassSlowDivision - This optimization identifies DIV instructions that can<br>
// be profitably bypassed and carried out with a shorter, faster divide.<br>
bool bypassSlowDivision(Function &F,<br>
Function::iterator &I,<br>
- const llvm::DenseMap<Type *, Type *> &BypassTypeMap)<br>
-{<br>
+ const llvm::DenseMap<Type *, Type *> &BypassTypeMap) {<br>
DivCacheTy DivCache;<br>
<br>
bool MadeChange = false;<br>
@@ -230,20 +229,22 @@<br>
unsigned Opcode = J->getOpcode();<br>
bool UseDivOp = Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;<br>
bool UseRemOp = Opcode == Instruction::SRem || Opcode == Instruction::URem;<br>
- bool UseSignedOp = Opcode == Instruction::SDiv || Opcode == Instruction::SRem;<br>
+ bool UseSignedOp = Opcode == Instruction::SDiv ||<br>
+ Opcode == Instruction::SRem;<br>
<br>
// Only optimize div or rem ops<br>
- if (!UseDivOp && !UseRemOp) {<br>
+ if (!UseDivOp && !UseRemOp)<br>
continue;<br>
- }<br>
+<br>
// Continue if div/rem type is not bypassed<br>
- DenseMap<Type *, Type *>::const_iterator BT = BypassTypeMap.find(J->getType());<br>
- if (BT == BypassTypeMap.end()) {<br>
+ DenseMap<Type *, Type *>::const_iterator BT =<br>
+ BypassTypeMap.find(J->getType());<br>
+ if (BT == BypassTypeMap.end())<br>
continue;<br>
- }<br>
<br>
- IntegerType *BypassType = (IntegerType *)BT->second;<br>
- reuseOrInsertFastDiv(F, I, J, BypassType, UseDivOp, UseSignedOp, DivCache);<br>
+ IntegerType *BypassType = cast<IntegerType>(BT->second);<br>
+ MadeChange |= reuseOrInsertFastDiv(F, I, J, BypassType, UseDivOp,<br>
+ UseSignedOp, DivCache);<br>
MadeChange = true;<br>
}<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" class="cremed">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank" class="cremed">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>
</blockquote></div><br></div></div></body></html>