Index: CMakeLists.txt =================================================================== --- CMakeLists.txt +++ CMakeLists.txt @@ -68,6 +68,8 @@ endif() endif() +set(LLVM_BUILD_GLOBAL_ISEL OFF CACHE BOOL "Experimental: Build GlobalISel") + set(LLVM_PARALLEL_LINK_JOBS "" CACHE STRING "Define the maximum number of concurrent link jobs.") if(LLVM_PARALLEL_LINK_JOBS) Index: include/llvm/CodeGen/GlobalISel/IRTranslator.h =================================================================== --- /dev/null +++ include/llvm/CodeGen/GlobalISel/IRTranslator.h @@ -0,0 +1,127 @@ +//===-- llvm/CodeGen/GlobalISel/IRTranslator.h - IRTranslator ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// \file +/// This file declares the IRTranslator pass. +/// This pass is responsible for translating LLVM IR into MachineInstr. +/// It uses target hooks to lower the ABI but aside from that, the pass +/// generated code is generic. This is the default translator used for +/// GlobalISel. +/// +/// \todo Replace the comments with actual doxygen comments. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_GLOBALISEL_IRTRANSLATOR_H +#define LLVM_CODEGEN_GLOBALISEL_IRTRANSLATOR_H + +#include "Types.h" +#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/IR/Constants.h" + +namespace llvm { +// Forward declarations. +class Constant; +class Instruction; +class MachineInstr; +class MachineIRBuilder; + +// Technically the pass should run on an hypothetical MachineModule, +// since it should translate Global into some sort of MachineGlobal. +// The MachineGlobal should ultimately just be a transfer of ownership of +// the interesting bits that are relevant to represent a global value. +// That being said, we could investigate what would it cost to just duplicate +// the information from the LLVM IR. +// The idea is that ultimately we would be able to free up the memory used +// by the LLVM IR as soon as the translation is over. +class IRTranslator : public MachineFunctionPass { +public: + static char ID; + +private: + // Interface used to lower the everything related to calls. + // TargetLowering *CallLowering; + // Mapping of the values of the current LLVM IR function + // to the related virtual registers. + // We need several virtual registers for the lowering of things + // like structures. Right now, this is just a list of virtual + // registers, but we would need to encapsulate that in a higher + // level class. + ValueToVRegs ValToVRegs; + // Mapping of a constant to the instructions to produce + // that constant. + // Constants are special because when we encounter one, + // we do not know at first where to insert the definition since + // this depends on all its uses. + // Thus, we will insert the sequences to materialize them when + // we know all their users. + // In the meantime, just keep it in a map. + // Note: Constants that end up as immediate in the related instructions, + // do not appear in that map. + DenseMap> ConstantToSequence; + + /* A bunch of methods targeting fa fas ADD, SUB, etc. */ + // Return true if the translation was successful, false + // otherwise. + // Note: The MachineIRBuilder would encapsulate a + // MachineRegisterInfo to create virtual registers. + // + // Algo: + // 1. Look for a virtual register for each operand or + // create one. + // 2 Update the ValToVReg accordingly. + // 2.alt. For constant arguments, if they are compile time constants, + // produce an immediate in the right operand and do not touch + // ValToReg. Otherwise, update ValToVReg and register the + // sequence to materialize the constant in ConstantToSequence. + // 3. Create the generic instruction. + bool translateADD(const Instruction &Inst); + + // Builder for machine instruction a la IRBuilder. + // I.e., compared to regular MIBuilder, this one also inserts the instruction + // in the current block, it can creates block, etc., basically a kind of + // IRBuilder, but for Machine IR. + MachineIRBuilder *MIRBuilder; + + // Return true if the translation from LLVM IR to Machine IR + // suceeded. + // See translateXXX for details. + bool translate(const Instruction &); + + // * Insert all the code needed to materialize the constants + // at the proper place. E.g., Entry block or dominator block + // of each constant depending ob how fancy we want to be. + // * Clear the different maps. + void finalize(); +public: + // Ctor, nothing fancy. + IRTranslator(); + + // Instead of having the instance of the IRTranslatorToolkit + // as an argument of the constructor of IRTranslator, we ask + // the target the instance of the toolkit for each MachineFunction. + // The interest is that we may have different translator for different + // subtract or optimization. E.g., we could have a translator optimized + // to produce small code size. + // + // Algo: + // CallLowering = MF.subtarget.getCallLowering() + // F = MF.getParent() + // MIRBuilder.reset(MF) + // MIRBuilder.getOrCreateBB(F.getEntryBB()) + // CallLowering->translateArguments(MIRBuilder, F, ValToVReg) + // for each bb in F + // MIRBuilder.getOrCreateBB(bb) + // for each inst in bb + // if (!translate(MIRBuilder, inst, ValToVReg, ConstantToSequence)) + // report_fatal_error(“Don’t know how to translate input"); + // finalize() + bool runOnMachineFunction(MachineFunction &MF) override; +}; + +} // End namespace llvm. +#endif Index: include/llvm/CodeGen/GlobalISel/Types.h =================================================================== --- /dev/null +++ include/llvm/CodeGen/GlobalISel/Types.h @@ -0,0 +1,34 @@ +//===-- llvm/CodeGen/GlobalISel/Types.h - Types used by GISel ----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This file describes high level types that are used by several passes or +/// APIs involved in the GlobalISel pipeline. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_GLOBALISEL_TYPES_H +#define LLVM_CODEGEN_GLOBALISEL_TYPES_H + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/IR/Value.h" + +namespace llvm { + +/// Map a value to virtual registers. +/// We must support several virtual registers for a value. +/// Indeed each virtual register is mapped to one EVT, but a value +/// may span over several EVT when it is a type representing a structure. +/// In that case the value will be break into EVTs. +/// Note: We need to expose this type to the target hooks for thing like +/// ABI lowering that would be used during IRTranslation. +typedef DenseMap> ValueToVRegs; + +} // End namespace llvm. +#endif Index: lib/CodeGen/CMakeLists.txt =================================================================== --- lib/CodeGen/CMakeLists.txt +++ lib/CodeGen/CMakeLists.txt @@ -145,3 +145,6 @@ add_subdirectory(SelectionDAG) add_subdirectory(AsmPrinter) add_subdirectory(MIRParser) +if(LLVM_BUILD_GLOBAL_ISEL) + add_subdirectory(GlobalISel) +endif() Index: lib/CodeGen/GlobalISel/CMakeLists.txt =================================================================== --- /dev/null +++ lib/CodeGen/GlobalISel/CMakeLists.txt @@ -0,0 +1,5 @@ +add_llvm_library(LLVMGlobalISel + IRTranslator.cpp + ) + +add_dependencies(LLVMGlobalISel intrinsics_gen) Index: lib/CodeGen/GlobalISel/IRTranslator.cpp =================================================================== --- /dev/null +++ lib/CodeGen/GlobalISel/IRTranslator.cpp @@ -0,0 +1,37 @@ +//===-- llvm/CodeGen/GlobalISel/IRTranslator.cpp - IRTranslator --*- C++ -*-==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// \file +/// This file implements the IRTranslator class. +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/GlobalISel/IRTranslator.h" + +using namespace llvm; + +char IRTranslator::ID = 0; + +bool IRTranslator::translateADD(const Instruction &Inst) { + return false; +} + +bool IRTranslator::translate(const Instruction &) { + return false; +} + + +void IRTranslator::finalize() { +} + +IRTranslator::IRTranslator() + : MachineFunctionPass(ID) { +} + +bool IRTranslator::runOnMachineFunction(MachineFunction &MF) { + return false; +} Index: lib/CodeGen/GlobalISel/LLVMBuild.txt =================================================================== --- /dev/null +++ lib/CodeGen/GlobalISel/LLVMBuild.txt @@ -0,0 +1,22 @@ +;===- ./lib/CodeGen/GlobalISel/LLVMBuild.txt -----------------*- Conf -*--===; +; +; The LLVM Compiler Infrastructure +; +; This file is distributed under the University of Illinois Open Source +; License. See LICENSE.TXT for details. +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = GlobalISel +parent = CodeGen +required_libraries = Analysis CodeGen Core MC Support Target TransformUtils Index: lib/CodeGen/LLVMBuild.txt =================================================================== --- lib/CodeGen/LLVMBuild.txt +++ lib/CodeGen/LLVMBuild.txt @@ -16,7 +16,7 @@ ;===------------------------------------------------------------------------===; [common] -subdirectories = AsmPrinter SelectionDAG MIRParser +subdirectories = AsmPrinter SelectionDAG MIRParser GlobalISel [component_0] type = Library Index: tools/llc/CMakeLists.txt =================================================================== --- tools/llc/CMakeLists.txt +++ tools/llc/CMakeLists.txt @@ -1,3 +1,10 @@ +# Add GlobalISel to the dependencies if the user wants to build it. +if(LLVM_BUILD_GLOBAL_ISEL) + set(GLOBAL_ISEL GlobalISel) +else() + set(GLOBAL_ISEL "") +endif() + set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} Analysis @@ -5,6 +12,7 @@ CodeGen Core IRReader + ${GLOBAL_ISEL} MC MIRParser ScalarOpts