2012-12-12 05:25:42 +08:00
|
|
|
//===-- AMDILCFGStructurizer.cpp - CFG Structurizer -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
//==-----------------------------------------------------------------------===//
|
|
|
|
|
2013-06-08 04:37:48 +08:00
|
|
|
#include "AMDGPU.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "AMDGPUInstrInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "AMDGPUSubtarget.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "R600InstrInfo.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2013-01-02 18:22:59 +08:00
|
|
|
#include "llvm/CodeGen/MachinePostDominators.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2013-06-08 04:37:48 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include <deque>
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "structcfg"
|
|
|
|
|
2013-06-08 04:37:48 +08:00
|
|
|
#define DEFAULT_VEC_SLOTS 8
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
// TODO: move-begin.
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Statistics for CFGStructurizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
STATISTIC(numSerialPatternMatch, "CFGStructurizer number of serial pattern "
|
|
|
|
"matched");
|
|
|
|
STATISTIC(numIfPatternMatch, "CFGStructurizer number of if pattern "
|
|
|
|
"matched");
|
|
|
|
STATISTIC(numClonedBlock, "CFGStructurizer cloned blocks");
|
|
|
|
STATISTIC(numClonedInstr, "CFGStructurizer cloned instructions");
|
|
|
|
|
2013-12-12 01:51:47 +08:00
|
|
|
namespace llvm {
|
|
|
|
void initializeAMDGPUCFGStructurizerPass(PassRegistry&);
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Miscellaneous utility for CFGStructurizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-05-23 23:43:05 +08:00
|
|
|
namespace {
|
2012-12-12 05:25:42 +08:00
|
|
|
#define SHOWNEWINSTR(i) \
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(dbgs() << "New instr: " << *i << "\n");
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
#define SHOWNEWBLK(b, msg) \
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG( \
|
|
|
|
dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
|
|
|
|
dbgs() << "\n"; \
|
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
#define SHOWBLK_DETAIL(b, msg) \
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG( \
|
2012-12-12 05:25:42 +08:00
|
|
|
if (b) { \
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
|
|
|
|
b->print(dbgs()); \
|
|
|
|
dbgs() << "\n"; \
|
2012-12-12 05:25:42 +08:00
|
|
|
} \
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
#define INVALIDSCCNUM -1
|
|
|
|
|
|
|
|
template<class NodeT>
|
2013-07-14 12:42:23 +08:00
|
|
|
void ReverseVector(SmallVectorImpl<NodeT *> &Src) {
|
2012-12-12 05:25:42 +08:00
|
|
|
size_t sz = Src.size();
|
|
|
|
for (size_t i = 0; i < sz/2; ++i) {
|
|
|
|
NodeT *t = Src[i];
|
|
|
|
Src[i] = Src[sz - i - 1];
|
|
|
|
Src[sz - i - 1] = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
} // end anonymous namespace
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// supporting data structure for CFGStructurizer
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
namespace {
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
class BlockInformation {
|
|
|
|
public:
|
2013-07-20 05:45:06 +08:00
|
|
|
bool IsRetired;
|
|
|
|
int SccNum;
|
|
|
|
BlockInformation() : IsRetired(false), SccNum(INVALIDSCCNUM) {}
|
2012-12-12 05:25:42 +08:00
|
|
|
};
|
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
} // end anonymous namespace
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// CFGStructurizer
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
namespace {
|
2013-07-20 05:45:06 +08:00
|
|
|
class AMDGPUCFGStructurizer : public MachineFunctionPass {
|
2012-12-12 05:25:42 +08:00
|
|
|
public:
|
2013-07-20 05:45:06 +08:00
|
|
|
typedef SmallVector<MachineBasicBlock *, 32> MBBVector;
|
|
|
|
typedef std::map<MachineBasicBlock *, BlockInformation *> MBBInfoMap;
|
|
|
|
typedef std::map<MachineLoop *, MachineBasicBlock *> LoopLandInfoMap;
|
|
|
|
|
|
|
|
enum PathToKind {
|
2012-12-12 05:25:42 +08:00
|
|
|
Not_SinglePath = 0,
|
|
|
|
SinglePath_InPath = 1,
|
|
|
|
SinglePath_NotInPath = 2
|
2013-07-20 05:45:06 +08:00
|
|
|
};
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
static char ID;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-12-12 01:51:47 +08:00
|
|
|
AMDGPUCFGStructurizer() :
|
2014-04-25 13:30:21 +08:00
|
|
|
MachineFunctionPass(ID), TII(nullptr), TRI(nullptr) {
|
2013-12-12 01:51:47 +08:00
|
|
|
initializeAMDGPUCFGStructurizerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2013-12-12 01:51:47 +08:00
|
|
|
return "AMDGPU Control Flow Graph structurizer Pass";
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
|
|
|
|
2014-04-29 15:57:24 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-07-20 05:45:06 +08:00
|
|
|
AU.addRequired<MachineDominatorTree>();
|
|
|
|
AU.addRequired<MachinePostDominatorTree>();
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
2016-08-24 09:52:46 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
/// Perform the CFG structurization
|
2013-07-20 05:45:06 +08:00
|
|
|
bool run();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
/// Perform the CFG preparation
|
2013-07-20 05:45:06 +08:00
|
|
|
/// This step will remove every unconditionnal/dead jump instructions and make
|
|
|
|
/// sure all loops have an exit block
|
|
|
|
bool prepare();
|
|
|
|
|
2014-04-29 15:57:24 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override {
|
2016-06-24 14:30:11 +08:00
|
|
|
TII = MF.getSubtarget<R600Subtarget>().getInstrInfo();
|
2013-12-12 01:51:47 +08:00
|
|
|
TRI = &TII->getRegisterInfo();
|
2013-07-20 05:45:06 +08:00
|
|
|
DEBUG(MF.dump(););
|
|
|
|
OrderedBlks.clear();
|
2015-03-14 01:32:46 +08:00
|
|
|
Visited.clear();
|
2013-07-20 05:45:06 +08:00
|
|
|
FuncRep = &MF;
|
|
|
|
MLI = &getAnalysis<MachineLoopInfo>();
|
|
|
|
DEBUG(dbgs() << "LoopInfo:\n"; PrintLoopinfo(*MLI););
|
|
|
|
MDT = &getAnalysis<MachineDominatorTree>();
|
2014-04-28 12:05:08 +08:00
|
|
|
DEBUG(MDT->print(dbgs(), (const llvm::Module*)nullptr););
|
2013-07-20 05:45:06 +08:00
|
|
|
PDT = &getAnalysis<MachinePostDominatorTree>();
|
|
|
|
DEBUG(PDT->print(dbgs()););
|
|
|
|
prepare();
|
|
|
|
run();
|
|
|
|
DEBUG(MF.dump(););
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
protected:
|
|
|
|
MachineDominatorTree *MDT;
|
|
|
|
MachinePostDominatorTree *PDT;
|
|
|
|
MachineLoopInfo *MLI;
|
|
|
|
const R600InstrInfo *TII;
|
2015-09-19 14:41:10 +08:00
|
|
|
const R600RegisterInfo *TRI;
|
2013-07-20 05:45:06 +08:00
|
|
|
|
|
|
|
// PRINT FUNCTIONS
|
|
|
|
/// Print the ordered Blocks.
|
|
|
|
void printOrderedBlocks() const {
|
|
|
|
size_t i = 0;
|
|
|
|
for (MBBVector::const_iterator iterBlk = OrderedBlks.begin(),
|
|
|
|
iterBlkEnd = OrderedBlks.end(); iterBlk != iterBlkEnd; ++iterBlk, ++i) {
|
|
|
|
dbgs() << "BB" << (*iterBlk)->getNumber();
|
|
|
|
dbgs() << "(" << getSCCNum(*iterBlk) << "," << (*iterBlk)->size() << ")";
|
|
|
|
if (i != 0 && i % 10 == 0) {
|
|
|
|
dbgs() << "\n";
|
|
|
|
} else {
|
|
|
|
dbgs() << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void PrintLoopinfo(const MachineLoopInfo &LoopInfo) {
|
|
|
|
for (MachineLoop::iterator iter = LoopInfo.begin(),
|
|
|
|
iterEnd = LoopInfo.end(); iter != iterEnd; ++iter) {
|
|
|
|
(*iter)->print(dbgs(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UTILITY FUNCTIONS
|
|
|
|
int getSCCNum(MachineBasicBlock *MBB) const;
|
|
|
|
MachineBasicBlock *getLoopLandInfo(MachineLoop *LoopRep) const;
|
|
|
|
bool hasBackEdge(MachineBasicBlock *MBB) const;
|
|
|
|
bool isRetiredBlock(MachineBasicBlock *MBB) const;
|
|
|
|
bool isActiveLoophead(MachineBasicBlock *MBB) const;
|
|
|
|
PathToKind singlePathTo(MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB,
|
|
|
|
bool AllowSideEntry = true) const;
|
|
|
|
int countActiveBlock(MBBVector::const_iterator It,
|
|
|
|
MBBVector::const_iterator E) const;
|
|
|
|
bool needMigrateBlock(MachineBasicBlock *MBB) const;
|
|
|
|
|
|
|
|
// Utility Functions
|
2016-08-13 09:12:49 +08:00
|
|
|
void reversePredicateSetter(MachineBasicBlock::iterator I,
|
|
|
|
MachineBasicBlock &MBB);
|
2013-07-20 05:45:06 +08:00
|
|
|
/// Compute the reversed DFS post order of Blocks
|
|
|
|
void orderBlocks(MachineFunction *MF);
|
|
|
|
|
2014-01-25 01:20:08 +08:00
|
|
|
// Function originally from CFGStructTraits
|
2013-07-20 05:45:06 +08:00
|
|
|
void insertInstrEnd(MachineBasicBlock *MBB, int NewOpcode,
|
2016-06-12 23:39:02 +08:00
|
|
|
const DebugLoc &DL = DebugLoc());
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineInstr *insertInstrBefore(MachineBasicBlock *MBB, int NewOpcode,
|
2016-06-12 23:39:02 +08:00
|
|
|
const DebugLoc &DL = DebugLoc());
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineInstr *insertInstrBefore(MachineBasicBlock::iterator I, int NewOpcode);
|
|
|
|
void insertCondBranchBefore(MachineBasicBlock::iterator I, int NewOpcode,
|
2016-06-12 23:39:02 +08:00
|
|
|
const DebugLoc &DL);
|
2013-07-20 05:45:06 +08:00
|
|
|
void insertCondBranchBefore(MachineBasicBlock *MBB,
|
2016-06-12 23:39:02 +08:00
|
|
|
MachineBasicBlock::iterator I, int NewOpcode,
|
|
|
|
int RegNum, const DebugLoc &DL);
|
2013-07-20 05:45:06 +08:00
|
|
|
static int getBranchNzeroOpcode(int OldOpcode);
|
|
|
|
static int getBranchZeroOpcode(int OldOpcode);
|
|
|
|
static int getContinueNzeroOpcode(int OldOpcode);
|
|
|
|
static int getContinueZeroOpcode(int OldOpcode);
|
|
|
|
static MachineBasicBlock *getTrueBranch(MachineInstr *MI);
|
|
|
|
static void setTrueBranch(MachineInstr *MI, MachineBasicBlock *MBB);
|
|
|
|
static MachineBasicBlock *getFalseBranch(MachineBasicBlock *MBB,
|
|
|
|
MachineInstr *MI);
|
|
|
|
static bool isCondBranch(MachineInstr *MI);
|
|
|
|
static bool isUncondBranch(MachineInstr *MI);
|
|
|
|
static DebugLoc getLastDebugLocInBB(MachineBasicBlock *MBB);
|
|
|
|
static MachineInstr *getNormalBlockBranchInstr(MachineBasicBlock *MBB);
|
|
|
|
/// The correct naming for this is getPossibleLoopendBlockBranchInstr.
|
|
|
|
///
|
|
|
|
/// BB with backward-edge could have move instructions after the branch
|
|
|
|
/// instruction. Such move instruction "belong to" the loop backward-edge.
|
|
|
|
MachineInstr *getLoopendBlockBranchInstr(MachineBasicBlock *MBB);
|
|
|
|
static MachineInstr *getReturnInstr(MachineBasicBlock *MBB);
|
|
|
|
static bool isReturnBlock(MachineBasicBlock *MBB);
|
|
|
|
static void cloneSuccessorList(MachineBasicBlock *DstMBB,
|
|
|
|
MachineBasicBlock *SrcMBB) ;
|
|
|
|
static MachineBasicBlock *clone(MachineBasicBlock *MBB);
|
|
|
|
/// MachineBasicBlock::ReplaceUsesOfBlockWith doesn't serve the purpose
|
|
|
|
/// because the AMDGPU instruction is not recognized as terminator fix this
|
|
|
|
/// and retire this routine
|
|
|
|
void replaceInstrUseOfBlockWith(MachineBasicBlock *SrcMBB,
|
|
|
|
MachineBasicBlock *OldMBB, MachineBasicBlock *NewBlk);
|
|
|
|
static void wrapup(MachineBasicBlock *MBB);
|
|
|
|
|
|
|
|
|
|
|
|
int patternMatch(MachineBasicBlock *MBB);
|
|
|
|
int patternMatchGroup(MachineBasicBlock *MBB);
|
|
|
|
int serialPatternMatch(MachineBasicBlock *MBB);
|
|
|
|
int ifPatternMatch(MachineBasicBlock *MBB);
|
|
|
|
int loopendPatternMatch();
|
|
|
|
int mergeLoop(MachineLoop *LoopRep);
|
|
|
|
|
|
|
|
/// return true iff src1Blk->succ_size() == 0 && src1Blk and src2Blk are in
|
|
|
|
/// the same loop with LoopLandInfo without explicitly keeping track of
|
|
|
|
/// loopContBlks and loopBreakBlks, this is a method to get the information.
|
|
|
|
bool isSameloopDetachedContbreak(MachineBasicBlock *Src1MBB,
|
|
|
|
MachineBasicBlock *Src2MBB);
|
|
|
|
int handleJumpintoIf(MachineBasicBlock *HeadMBB,
|
|
|
|
MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB);
|
|
|
|
int handleJumpintoIfImp(MachineBasicBlock *HeadMBB,
|
|
|
|
MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB);
|
|
|
|
int improveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
|
|
|
|
MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
|
|
|
|
MachineBasicBlock **LandMBBPtr);
|
|
|
|
void showImproveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
|
|
|
|
MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
|
|
|
|
MachineBasicBlock *LandMBB, bool Detail = false);
|
|
|
|
int cloneOnSideEntryTo(MachineBasicBlock *PreMBB,
|
|
|
|
MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB);
|
|
|
|
void mergeSerialBlock(MachineBasicBlock *DstMBB,
|
|
|
|
MachineBasicBlock *SrcMBB);
|
|
|
|
|
|
|
|
void mergeIfthenelseBlock(MachineInstr *BranchMI,
|
|
|
|
MachineBasicBlock *MBB, MachineBasicBlock *TrueMBB,
|
|
|
|
MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB);
|
|
|
|
void mergeLooplandBlock(MachineBasicBlock *DstMBB,
|
|
|
|
MachineBasicBlock *LandMBB);
|
|
|
|
void mergeLoopbreakBlock(MachineBasicBlock *ExitingMBB,
|
|
|
|
MachineBasicBlock *LandMBB);
|
|
|
|
void settleLoopcontBlock(MachineBasicBlock *ContingMBB,
|
|
|
|
MachineBasicBlock *ContMBB);
|
|
|
|
/// normalizeInfiniteLoopExit change
|
|
|
|
/// B1:
|
|
|
|
/// uncond_br LoopHeader
|
|
|
|
///
|
|
|
|
/// to
|
|
|
|
/// B1:
|
|
|
|
/// cond_br 1 LoopHeader dummyExit
|
|
|
|
/// and return the newly added dummy exit block
|
|
|
|
MachineBasicBlock *normalizeInfiniteLoopExit(MachineLoop *LoopRep);
|
|
|
|
void removeUnconditionalBranch(MachineBasicBlock *MBB);
|
|
|
|
/// Remove duplicate branches instructions in a block.
|
|
|
|
/// For instance
|
|
|
|
/// B0:
|
|
|
|
/// cond_br X B1 B2
|
|
|
|
/// cond_br X B1 B2
|
|
|
|
/// is transformed to
|
|
|
|
/// B0:
|
|
|
|
/// cond_br X B1 B2
|
|
|
|
void removeRedundantConditionalBranch(MachineBasicBlock *MBB);
|
|
|
|
void addDummyExitBlock(SmallVectorImpl<MachineBasicBlock *> &RetMBB);
|
|
|
|
void removeSuccessor(MachineBasicBlock *MBB);
|
|
|
|
MachineBasicBlock *cloneBlockForPredecessor(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock *PredMBB);
|
|
|
|
void migrateInstruction(MachineBasicBlock *SrcMBB,
|
|
|
|
MachineBasicBlock *DstMBB, MachineBasicBlock::iterator I);
|
|
|
|
void recordSccnum(MachineBasicBlock *MBB, int SCCNum);
|
|
|
|
void retireBlock(MachineBasicBlock *MBB);
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
private:
|
2013-07-20 05:45:06 +08:00
|
|
|
MBBInfoMap BlockInfoMap;
|
|
|
|
LoopLandInfoMap LLInfoMap;
|
|
|
|
std::map<MachineLoop *, bool> Visited;
|
|
|
|
MachineFunction *FuncRep;
|
|
|
|
SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> OrderedBlks;
|
|
|
|
};
|
|
|
|
|
|
|
|
int AMDGPUCFGStructurizer::getSCCNum(MachineBasicBlock *MBB) const {
|
|
|
|
MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
|
|
|
|
if (It == BlockInfoMap.end())
|
|
|
|
return INVALIDSCCNUM;
|
|
|
|
return (*It).second->SccNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *AMDGPUCFGStructurizer::getLoopLandInfo(MachineLoop *LoopRep)
|
|
|
|
const {
|
|
|
|
LoopLandInfoMap::const_iterator It = LLInfoMap.find(LoopRep);
|
|
|
|
if (It == LLInfoMap.end())
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
return (*It).second;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
bool AMDGPUCFGStructurizer::hasBackEdge(MachineBasicBlock *MBB) const {
|
|
|
|
MachineLoop *LoopRep = MLI->getLoopFor(MBB);
|
|
|
|
if (!LoopRep)
|
|
|
|
return false;
|
|
|
|
MachineBasicBlock *LoopHeader = LoopRep->getHeader();
|
|
|
|
return MBB->isSuccessor(LoopHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AMDGPUCFGStructurizer::isRetiredBlock(MachineBasicBlock *MBB) const {
|
|
|
|
MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
|
|
|
|
if (It == BlockInfoMap.end())
|
|
|
|
return false;
|
|
|
|
return (*It).second->IsRetired;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
bool AMDGPUCFGStructurizer::isActiveLoophead(MachineBasicBlock *MBB) const {
|
|
|
|
MachineLoop *LoopRep = MLI->getLoopFor(MBB);
|
|
|
|
while (LoopRep && LoopRep->getHeader() == MBB) {
|
|
|
|
MachineBasicBlock *LoopLand = getLoopLandInfo(LoopRep);
|
|
|
|
if(!LoopLand)
|
|
|
|
return true;
|
|
|
|
if (!isRetiredBlock(LoopLand))
|
|
|
|
return true;
|
|
|
|
LoopRep = LoopRep->getParentLoop();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
AMDGPUCFGStructurizer::PathToKind AMDGPUCFGStructurizer::singlePathTo(
|
|
|
|
MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB,
|
|
|
|
bool AllowSideEntry) const {
|
|
|
|
assert(DstMBB);
|
|
|
|
if (SrcMBB == DstMBB)
|
|
|
|
return SinglePath_InPath;
|
|
|
|
while (SrcMBB && SrcMBB->succ_size() == 1) {
|
|
|
|
SrcMBB = *SrcMBB->succ_begin();
|
|
|
|
if (SrcMBB == DstMBB)
|
|
|
|
return SinglePath_InPath;
|
|
|
|
if (!AllowSideEntry && SrcMBB->pred_size() > 1)
|
|
|
|
return Not_SinglePath;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
if (SrcMBB && SrcMBB->succ_size()==0)
|
|
|
|
return SinglePath_NotInPath;
|
|
|
|
return Not_SinglePath;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::countActiveBlock(MBBVector::const_iterator It,
|
|
|
|
MBBVector::const_iterator E) const {
|
|
|
|
int Count = 0;
|
|
|
|
while (It != E) {
|
|
|
|
if (!isRetiredBlock(*It))
|
|
|
|
++Count;
|
|
|
|
++It;
|
|
|
|
}
|
|
|
|
return Count;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
bool AMDGPUCFGStructurizer::needMigrateBlock(MachineBasicBlock *MBB) const {
|
|
|
|
unsigned BlockSizeThreshold = 30;
|
|
|
|
unsigned CloneInstrThreshold = 100;
|
|
|
|
bool MultiplePreds = MBB && (MBB->pred_size() > 1);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if(!MultiplePreds)
|
|
|
|
return false;
|
|
|
|
unsigned BlkSize = MBB->size();
|
|
|
|
return ((BlkSize > BlockSizeThreshold) &&
|
|
|
|
(BlkSize * (MBB->pred_size() - 1) > CloneInstrThreshold));
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::reversePredicateSetter(
|
2016-08-13 09:12:49 +08:00
|
|
|
MachineBasicBlock::iterator I, MachineBasicBlock &MBB) {
|
ADT: Remove all ilist_iterator => pointer casts, NFC
Remove all ilist_iterator to pointer casts. There were two reasons for
casts:
- Checking for an uninitialized (i.e., null) iterator. I added
MachineInstrBundleIterator::isValid() to check for that case.
- Comparing an iterator against the underlying pointer value while
avoiding converting the pointer value to an iterator. This is
occasionally necessary in MachineInstrBundleIterator, since there is
an assertion in the constructors that the underlying MachineInstr is
not bundled (but we don't care about that if we're just checking for
pointer equality).
To support the latter case, I rewrote the == and != operators for
ilist_iterator and MachineInstrBundleIterator.
- The implicit constructors now use enable_if to exclude
const-iterator => non-const-iterator conversions from overload
resolution (previously it was a compiler error on instantiation, now
it's SFINAE).
- The == and != operators are now global (friends), and are not
templated.
- MachineInstrBundleIterator has overloads to compare against both
const_pointer and const_reference. This avoids the implicit
conversions to MachineInstrBundleIterator that assert, instead just
checking the address (and I added unit tests to confirm this).
Notably, the only remaining uses of ilist_iterator::getNodePtrUnchecked
are in ilist.h, and no code outside of ilist*.h directly relies on this
UB end-iterator-to-pointer conversion anymore. It's still needed for
ilist_*sentinel_traits, but I'll clean that up soon.
llvm-svn: 278478
2016-08-12 13:05:36 +08:00
|
|
|
assert(I.isValid() && "Expected valid iterator");
|
2016-07-09 03:00:17 +08:00
|
|
|
for (;; --I) {
|
2016-08-13 09:12:49 +08:00
|
|
|
if (I == MBB.end())
|
|
|
|
continue;
|
2013-07-20 05:45:06 +08:00
|
|
|
if (I->getOpcode() == AMDGPU::PRED_X) {
|
ADT: Remove all ilist_iterator => pointer casts, NFC
Remove all ilist_iterator to pointer casts. There were two reasons for
casts:
- Checking for an uninitialized (i.e., null) iterator. I added
MachineInstrBundleIterator::isValid() to check for that case.
- Comparing an iterator against the underlying pointer value while
avoiding converting the pointer value to an iterator. This is
occasionally necessary in MachineInstrBundleIterator, since there is
an assertion in the constructors that the underlying MachineInstr is
not bundled (but we don't care about that if we're just checking for
pointer equality).
To support the latter case, I rewrote the == and != operators for
ilist_iterator and MachineInstrBundleIterator.
- The implicit constructors now use enable_if to exclude
const-iterator => non-const-iterator conversions from overload
resolution (previously it was a compiler error on instantiation, now
it's SFINAE).
- The == and != operators are now global (friends), and are not
templated.
- MachineInstrBundleIterator has overloads to compare against both
const_pointer and const_reference. This avoids the implicit
conversions to MachineInstrBundleIterator that assert, instead just
checking the address (and I added unit tests to confirm this).
Notably, the only remaining uses of ilist_iterator::getNodePtrUnchecked
are in ilist.h, and no code outside of ilist*.h directly relies on this
UB end-iterator-to-pointer conversion anymore. It's still needed for
ilist_*sentinel_traits, but I'll clean that up soon.
llvm-svn: 278478
2016-08-12 13:05:36 +08:00
|
|
|
switch (I->getOperand(2).getImm()) {
|
2016-08-13 09:43:46 +08:00
|
|
|
case AMDGPU::PRED_SETE_INT:
|
|
|
|
I->getOperand(2).setImm(AMDGPU::PRED_SETNE_INT);
|
2013-07-20 05:45:06 +08:00
|
|
|
return;
|
2016-08-13 09:43:46 +08:00
|
|
|
case AMDGPU::PRED_SETNE_INT:
|
|
|
|
I->getOperand(2).setImm(AMDGPU::PRED_SETE_INT);
|
2013-07-20 05:45:06 +08:00
|
|
|
return;
|
2016-08-13 09:43:46 +08:00
|
|
|
case AMDGPU::PRED_SETE:
|
|
|
|
I->getOperand(2).setImm(AMDGPU::PRED_SETNE);
|
2013-07-20 05:45:06 +08:00
|
|
|
return;
|
2016-08-13 09:43:46 +08:00
|
|
|
case AMDGPU::PRED_SETNE:
|
|
|
|
I->getOperand(2).setImm(AMDGPU::PRED_SETE);
|
2013-07-20 05:45:06 +08:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("PRED_X Opcode invalid!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::insertInstrEnd(MachineBasicBlock *MBB,
|
2016-06-12 23:39:02 +08:00
|
|
|
int NewOpcode, const DebugLoc &DL) {
|
|
|
|
MachineInstr *MI =
|
|
|
|
MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DL);
|
2013-07-20 05:45:06 +08:00
|
|
|
MBB->push_back(MI);
|
|
|
|
//assume the instruction doesn't take any reg operand ...
|
|
|
|
SHOWNEWINSTR(MI);
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineInstr *AMDGPUCFGStructurizer::insertInstrBefore(MachineBasicBlock *MBB,
|
2016-06-12 23:39:02 +08:00
|
|
|
int NewOpcode,
|
|
|
|
const DebugLoc &DL) {
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineInstr *MI =
|
|
|
|
MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DL);
|
|
|
|
if (MBB->begin() != MBB->end())
|
|
|
|
MBB->insert(MBB->begin(), MI);
|
|
|
|
else
|
|
|
|
MBB->push_back(MI);
|
|
|
|
SHOWNEWINSTR(MI);
|
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *AMDGPUCFGStructurizer::insertInstrBefore(
|
|
|
|
MachineBasicBlock::iterator I, int NewOpcode) {
|
|
|
|
MachineInstr *OldMI = &(*I);
|
|
|
|
MachineBasicBlock *MBB = OldMI->getParent();
|
|
|
|
MachineInstr *NewMBB =
|
|
|
|
MBB->getParent()->CreateMachineInstr(TII->get(NewOpcode), DebugLoc());
|
|
|
|
MBB->insert(I, NewMBB);
|
|
|
|
//assume the instruction doesn't take any reg operand ...
|
|
|
|
SHOWNEWINSTR(NewMBB);
|
|
|
|
return NewMBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUCFGStructurizer::insertCondBranchBefore(
|
2016-06-12 23:39:02 +08:00
|
|
|
MachineBasicBlock::iterator I, int NewOpcode, const DebugLoc &DL) {
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineInstr *OldMI = &(*I);
|
|
|
|
MachineBasicBlock *MBB = OldMI->getParent();
|
|
|
|
MachineFunction *MF = MBB->getParent();
|
|
|
|
MachineInstr *NewMI = MF->CreateMachineInstr(TII->get(NewOpcode), DL);
|
|
|
|
MBB->insert(I, NewMI);
|
|
|
|
MachineInstrBuilder MIB(*MF, NewMI);
|
|
|
|
MIB.addReg(OldMI->getOperand(1).getReg(), false);
|
|
|
|
SHOWNEWINSTR(NewMI);
|
|
|
|
//erase later oldInstr->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:39:02 +08:00
|
|
|
void AMDGPUCFGStructurizer::insertCondBranchBefore(
|
|
|
|
MachineBasicBlock *blk, MachineBasicBlock::iterator I, int NewOpcode,
|
|
|
|
int RegNum, const DebugLoc &DL) {
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineFunction *MF = blk->getParent();
|
|
|
|
MachineInstr *NewInstr = MF->CreateMachineInstr(TII->get(NewOpcode), DL);
|
|
|
|
//insert before
|
|
|
|
blk->insert(I, NewInstr);
|
|
|
|
MachineInstrBuilder(*MF, NewInstr).addReg(RegNum, false);
|
|
|
|
SHOWNEWINSTR(NewInstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int AMDGPUCFGStructurizer::getBranchNzeroOpcode(int OldOpcode) {
|
|
|
|
switch(OldOpcode) {
|
|
|
|
case AMDGPU::JUMP_COND:
|
|
|
|
case AMDGPU::JUMP: return AMDGPU::IF_PREDICATE_SET;
|
|
|
|
case AMDGPU::BRANCH_COND_i32:
|
|
|
|
case AMDGPU::BRANCH_COND_f32: return AMDGPU::IF_LOGICALNZ_f32;
|
|
|
|
default: llvm_unreachable("internal error");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AMDGPUCFGStructurizer::getBranchZeroOpcode(int OldOpcode) {
|
|
|
|
switch(OldOpcode) {
|
|
|
|
case AMDGPU::JUMP_COND:
|
|
|
|
case AMDGPU::JUMP: return AMDGPU::IF_PREDICATE_SET;
|
|
|
|
case AMDGPU::BRANCH_COND_i32:
|
|
|
|
case AMDGPU::BRANCH_COND_f32: return AMDGPU::IF_LOGICALZ_f32;
|
|
|
|
default: llvm_unreachable("internal error");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AMDGPUCFGStructurizer::getContinueNzeroOpcode(int OldOpcode) {
|
|
|
|
switch(OldOpcode) {
|
|
|
|
case AMDGPU::JUMP_COND:
|
|
|
|
case AMDGPU::JUMP: return AMDGPU::CONTINUE_LOGICALNZ_i32;
|
|
|
|
default: llvm_unreachable("internal error");
|
|
|
|
};
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AMDGPUCFGStructurizer::getContinueZeroOpcode(int OldOpcode) {
|
|
|
|
switch(OldOpcode) {
|
|
|
|
case AMDGPU::JUMP_COND:
|
|
|
|
case AMDGPU::JUMP: return AMDGPU::CONTINUE_LOGICALZ_i32;
|
|
|
|
default: llvm_unreachable("internal error");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *AMDGPUCFGStructurizer::getTrueBranch(MachineInstr *MI) {
|
|
|
|
return MI->getOperand(0).getMBB();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUCFGStructurizer::setTrueBranch(MachineInstr *MI,
|
|
|
|
MachineBasicBlock *MBB) {
|
|
|
|
MI->getOperand(0).setMBB(MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *
|
|
|
|
AMDGPUCFGStructurizer::getFalseBranch(MachineBasicBlock *MBB,
|
|
|
|
MachineInstr *MI) {
|
|
|
|
assert(MBB->succ_size() == 2);
|
|
|
|
MachineBasicBlock *TrueBranch = getTrueBranch(MI);
|
|
|
|
MachineBasicBlock::succ_iterator It = MBB->succ_begin();
|
|
|
|
MachineBasicBlock::succ_iterator Next = It;
|
|
|
|
++Next;
|
|
|
|
return (*It == TrueBranch) ? *Next : *It;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AMDGPUCFGStructurizer::isCondBranch(MachineInstr *MI) {
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case AMDGPU::JUMP_COND:
|
|
|
|
case AMDGPU::BRANCH_COND_i32:
|
|
|
|
case AMDGPU::BRANCH_COND_f32: return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AMDGPUCFGStructurizer::isUncondBranch(MachineInstr *MI) {
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case AMDGPU::JUMP:
|
|
|
|
case AMDGPU::BRANCH:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugLoc AMDGPUCFGStructurizer::getLastDebugLocInBB(MachineBasicBlock *MBB) {
|
|
|
|
//get DebugLoc from the first MachineBasicBlock instruction with debug info
|
|
|
|
DebugLoc DL;
|
|
|
|
for (MachineBasicBlock::iterator It = MBB->begin(); It != MBB->end();
|
|
|
|
++It) {
|
|
|
|
MachineInstr *instr = &(*It);
|
2015-03-31 03:14:47 +08:00
|
|
|
if (instr->getDebugLoc())
|
2013-07-20 05:45:06 +08:00
|
|
|
DL = instr->getDebugLoc();
|
|
|
|
}
|
|
|
|
return DL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *AMDGPUCFGStructurizer::getNormalBlockBranchInstr(
|
|
|
|
MachineBasicBlock *MBB) {
|
|
|
|
MachineBasicBlock::reverse_iterator It = MBB->rbegin();
|
|
|
|
MachineInstr *MI = &*It;
|
|
|
|
if (MI && (isCondBranch(MI) || isUncondBranch(MI)))
|
|
|
|
return MI;
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *AMDGPUCFGStructurizer::getLoopendBlockBranchInstr(
|
|
|
|
MachineBasicBlock *MBB) {
|
|
|
|
for (MachineBasicBlock::reverse_iterator It = MBB->rbegin(), E = MBB->rend();
|
|
|
|
It != E; ++It) {
|
|
|
|
// FIXME: Simplify
|
|
|
|
MachineInstr *MI = &*It;
|
|
|
|
if (MI) {
|
|
|
|
if (isCondBranch(MI) || isUncondBranch(MI))
|
|
|
|
return MI;
|
|
|
|
else if (!TII->isMov(MI->getOpcode()))
|
|
|
|
break;
|
2013-07-20 05:44:56 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *AMDGPUCFGStructurizer::getReturnInstr(MachineBasicBlock *MBB) {
|
|
|
|
MachineBasicBlock::reverse_iterator It = MBB->rbegin();
|
|
|
|
if (It != MBB->rend()) {
|
|
|
|
MachineInstr *instr = &(*It);
|
|
|
|
if (instr->getOpcode() == AMDGPU::RETURN)
|
|
|
|
return instr;
|
|
|
|
}
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AMDGPUCFGStructurizer::isReturnBlock(MachineBasicBlock *MBB) {
|
|
|
|
MachineInstr *MI = getReturnInstr(MBB);
|
|
|
|
bool IsReturn = (MBB->succ_size() == 0);
|
|
|
|
if (MI)
|
|
|
|
assert(IsReturn);
|
|
|
|
else if (IsReturn)
|
|
|
|
DEBUG(
|
|
|
|
dbgs() << "BB" << MBB->getNumber()
|
|
|
|
<<" is return block without RETURN instr\n";);
|
|
|
|
return IsReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUCFGStructurizer::cloneSuccessorList(MachineBasicBlock *DstMBB,
|
|
|
|
MachineBasicBlock *SrcMBB) {
|
|
|
|
for (MachineBasicBlock::succ_iterator It = SrcMBB->succ_begin(),
|
|
|
|
iterEnd = SrcMBB->succ_end(); It != iterEnd; ++It)
|
|
|
|
DstMBB->addSuccessor(*It); // *iter's predecessor is also taken care of
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *AMDGPUCFGStructurizer::clone(MachineBasicBlock *MBB) {
|
|
|
|
MachineFunction *Func = MBB->getParent();
|
|
|
|
MachineBasicBlock *NewMBB = Func->CreateMachineBasicBlock();
|
|
|
|
Func->push_back(NewMBB); //insert to function
|
2016-07-09 03:16:05 +08:00
|
|
|
for (const MachineInstr &It : *MBB)
|
|
|
|
NewMBB->push_back(Func->CloneMachineInstr(&It));
|
2013-07-20 05:45:06 +08:00
|
|
|
return NewMBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUCFGStructurizer::replaceInstrUseOfBlockWith(
|
|
|
|
MachineBasicBlock *SrcMBB, MachineBasicBlock *OldMBB,
|
|
|
|
MachineBasicBlock *NewBlk) {
|
|
|
|
MachineInstr *BranchMI = getLoopendBlockBranchInstr(SrcMBB);
|
|
|
|
if (BranchMI && isCondBranch(BranchMI) &&
|
|
|
|
getTrueBranch(BranchMI) == OldMBB)
|
|
|
|
setTrueBranch(BranchMI, NewBlk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUCFGStructurizer::wrapup(MachineBasicBlock *MBB) {
|
|
|
|
assert((!MBB->getParent()->getJumpTableInfo()
|
|
|
|
|| MBB->getParent()->getJumpTableInfo()->isEmpty())
|
|
|
|
&& "found a jump table");
|
|
|
|
|
|
|
|
//collect continue right before endloop
|
|
|
|
SmallVector<MachineInstr *, DEFAULT_VEC_SLOTS> ContInstr;
|
|
|
|
MachineBasicBlock::iterator Pre = MBB->begin();
|
|
|
|
MachineBasicBlock::iterator E = MBB->end();
|
|
|
|
MachineBasicBlock::iterator It = Pre;
|
|
|
|
while (It != E) {
|
|
|
|
if (Pre->getOpcode() == AMDGPU::CONTINUE
|
|
|
|
&& It->getOpcode() == AMDGPU::ENDLOOP)
|
2016-07-09 03:16:05 +08:00
|
|
|
ContInstr.push_back(&*Pre);
|
2013-07-20 05:45:06 +08:00
|
|
|
Pre = It;
|
|
|
|
++It;
|
|
|
|
}
|
|
|
|
|
|
|
|
//delete continue right before endloop
|
|
|
|
for (unsigned i = 0; i < ContInstr.size(); ++i)
|
|
|
|
ContInstr[i]->eraseFromParent();
|
|
|
|
|
|
|
|
// TODO to fix up jump table so later phase won't be confused. if
|
|
|
|
// (jumpTableInfo->isEmpty() == false) { need to clean the jump table, but
|
|
|
|
// there isn't such an interface yet. alternatively, replace all the other
|
|
|
|
// blocks in the jump table with the entryBlk //}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AMDGPUCFGStructurizer::prepare() {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
//FIXME: if not reducible flow graph, make it so ???
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
DEBUG(dbgs() << "AMDGPUCFGStructurizer::prepare\n";);
|
|
|
|
|
|
|
|
orderBlocks(FuncRep);
|
|
|
|
|
|
|
|
SmallVector<MachineBasicBlock *, DEFAULT_VEC_SLOTS> RetBlks;
|
|
|
|
|
|
|
|
// Add an ExitBlk to loop that don't have one
|
|
|
|
for (MachineLoopInfo::iterator It = MLI->begin(),
|
|
|
|
E = MLI->end(); It != E; ++It) {
|
|
|
|
MachineLoop *LoopRep = (*It);
|
|
|
|
MBBVector ExitingMBBs;
|
|
|
|
LoopRep->getExitingBlocks(ExitingMBBs);
|
|
|
|
|
|
|
|
if (ExitingMBBs.size() == 0) {
|
|
|
|
MachineBasicBlock* DummyExitBlk = normalizeInfiniteLoopExit(LoopRep);
|
|
|
|
if (DummyExitBlk)
|
|
|
|
RetBlks.push_back(DummyExitBlk);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove unconditional branch instr.
|
|
|
|
// Add dummy exit block iff there are multiple returns.
|
2013-07-20 05:45:06 +08:00
|
|
|
for (SmallVectorImpl<MachineBasicBlock *>::const_iterator
|
|
|
|
It = OrderedBlks.begin(), E = OrderedBlks.end(); It != E; ++It) {
|
|
|
|
MachineBasicBlock *MBB = *It;
|
|
|
|
removeUnconditionalBranch(MBB);
|
|
|
|
removeRedundantConditionalBranch(MBB);
|
|
|
|
if (isReturnBlock(MBB)) {
|
|
|
|
RetBlks.push_back(MBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
assert(MBB->succ_size() <= 2);
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (RetBlks.size() >= 2) {
|
|
|
|
addDummyExitBlock(RetBlks);
|
|
|
|
Changed = true;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
bool AMDGPUCFGStructurizer::run() {
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
//Assume reducible CFG...
|
2014-03-25 04:29:02 +08:00
|
|
|
DEBUG(dbgs() << "AMDGPUCFGStructurizer::run\n");
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
#ifdef STRESSTEST
|
|
|
|
//Use the worse block ordering to test the algorithm.
|
|
|
|
ReverseVector(orderedBlks);
|
|
|
|
#endif
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
DEBUG(dbgs() << "Ordered blocks:\n"; printOrderedBlocks(););
|
|
|
|
int NumIter = 0;
|
|
|
|
bool Finish = false;
|
|
|
|
MachineBasicBlock *MBB;
|
|
|
|
bool MakeProgress = false;
|
|
|
|
int NumRemainedBlk = countActiveBlock(OrderedBlks.begin(),
|
|
|
|
OrderedBlks.end());
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
do {
|
2013-07-20 05:45:06 +08:00
|
|
|
++NumIter;
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "numIter = " << NumIter
|
|
|
|
<< ", numRemaintedBlk = " << NumRemainedBlk << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
SmallVectorImpl<MachineBasicBlock *>::const_iterator It =
|
|
|
|
OrderedBlks.begin();
|
|
|
|
SmallVectorImpl<MachineBasicBlock *>::const_iterator E =
|
|
|
|
OrderedBlks.end();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
SmallVectorImpl<MachineBasicBlock *>::const_iterator SccBeginIter =
|
|
|
|
It;
|
2014-04-25 13:30:21 +08:00
|
|
|
MachineBasicBlock *SccBeginMBB = nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
int SccNumBlk = 0; // The number of active blocks, init to a
|
2012-12-12 05:25:42 +08:00
|
|
|
// maximum possible number.
|
2013-07-20 05:45:06 +08:00
|
|
|
int SccNumIter; // Number of iteration in this SCC.
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
while (It != E) {
|
|
|
|
MBB = *It;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (!SccBeginMBB) {
|
|
|
|
SccBeginIter = It;
|
|
|
|
SccBeginMBB = MBB;
|
|
|
|
SccNumIter = 0;
|
|
|
|
SccNumBlk = NumRemainedBlk; // Init to maximum possible number.
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "start processing SCC" << getSCCNum(SccBeginMBB);
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (!isRetiredBlock(MBB))
|
|
|
|
patternMatch(MBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
++It;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
bool ContNextScc = true;
|
|
|
|
if (It == E
|
|
|
|
|| getSCCNum(SccBeginMBB) != getSCCNum(*It)) {
|
2012-12-12 05:25:42 +08:00
|
|
|
// Just finish one scc.
|
2013-07-20 05:45:06 +08:00
|
|
|
++SccNumIter;
|
|
|
|
int sccRemainedNumBlk = countActiveBlock(SccBeginIter, It);
|
|
|
|
if (sccRemainedNumBlk != 1 && sccRemainedNumBlk >= SccNumBlk) {
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "Can't reduce SCC " << getSCCNum(MBB)
|
|
|
|
<< ", sccNumIter = " << SccNumIter;
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "doesn't make any progress\n";
|
|
|
|
);
|
2013-07-20 05:45:06 +08:00
|
|
|
ContNextScc = true;
|
|
|
|
} else if (sccRemainedNumBlk != 1 && sccRemainedNumBlk < SccNumBlk) {
|
|
|
|
SccNumBlk = sccRemainedNumBlk;
|
|
|
|
It = SccBeginIter;
|
|
|
|
ContNextScc = false;
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "repeat processing SCC" << getSCCNum(MBB)
|
2014-03-25 04:29:02 +08:00
|
|
|
<< "sccNumIter = " << SccNumIter << '\n';
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
|
|
|
// Finish the current scc.
|
2013-07-20 05:45:06 +08:00
|
|
|
ContNextScc = true;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Continue on next component in the current scc.
|
2013-07-20 05:45:06 +08:00
|
|
|
ContNextScc = false;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (ContNextScc)
|
2014-04-25 13:30:21 +08:00
|
|
|
SccBeginMBB = nullptr;
|
2012-12-12 05:25:42 +08:00
|
|
|
} //while, "one iteration" over the function.
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *EntryMBB =
|
2016-08-20 05:20:13 +08:00
|
|
|
*GraphTraits<MachineFunction *>::nodes_begin(FuncRep);
|
2013-07-20 05:45:06 +08:00
|
|
|
if (EntryMBB->succ_size() == 0) {
|
|
|
|
Finish = true;
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
|
|
|
dbgs() << "Reduce to one block\n";
|
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
2013-07-20 05:45:06 +08:00
|
|
|
int NewnumRemainedBlk
|
|
|
|
= countActiveBlock(OrderedBlks.begin(), OrderedBlks.end());
|
2012-12-12 05:25:42 +08:00
|
|
|
// consider cloned blocks ??
|
2013-07-20 05:45:06 +08:00
|
|
|
if (NewnumRemainedBlk == 1 || NewnumRemainedBlk < NumRemainedBlk) {
|
|
|
|
MakeProgress = true;
|
|
|
|
NumRemainedBlk = NewnumRemainedBlk;
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
2013-07-20 05:45:06 +08:00
|
|
|
MakeProgress = false;
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
|
|
|
dbgs() << "No progress\n";
|
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
} while (!Finish && MakeProgress);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
// Misc wrap up to maintain the consistency of the Function representation.
|
2016-08-20 05:20:13 +08:00
|
|
|
wrapup(*GraphTraits<MachineFunction *>::nodes_begin(FuncRep));
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
// Detach retired Block, release memory.
|
2013-07-20 05:45:06 +08:00
|
|
|
for (MBBInfoMap::iterator It = BlockInfoMap.begin(), E = BlockInfoMap.end();
|
|
|
|
It != E; ++It) {
|
|
|
|
if ((*It).second && (*It).second->IsRetired) {
|
|
|
|
assert(((*It).first)->getNumber() != -1);
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "Erase BB" << ((*It).first)->getNumber() << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2013-07-20 05:45:06 +08:00
|
|
|
(*It).first->eraseFromParent(); //Remove from the parent Function.
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
delete (*It).second;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
BlockInfoMap.clear();
|
|
|
|
LLInfoMap.clear();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2014-03-25 04:29:02 +08:00
|
|
|
if (!Finish) {
|
|
|
|
DEBUG(FuncRep->viewCFG());
|
2016-03-02 11:33:55 +08:00
|
|
|
report_fatal_error("IRREDUCIBLE_CFG");
|
2014-03-25 04:29:02 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
return true;
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AMDGPUCFGStructurizer::orderBlocks(MachineFunction *MF) {
|
|
|
|
int SccNum = 0;
|
|
|
|
MachineBasicBlock *MBB;
|
2014-02-05 03:19:07 +08:00
|
|
|
for (scc_iterator<MachineFunction *> It = scc_begin(MF); !It.isAtEnd();
|
|
|
|
++It, ++SccNum) {
|
2014-04-26 02:24:50 +08:00
|
|
|
const std::vector<MachineBasicBlock *> &SccNext = *It;
|
2013-07-20 05:45:06 +08:00
|
|
|
for (std::vector<MachineBasicBlock *>::const_iterator
|
|
|
|
blockIter = SccNext.begin(), blockEnd = SccNext.end();
|
2012-12-12 05:25:42 +08:00
|
|
|
blockIter != blockEnd; ++blockIter) {
|
2013-07-20 05:45:06 +08:00
|
|
|
MBB = *blockIter;
|
|
|
|
OrderedBlks.push_back(MBB);
|
|
|
|
recordSccnum(MBB, SccNum);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//walk through all the block in func to check for unreachable
|
2013-07-20 05:45:06 +08:00
|
|
|
typedef GraphTraits<MachineFunction *> GTM;
|
2016-08-20 05:20:13 +08:00
|
|
|
auto It = GTM::nodes_begin(MF), E = GTM::nodes_end(MF);
|
2013-07-20 05:45:06 +08:00
|
|
|
for (; It != E; ++It) {
|
2016-08-20 05:20:13 +08:00
|
|
|
MachineBasicBlock *MBB = *It;
|
2013-07-20 05:45:06 +08:00
|
|
|
SccNum = getSCCNum(MBB);
|
|
|
|
if (SccNum == INVALIDSCCNUM)
|
|
|
|
dbgs() << "unreachable block BB" << MBB->getNumber() << "\n";
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::patternMatch(MachineBasicBlock *MBB) {
|
|
|
|
int NumMatch = 0;
|
|
|
|
int CurMatch;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "Begin patternMatch BB" << MBB->getNumber() << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
while ((CurMatch = patternMatchGroup(MBB)) > 0)
|
|
|
|
NumMatch += CurMatch;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "End patternMatch BB" << MBB->getNumber()
|
|
|
|
<< ", numMatch = " << NumMatch << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
return NumMatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AMDGPUCFGStructurizer::patternMatchGroup(MachineBasicBlock *MBB) {
|
|
|
|
int NumMatch = 0;
|
|
|
|
NumMatch += loopendPatternMatch();
|
|
|
|
NumMatch += serialPatternMatch(MBB);
|
|
|
|
NumMatch += ifPatternMatch(MBB);
|
|
|
|
return NumMatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int AMDGPUCFGStructurizer::serialPatternMatch(MachineBasicBlock *MBB) {
|
|
|
|
if (MBB->succ_size() != 1)
|
2012-12-12 05:25:42 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *childBlk = *MBB->succ_begin();
|
|
|
|
if (childBlk->pred_size() != 1 || isActiveLoophead(childBlk))
|
2012-12-12 05:25:42 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
mergeSerialBlock(MBB, childBlk);
|
2012-12-12 05:25:42 +08:00
|
|
|
++numSerialPatternMatch;
|
|
|
|
return 1;
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::ifPatternMatch(MachineBasicBlock *MBB) {
|
2012-12-12 05:25:42 +08:00
|
|
|
//two edges
|
2013-07-20 05:45:06 +08:00
|
|
|
if (MBB->succ_size() != 2)
|
2012-12-12 05:25:42 +08:00
|
|
|
return 0;
|
2013-07-20 05:45:06 +08:00
|
|
|
if (hasBackEdge(MBB))
|
2012-12-12 05:25:42 +08:00
|
|
|
return 0;
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
|
|
|
|
if (!BranchMI)
|
2012-12-12 05:25:42 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
assert(isCondBranch(BranchMI));
|
2013-11-19 03:43:38 +08:00
|
|
|
int NumMatch = 0;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *TrueMBB = getTrueBranch(BranchMI);
|
2013-11-19 03:43:38 +08:00
|
|
|
NumMatch += serialPatternMatch(TrueMBB);
|
|
|
|
NumMatch += ifPatternMatch(TrueMBB);
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *FalseMBB = getFalseBranch(MBB, BranchMI);
|
2013-11-19 03:43:38 +08:00
|
|
|
NumMatch += serialPatternMatch(FalseMBB);
|
|
|
|
NumMatch += ifPatternMatch(FalseMBB);
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *LandBlk;
|
|
|
|
int Cloned = 0;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
assert (!TrueMBB->succ_empty() || !FalseMBB->succ_empty());
|
2012-12-12 05:25:42 +08:00
|
|
|
// TODO: Simplify
|
2013-07-20 05:45:06 +08:00
|
|
|
if (TrueMBB->succ_size() == 1 && FalseMBB->succ_size() == 1
|
|
|
|
&& *TrueMBB->succ_begin() == *FalseMBB->succ_begin()) {
|
|
|
|
// Diamond pattern
|
|
|
|
LandBlk = *TrueMBB->succ_begin();
|
|
|
|
} else if (TrueMBB->succ_size() == 1 && *TrueMBB->succ_begin() == FalseMBB) {
|
|
|
|
// Triangle pattern, false is empty
|
|
|
|
LandBlk = FalseMBB;
|
2014-04-25 13:30:21 +08:00
|
|
|
FalseMBB = nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
} else if (FalseMBB->succ_size() == 1
|
|
|
|
&& *FalseMBB->succ_begin() == TrueMBB) {
|
|
|
|
// Triangle pattern, true is empty
|
2013-07-20 05:45:15 +08:00
|
|
|
// We reverse the predicate to make a triangle, empty false pattern;
|
|
|
|
std::swap(TrueMBB, FalseMBB);
|
2016-08-13 09:12:49 +08:00
|
|
|
reversePredicateSetter(MBB->end(), *MBB);
|
2013-07-20 05:45:15 +08:00
|
|
|
LandBlk = FalseMBB;
|
2014-04-25 13:30:21 +08:00
|
|
|
FalseMBB = nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
} else if (FalseMBB->succ_size() == 1
|
|
|
|
&& isSameloopDetachedContbreak(TrueMBB, FalseMBB)) {
|
|
|
|
LandBlk = *FalseMBB->succ_begin();
|
|
|
|
} else if (TrueMBB->succ_size() == 1
|
|
|
|
&& isSameloopDetachedContbreak(FalseMBB, TrueMBB)) {
|
|
|
|
LandBlk = *TrueMBB->succ_begin();
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
2013-11-19 03:43:38 +08:00
|
|
|
return NumMatch + handleJumpintoIf(MBB, TrueMBB, FalseMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// improveSimpleJumpinfoIf can handle the case where landBlk == NULL but the
|
|
|
|
// new BB created for landBlk==NULL may introduce new challenge to the
|
|
|
|
// reduction process.
|
2013-07-20 05:45:06 +08:00
|
|
|
if (LandBlk &&
|
|
|
|
((TrueMBB && TrueMBB->pred_size() > 1)
|
|
|
|
|| (FalseMBB && FalseMBB->pred_size() > 1))) {
|
|
|
|
Cloned += improveSimpleJumpintoIf(MBB, TrueMBB, FalseMBB, &LandBlk);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (TrueMBB && TrueMBB->pred_size() > 1) {
|
|
|
|
TrueMBB = cloneBlockForPredecessor(TrueMBB, MBB);
|
|
|
|
++Cloned;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (FalseMBB && FalseMBB->pred_size() > 1) {
|
|
|
|
FalseMBB = cloneBlockForPredecessor(FalseMBB, MBB);
|
|
|
|
++Cloned;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
mergeIfthenelseBlock(BranchMI, MBB, TrueMBB, FalseMBB, LandBlk);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
++numIfPatternMatch;
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
numClonedBlock += Cloned;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-11-19 03:43:38 +08:00
|
|
|
return 1 + Cloned + NumMatch;
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::loopendPatternMatch() {
|
2015-03-14 01:32:43 +08:00
|
|
|
std::deque<MachineLoop *> NestedLoops;
|
|
|
|
for (auto &It: *MLI)
|
|
|
|
for (MachineLoop *ML : depth_first(It))
|
|
|
|
NestedLoops.push_front(ML);
|
2014-04-11 09:50:01 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (NestedLoops.size() == 0)
|
2012-12-12 05:25:42 +08:00
|
|
|
return 0;
|
|
|
|
|
2015-03-14 01:32:43 +08:00
|
|
|
// Process nested loop outside->inside (we did push_front),
|
|
|
|
// so "continue" to a outside loop won't be mistaken as "break"
|
|
|
|
// of the current loop.
|
2013-07-20 05:45:06 +08:00
|
|
|
int Num = 0;
|
2015-03-14 01:32:43 +08:00
|
|
|
for (MachineLoop *ExaminedLoop : NestedLoops) {
|
2013-07-20 05:45:06 +08:00
|
|
|
if (ExaminedLoop->getNumBlocks() == 0 || Visited[ExaminedLoop])
|
2012-12-12 05:25:42 +08:00
|
|
|
continue;
|
2013-07-20 05:45:06 +08:00
|
|
|
DEBUG(dbgs() << "Processing:\n"; ExaminedLoop->dump(););
|
|
|
|
int NumBreak = mergeLoop(ExaminedLoop);
|
|
|
|
if (NumBreak == -1)
|
2012-12-12 05:25:42 +08:00
|
|
|
break;
|
2013-07-20 05:45:06 +08:00
|
|
|
Num += NumBreak;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
return Num;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::mergeLoop(MachineLoop *LoopRep) {
|
|
|
|
MachineBasicBlock *LoopHeader = LoopRep->getHeader();
|
|
|
|
MBBVector ExitingMBBs;
|
|
|
|
LoopRep->getExitingBlocks(ExitingMBBs);
|
|
|
|
assert(!ExitingMBBs.empty() && "Infinite Loop not supported");
|
|
|
|
DEBUG(dbgs() << "Loop has " << ExitingMBBs.size() << " exiting blocks\n";);
|
|
|
|
// We assume a single ExitBlk
|
|
|
|
MBBVector ExitBlks;
|
|
|
|
LoopRep->getExitBlocks(ExitBlks);
|
|
|
|
SmallPtrSet<MachineBasicBlock *, 2> ExitBlkSet;
|
|
|
|
for (unsigned i = 0, e = ExitBlks.size(); i < e; ++i)
|
|
|
|
ExitBlkSet.insert(ExitBlks[i]);
|
|
|
|
assert(ExitBlkSet.size() == 1);
|
|
|
|
MachineBasicBlock *ExitBlk = *ExitBlks.begin();
|
|
|
|
assert(ExitBlk && "Loop has several exit block");
|
|
|
|
MBBVector LatchBlks;
|
|
|
|
typedef GraphTraits<Inverse<MachineBasicBlock*> > InvMBBTraits;
|
|
|
|
InvMBBTraits::ChildIteratorType PI = InvMBBTraits::child_begin(LoopHeader),
|
|
|
|
PE = InvMBBTraits::child_end(LoopHeader);
|
|
|
|
for (; PI != PE; PI++) {
|
|
|
|
if (LoopRep->contains(*PI))
|
|
|
|
LatchBlks.push_back(*PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = ExitingMBBs.size(); i < e; ++i)
|
|
|
|
mergeLoopbreakBlock(ExitingMBBs[i], ExitBlk);
|
|
|
|
for (unsigned i = 0, e = LatchBlks.size(); i < e; ++i)
|
|
|
|
settleLoopcontBlock(LatchBlks[i], LoopHeader);
|
|
|
|
int Match = 0;
|
|
|
|
do {
|
|
|
|
Match = 0;
|
|
|
|
Match += serialPatternMatch(LoopHeader);
|
|
|
|
Match += ifPatternMatch(LoopHeader);
|
|
|
|
} while (Match > 0);
|
|
|
|
mergeLooplandBlock(LoopHeader, ExitBlk);
|
|
|
|
MachineLoop *ParentLoop = LoopRep->getParentLoop();
|
|
|
|
if (ParentLoop)
|
|
|
|
MLI->changeLoopFor(LoopHeader, ParentLoop);
|
|
|
|
else
|
|
|
|
MLI->removeBlock(LoopHeader);
|
|
|
|
Visited[LoopRep] = true;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
bool AMDGPUCFGStructurizer::isSameloopDetachedContbreak(
|
|
|
|
MachineBasicBlock *Src1MBB, MachineBasicBlock *Src2MBB) {
|
|
|
|
if (Src1MBB->succ_size() == 0) {
|
|
|
|
MachineLoop *LoopRep = MLI->getLoopFor(Src1MBB);
|
|
|
|
if (LoopRep&& LoopRep == MLI->getLoopFor(Src2MBB)) {
|
|
|
|
MachineBasicBlock *&TheEntry = LLInfoMap[LoopRep];
|
|
|
|
if (TheEntry) {
|
|
|
|
DEBUG(
|
|
|
|
dbgs() << "isLoopContBreakBlock yes src1 = BB"
|
|
|
|
<< Src1MBB->getNumber()
|
|
|
|
<< " src2 = BB" << Src2MBB->getNumber() << "\n";
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::handleJumpintoIf(MachineBasicBlock *HeadMBB,
|
|
|
|
MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB) {
|
|
|
|
int Num = handleJumpintoIfImp(HeadMBB, TrueMBB, FalseMBB);
|
|
|
|
if (Num == 0) {
|
|
|
|
DEBUG(
|
|
|
|
dbgs() << "handleJumpintoIf swap trueBlk and FalseBlk" << "\n";
|
|
|
|
);
|
|
|
|
Num = handleJumpintoIfImp(HeadMBB, FalseMBB, TrueMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
return Num;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::handleJumpintoIfImp(MachineBasicBlock *HeadMBB,
|
|
|
|
MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB) {
|
|
|
|
int Num = 0;
|
|
|
|
MachineBasicBlock *DownBlk;
|
|
|
|
|
|
|
|
//trueBlk could be the common post dominator
|
|
|
|
DownBlk = TrueMBB;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "handleJumpintoIfImp head = BB" << HeadMBB->getNumber()
|
|
|
|
<< " true = BB" << TrueMBB->getNumber()
|
|
|
|
<< ", numSucc=" << TrueMBB->succ_size()
|
|
|
|
<< " false = BB" << FalseMBB->getNumber() << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
while (DownBlk) {
|
|
|
|
DEBUG(
|
|
|
|
dbgs() << "check down = BB" << DownBlk->getNumber();
|
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (singlePathTo(FalseMBB, DownBlk) == SinglePath_InPath) {
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << " working\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
Num += cloneOnSideEntryTo(HeadMBB, TrueMBB, DownBlk);
|
|
|
|
Num += cloneOnSideEntryTo(HeadMBB, FalseMBB, DownBlk);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
numClonedBlock += Num;
|
|
|
|
Num += serialPatternMatch(*HeadMBB->succ_begin());
|
2014-03-02 20:27:27 +08:00
|
|
|
Num += serialPatternMatch(*std::next(HeadMBB->succ_begin()));
|
2013-07-20 05:45:06 +08:00
|
|
|
Num += ifPatternMatch(HeadMBB);
|
|
|
|
assert(Num > 0);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
|
|
|
dbgs() << " not working\n";
|
|
|
|
);
|
2014-04-25 13:30:21 +08:00
|
|
|
DownBlk = (DownBlk->succ_size() == 1) ? (*DownBlk->succ_begin()) : nullptr;
|
2012-12-12 05:25:42 +08:00
|
|
|
} // walk down the postDomTree
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
return Num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUCFGStructurizer::showImproveSimpleJumpintoIf(
|
|
|
|
MachineBasicBlock *HeadMBB, MachineBasicBlock *TrueMBB,
|
|
|
|
MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB, bool Detail) {
|
|
|
|
dbgs() << "head = BB" << HeadMBB->getNumber()
|
|
|
|
<< " size = " << HeadMBB->size();
|
|
|
|
if (Detail) {
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2013-07-20 05:45:06 +08:00
|
|
|
HeadMBB->print(dbgs());
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (TrueMBB) {
|
|
|
|
dbgs() << ", true = BB" << TrueMBB->getNumber() << " size = "
|
|
|
|
<< TrueMBB->size() << " numPred = " << TrueMBB->pred_size();
|
|
|
|
if (Detail) {
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2013-07-20 05:45:06 +08:00
|
|
|
TrueMBB->print(dbgs());
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
if (FalseMBB) {
|
|
|
|
dbgs() << ", false = BB" << FalseMBB->getNumber() << " size = "
|
|
|
|
<< FalseMBB->size() << " numPred = " << FalseMBB->pred_size();
|
|
|
|
if (Detail) {
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2013-07-20 05:45:06 +08:00
|
|
|
FalseMBB->print(dbgs());
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
if (LandMBB) {
|
|
|
|
dbgs() << ", land = BB" << LandMBB->getNumber() << " size = "
|
|
|
|
<< LandMBB->size() << " numPred = " << LandMBB->pred_size();
|
|
|
|
if (Detail) {
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2013-07-20 05:45:06 +08:00
|
|
|
LandMBB->print(dbgs());
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::improveSimpleJumpintoIf(MachineBasicBlock *HeadMBB,
|
|
|
|
MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB,
|
|
|
|
MachineBasicBlock **LandMBBPtr) {
|
|
|
|
bool MigrateTrue = false;
|
|
|
|
bool MigrateFalse = false;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *LandBlk = *LandMBBPtr;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
assert((!TrueMBB || TrueMBB->succ_size() <= 1)
|
|
|
|
&& (!FalseMBB || FalseMBB->succ_size() <= 1));
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (TrueMBB == FalseMBB)
|
2012-12-12 05:25:42 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MigrateTrue = needMigrateBlock(TrueMBB);
|
|
|
|
MigrateFalse = needMigrateBlock(FalseMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (!MigrateTrue && !MigrateFalse)
|
2012-12-12 05:25:42 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If we need to migrate either trueBlk and falseBlk, migrate the rest that
|
|
|
|
// have more than one predecessors. without doing this, its predecessor
|
|
|
|
// rather than headBlk will have undefined value in initReg.
|
2013-07-20 05:45:06 +08:00
|
|
|
if (!MigrateTrue && TrueMBB && TrueMBB->pred_size() > 1)
|
|
|
|
MigrateTrue = true;
|
|
|
|
if (!MigrateFalse && FalseMBB && FalseMBB->pred_size() > 1)
|
|
|
|
MigrateFalse = true;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
|
|
|
dbgs() << "before improveSimpleJumpintoIf: ";
|
2013-07-20 05:45:06 +08:00
|
|
|
showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0);
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
// org: headBlk => if () {trueBlk} else {falseBlk} => landBlk
|
|
|
|
//
|
|
|
|
// new: headBlk => if () {initReg = 1; org trueBlk branch} else
|
|
|
|
// {initReg = 0; org falseBlk branch }
|
|
|
|
// => landBlk => if (initReg) {org trueBlk} else {org falseBlk}
|
|
|
|
// => org landBlk
|
|
|
|
// if landBlk->pred_size() > 2, put the about if-else inside
|
|
|
|
// if (initReg !=2) {...}
|
|
|
|
//
|
|
|
|
// add initReg = initVal to headBlk
|
|
|
|
|
|
|
|
const TargetRegisterClass * I32RC = TRI->getCFGStructurizerRegClass(MVT::i32);
|
2013-10-17 01:06:02 +08:00
|
|
|
if (!MigrateTrue || !MigrateFalse) {
|
|
|
|
// XXX: We have an opportunity here to optimize the "branch into if" case
|
|
|
|
// here. Branch into if looks like this:
|
|
|
|
// entry
|
2013-10-18 22:12:50 +08:00
|
|
|
// / |
|
2013-10-17 01:06:02 +08:00
|
|
|
// diamond_head branch_from
|
|
|
|
// / \ |
|
|
|
|
// diamond_false diamond_true
|
|
|
|
// \ /
|
|
|
|
// done
|
|
|
|
//
|
|
|
|
// The diamond_head block begins the "if" and the diamond_true block
|
|
|
|
// is the block being "branched into".
|
|
|
|
//
|
|
|
|
// If MigrateTrue is true, then TrueBB is the block being "branched into"
|
|
|
|
// and if MigrateFalse is true, then FalseBB is the block being
|
|
|
|
// "branched into"
|
2015-09-11 05:51:19 +08:00
|
|
|
//
|
2013-10-17 01:06:02 +08:00
|
|
|
// Here is the pseudo code for how I think the optimization should work:
|
|
|
|
// 1. Insert MOV GPR0, 0 before the branch instruction in diamond_head.
|
|
|
|
// 2. Insert MOV GPR0, 1 before the branch instruction in branch_from.
|
|
|
|
// 3. Move the branch instruction from diamond_head into its own basic
|
|
|
|
// block (new_block).
|
|
|
|
// 4. Add an unconditional branch from diamond_head to new_block
|
|
|
|
// 5. Replace the branch instruction in branch_from with an unconditional
|
|
|
|
// branch to new_block. If branch_from has multiple predecessors, then
|
|
|
|
// we need to replace the True/False block in the branch
|
|
|
|
// instruction instead of replacing it.
|
|
|
|
// 6. Change the condition of the branch instruction in new_block from
|
|
|
|
// COND to (COND || GPR0)
|
|
|
|
//
|
|
|
|
// In order insert these MOV instruction, we will need to use the
|
|
|
|
// RegisterScavenger. Usually liveness stops being tracked during
|
|
|
|
// the late machine optimization passes, however if we implement
|
|
|
|
// bool TargetRegisterInfo::requiresRegisterScavenging(
|
|
|
|
// const MachineFunction &MF)
|
2015-09-11 05:51:19 +08:00
|
|
|
// and have it return true, liveness will be tracked correctly
|
2013-10-17 01:06:02 +08:00
|
|
|
// by generic optimization passes. We will also need to make sure that
|
|
|
|
// all of our target-specific passes that run after regalloc and before
|
|
|
|
// the CFGStructurizer track liveness and we will need to modify this pass
|
|
|
|
// to correctly track liveness.
|
|
|
|
//
|
|
|
|
// After the above changes, the new CFG should look like this:
|
|
|
|
// entry
|
2013-10-18 22:12:50 +08:00
|
|
|
// / |
|
2013-10-17 01:06:02 +08:00
|
|
|
// diamond_head branch_from
|
|
|
|
// \ /
|
|
|
|
// new_block
|
2013-10-18 22:12:50 +08:00
|
|
|
// / |
|
2013-10-17 01:06:02 +08:00
|
|
|
// diamond_false diamond_true
|
|
|
|
// \ /
|
|
|
|
// done
|
|
|
|
//
|
|
|
|
// Without this optimization, we are forced to duplicate the diamond_true
|
|
|
|
// block and we will end up with a CFG like this:
|
|
|
|
//
|
|
|
|
// entry
|
2013-10-18 22:12:50 +08:00
|
|
|
// / |
|
2013-10-17 01:06:02 +08:00
|
|
|
// diamond_head branch_from
|
|
|
|
// / \ |
|
|
|
|
// diamond_false diamond_true diamond_true (duplicate)
|
|
|
|
// \ / |
|
|
|
|
// done --------------------|
|
|
|
|
//
|
|
|
|
// Duplicating diamond_true can be very costly especially if it has a
|
|
|
|
// lot of instructions.
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int NumNewBlk = 0;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
bool LandBlkHasOtherPred = (LandBlk->pred_size() > 2);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
//insert AMDGPU::ENDIF to avoid special case "input landBlk == NULL"
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock::iterator I = insertInstrBefore(LandBlk, AMDGPU::ENDIF);
|
|
|
|
|
|
|
|
if (LandBlkHasOtherPred) {
|
2016-03-02 11:33:55 +08:00
|
|
|
report_fatal_error("Extra register needed to handle CFG");
|
2013-07-20 05:45:06 +08:00
|
|
|
unsigned CmpResReg =
|
|
|
|
HeadMBB->getParent()->getRegInfo().createVirtualRegister(I32RC);
|
2016-03-02 11:33:55 +08:00
|
|
|
report_fatal_error("Extra compare instruction needed to handle CFG");
|
2013-07-20 05:45:06 +08:00
|
|
|
insertCondBranchBefore(LandBlk, I, AMDGPU::IF_PREDICATE_SET,
|
|
|
|
CmpResReg, DebugLoc());
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-10-17 01:05:56 +08:00
|
|
|
// XXX: We are running this after RA, so creating virtual registers will
|
|
|
|
// cause an assertion failure in the PostRA scheduling pass.
|
|
|
|
unsigned InitReg =
|
|
|
|
HeadMBB->getParent()->getRegInfo().createVirtualRegister(I32RC);
|
2013-07-20 05:45:06 +08:00
|
|
|
insertCondBranchBefore(LandBlk, I, AMDGPU::IF_PREDICATE_SET, InitReg,
|
|
|
|
DebugLoc());
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (MigrateTrue) {
|
|
|
|
migrateInstruction(TrueMBB, LandBlk, I);
|
2012-12-12 05:25:42 +08:00
|
|
|
// need to uncondionally insert the assignment to ensure a path from its
|
|
|
|
// predecessor rather than headBlk has valid value in initReg if
|
|
|
|
// (initVal != 1).
|
2016-03-02 11:33:55 +08:00
|
|
|
report_fatal_error("Extra register needed to handle CFG");
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
insertInstrBefore(I, AMDGPU::ELSE);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (MigrateFalse) {
|
|
|
|
migrateInstruction(FalseMBB, LandBlk, I);
|
2012-12-12 05:25:42 +08:00
|
|
|
// need to uncondionally insert the assignment to ensure a path from its
|
|
|
|
// predecessor rather than headBlk has valid value in initReg if
|
|
|
|
// (initVal != 0)
|
2016-03-02 11:33:55 +08:00
|
|
|
report_fatal_error("Extra register needed to handle CFG");
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (LandBlkHasOtherPred) {
|
2012-12-12 05:25:42 +08:00
|
|
|
// add endif
|
2013-07-20 05:45:06 +08:00
|
|
|
insertInstrBefore(I, AMDGPU::ENDIF);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
// put initReg = 2 to other predecessors of landBlk
|
2013-07-20 05:45:06 +08:00
|
|
|
for (MachineBasicBlock::pred_iterator PI = LandBlk->pred_begin(),
|
|
|
|
PE = LandBlk->pred_end(); PI != PE; ++PI) {
|
|
|
|
MachineBasicBlock *MBB = *PI;
|
|
|
|
if (MBB != TrueMBB && MBB != FalseMBB)
|
2016-03-02 11:33:55 +08:00
|
|
|
report_fatal_error("Extra register needed to handle CFG");
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
|
|
|
dbgs() << "result from improveSimpleJumpintoIf: ";
|
2013-07-20 05:45:06 +08:00
|
|
|
showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0);
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
// update landBlk
|
2013-07-20 05:45:06 +08:00
|
|
|
*LandMBBPtr = LandBlk;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
return NumNewBlk;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::mergeSerialBlock(MachineBasicBlock *DstMBB,
|
|
|
|
MachineBasicBlock *SrcMBB) {
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "serialPattern BB" << DstMBB->getNumber()
|
|
|
|
<< " <= BB" << SrcMBB->getNumber() << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2013-07-20 05:45:06 +08:00
|
|
|
DstMBB->splice(DstMBB->end(), SrcMBB, SrcMBB->begin(), SrcMBB->end());
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2015-12-13 17:26:17 +08:00
|
|
|
DstMBB->removeSuccessor(SrcMBB, true);
|
2013-07-20 05:45:06 +08:00
|
|
|
cloneSuccessorList(DstMBB, SrcMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
removeSuccessor(SrcMBB);
|
|
|
|
MLI->removeBlock(SrcMBB);
|
|
|
|
retireBlock(SrcMBB);
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::mergeIfthenelseBlock(MachineInstr *BranchMI,
|
|
|
|
MachineBasicBlock *MBB, MachineBasicBlock *TrueMBB,
|
|
|
|
MachineBasicBlock *FalseMBB, MachineBasicBlock *LandMBB) {
|
2013-07-20 05:45:15 +08:00
|
|
|
assert (TrueMBB);
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "ifPattern BB" << MBB->getNumber();
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "{ ";
|
2013-07-20 05:45:06 +08:00
|
|
|
if (TrueMBB) {
|
|
|
|
dbgs() << "BB" << TrueMBB->getNumber();
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << " } else ";
|
|
|
|
dbgs() << "{ ";
|
2013-07-20 05:45:06 +08:00
|
|
|
if (FalseMBB) {
|
|
|
|
dbgs() << "BB" << FalseMBB->getNumber();
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << " }\n ";
|
|
|
|
dbgs() << "landBlock: ";
|
2013-07-20 05:45:06 +08:00
|
|
|
if (!LandMBB) {
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "NULL";
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "BB" << LandMBB->getNumber();
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:44:56 +08:00
|
|
|
dbgs() << "\n";
|
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int OldOpcode = BranchMI->getOpcode();
|
|
|
|
DebugLoc BranchDL = BranchMI->getDebugLoc();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
// transform to
|
|
|
|
// if cond
|
|
|
|
// trueBlk
|
|
|
|
// else
|
|
|
|
// falseBlk
|
|
|
|
// endif
|
|
|
|
// landBlk
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock::iterator I = BranchMI;
|
|
|
|
insertCondBranchBefore(I, getBranchNzeroOpcode(OldOpcode),
|
|
|
|
BranchDL);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (TrueMBB) {
|
|
|
|
MBB->splice(I, TrueMBB, TrueMBB->begin(), TrueMBB->end());
|
2015-12-13 17:26:17 +08:00
|
|
|
MBB->removeSuccessor(TrueMBB, true);
|
2013-07-20 05:45:06 +08:00
|
|
|
if (LandMBB && TrueMBB->succ_size()!=0)
|
2015-12-13 17:26:17 +08:00
|
|
|
TrueMBB->removeSuccessor(LandMBB, true);
|
2013-07-20 05:45:06 +08:00
|
|
|
retireBlock(TrueMBB);
|
|
|
|
MLI->removeBlock(TrueMBB);
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (FalseMBB) {
|
|
|
|
insertInstrBefore(I, AMDGPU::ELSE);
|
|
|
|
MBB->splice(I, FalseMBB, FalseMBB->begin(),
|
|
|
|
FalseMBB->end());
|
2015-12-13 17:26:17 +08:00
|
|
|
MBB->removeSuccessor(FalseMBB, true);
|
2013-07-20 05:45:06 +08:00
|
|
|
if (LandMBB && FalseMBB->succ_size() != 0)
|
2015-12-13 17:26:17 +08:00
|
|
|
FalseMBB->removeSuccessor(LandMBB, true);
|
2013-07-20 05:45:06 +08:00
|
|
|
retireBlock(FalseMBB);
|
|
|
|
MLI->removeBlock(FalseMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
insertInstrBefore(I, AMDGPU::ENDIF);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
BranchMI->eraseFromParent();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (LandMBB && TrueMBB && FalseMBB)
|
|
|
|
MBB->addSuccessor(LandMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::mergeLooplandBlock(MachineBasicBlock *DstBlk,
|
|
|
|
MachineBasicBlock *LandMBB) {
|
|
|
|
DEBUG(dbgs() << "loopPattern header = BB" << DstBlk->getNumber()
|
|
|
|
<< " land = BB" << LandMBB->getNumber() << "\n";);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-08-01 03:31:14 +08:00
|
|
|
insertInstrBefore(DstBlk, AMDGPU::WHILELOOP, DebugLoc());
|
|
|
|
insertInstrEnd(DstBlk, AMDGPU::ENDLOOP, DebugLoc());
|
2015-12-01 13:29:22 +08:00
|
|
|
DstBlk->replaceSuccessor(DstBlk, LandMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::mergeLoopbreakBlock(MachineBasicBlock *ExitingMBB,
|
|
|
|
MachineBasicBlock *LandMBB) {
|
|
|
|
DEBUG(dbgs() << "loopbreakPattern exiting = BB" << ExitingMBB->getNumber()
|
|
|
|
<< " land = BB" << LandMBB->getNumber() << "\n";);
|
|
|
|
MachineInstr *BranchMI = getLoopendBlockBranchInstr(ExitingMBB);
|
|
|
|
assert(BranchMI && isCondBranch(BranchMI));
|
|
|
|
DebugLoc DL = BranchMI->getDebugLoc();
|
|
|
|
MachineBasicBlock *TrueBranch = getTrueBranch(BranchMI);
|
|
|
|
MachineBasicBlock::iterator I = BranchMI;
|
|
|
|
if (TrueBranch != LandMBB)
|
2016-08-13 09:12:49 +08:00
|
|
|
reversePredicateSetter(I, *I->getParent());
|
2013-08-01 03:31:14 +08:00
|
|
|
insertCondBranchBefore(ExitingMBB, I, AMDGPU::IF_PREDICATE_SET, AMDGPU::PREDICATE_BIT, DL);
|
|
|
|
insertInstrBefore(I, AMDGPU::BREAK);
|
|
|
|
insertInstrBefore(I, AMDGPU::ENDIF);
|
2012-12-12 05:25:42 +08:00
|
|
|
//now branchInst can be erase safely
|
2013-07-20 05:45:06 +08:00
|
|
|
BranchMI->eraseFromParent();
|
2012-12-12 05:25:42 +08:00
|
|
|
//now take care of successors, retire blocks
|
2015-12-13 17:26:17 +08:00
|
|
|
ExitingMBB->removeSuccessor(LandMBB, true);
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::settleLoopcontBlock(MachineBasicBlock *ContingMBB,
|
|
|
|
MachineBasicBlock *ContMBB) {
|
|
|
|
DEBUG(dbgs() << "settleLoopcontBlock conting = BB"
|
|
|
|
<< ContingMBB->getNumber()
|
|
|
|
<< ", cont = BB" << ContMBB->getNumber() << "\n";);
|
|
|
|
|
|
|
|
MachineInstr *MI = getLoopendBlockBranchInstr(ContingMBB);
|
|
|
|
if (MI) {
|
|
|
|
assert(isCondBranch(MI));
|
|
|
|
MachineBasicBlock::iterator I = MI;
|
|
|
|
MachineBasicBlock *TrueBranch = getTrueBranch(MI);
|
|
|
|
int OldOpcode = MI->getOpcode();
|
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
bool UseContinueLogical = ((&*ContingMBB->rbegin()) == MI);
|
|
|
|
|
2015-03-24 04:56:44 +08:00
|
|
|
if (!UseContinueLogical) {
|
2013-07-20 05:45:06 +08:00
|
|
|
int BranchOpcode =
|
|
|
|
TrueBranch == ContMBB ? getBranchNzeroOpcode(OldOpcode) :
|
|
|
|
getBranchZeroOpcode(OldOpcode);
|
|
|
|
insertCondBranchBefore(I, BranchOpcode, DL);
|
|
|
|
// insertEnd to ensure phi-moves, if exist, go before the continue-instr.
|
|
|
|
insertInstrEnd(ContingMBB, AMDGPU::CONTINUE, DL);
|
|
|
|
insertInstrEnd(ContingMBB, AMDGPU::ENDIF, DL);
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
2013-07-20 05:45:06 +08:00
|
|
|
int BranchOpcode =
|
|
|
|
TrueBranch == ContMBB ? getContinueNzeroOpcode(OldOpcode) :
|
|
|
|
getContinueZeroOpcode(OldOpcode);
|
|
|
|
insertCondBranchBefore(I, BranchOpcode, DL);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MI->eraseFromParent();
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
|
|
|
// if we've arrived here then we've already erased the branch instruction
|
2013-07-20 05:45:06 +08:00
|
|
|
// travel back up the basic block to see the last reference of our debug
|
|
|
|
// location we've just inserted that reference here so it should be
|
|
|
|
// representative insertEnd to ensure phi-moves, if exist, go before the
|
|
|
|
// continue-instr.
|
|
|
|
insertInstrEnd(ContingMBB, AMDGPU::CONTINUE,
|
|
|
|
getLastDebugLocInBB(ContingMBB));
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
int AMDGPUCFGStructurizer::cloneOnSideEntryTo(MachineBasicBlock *PreMBB,
|
|
|
|
MachineBasicBlock *SrcMBB, MachineBasicBlock *DstMBB) {
|
|
|
|
int Cloned = 0;
|
|
|
|
assert(PreMBB->isSuccessor(SrcMBB));
|
|
|
|
while (SrcMBB && SrcMBB != DstMBB) {
|
|
|
|
assert(SrcMBB->succ_size() == 1);
|
|
|
|
if (SrcMBB->pred_size() > 1) {
|
|
|
|
SrcMBB = cloneBlockForPredecessor(SrcMBB, PreMBB);
|
|
|
|
++Cloned;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
PreMBB = SrcMBB;
|
|
|
|
SrcMBB = *SrcMBB->succ_begin();
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
return Cloned;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *
|
|
|
|
AMDGPUCFGStructurizer::cloneBlockForPredecessor(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock *PredMBB) {
|
|
|
|
assert(PredMBB->isSuccessor(MBB) &&
|
2012-12-12 05:25:42 +08:00
|
|
|
"succBlk is not a prececessor of curBlk");
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *CloneMBB = clone(MBB); //clone instructions
|
|
|
|
replaceInstrUseOfBlockWith(PredMBB, MBB, CloneMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
//srcBlk, oldBlk, newBlk
|
|
|
|
|
2015-12-01 13:29:22 +08:00
|
|
|
PredMBB->replaceSuccessor(MBB, CloneMBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
// add all successor to cloneBlk
|
2013-07-20 05:45:06 +08:00
|
|
|
cloneSuccessorList(CloneMBB, MBB);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
numClonedInstr += MBB->size();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
|
|
|
dbgs() << "Cloned block: " << "BB"
|
2013-07-20 05:45:06 +08:00
|
|
|
<< MBB->getNumber() << "size " << MBB->size() << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
SHOWNEWBLK(CloneMBB, "result of Cloned block: ");
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
return CloneMBB;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::migrateInstruction(MachineBasicBlock *SrcMBB,
|
|
|
|
MachineBasicBlock *DstMBB, MachineBasicBlock::iterator I) {
|
|
|
|
MachineBasicBlock::iterator SpliceEnd;
|
2012-12-12 05:25:42 +08:00
|
|
|
//look for the input branchinstr, not the AMDGPU branchinstr
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineInstr *BranchMI = getNormalBlockBranchInstr(SrcMBB);
|
|
|
|
if (!BranchMI) {
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
|
|
|
dbgs() << "migrateInstruction don't see branch instr\n" ;
|
|
|
|
);
|
2013-07-20 05:45:06 +08:00
|
|
|
SpliceEnd = SrcMBB->end();
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
2015-09-11 05:51:19 +08:00
|
|
|
DEBUG(dbgs() << "migrateInstruction see branch instr: " << *BranchMI);
|
2013-07-20 05:45:06 +08:00
|
|
|
SpliceEnd = BranchMI;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "migrateInstruction before splice dstSize = " << DstMBB->size()
|
|
|
|
<< "srcSize = " << SrcMBB->size() << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
//splice insert before insertPos
|
2013-07-20 05:45:06 +08:00
|
|
|
DstMBB->splice(I, SrcMBB, SrcMBB->begin(), SpliceEnd);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "migrateInstruction after splice dstSize = " << DstMBB->size()
|
2015-09-11 05:51:19 +08:00
|
|
|
<< "srcSize = " << SrcMBB->size() << '\n';
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineBasicBlock *
|
|
|
|
AMDGPUCFGStructurizer::normalizeInfiniteLoopExit(MachineLoop* LoopRep) {
|
|
|
|
MachineBasicBlock *LoopHeader = LoopRep->getHeader();
|
|
|
|
MachineBasicBlock *LoopLatch = LoopRep->getLoopLatch();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (!LoopHeader || !LoopLatch)
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
MachineInstr *BranchMI = getLoopendBlockBranchInstr(LoopLatch);
|
|
|
|
// Is LoopRep an infinite loop ?
|
|
|
|
if (!BranchMI || !isUncondBranch(BranchMI))
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
|
|
|
|
MachineBasicBlock *DummyExitBlk = FuncRep->CreateMachineBasicBlock();
|
|
|
|
FuncRep->push_back(DummyExitBlk); //insert to function
|
|
|
|
SHOWNEWBLK(DummyExitBlk, "DummyExitBlock to normalize infiniteLoop: ");
|
|
|
|
DEBUG(dbgs() << "Old branch instr: " << *BranchMI << "\n";);
|
2015-07-16 23:38:29 +08:00
|
|
|
LLVMContext &Ctx = LoopHeader->getParent()->getFunction()->getContext();
|
|
|
|
Ctx.emitError("Extra register needed to handle CFG");
|
|
|
|
return nullptr;
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::removeUnconditionalBranch(MachineBasicBlock *MBB) {
|
|
|
|
MachineInstr *BranchMI;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
// I saw two unconditional branch in one basic block in example
|
|
|
|
// test_fc_do_while_or.c need to fix the upstream on this to remove the loop.
|
2013-07-20 05:45:06 +08:00
|
|
|
while ((BranchMI = getLoopendBlockBranchInstr(MBB))
|
|
|
|
&& isUncondBranch(BranchMI)) {
|
2015-09-11 05:51:19 +08:00
|
|
|
DEBUG(dbgs() << "Removing uncond branch instr: " << *BranchMI);
|
2013-07-20 05:45:06 +08:00
|
|
|
BranchMI->eraseFromParent();
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::removeRedundantConditionalBranch(
|
|
|
|
MachineBasicBlock *MBB) {
|
|
|
|
if (MBB->succ_size() != 2)
|
|
|
|
return;
|
|
|
|
MachineBasicBlock *MBB1 = *MBB->succ_begin();
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineBasicBlock *MBB2 = *std::next(MBB->succ_begin());
|
2013-07-20 05:45:06 +08:00
|
|
|
if (MBB1 != MBB2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
|
|
|
|
assert(BranchMI && isCondBranch(BranchMI));
|
2015-09-11 05:51:19 +08:00
|
|
|
DEBUG(dbgs() << "Removing unneeded cond branch instr: " << *BranchMI);
|
2013-07-20 05:45:06 +08:00
|
|
|
BranchMI->eraseFromParent();
|
|
|
|
SHOWNEWBLK(MBB1, "Removing redundant successor");
|
2015-12-13 17:26:17 +08:00
|
|
|
MBB->removeSuccessor(MBB1, true);
|
2013-07-20 05:45:06 +08:00
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::addDummyExitBlock(
|
|
|
|
SmallVectorImpl<MachineBasicBlock*> &RetMBB) {
|
|
|
|
MachineBasicBlock *DummyExitBlk = FuncRep->CreateMachineBasicBlock();
|
|
|
|
FuncRep->push_back(DummyExitBlk); //insert to function
|
|
|
|
insertInstrEnd(DummyExitBlk, AMDGPU::RETURN);
|
|
|
|
|
|
|
|
for (SmallVectorImpl<MachineBasicBlock *>::iterator It = RetMBB.begin(),
|
|
|
|
E = RetMBB.end(); It != E; ++It) {
|
|
|
|
MachineBasicBlock *MBB = *It;
|
|
|
|
MachineInstr *MI = getReturnInstr(MBB);
|
|
|
|
if (MI)
|
|
|
|
MI->eraseFromParent();
|
|
|
|
MBB->addSuccessor(DummyExitBlk);
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "Add dummyExitBlock to BB" << MBB->getNumber()
|
2012-12-12 05:25:42 +08:00
|
|
|
<< " successors\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-07-20 05:45:06 +08:00
|
|
|
SHOWNEWBLK(DummyExitBlk, "DummyExitBlock: ");
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::removeSuccessor(MachineBasicBlock *MBB) {
|
|
|
|
while (MBB->succ_size())
|
|
|
|
MBB->removeSuccessor(*MBB->succ_begin());
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::recordSccnum(MachineBasicBlock *MBB,
|
|
|
|
int SccNum) {
|
|
|
|
BlockInformation *&srcBlkInfo = BlockInfoMap[MBB];
|
|
|
|
if (!srcBlkInfo)
|
|
|
|
srcBlkInfo = new BlockInformation();
|
|
|
|
srcBlkInfo->SccNum = SccNum;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
void AMDGPUCFGStructurizer::retireBlock(MachineBasicBlock *MBB) {
|
2013-07-20 05:44:56 +08:00
|
|
|
DEBUG(
|
2013-07-20 05:45:06 +08:00
|
|
|
dbgs() << "Retiring BB" << MBB->getNumber() << "\n";
|
2013-07-20 05:44:56 +08:00
|
|
|
);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
BlockInformation *&SrcBlkInfo = BlockInfoMap[MBB];
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
if (!SrcBlkInfo)
|
|
|
|
SrcBlkInfo = new BlockInformation();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
SrcBlkInfo->IsRetired = true;
|
|
|
|
assert(MBB->succ_size() == 0 && MBB->pred_size() == 0
|
2012-12-12 05:25:42 +08:00
|
|
|
&& "can't retire block yet");
|
|
|
|
}
|
|
|
|
|
2013-07-20 05:45:06 +08:00
|
|
|
char AMDGPUCFGStructurizer::ID = 0;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
} // end anonymous namespace
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
|
2013-12-12 01:51:47 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(AMDGPUCFGStructurizer, "amdgpustructurizer",
|
|
|
|
"AMDGPU CFG Structurizer", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
|
|
|
INITIALIZE_PASS_END(AMDGPUCFGStructurizer, "amdgpustructurizer",
|
|
|
|
"AMDGPU CFG Structurizer", false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createAMDGPUCFGStructurizerPass() {
|
|
|
|
return new AMDGPUCFGStructurizer();
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|