2016-10-15 06:18:18 +08:00
|
|
|
//===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===//
|
2016-07-23 04:03:43 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-10-15 06:18:18 +08:00
|
|
|
/// \file This file implements the LegalizerHelper class to legalize individual
|
|
|
|
/// instructions and the LegalizePass wrapper pass for the primary
|
2016-07-23 04:03:43 +08:00
|
|
|
/// legalization.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-10-15 06:18:18 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/Legalizer.h"
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2017-08-31 03:32:59 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2019-01-16 08:40:37 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
|
|
|
|
#include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h"
|
2018-12-06 04:14:52 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/GISelWorkList.h"
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h"
|
2016-10-15 06:18:18 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
|
2017-02-24 05:05:42 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/Utils.h"
|
|
|
|
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
|
2016-08-27 10:38:21 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2016-07-23 04:03:43 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
2017-04-20 23:46:12 +08:00
|
|
|
#include <iterator>
|
|
|
|
|
2016-10-15 06:18:18 +08:00
|
|
|
#define DEBUG_TYPE "legalizer"
|
2016-07-23 04:03:43 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2019-01-16 08:40:37 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
EnableCSEInLegalizer("enable-cse-in-legalizer",
|
|
|
|
cl::desc("Should enable CSE in Legalizer"),
|
|
|
|
cl::Optional, cl::init(false));
|
|
|
|
|
2016-10-15 06:18:18 +08:00
|
|
|
char Legalizer::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,
|
2016-08-27 10:38:21 +08:00
|
|
|
"Legalize the Machine IR a function's Machine IR", false,
|
|
|
|
false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
|
2019-01-16 08:40:37 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)
|
2016-10-15 06:18:18 +08:00
|
|
|
INITIALIZE_PASS_END(Legalizer, DEBUG_TYPE,
|
2016-08-27 10:38:21 +08:00
|
|
|
"Legalize the Machine IR a function's Machine IR", false,
|
|
|
|
false)
|
2016-07-23 04:03:43 +08:00
|
|
|
|
2016-10-15 06:18:18 +08:00
|
|
|
Legalizer::Legalizer() : MachineFunctionPass(ID) {
|
|
|
|
initializeLegalizerPass(*PassRegistry::getPassRegistry());
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
|
|
|
|
2016-10-15 06:18:18 +08:00
|
|
|
void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const {
|
2016-08-27 10:38:21 +08:00
|
|
|
AU.addRequired<TargetPassConfig>();
|
2019-01-16 08:40:37 +08:00
|
|
|
AU.addRequired<GISelCSEAnalysisWrapperPass>();
|
|
|
|
AU.addPreserved<GISelCSEAnalysisWrapperPass>();
|
2018-07-13 08:08:38 +08:00
|
|
|
getSelectionDAGFallbackAnalysisUsage(AU);
|
2016-08-27 10:38:21 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:18:18 +08:00
|
|
|
void Legalizer::init(MachineFunction &MF) {
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
|
|
|
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
static bool isArtifact(const MachineInstr &MI) {
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case TargetOpcode::G_TRUNC:
|
|
|
|
case TargetOpcode::G_ZEXT:
|
|
|
|
case TargetOpcode::G_ANYEXT:
|
|
|
|
case TargetOpcode::G_SEXT:
|
|
|
|
case TargetOpcode::G_MERGE_VALUES:
|
|
|
|
case TargetOpcode::G_UNMERGE_VALUES:
|
2018-12-11 02:44:58 +08:00
|
|
|
case TargetOpcode::G_CONCAT_VECTORS:
|
|
|
|
case TargetOpcode::G_BUILD_VECTOR:
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 04:14:52 +08:00
|
|
|
using InstListTy = GISelWorkList<256>;
|
|
|
|
using ArtifactListTy = GISelWorkList<128>;
|
|
|
|
|
2019-01-13 02:36:22 +08:00
|
|
|
namespace {
|
2018-12-06 04:14:52 +08:00
|
|
|
class LegalizerWorkListManager : public GISelChangeObserver {
|
|
|
|
InstListTy &InstList;
|
|
|
|
ArtifactListTy &ArtifactList;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LegalizerWorkListManager(InstListTy &Insts, ArtifactListTy &Arts)
|
|
|
|
: InstList(Insts), ArtifactList(Arts) {}
|
|
|
|
|
2019-01-16 08:40:37 +08:00
|
|
|
void createdInstr(MachineInstr &MI) override {
|
2018-12-06 04:14:52 +08:00
|
|
|
// Only legalize pre-isel generic instructions.
|
|
|
|
// Legalization process could generate Target specific pseudo
|
|
|
|
// instructions with generic types. Don't record them
|
|
|
|
if (isPreISelGenericOpcode(MI.getOpcode())) {
|
|
|
|
if (isArtifact(MI))
|
|
|
|
ArtifactList.insert(&MI);
|
|
|
|
else
|
|
|
|
InstList.insert(&MI);
|
|
|
|
}
|
2018-12-13 07:48:13 +08:00
|
|
|
LLVM_DEBUG(dbgs() << ".. .. New MI: " << MI);
|
2018-12-06 04:14:52 +08:00
|
|
|
}
|
|
|
|
|
2019-01-16 08:40:37 +08:00
|
|
|
void erasingInstr(MachineInstr &MI) override {
|
2018-12-13 07:48:13 +08:00
|
|
|
LLVM_DEBUG(dbgs() << ".. .. Erasing: " << MI);
|
2018-12-06 04:14:52 +08:00
|
|
|
InstList.remove(&MI);
|
|
|
|
ArtifactList.remove(&MI);
|
|
|
|
}
|
|
|
|
|
2019-01-16 08:40:37 +08:00
|
|
|
void changingInstr(MachineInstr &MI) override {
|
2018-12-13 07:48:13 +08:00
|
|
|
LLVM_DEBUG(dbgs() << ".. .. Changing MI: " << MI);
|
|
|
|
}
|
|
|
|
|
2019-01-16 08:40:37 +08:00
|
|
|
void changedInstr(MachineInstr &MI) override {
|
2018-12-06 04:14:52 +08:00
|
|
|
// When insts change, we want to revisit them to legalize them again.
|
|
|
|
// We'll consider them the same as created.
|
2018-12-13 07:48:13 +08:00
|
|
|
LLVM_DEBUG(dbgs() << ".. .. Changed MI: " << MI);
|
2018-12-06 04:14:52 +08:00
|
|
|
createdInstr(MI);
|
|
|
|
}
|
|
|
|
};
|
2019-01-13 02:36:22 +08:00
|
|
|
} // namespace
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
|
2016-10-15 06:18:18 +08:00
|
|
|
bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
|
2016-08-27 08:18:24 +08:00
|
|
|
// If the ISel pipeline failed, do not bother running that pass.
|
|
|
|
if (MF.getProperties().hasProperty(
|
|
|
|
MachineFunctionProperties::Property::FailedISel))
|
|
|
|
return false;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
|
2016-07-23 04:03:43 +08:00
|
|
|
init(MF);
|
2016-08-27 10:38:21 +08:00
|
|
|
const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
|
2019-01-16 08:40:37 +08:00
|
|
|
GISelCSEAnalysisWrapper &Wrapper =
|
|
|
|
getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
|
2017-02-24 05:05:42 +08:00
|
|
|
MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
|
2016-07-23 04:03:43 +08:00
|
|
|
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
const size_t NumBlocks = MF.size();
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
2016-08-02 19:41:09 +08:00
|
|
|
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
// Populate Insts
|
2019-01-16 08:40:37 +08:00
|
|
|
InstListTy InstList;
|
|
|
|
ArtifactListTy ArtifactList;
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
|
|
|
|
// Perform legalization bottom up so we can DCE as we legalize.
|
|
|
|
// Traverse BB in RPOT and within each basic block, add insts top down,
|
|
|
|
// so when we pop_back_val in the legalization process, we traverse bottom-up.
|
|
|
|
for (auto *MBB : RPOT) {
|
|
|
|
if (MBB->empty())
|
|
|
|
continue;
|
|
|
|
for (MachineInstr &MI : *MBB) {
|
2016-08-02 19:41:09 +08:00
|
|
|
// Only legalize pre-isel generic instructions: others don't have types
|
|
|
|
// and are assumed to be legal.
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
if (!isPreISelGenericOpcode(MI.getOpcode()))
|
2016-08-02 19:41:09 +08:00
|
|
|
continue;
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
if (isArtifact(MI))
|
|
|
|
ArtifactList.insert(&MI);
|
|
|
|
else
|
|
|
|
InstList.insert(&MI);
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
2017-04-20 23:46:12 +08:00
|
|
|
}
|
2019-01-16 08:40:37 +08:00
|
|
|
std::unique_ptr<MachineIRBuilder> MIRBuilder;
|
|
|
|
GISelCSEInfo *CSEInfo = nullptr;
|
|
|
|
bool IsO0 = TPC.getOptLevel() == CodeGenOpt::Level::None;
|
|
|
|
// Disable CSE for O0.
|
|
|
|
bool EnableCSE = !IsO0 && EnableCSEInLegalizer;
|
|
|
|
if (EnableCSE) {
|
|
|
|
MIRBuilder = make_unique<CSEMIRBuilder>();
|
|
|
|
std::unique_ptr<CSEConfig> Config = make_unique<CSEConfig>();
|
|
|
|
CSEInfo = &Wrapper.get(std::move(Config));
|
|
|
|
MIRBuilder->setCSEInfo(CSEInfo);
|
|
|
|
} else
|
|
|
|
MIRBuilder = make_unique<MachineIRBuilder>();
|
|
|
|
// This observer keeps the worklist updated.
|
2018-12-06 04:14:52 +08:00
|
|
|
LegalizerWorkListManager WorkListObserver(InstList, ArtifactList);
|
2019-01-16 08:40:37 +08:00
|
|
|
// We want both WorkListObserver as well as CSEInfo to observe all changes.
|
|
|
|
// Use the wrapper observer.
|
|
|
|
GISelObserverWrapper WrapperObserver(&WorkListObserver);
|
|
|
|
if (EnableCSE && CSEInfo)
|
|
|
|
WrapperObserver.addObserver(CSEInfo);
|
|
|
|
// Now install the observer as the delegate to MF.
|
|
|
|
// This will keep all the observers notified about new insertions/deletions.
|
|
|
|
RAIIDelegateInstaller DelInstall(MF, &WrapperObserver);
|
|
|
|
LegalizerHelper Helper(MF, WrapperObserver, *MIRBuilder.get());
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
const LegalizerInfo &LInfo(Helper.getLegalizerInfo());
|
2019-01-16 08:40:37 +08:00
|
|
|
LegalizationArtifactCombiner ArtCombiner(*MIRBuilder.get(), MF.getRegInfo(),
|
|
|
|
LInfo);
|
|
|
|
auto RemoveDeadInstFromLists = [&WrapperObserver](MachineInstr *DeadMI) {
|
|
|
|
WrapperObserver.erasingInstr(*DeadMI);
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
};
|
|
|
|
bool Changed = false;
|
|
|
|
do {
|
|
|
|
while (!InstList.empty()) {
|
|
|
|
MachineInstr &MI = *InstList.pop_back_val();
|
|
|
|
assert(isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode");
|
|
|
|
if (isTriviallyDead(MI, MRI)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n");
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
MI.eraseFromParentAndMarkDBGValuesForRemoval();
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-31 04:51:25 +08:00
|
|
|
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
// Do the legalization for this instruction.
|
|
|
|
auto Res = Helper.legalizeInstrStep(MI);
|
|
|
|
// Error out if we couldn't legalize this instruction. We may want to
|
|
|
|
// fall back to DAG ISel instead in the future.
|
|
|
|
if (Res == LegalizerHelper::UnableToLegalize) {
|
2018-12-06 04:14:52 +08:00
|
|
|
Helper.MIRBuilder.stopObservingChanges();
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
|
|
|
|
"unable to legalize instruction", MI);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Changed |= Res == LegalizerHelper::Legalized;
|
|
|
|
}
|
|
|
|
while (!ArtifactList.empty()) {
|
|
|
|
MachineInstr &MI = *ArtifactList.pop_back_val();
|
|
|
|
assert(isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode");
|
|
|
|
if (isTriviallyDead(MI, MRI)) {
|
2018-12-13 05:32:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << MI << "Is dead\n");
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
RemoveDeadInstFromLists(&MI);
|
|
|
|
MI.eraseFromParentAndMarkDBGValuesForRemoval();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SmallVector<MachineInstr *, 4> DeadInstructions;
|
|
|
|
if (ArtCombiner.tryCombineInstruction(MI, DeadInstructions)) {
|
|
|
|
for (auto *DeadMI : DeadInstructions) {
|
2018-12-13 05:32:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << *DeadMI << "Is dead\n");
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
RemoveDeadInstFromLists(DeadMI);
|
|
|
|
DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
|
|
|
|
}
|
|
|
|
Changed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If this was not an artifact (that could be combined away), this might
|
|
|
|
// need special handling. Add it to InstList, so when it's processed
|
|
|
|
// there, it has to be legal or specially handled.
|
|
|
|
else
|
|
|
|
InstList.insert(&MI);
|
2016-08-31 04:51:25 +08:00
|
|
|
}
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
} while (!InstList.empty());
|
|
|
|
|
|
|
|
// For now don't support if new blocks are inserted - we would need to fix the
|
|
|
|
// outerloop for that.
|
|
|
|
if (MF.size() != NumBlocks) {
|
|
|
|
MachineOptimizationRemarkMissed R("gisel-legalize", "GISelFailure",
|
2017-12-16 06:22:58 +08:00
|
|
|
MF.getFunction().getSubprogram(),
|
[GISel]: Rework legalization algorithm for better elimination of
artifacts along with DCE
Legalization Artifacts are all those insts that are there to make the
type system happy. Currently, the target needs to say all combinations
of extends and truncs are legal and there's no way of verifying that
post legalization, we only have *truly* legal instructions. This patch
changes roughly the legalization algorithm to process all illegal insts
at one go, and then process all truncs/extends that were added to
satisfy the type constraints separately trying to combine trivial cases
until they converge. This has the added benefit that, the target
legalizerinfo can only say which truncs and extends are okay and the
artifact combiner would combine away other exts and truncs.
Updated legalization algorithm to roughly the following pseudo code.
WorkList Insts, Artifacts;
collect_all_insts_and_artifacts(Insts, Artifacts);
do {
for (Inst in Insts)
legalizeInstrStep(Inst, Insts, Artifacts);
for (Artifact in Artifacts)
tryCombineArtifact(Artifact, Insts, Artifacts);
} while(!Insts.empty());
Also, wrote a simple wrapper equivalent to SetVector, except for
erasing, it avoids moving all elements over by one and instead just
nulls them out.
llvm-svn: 318210
2017-11-15 06:42:19 +08:00
|
|
|
/*MBB=*/nullptr);
|
|
|
|
R << "inserting blocks is not supported yet";
|
|
|
|
reportGISelFailure(MF, TPC, MORE, R);
|
|
|
|
return false;
|
2016-08-31 04:51:25 +08:00
|
|
|
}
|
|
|
|
|
2016-07-23 04:03:43 +08:00
|
|
|
return Changed;
|
|
|
|
}
|