LCOV - code coverage report
Current view: top level - lib/CodeGen/GlobalISel - LegalizerInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 83 90 92.2 %
Date: 2017-09-14 15:23:50 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- lib/CodeGen/GlobalISel/LegalizerInfo.cpp - Legalizer ---------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Implement an interface to specify and query how an illegal operation on a
      11             : // given type should be expanded.
      12             : //
      13             : // Issues to be resolved:
      14             : //   + Make it fast.
      15             : //   + Support weird types like i3, <7 x i3>, ...
      16             : //   + Operations with more than one type (ICMP, CMPXCHG, intrinsics, ...)
      17             : //
      18             : //===----------------------------------------------------------------------===//
      19             : 
      20             : #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
      21             : #include "llvm/ADT/SmallBitVector.h"
      22             : #include "llvm/CodeGen/MachineInstr.h"
      23             : #include "llvm/CodeGen/MachineOperand.h"
      24             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      25             : #include "llvm/MC/MCInstrDesc.h"
      26             : #include "llvm/Support/ErrorHandling.h"
      27             : #include "llvm/Support/LowLevelTypeImpl.h"
      28             : #include "llvm/Support/MathExtras.h"
      29             : #include "llvm/Target/TargetOpcodes.h"
      30             : #include <algorithm>
      31             : #include <cassert>
      32             : #include <tuple>
      33             : #include <utility>
      34             : 
      35             : using namespace llvm;
      36             : 
      37     1195117 : LegalizerInfo::LegalizerInfo() {
      38       31042 :   DefaultActions[TargetOpcode::G_IMPLICIT_DEF] = NarrowScalar;
      39             : 
      40             :   // FIXME: these two can be legalized to the fundamental load/store Jakob
      41             :   // proposed. Once loads & stores are supported.
      42       31042 :   DefaultActions[TargetOpcode::G_ANYEXT] = Legal;
      43       31042 :   DefaultActions[TargetOpcode::G_TRUNC] = Legal;
      44             : 
      45       31042 :   DefaultActions[TargetOpcode::G_INTRINSIC] = Legal;
      46       31042 :   DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal;
      47             : 
      48       31042 :   DefaultActions[TargetOpcode::G_ADD] = NarrowScalar;
      49       31042 :   DefaultActions[TargetOpcode::G_LOAD] = NarrowScalar;
      50       31042 :   DefaultActions[TargetOpcode::G_STORE] = NarrowScalar;
      51             : 
      52       31042 :   DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar;
      53       31042 :   DefaultActions[TargetOpcode::G_INSERT] = NarrowScalar;
      54       31042 :   DefaultActions[TargetOpcode::G_EXTRACT] = NarrowScalar;
      55       31042 :   DefaultActions[TargetOpcode::G_FNEG] = Lower;
      56       15521 : }
      57             : 
      58       15521 : void LegalizerInfo::computeTables() {
      59     1148554 :   for (unsigned Opcode = 0; Opcode <= LastOp - FirstOp; ++Opcode) {
      60     3952234 :     for (unsigned Idx = 0; Idx != Actions[Opcode].size(); ++Idx) {
      61     4097669 :       for (auto &Action : Actions[Opcode][Idx]) {
      62     1849445 :         LLT Ty = Action.first;
      63     1849445 :         if (!Ty.isVector())
      64     1581064 :           continue;
      65             : 
      66      805143 :         auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp,
      67     1073524 :                                                         Ty.getElementType())];
      68      536762 :         Entry = std::max(Entry, Ty.getNumElements());
      69             :       }
      70             :     }
      71             :   }
      72             : 
      73       15521 :   TablesInitialized = true;
      74       15521 : }
      75             : 
      76             : // FIXME: inefficient implementation for now. Without ComputeValueVTs we're
      77             : // probably going to need specialized lookup structures for various types before
      78             : // we have any hope of doing well with something like <13 x i3>. Even the common
      79             : // cases should do better than what we have now.
      80             : std::pair<LegalizerInfo::LegalizeAction, LLT>
      81        5023 : LegalizerInfo::getAction(const InstrAspect &Aspect) const {
      82             :   assert(TablesInitialized && "backend forgot to call computeTables");
      83             :   // These *have* to be implemented for now, they're the fundamental basis of
      84             :   // how everything else is transformed.
      85             : 
      86             :   // FIXME: the long-term plan calls for expansion in terms of load/store (if
      87             :   // they're not legal).
      88        5023 :   if (Aspect.Opcode == TargetOpcode::G_MERGE_VALUES ||
      89             :       Aspect.Opcode == TargetOpcode::G_UNMERGE_VALUES)
      90         546 :     return std::make_pair(Legal, Aspect.Type);
      91             : 
      92        4750 :   LLT Ty = Aspect.Type;
      93        4750 :   LegalizeAction Action = findInActions(Aspect);
      94             :   // LegalizerHelper is not able to handle non-power-of-2 types right now, so do
      95             :   // not try to legalize them unless they are marked as Legal or Custom.
      96             :   // FIXME: This is a temporary hack until the general non-power-of-2
      97             :   // legalization works.
      98        4760 :   if (!isPowerOf2_64(Ty.getSizeInBits()) &&
      99           5 :       !(Action == Legal || Action == Custom))
     100          10 :     return std::make_pair(Unsupported, LLT());
     101             : 
     102        4745 :   if (Action != NotFound)
     103        4144 :     return findLegalAction(Aspect, Action);
     104             : 
     105         601 :   unsigned Opcode = Aspect.Opcode;
     106         601 :   if (!Ty.isVector()) {
     107         566 :     auto DefaultAction = DefaultActions.find(Aspect.Opcode);
     108        1693 :     if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
     109        1044 :       return std::make_pair(Legal, Ty);
     110             : 
     111         127 :     if (DefaultAction != DefaultActions.end() && DefaultAction->second == Lower)
     112           4 :       return std::make_pair(Lower, Ty);
     113             : 
     114         121 :     if (DefaultAction == DefaultActions.end() ||
     115          37 :         DefaultAction->second != NarrowScalar)
     116          10 :       return std::make_pair(Unsupported, LLT());
     117          37 :     return findLegalAction(Aspect, NarrowScalar);
     118             :   }
     119             : 
     120          35 :   LLT EltTy = Ty.getElementType();
     121          35 :   int NumElts = Ty.getNumElements();
     122             : 
     123          70 :   auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
     124          72 :   if (ScalarAction != ScalarInVectorActions.end() &&
     125           2 :       ScalarAction->second != Legal)
     126           0 :     return findLegalAction(Aspect, ScalarAction->second);
     127             : 
     128             :   // The element type is legal in principle, but the number of elements is
     129             :   // wrong.
     130         105 :   auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
     131          35 :   if (MaxLegalElts > NumElts)
     132           1 :     return findLegalAction(Aspect, MoreElements);
     133             : 
     134          34 :   if (MaxLegalElts == 0) {
     135             :     // Scalarize if there's no legal vector type, which is just a special case
     136             :     // of FewerElements.
     137           4 :     return std::make_pair(FewerElements, EltTy);
     138             :   }
     139             : 
     140          32 :   return findLegalAction(Aspect, FewerElements);
     141             : }
     142             : 
     143             : std::tuple<LegalizerInfo::LegalizeAction, unsigned, LLT>
     144        3647 : LegalizerInfo::getAction(const MachineInstr &MI,
     145             :                          const MachineRegisterInfo &MRI) const {
     146        7294 :   SmallBitVector SeenTypes(8);
     147        3647 :   const MCOperandInfo *OpInfo = MI.getDesc().OpInfo;
     148       20436 :   for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
     149       14082 :     if (!OpInfo[i].isGenericType())
     150        2778 :       continue;
     151             : 
     152             :     // We don't want to repeatedly check the same operand index, that
     153             :     // could get expensive.
     154       12586 :     unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
     155       18879 :     if (SeenTypes[TypeIdx])
     156        1282 :       continue;
     157             : 
     158        5011 :     SeenTypes.set(TypeIdx);
     159             : 
     160       10022 :     LLT Ty = MRI.getType(MI.getOperand(i).getReg());
     161       15033 :     auto Action = getAction({MI.getOpcode(), TypeIdx, Ty});
     162        5011 :     if (Action.first != Legal)
     163         940 :       return std::make_tuple(Action.first, TypeIdx, Action.second);
     164             :   }
     165        9531 :   return std::make_tuple(Legal, 0, LLT{});
     166             : }
     167             : 
     168           0 : bool LegalizerInfo::isLegal(const MachineInstr &MI,
     169             :                             const MachineRegisterInfo &MRI) const {
     170           0 :   return std::get<0>(getAction(MI, MRI)) == Legal;
     171             : }
     172             : 
     173        4214 : Optional<LLT> LegalizerInfo::findLegalType(const InstrAspect &Aspect,
     174             :                                  LegalizeAction Action) const {
     175        4214 :   switch(Action) {
     176           0 :   default:
     177           0 :     llvm_unreachable("Cannot find legal type");
     178        3935 :   case Legal:
     179             :   case Lower:
     180             :   case Libcall:
     181             :   case Custom:
     182        3935 :     return Aspect.Type;
     183             :   case NarrowScalar: {
     184             :     return findLegalizableSize(
     185         117 :         Aspect, [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
     186             :   }
     187             :   case WidenScalar: {
     188         365 :     return findLegalizableSize(Aspect, [&](LLT Ty) -> LLT {
     189         670 :       return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
     190         414 :     });
     191             :   }
     192             :   case FewerElements: {
     193             :     return findLegalizableSize(
     194         105 :         Aspect, [&](LLT Ty) -> LLT { return Ty.halfElements(); });
     195             :   }
     196             :   case MoreElements: {
     197             :     return findLegalizableSize(
     198           4 :         Aspect, [&](LLT Ty) -> LLT { return Ty.doubleElements(); });
     199             :   }
     200             :   }
     201             : }
     202             : 
     203           0 : bool LegalizerInfo::legalizeCustom(MachineInstr &MI,
     204             :                                    MachineRegisterInfo &MRI,
     205             :                                    MachineIRBuilder &MIRBuilder) const {
     206           0 :   return false;
     207             : }

Generated by: LCOV version 1.13