2011-12-09 14:19:40 +08:00
|
|
|
//===-- UnrollLoopRuntime.cpp - Runtime Loop unrolling utilities ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements some loop unrolling utilities for loops with run-time
|
|
|
|
// trip counts. See LoopUnroll.cpp for unrolling loops with compile-time
|
|
|
|
// trip counts.
|
|
|
|
//
|
2011-12-19 05:52:30 +08:00
|
|
|
// The functions in this file are used to generate extra code when the
|
2011-12-09 14:19:40 +08:00
|
|
|
// run-time trip count modulo the unroll factor is not 0. When this is the
|
|
|
|
// case, we need to generate code to execute these 'left over' iterations.
|
|
|
|
//
|
2011-12-19 05:52:30 +08:00
|
|
|
// The current strategy generates an if-then-else sequence prior to the
|
2016-04-05 20:19:35 +08:00
|
|
|
// unrolled loop to execute the 'left over' iterations before or after the
|
|
|
|
// unrolled loop.
|
2011-12-09 14:19:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/UnrollLoop.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2015-01-18 17:21:15 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2011-12-09 14:19:40 +08:00
|
|
|
#include "llvm/Analysis/LoopIterator.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2015-01-18 10:39:37 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
#include "llvm/IR/Metadata.h"
|
2015-03-10 10:37:25 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-12-09 14:19:40 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-01-18 17:21:15 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2011-12-09 14:19:40 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "loop-unroll"
|
|
|
|
|
2011-12-19 05:52:30 +08:00
|
|
|
STATISTIC(NumRuntimeUnrolled,
|
2011-12-09 14:19:40 +08:00
|
|
|
"Number of loops unrolled with run-time trip counts");
|
|
|
|
|
|
|
|
/// Connect the unrolling prolog code to the original loop.
|
|
|
|
/// The unrolling prolog code contains code to execute the
|
|
|
|
/// 'extra' iterations if the run-time trip count modulo the
|
|
|
|
/// unroll count is non-zero.
|
|
|
|
///
|
|
|
|
/// This function performs the following:
|
|
|
|
/// - Create PHI nodes at prolog end block to combine values
|
|
|
|
/// that exit the prolog code and jump around the prolog.
|
|
|
|
/// - Add a PHI operand to a PHI node at the loop exit block
|
|
|
|
/// for values that exit the prolog and go around the loop.
|
|
|
|
/// - Branch around the original loop if the trip count is less
|
|
|
|
/// than the unroll factor.
|
|
|
|
///
|
2015-02-19 03:32:25 +08:00
|
|
|
static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
|
2016-04-05 20:19:35 +08:00
|
|
|
BasicBlock *PrologExit, BasicBlock *PreHeader,
|
|
|
|
BasicBlock *NewPreHeader, ValueToValueMapTy &VMap,
|
|
|
|
DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA) {
|
2011-12-09 14:19:40 +08:00
|
|
|
BasicBlock *Latch = L->getLoopLatch();
|
2014-04-28 12:05:08 +08:00
|
|
|
assert(Latch && "Loop must have a latch");
|
2016-04-05 20:19:35 +08:00
|
|
|
BasicBlock *PrologLatch = cast<BasicBlock>(VMap[Latch]);
|
2011-12-09 14:19:40 +08:00
|
|
|
|
|
|
|
// Create a PHI node for each outgoing value from the original loop
|
|
|
|
// (which means it is an outgoing value from the prolog code too).
|
|
|
|
// The new PHI node is inserted in the prolog end basic block.
|
2016-04-05 20:19:35 +08:00
|
|
|
// The new PHI node value is added as an operand of a PHI node in either
|
2011-12-09 14:19:40 +08:00
|
|
|
// the loop header or the loop exit block.
|
2016-04-05 20:19:35 +08:00
|
|
|
for (BasicBlock *Succ : successors(Latch)) {
|
|
|
|
for (Instruction &BBI : *Succ) {
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(&BBI);
|
|
|
|
// Exit when we passed all PHI nodes.
|
|
|
|
if (!PN)
|
|
|
|
break;
|
2011-12-09 14:19:40 +08:00
|
|
|
// Add a new PHI node to the prolog end block and add the
|
|
|
|
// appropriate incoming values.
|
2016-04-05 20:19:35 +08:00
|
|
|
PHINode *NewPN = PHINode::Create(PN->getType(), 2, PN->getName() + ".unr",
|
|
|
|
PrologExit->getFirstNonPHI());
|
2011-12-09 14:19:40 +08:00
|
|
|
// Adding a value to the new PHI node from the original loop preheader.
|
|
|
|
// This is the value that skips all the prolog code.
|
|
|
|
if (L->contains(PN)) {
|
2016-04-05 20:19:35 +08:00
|
|
|
NewPN->addIncoming(PN->getIncomingValueForBlock(NewPreHeader),
|
|
|
|
PreHeader);
|
2011-12-09 14:19:40 +08:00
|
|
|
} else {
|
2016-04-05 20:19:35 +08:00
|
|
|
NewPN->addIncoming(UndefValue::get(PN->getType()), PreHeader);
|
2011-12-09 14:19:40 +08:00
|
|
|
}
|
2011-12-19 05:52:30 +08:00
|
|
|
|
|
|
|
Value *V = PN->getIncomingValueForBlock(Latch);
|
2011-12-09 14:19:40 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
|
|
|
if (L->contains(I)) {
|
2016-04-18 03:26:49 +08:00
|
|
|
V = VMap.lookup(I);
|
2011-12-09 14:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Adding a value to the new PHI node from the last prolog block
|
|
|
|
// that was created.
|
2016-04-05 20:19:35 +08:00
|
|
|
NewPN->addIncoming(V, PrologLatch);
|
2011-12-09 14:19:40 +08:00
|
|
|
|
|
|
|
// Update the existing PHI node operand with the value from the
|
|
|
|
// new PHI node. How this is done depends on if the existing
|
|
|
|
// PHI node is in the original loop block, or the exit block.
|
|
|
|
if (L->contains(PN)) {
|
2016-04-05 20:19:35 +08:00
|
|
|
PN->setIncomingValue(PN->getBasicBlockIndex(NewPreHeader), NewPN);
|
2011-12-09 14:19:40 +08:00
|
|
|
} else {
|
2016-04-05 20:19:35 +08:00
|
|
|
PN->addIncoming(NewPN, PrologExit);
|
2011-12-09 14:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 03:19:31 +08:00
|
|
|
// Make sure that created prolog loop is in simplified form
|
|
|
|
SmallVector<BasicBlock *, 4> PrologExitPreds;
|
|
|
|
Loop *PrologLoop = LI->getLoopFor(PrologLatch);
|
|
|
|
if (PrologLoop) {
|
|
|
|
for (BasicBlock *PredBB : predecessors(PrologExit))
|
|
|
|
if (PrologLoop->contains(PredBB))
|
|
|
|
PrologExitPreds.push_back(PredBB);
|
|
|
|
|
|
|
|
SplitBlockPredecessors(PrologExit, PrologExitPreds, ".unr-lcssa", DT, LI,
|
|
|
|
PreserveLCSSA);
|
|
|
|
}
|
|
|
|
|
2016-02-09 03:27:33 +08:00
|
|
|
// Create a branch around the original loop, which is taken if there are no
|
2015-02-19 03:32:25 +08:00
|
|
|
// iterations remaining to be executed after running the prologue.
|
2016-04-05 20:19:35 +08:00
|
|
|
Instruction *InsertPt = PrologExit->getTerminator();
|
2015-06-12 02:25:44 +08:00
|
|
|
IRBuilder<> B(InsertPt);
|
2015-02-19 03:32:25 +08:00
|
|
|
|
|
|
|
assert(Count != 0 && "nonsensical Count!");
|
|
|
|
|
2016-03-25 22:24:52 +08:00
|
|
|
// If BECount <u (Count - 1) then (BECount + 1) % Count == (BECount + 1)
|
|
|
|
// This means %xtraiter is (BECount + 1) and all of the iterations of this
|
|
|
|
// loop were executed by the prologue. Note that if BECount <u (Count - 1)
|
|
|
|
// then (BECount + 1) cannot unsigned-overflow.
|
2015-06-12 02:25:44 +08:00
|
|
|
Value *BrLoopExit =
|
|
|
|
B.CreateICmpULT(BECount, ConstantInt::get(BECount->getType(), Count - 1));
|
2011-12-09 14:19:40 +08:00
|
|
|
BasicBlock *Exit = L->getUniqueExitBlock();
|
2014-04-28 12:05:08 +08:00
|
|
|
assert(Exit && "Loop must have a single exit block only");
|
2011-12-09 14:19:40 +08:00
|
|
|
// Split the exit to maintain loop canonicalization guarantees
|
2016-04-05 20:19:35 +08:00
|
|
|
SmallVector<BasicBlock*, 4> Preds(predecessors(Exit));
|
2015-07-22 17:52:54 +08:00
|
|
|
SplitBlockPredecessors(Exit, Preds, ".unr-lcssa", DT, LI,
|
2015-12-16 03:40:57 +08:00
|
|
|
PreserveLCSSA);
|
2011-12-09 14:19:40 +08:00
|
|
|
// Add the branch to the exit block (around the unrolled loop)
|
2016-04-05 20:19:35 +08:00
|
|
|
B.CreateCondBr(BrLoopExit, Exit, NewPreHeader);
|
|
|
|
InsertPt->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Connect the unrolling epilog code to the original loop.
|
|
|
|
/// The unrolling epilog code contains code to execute the
|
|
|
|
/// 'extra' iterations if the run-time trip count modulo the
|
|
|
|
/// unroll count is non-zero.
|
|
|
|
///
|
|
|
|
/// This function performs the following:
|
|
|
|
/// - Update PHI nodes at the unrolling loop exit and epilog loop exit
|
|
|
|
/// - Create PHI nodes at the unrolling loop exit to combine
|
|
|
|
/// values that exit the unrolling loop code and jump around it.
|
|
|
|
/// - Update PHI operands in the epilog loop by the new PHI nodes
|
|
|
|
/// - Branch around the epilog loop if extra iters (ModVal) is zero.
|
|
|
|
///
|
|
|
|
static void ConnectEpilog(Loop *L, Value *ModVal, BasicBlock *NewExit,
|
|
|
|
BasicBlock *Exit, BasicBlock *PreHeader,
|
|
|
|
BasicBlock *EpilogPreHeader, BasicBlock *NewPreHeader,
|
|
|
|
ValueToValueMapTy &VMap, DominatorTree *DT,
|
|
|
|
LoopInfo *LI, bool PreserveLCSSA) {
|
|
|
|
BasicBlock *Latch = L->getLoopLatch();
|
|
|
|
assert(Latch && "Loop must have a latch");
|
|
|
|
BasicBlock *EpilogLatch = cast<BasicBlock>(VMap[Latch]);
|
|
|
|
|
|
|
|
// Loop structure should be the following:
|
|
|
|
//
|
|
|
|
// PreHeader
|
|
|
|
// NewPreHeader
|
|
|
|
// Header
|
|
|
|
// ...
|
|
|
|
// Latch
|
|
|
|
// NewExit (PN)
|
|
|
|
// EpilogPreHeader
|
|
|
|
// EpilogHeader
|
|
|
|
// ...
|
|
|
|
// EpilogLatch
|
|
|
|
// Exit (EpilogPN)
|
|
|
|
|
|
|
|
// Update PHI nodes at NewExit and Exit.
|
|
|
|
for (Instruction &BBI : *NewExit) {
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(&BBI);
|
|
|
|
// Exit when we passed all PHI nodes.
|
|
|
|
if (!PN)
|
|
|
|
break;
|
|
|
|
// PN should be used in another PHI located in Exit block as
|
|
|
|
// Exit was split by SplitBlockPredecessors into Exit and NewExit
|
|
|
|
// Basicaly it should look like:
|
|
|
|
// NewExit:
|
|
|
|
// PN = PHI [I, Latch]
|
|
|
|
// ...
|
|
|
|
// Exit:
|
|
|
|
// EpilogPN = PHI [PN, EpilogPreHeader]
|
|
|
|
//
|
|
|
|
// There is EpilogPreHeader incoming block instead of NewExit as
|
|
|
|
// NewExit was spilt 1 more time to get EpilogPreHeader.
|
|
|
|
assert(PN->hasOneUse() && "The phi should have 1 use");
|
|
|
|
PHINode *EpilogPN = cast<PHINode> (PN->use_begin()->getUser());
|
|
|
|
assert(EpilogPN->getParent() == Exit && "EpilogPN should be in Exit block");
|
|
|
|
|
|
|
|
// Add incoming PreHeader from branch around the Loop
|
|
|
|
PN->addIncoming(UndefValue::get(PN->getType()), PreHeader);
|
|
|
|
|
|
|
|
Value *V = PN->getIncomingValueForBlock(Latch);
|
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (I && L->contains(I))
|
|
|
|
// If value comes from an instruction in the loop add VMap value.
|
2016-04-18 03:26:49 +08:00
|
|
|
V = VMap.lookup(I);
|
2016-04-05 20:19:35 +08:00
|
|
|
// For the instruction out of the loop, constant or undefined value
|
|
|
|
// insert value itself.
|
|
|
|
EpilogPN->addIncoming(V, EpilogLatch);
|
|
|
|
|
|
|
|
assert(EpilogPN->getBasicBlockIndex(EpilogPreHeader) >= 0 &&
|
|
|
|
"EpilogPN should have EpilogPreHeader incoming block");
|
|
|
|
// Change EpilogPreHeader incoming block to NewExit.
|
|
|
|
EpilogPN->setIncomingBlock(EpilogPN->getBasicBlockIndex(EpilogPreHeader),
|
|
|
|
NewExit);
|
|
|
|
// Now PHIs should look like:
|
|
|
|
// NewExit:
|
|
|
|
// PN = PHI [I, Latch], [undef, PreHeader]
|
|
|
|
// ...
|
|
|
|
// Exit:
|
|
|
|
// EpilogPN = PHI [PN, NewExit], [VMap[I], EpilogLatch]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create PHI nodes at NewExit (from the unrolling loop Latch and PreHeader).
|
|
|
|
// Update corresponding PHI nodes in epilog loop.
|
|
|
|
for (BasicBlock *Succ : successors(Latch)) {
|
|
|
|
// Skip this as we already updated phis in exit blocks.
|
|
|
|
if (!L->contains(Succ))
|
|
|
|
continue;
|
|
|
|
for (Instruction &BBI : *Succ) {
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(&BBI);
|
|
|
|
// Exit when we passed all PHI nodes.
|
|
|
|
if (!PN)
|
|
|
|
break;
|
|
|
|
// Add new PHI nodes to the loop exit block and update epilog
|
|
|
|
// PHIs with the new PHI values.
|
|
|
|
PHINode *NewPN = PHINode::Create(PN->getType(), 2, PN->getName() + ".unr",
|
|
|
|
NewExit->getFirstNonPHI());
|
|
|
|
// Adding a value to the new PHI node from the unrolling loop preheader.
|
|
|
|
NewPN->addIncoming(PN->getIncomingValueForBlock(NewPreHeader), PreHeader);
|
|
|
|
// Adding a value to the new PHI node from the unrolling loop latch.
|
|
|
|
NewPN->addIncoming(PN->getIncomingValueForBlock(Latch), Latch);
|
|
|
|
|
|
|
|
// Update the existing PHI node operand with the value from the new PHI
|
|
|
|
// node. Corresponding instruction in epilog loop should be PHI.
|
|
|
|
PHINode *VPN = cast<PHINode>(VMap[&BBI]);
|
|
|
|
VPN->setIncomingValue(VPN->getBasicBlockIndex(EpilogPreHeader), NewPN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InsertPt = NewExit->getTerminator();
|
|
|
|
IRBuilder<> B(InsertPt);
|
2016-04-27 11:04:54 +08:00
|
|
|
Value *BrLoopExit = B.CreateIsNotNull(ModVal, "lcmp.mod");
|
2016-04-05 20:19:35 +08:00
|
|
|
assert(Exit && "Loop must have a single exit block only");
|
|
|
|
// Split the exit to maintain loop canonicalization guarantees
|
|
|
|
SmallVector<BasicBlock*, 4> Preds(predecessors(Exit));
|
|
|
|
SplitBlockPredecessors(Exit, Preds, ".epilog-lcssa", DT, LI,
|
|
|
|
PreserveLCSSA);
|
|
|
|
// Add the branch to the exit block (around the unrolling loop)
|
|
|
|
B.CreateCondBr(BrLoopExit, EpilogPreHeader, Exit);
|
2011-12-09 14:19:40 +08:00
|
|
|
InsertPt->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a clone of the blocks in a loop and connect them together.
|
2016-04-05 20:19:35 +08:00
|
|
|
/// If CreateRemainderLoop is false, loop structure will not be cloned,
|
|
|
|
/// otherwise a new loop will be created including all cloned blocks, and the
|
|
|
|
/// iterator of it switches to count NewIter down to 0.
|
|
|
|
/// The cloned blocks should be inserted between InsertTop and InsertBot.
|
|
|
|
/// If loop structure is cloned InsertTop should be new preheader, InsertBot
|
|
|
|
/// new loop exit.
|
2011-12-09 14:19:40 +08:00
|
|
|
///
|
2016-04-05 20:19:35 +08:00
|
|
|
static void CloneLoopBlocks(Loop *L, Value *NewIter,
|
|
|
|
const bool CreateRemainderLoop,
|
|
|
|
const bool UseEpilogRemainder,
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
BasicBlock *InsertTop, BasicBlock *InsertBot,
|
2016-04-05 20:19:35 +08:00
|
|
|
BasicBlock *Preheader,
|
2011-12-09 14:19:40 +08:00
|
|
|
std::vector<BasicBlock *> &NewBlocks,
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
LoopBlocksDFS &LoopBlocks, ValueToValueMapTy &VMap,
|
2015-03-09 15:26:37 +08:00
|
|
|
LoopInfo *LI) {
|
2016-04-05 20:19:35 +08:00
|
|
|
StringRef suffix = UseEpilogRemainder ? "epil" : "prol";
|
2011-12-09 14:19:40 +08:00
|
|
|
BasicBlock *Header = L->getHeader();
|
|
|
|
BasicBlock *Latch = L->getLoopLatch();
|
|
|
|
Function *F = Header->getParent();
|
|
|
|
LoopBlocksDFS::RPOIterator BlockBegin = LoopBlocks.beginRPO();
|
|
|
|
LoopBlocksDFS::RPOIterator BlockEnd = LoopBlocks.endRPO();
|
2015-10-07 07:24:35 +08:00
|
|
|
Loop *NewLoop = nullptr;
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
Loop *ParentLoop = L->getParentLoop();
|
2016-04-05 20:19:35 +08:00
|
|
|
if (CreateRemainderLoop) {
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
NewLoop = new Loop();
|
2015-03-09 15:26:37 +08:00
|
|
|
if (ParentLoop)
|
|
|
|
ParentLoop->addChildLoop(NewLoop);
|
|
|
|
else
|
|
|
|
LI->addTopLevelLoop(NewLoop);
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
}
|
|
|
|
|
2011-12-09 14:19:40 +08:00
|
|
|
// For each block in the original loop, create a new copy,
|
|
|
|
// and update the value map with the newly created values.
|
|
|
|
for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
|
2016-04-05 20:19:35 +08:00
|
|
|
BasicBlock *NewBB = CloneBasicBlock(*BB, VMap, "." + suffix, F);
|
2011-12-09 14:19:40 +08:00
|
|
|
NewBlocks.push_back(NewBB);
|
|
|
|
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
if (NewLoop)
|
2015-01-18 09:25:51 +08:00
|
|
|
NewLoop->addBasicBlockToLoop(NewBB, *LI);
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
else if (ParentLoop)
|
2015-01-18 09:25:51 +08:00
|
|
|
ParentLoop->addBasicBlockToLoop(NewBB, *LI);
|
2011-12-09 14:19:40 +08:00
|
|
|
|
|
|
|
VMap[*BB] = NewBB;
|
|
|
|
if (Header == *BB) {
|
|
|
|
// For the first block, add a CFG connection to this newly
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
// created block.
|
2011-12-09 14:19:40 +08:00
|
|
|
InsertTop->getTerminator()->setSuccessor(0, NewBB);
|
|
|
|
}
|
2016-01-28 09:23:18 +08:00
|
|
|
|
2011-12-09 14:19:40 +08:00
|
|
|
if (Latch == *BB) {
|
2016-04-05 20:19:35 +08:00
|
|
|
// For the last block, if CreateRemainderLoop is false, create a direct
|
|
|
|
// jump to InsertBot. If not, create a loop back to cloned head.
|
2011-12-09 14:19:40 +08:00
|
|
|
VMap.erase((*BB)->getTerminator());
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
BasicBlock *FirstLoopBB = cast<BasicBlock>(VMap[Header]);
|
|
|
|
BranchInst *LatchBR = cast<BranchInst>(NewBB->getTerminator());
|
2015-06-12 02:25:44 +08:00
|
|
|
IRBuilder<> Builder(LatchBR);
|
2016-04-05 20:19:35 +08:00
|
|
|
if (!CreateRemainderLoop) {
|
2015-06-12 02:25:44 +08:00
|
|
|
Builder.CreateBr(InsertBot);
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
} else {
|
2016-04-05 20:19:35 +08:00
|
|
|
PHINode *NewIdx = PHINode::Create(NewIter->getType(), 2,
|
|
|
|
suffix + ".iter",
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
FirstLoopBB->getFirstNonPHI());
|
|
|
|
Value *IdxSub =
|
|
|
|
Builder.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1),
|
|
|
|
NewIdx->getName() + ".sub");
|
|
|
|
Value *IdxCmp =
|
|
|
|
Builder.CreateIsNotNull(IdxSub, NewIdx->getName() + ".cmp");
|
2015-06-12 02:25:44 +08:00
|
|
|
Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot);
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
NewIdx->addIncoming(NewIter, InsertTop);
|
|
|
|
NewIdx->addIncoming(IdxSub, NewBB);
|
|
|
|
}
|
2015-06-12 02:25:44 +08:00
|
|
|
LatchBR->eraseFromParent();
|
2011-12-09 14:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
|
|
|
|
// Change the incoming values to the ones defined in the preheader or
|
|
|
|
// cloned loop.
|
|
|
|
for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
|
2015-10-13 10:39:05 +08:00
|
|
|
PHINode *NewPHI = cast<PHINode>(VMap[&*I]);
|
2016-04-05 20:19:35 +08:00
|
|
|
if (!CreateRemainderLoop) {
|
|
|
|
if (UseEpilogRemainder) {
|
|
|
|
unsigned idx = NewPHI->getBasicBlockIndex(Preheader);
|
|
|
|
NewPHI->setIncomingBlock(idx, InsertTop);
|
|
|
|
NewPHI->removeIncomingValue(Latch, false);
|
|
|
|
} else {
|
|
|
|
VMap[&*I] = NewPHI->getIncomingValueForBlock(Preheader);
|
|
|
|
cast<BasicBlock>(VMap[Header])->getInstList().erase(NewPHI);
|
|
|
|
}
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
} else {
|
|
|
|
unsigned idx = NewPHI->getBasicBlockIndex(Preheader);
|
|
|
|
NewPHI->setIncomingBlock(idx, InsertTop);
|
|
|
|
BasicBlock *NewLatch = cast<BasicBlock>(VMap[Latch]);
|
|
|
|
idx = NewPHI->getBasicBlockIndex(Latch);
|
|
|
|
Value *InVal = NewPHI->getIncomingValue(idx);
|
|
|
|
NewPHI->setIncomingBlock(idx, NewLatch);
|
2016-04-18 03:26:49 +08:00
|
|
|
if (Value *V = VMap.lookup(InVal))
|
|
|
|
NewPHI->setIncomingValue(idx, V);
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NewLoop) {
|
|
|
|
// Add unroll disable metadata to disable future unrolling for this loop.
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
SmallVector<Metadata *, 4> MDs;
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
// Reserve first location for self reference to the LoopID metadata node.
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
MDs.push_back(nullptr);
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
MDNode *LoopID = NewLoop->getLoopID();
|
|
|
|
if (LoopID) {
|
|
|
|
// First remove any existing loop unrolling metadata.
|
|
|
|
for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
|
|
|
|
bool IsUnrollMetadata = false;
|
|
|
|
MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
|
|
|
|
if (MD) {
|
|
|
|
const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
|
|
|
|
IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
|
|
|
|
}
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
if (!IsUnrollMetadata)
|
|
|
|
MDs.push_back(LoopID->getOperand(i));
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMContext &Context = NewLoop->getHeader()->getContext();
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
SmallVector<Metadata *, 1> DisableOperands;
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
|
|
|
|
MDNode *DisableNode = MDNode::get(Context, DisableOperands);
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
MDs.push_back(DisableNode);
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
MDNode *NewLoopID = MDNode::get(Context, MDs);
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
// Set operand 0 to refer to the loop id itself.
|
|
|
|
NewLoopID->replaceOperandWith(0, NewLoopID);
|
|
|
|
NewLoop->setLoopID(NewLoopID);
|
2011-12-09 14:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 20:19:35 +08:00
|
|
|
/// Insert code in the prolog/epilog code when unrolling a loop with a
|
2011-12-09 14:19:40 +08:00
|
|
|
/// run-time trip-count.
|
|
|
|
///
|
|
|
|
/// This method assumes that the loop unroll factor is total number
|
2016-02-13 05:01:37 +08:00
|
|
|
/// of loop bodies in the loop after unrolling. (Some folks refer
|
2011-12-09 14:19:40 +08:00
|
|
|
/// to the unroll factor as the number of *extra* copies added).
|
|
|
|
/// We assume also that the loop unroll factor is a power-of-two. So, after
|
|
|
|
/// unrolling the loop, the number of loop bodies executed is 2,
|
2011-12-19 05:52:30 +08:00
|
|
|
/// 4, 8, etc. Note - LLVM converts the if-then-sequence to a switch
|
2011-12-09 14:19:40 +08:00
|
|
|
/// instruction in SimplifyCFG.cpp. Then, the backend decides how code for
|
|
|
|
/// the switch instruction is generated.
|
|
|
|
///
|
2016-04-05 20:19:35 +08:00
|
|
|
/// ***Prolog case***
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
/// extraiters = tripcount % loopfactor
|
|
|
|
/// if (extraiters == 0) jump Loop:
|
2016-04-09 04:20:38 +08:00
|
|
|
/// else jump Prol:
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
/// Prol: LoopBody;
|
|
|
|
/// extraiters -= 1 // Omitted if unroll factor is 2.
|
|
|
|
/// if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
|
2016-04-09 04:20:38 +08:00
|
|
|
/// if (tripcount < loopfactor) jump End:
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
/// Loop:
|
|
|
|
/// ...
|
|
|
|
/// End:
|
2011-12-09 14:19:40 +08:00
|
|
|
///
|
2016-04-05 20:19:35 +08:00
|
|
|
/// ***Epilog case***
|
|
|
|
/// extraiters = tripcount % loopfactor
|
2016-04-27 11:04:54 +08:00
|
|
|
/// if (tripcount < loopfactor) jump LoopExit:
|
2016-04-05 20:19:35 +08:00
|
|
|
/// unroll_iters = tripcount - extraiters
|
|
|
|
/// Loop: LoopBody; (executes unroll_iter times);
|
|
|
|
/// unroll_iter -= 1
|
|
|
|
/// if (unroll_iter != 0) jump Loop:
|
|
|
|
/// LoopExit:
|
|
|
|
/// if (extraiters == 0) jump EpilExit:
|
|
|
|
/// Epil: LoopBody; (executes extraiters times)
|
|
|
|
/// extraiters -= 1 // Omitted if unroll factor is 2.
|
|
|
|
/// if (extraiters != 0) jump Epil: // Omitted if unroll factor is 2.
|
|
|
|
/// EpilExit:
|
|
|
|
|
|
|
|
bool llvm::UnrollRuntimeLoopRemainder(Loop *L, unsigned Count,
|
|
|
|
bool AllowExpensiveTripCount,
|
|
|
|
bool UseEpilogRemainder,
|
|
|
|
LoopInfo *LI, ScalarEvolution *SE,
|
|
|
|
DominatorTree *DT, bool PreserveLCSSA) {
|
|
|
|
// for now, only unroll loops that contain a single exit
|
2011-12-19 05:52:30 +08:00
|
|
|
if (!L->getExitingBlock())
|
2011-12-09 14:19:40 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure the loop is in canonical form, and there is a single
|
|
|
|
// exit block only.
|
2016-04-05 20:19:35 +08:00
|
|
|
if (!L->isLoopSimplifyForm())
|
|
|
|
return false;
|
|
|
|
BasicBlock *Exit = L->getUniqueExitBlock(); // successor out of loop
|
|
|
|
if (!Exit)
|
2011-12-09 14:19:40 +08:00
|
|
|
return false;
|
|
|
|
|
2016-02-09 03:27:33 +08:00
|
|
|
// Use Scalar Evolution to compute the trip count. This allows more loops to
|
|
|
|
// be unrolled than relying on induction var simplification.
|
2015-12-16 03:40:57 +08:00
|
|
|
if (!SE)
|
2012-05-08 10:52:09 +08:00
|
|
|
return false;
|
2011-12-09 14:19:40 +08:00
|
|
|
|
2016-02-09 03:27:33 +08:00
|
|
|
// Only unroll loops with a computable trip count, and the trip count needs
|
|
|
|
// to be an int value (allowing a pointer type is a TODO item).
|
2015-12-16 03:40:57 +08:00
|
|
|
const SCEV *BECountSC = SE->getBackedgeTakenCount(L);
|
2015-02-19 03:32:25 +08:00
|
|
|
if (isa<SCEVCouldNotCompute>(BECountSC) ||
|
|
|
|
!BECountSC->getType()->isIntegerTy())
|
2011-12-09 14:19:40 +08:00
|
|
|
return false;
|
|
|
|
|
2015-02-19 03:32:25 +08:00
|
|
|
unsigned BEWidth = cast<IntegerType>(BECountSC->getType())->getBitWidth();
|
2014-11-21 04:19:55 +08:00
|
|
|
|
2016-02-09 03:27:33 +08:00
|
|
|
// Add 1 since the backedge count doesn't include the first loop iteration.
|
2011-12-19 05:52:30 +08:00
|
|
|
const SCEV *TripCountSC =
|
2015-12-16 03:40:57 +08:00
|
|
|
SE->getAddExpr(BECountSC, SE->getConstant(BECountSC->getType(), 1));
|
2011-12-09 14:19:40 +08:00
|
|
|
if (isa<SCEVCouldNotCompute>(TripCountSC))
|
|
|
|
return false;
|
|
|
|
|
2015-04-14 11:20:38 +08:00
|
|
|
BasicBlock *Header = L->getHeader();
|
2016-04-05 20:19:35 +08:00
|
|
|
BasicBlock *PreHeader = L->getLoopPreheader();
|
|
|
|
BranchInst *PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
|
2015-04-14 11:20:38 +08:00
|
|
|
const DataLayout &DL = Header->getModule()->getDataLayout();
|
2015-12-16 03:40:57 +08:00
|
|
|
SCEVExpander Expander(*SE, DL, "loop-unroll");
|
2016-02-16 14:46:58 +08:00
|
|
|
if (!AllowExpensiveTripCount &&
|
|
|
|
Expander.isHighCostExpansion(TripCountSC, L, PreHeaderBR))
|
2015-04-14 11:20:38 +08:00
|
|
|
return false;
|
|
|
|
|
2015-02-19 03:32:25 +08:00
|
|
|
// This constraint lets us deal with an overflowing trip count easily; see the
|
2015-04-12 09:24:01 +08:00
|
|
|
// comment on ModVal below.
|
|
|
|
if (Log2_32(Count) > BEWidth)
|
2011-12-09 14:19:40 +08:00
|
|
|
return false;
|
|
|
|
|
2016-02-09 03:27:33 +08:00
|
|
|
// If this loop is nested, then the loop unroller changes the code in the
|
|
|
|
// parent loop, so the Scalar Evolution pass needs to be run again.
|
2011-12-09 14:19:40 +08:00
|
|
|
if (Loop *ParentLoop = L->getParentLoop())
|
2015-12-16 03:40:57 +08:00
|
|
|
SE->forgetLoop(ParentLoop);
|
2015-01-18 10:39:37 +08:00
|
|
|
|
2011-12-09 14:19:40 +08:00
|
|
|
BasicBlock *Latch = L->getLoopLatch();
|
|
|
|
|
2016-04-05 20:19:35 +08:00
|
|
|
// Loop structure is the following:
|
|
|
|
//
|
|
|
|
// PreHeader
|
|
|
|
// Header
|
|
|
|
// ...
|
|
|
|
// Latch
|
|
|
|
// Exit
|
|
|
|
|
|
|
|
BasicBlock *NewPreHeader;
|
|
|
|
BasicBlock *NewExit = nullptr;
|
|
|
|
BasicBlock *PrologExit = nullptr;
|
|
|
|
BasicBlock *EpilogPreHeader = nullptr;
|
|
|
|
BasicBlock *PrologPreHeader = nullptr;
|
|
|
|
|
|
|
|
if (UseEpilogRemainder) {
|
|
|
|
// If epilog remainder
|
|
|
|
// Split PreHeader to insert a branch around loop for unrolling.
|
|
|
|
NewPreHeader = SplitBlock(PreHeader, PreHeader->getTerminator(), DT, LI);
|
|
|
|
NewPreHeader->setName(PreHeader->getName() + ".new");
|
|
|
|
// Split Exit to create phi nodes from branch above.
|
|
|
|
SmallVector<BasicBlock*, 4> Preds(predecessors(Exit));
|
|
|
|
NewExit = SplitBlockPredecessors(Exit, Preds, ".unr-lcssa",
|
|
|
|
DT, LI, PreserveLCSSA);
|
|
|
|
// Split NewExit to insert epilog remainder loop.
|
|
|
|
EpilogPreHeader = SplitBlock(NewExit, NewExit->getTerminator(), DT, LI);
|
|
|
|
EpilogPreHeader->setName(Header->getName() + ".epil.preheader");
|
|
|
|
} else {
|
|
|
|
// If prolog remainder
|
|
|
|
// Split the original preheader twice to insert prolog remainder loop
|
|
|
|
PrologPreHeader = SplitEdge(PreHeader, Header, DT, LI);
|
|
|
|
PrologPreHeader->setName(Header->getName() + ".prol.preheader");
|
|
|
|
PrologExit = SplitBlock(PrologPreHeader, PrologPreHeader->getTerminator(),
|
|
|
|
DT, LI);
|
|
|
|
PrologExit->setName(Header->getName() + ".prol.loopexit");
|
|
|
|
// Split PrologExit to get NewPreHeader.
|
|
|
|
NewPreHeader = SplitBlock(PrologExit, PrologExit->getTerminator(), DT, LI);
|
|
|
|
NewPreHeader->setName(PreHeader->getName() + ".new");
|
|
|
|
}
|
|
|
|
// Loop structure should be the following:
|
|
|
|
// Epilog Prolog
|
|
|
|
//
|
|
|
|
// PreHeader PreHeader
|
|
|
|
// *NewPreHeader *PrologPreHeader
|
|
|
|
// Header *PrologExit
|
|
|
|
// ... *NewPreHeader
|
|
|
|
// Latch Header
|
|
|
|
// *NewExit ...
|
|
|
|
// *EpilogPreHeader Latch
|
|
|
|
// Exit Exit
|
|
|
|
|
|
|
|
// Calculate conditions for branch around loop for unrolling
|
|
|
|
// in epilog case and around prolog remainder loop in prolog case.
|
2011-12-09 14:19:40 +08:00
|
|
|
// Compute the number of extra iterations required, which is:
|
2016-04-05 20:19:35 +08:00
|
|
|
// extra iterations = run-time trip count % loop unroll factor
|
|
|
|
PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
|
2011-12-09 14:19:40 +08:00
|
|
|
Value *TripCount = Expander.expandCodeFor(TripCountSC, TripCountSC->getType(),
|
|
|
|
PreHeaderBR);
|
2015-02-19 03:32:25 +08:00
|
|
|
Value *BECount = Expander.expandCodeFor(BECountSC, BECountSC->getType(),
|
|
|
|
PreHeaderBR);
|
2014-06-21 21:46:25 +08:00
|
|
|
IRBuilder<> B(PreHeaderBR);
|
2016-03-25 22:24:52 +08:00
|
|
|
Value *ModVal;
|
|
|
|
// Calculate ModVal = (BECount + 1) % Count.
|
|
|
|
// Note that TripCount is BECount + 1.
|
|
|
|
if (isPowerOf2_32(Count)) {
|
2016-04-05 20:19:35 +08:00
|
|
|
// When Count is power of 2 we don't BECount for epilog case, however we'll
|
|
|
|
// need it for a branch around unrolling loop for prolog case.
|
2016-03-25 22:24:52 +08:00
|
|
|
ModVal = B.CreateAnd(TripCount, Count - 1, "xtraiter");
|
2016-04-05 20:19:35 +08:00
|
|
|
// 1. There are no iterations to be run in the prolog/epilog loop.
|
2016-03-25 22:24:52 +08:00
|
|
|
// OR
|
|
|
|
// 2. The addition computing TripCount overflowed.
|
|
|
|
//
|
|
|
|
// If (2) is true, we know that TripCount really is (1 << BEWidth) and so
|
|
|
|
// the number of iterations that remain to be run in the original loop is a
|
|
|
|
// multiple Count == (1 << Log2(Count)) because Log2(Count) <= BEWidth (we
|
|
|
|
// explicitly check this above).
|
|
|
|
} else {
|
|
|
|
// As (BECount + 1) can potentially unsigned overflow we count
|
|
|
|
// (BECount % Count) + 1 which is overflow safe as BECount % Count < Count.
|
|
|
|
Value *ModValTmp = B.CreateURem(BECount,
|
|
|
|
ConstantInt::get(BECount->getType(),
|
|
|
|
Count));
|
|
|
|
Value *ModValAdd = B.CreateAdd(ModValTmp,
|
|
|
|
ConstantInt::get(ModValTmp->getType(), 1));
|
|
|
|
// At that point (BECount % Count) + 1 could be equal to Count.
|
|
|
|
// To handle this case we need to take mod by Count one more time.
|
|
|
|
ModVal = B.CreateURem(ModValAdd,
|
|
|
|
ConstantInt::get(BECount->getType(), Count),
|
|
|
|
"xtraiter");
|
|
|
|
}
|
2016-04-27 11:04:54 +08:00
|
|
|
Value *BranchVal =
|
|
|
|
UseEpilogRemainder ? B.CreateICmpULT(BECount,
|
|
|
|
ConstantInt::get(BECount->getType(),
|
|
|
|
Count - 1)) :
|
|
|
|
B.CreateIsNotNull(ModVal, "lcmp.mod");
|
|
|
|
BasicBlock *RemainderLoop = UseEpilogRemainder ? NewExit : PrologPreHeader;
|
|
|
|
BasicBlock *UnrollingLoop = UseEpilogRemainder ? NewPreHeader : PrologExit;
|
2016-04-05 20:19:35 +08:00
|
|
|
// Branch to either remainder (extra iterations) loop or unrolling loop.
|
2016-04-27 11:04:54 +08:00
|
|
|
B.CreateCondBr(BranchVal, RemainderLoop, UnrollingLoop);
|
2011-12-09 14:19:40 +08:00
|
|
|
PreHeaderBR->eraseFromParent();
|
|
|
|
Function *F = Header->getParent();
|
|
|
|
// Get an ordered list of blocks in the loop to help with the ordering of the
|
2016-04-05 20:19:35 +08:00
|
|
|
// cloned blocks in the prolog/epilog code
|
2011-12-09 14:19:40 +08:00
|
|
|
LoopBlocksDFS LoopBlocks(L);
|
|
|
|
LoopBlocks.perform(LI);
|
|
|
|
|
|
|
|
//
|
|
|
|
// For each extra loop iteration, create a copy of the loop's basic blocks
|
|
|
|
// and generate a condition that branches to the copy depending on the
|
|
|
|
// number of 'left over' iterations.
|
|
|
|
//
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
std::vector<BasicBlock *> NewBlocks;
|
|
|
|
ValueToValueMapTy VMap;
|
|
|
|
|
2016-04-05 20:19:35 +08:00
|
|
|
// For unroll factor 2 remainder loop will have 1 iterations.
|
|
|
|
// Do not create 1 iteration loop.
|
|
|
|
bool CreateRemainderLoop = (Count != 2);
|
2014-11-21 04:19:55 +08:00
|
|
|
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
// Clone all the basic blocks in the loop. If Count is 2, we don't clone
|
|
|
|
// the loop, otherwise we create a cloned loop to execute the extra
|
|
|
|
// iterations. This function adds the appropriate CFG connections.
|
2016-04-05 20:19:35 +08:00
|
|
|
BasicBlock *InsertBot = UseEpilogRemainder ? Exit : PrologExit;
|
|
|
|
BasicBlock *InsertTop = UseEpilogRemainder ? EpilogPreHeader : PrologPreHeader;
|
|
|
|
CloneLoopBlocks(L, ModVal, CreateRemainderLoop, UseEpilogRemainder, InsertTop,
|
|
|
|
InsertBot, NewPreHeader, NewBlocks, LoopBlocks, VMap, LI);
|
|
|
|
|
|
|
|
// Insert the cloned blocks into the function.
|
|
|
|
F->getBasicBlockList().splice(InsertBot->getIterator(),
|
|
|
|
F->getBasicBlockList(),
|
|
|
|
NewBlocks[0]->getIterator(),
|
|
|
|
F->end());
|
|
|
|
|
|
|
|
// Loop structure should be the following:
|
|
|
|
// Epilog Prolog
|
|
|
|
//
|
|
|
|
// PreHeader PreHeader
|
|
|
|
// NewPreHeader PrologPreHeader
|
|
|
|
// Header PrologHeader
|
|
|
|
// ... ...
|
|
|
|
// Latch PrologLatch
|
|
|
|
// NewExit PrologExit
|
|
|
|
// EpilogPreHeader NewPreHeader
|
|
|
|
// EpilogHeader Header
|
|
|
|
// ... ...
|
|
|
|
// EpilogLatch Latch
|
|
|
|
// Exit Exit
|
Use a loop to simplify the runtime unrolling prologue.
Runtime unrolling will create a prologue to execute the extra
iterations which is can't divided by the unroll factor. It
generates an if-then-else sequence to jump into a factor -1
times unrolled loop body, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
if (extraiters == loopfactor) jump L1
if (extraiters == loopfactor-1) jump L2
...
L1: LoopBody;
L2: LoopBody;
...
if tripcount < loopfactor jump End
Loop:
...
End:
It means if the unroll factor is 4, the loop body will be 7
times unrolled, 3 are in loop prologue, and 4 are in the loop.
This commit is to use a loop to execute the extra iterations
in prologue, like
extraiters = tripcount % loopfactor
if (extraiters == 0) jump Loop:
else jump Prol
Prol: LoopBody;
extraiters -= 1 // Omitted if unroll factor is 2.
if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
if (tripcount < loopfactor) jump End
Loop:
...
End:
Then when unroll factor is 4, the loop body will be copied by
only 5 times, 1 in the prologue loop, 4 in the original loop.
And if the unroll factor is 2, new loop won't be created, just
as the original solution.
llvm-svn: 218604
2014-09-29 19:15:00 +08:00
|
|
|
|
2016-02-09 05:32:43 +08:00
|
|
|
// Rewrite the cloned instruction operands to use the values created when the
|
|
|
|
// clone is created.
|
|
|
|
for (BasicBlock *BB : NewBlocks) {
|
|
|
|
for (Instruction &I : *BB) {
|
|
|
|
RemapInstruction(&I, VMap,
|
2016-04-07 08:26:43 +08:00
|
|
|
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
|
2011-12-09 14:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 20:19:35 +08:00
|
|
|
if (UseEpilogRemainder) {
|
|
|
|
// Connect the epilog code to the original loop and update the
|
|
|
|
// PHI functions.
|
|
|
|
ConnectEpilog(L, ModVal, NewExit, Exit, PreHeader,
|
|
|
|
EpilogPreHeader, NewPreHeader, VMap, DT, LI,
|
|
|
|
PreserveLCSSA);
|
|
|
|
|
|
|
|
// Update counter in loop for unrolling.
|
|
|
|
// I should be multiply of Count.
|
|
|
|
IRBuilder<> B2(NewPreHeader->getTerminator());
|
|
|
|
Value *TestVal = B2.CreateSub(TripCount, ModVal, "unroll_iter");
|
|
|
|
BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
|
|
|
|
B2.SetInsertPoint(LatchBR);
|
|
|
|
PHINode *NewIdx = PHINode::Create(TestVal->getType(), 2, "niter",
|
|
|
|
Header->getFirstNonPHI());
|
|
|
|
Value *IdxSub =
|
|
|
|
B2.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1),
|
|
|
|
NewIdx->getName() + ".nsub");
|
|
|
|
Value *IdxCmp;
|
|
|
|
if (LatchBR->getSuccessor(0) == Header)
|
|
|
|
IdxCmp = B2.CreateIsNotNull(IdxSub, NewIdx->getName() + ".ncmp");
|
|
|
|
else
|
|
|
|
IdxCmp = B2.CreateIsNull(IdxSub, NewIdx->getName() + ".ncmp");
|
|
|
|
NewIdx->addIncoming(TestVal, NewPreHeader);
|
|
|
|
NewIdx->addIncoming(IdxSub, Latch);
|
|
|
|
LatchBR->setCondition(IdxCmp);
|
|
|
|
} else {
|
|
|
|
// Connect the prolog code to the original loop and update the
|
|
|
|
// PHI functions.
|
|
|
|
ConnectProlog(L, BECount, Count, PrologExit, PreHeader, NewPreHeader,
|
|
|
|
VMap, DT, LI, PreserveLCSSA);
|
|
|
|
}
|
2011-12-09 14:19:40 +08:00
|
|
|
NumRuntimeUnrolled++;
|
|
|
|
return true;
|
|
|
|
}
|