|
| 1 | +//=== llvm/Analysis/CSADescriptors.cpp - CSA Descriptors -*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file "describes" conditional scalar assignments (CSA). |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "llvm/Analysis/CSADescriptors.h" |
| 14 | +#include "llvm/IR/PatternMatch.h" |
| 15 | +#include "llvm/IR/Type.h" |
| 16 | + |
| 17 | +using namespace llvm; |
| 18 | +using namespace llvm::PatternMatch; |
| 19 | + |
| 20 | +#define DEBUG_TYPE "csa-descriptors" |
| 21 | + |
| 22 | +CSADescriptor CSADescriptor::isCSAPhi(PHINode *Phi, Loop *TheLoop) { |
| 23 | + // Return CSADescriptor that describes a CSA that matches one of these |
| 24 | + // patterns: |
| 25 | + // phi loop_inv, (select cmp, value, phi) |
| 26 | + // phi loop_inv, (select cmp, phi, value) |
| 27 | + // phi (select cmp, value, phi), loop_inv |
| 28 | + // phi (select cmp, phi, value), loop_inv |
| 29 | + // If the CSA does not match any of these paterns, return a CSADescriptor |
| 30 | + // that describes an InvalidCSA. |
| 31 | + |
| 32 | + // Must be a scalar |
| 33 | + Type *Type = Phi->getType(); |
| 34 | + if (!Type->isIntegerTy() && !Type->isFloatingPointTy() && |
| 35 | + !Type->isPointerTy()) |
| 36 | + return CSADescriptor(); |
| 37 | + |
| 38 | + // Match phi loop_inv, (select cmp, value, phi) |
| 39 | + // or phi loop_inv, (select cmp, phi, value) |
| 40 | + // or phi (select cmp, value, phi), loop_inv |
| 41 | + // or phi (select cmp, phi, value), loop_inv |
| 42 | + if (Phi->getNumIncomingValues() != 2) |
| 43 | + return CSADescriptor(); |
| 44 | + auto SelectInstIt = find_if(Phi->incoming_values(), [&Phi](Use &U) { |
| 45 | + return match(U.get(), m_Select(m_Value(), m_Specific(Phi), m_Value())) || |
| 46 | + match(U.get(), m_Select(m_Value(), m_Value(), m_Specific(Phi))); |
| 47 | + }); |
| 48 | + if (SelectInstIt == Phi->incoming_values().end()) |
| 49 | + return CSADescriptor(); |
| 50 | + auto LoopInvIt = find_if(Phi->incoming_values(), [&](Use &U) { |
| 51 | + return U.get() != *SelectInstIt && TheLoop->isLoopInvariant(U.get()); |
| 52 | + }); |
| 53 | + if (LoopInvIt == Phi->incoming_values().end()) |
| 54 | + return CSADescriptor(); |
| 55 | + |
| 56 | + // Phi or Sel must be used only outside the loop, |
| 57 | + // excluding if Phi use Sel or Sel use Phi |
| 58 | + auto IsOnlyUsedOutsideLoop = [=](Value *V, Value *Ignore) { |
| 59 | + return all_of(V->users(), [Ignore, TheLoop](User *U) { |
| 60 | + if (U == Ignore) |
| 61 | + return true; |
| 62 | + if (auto *I = dyn_cast<Instruction>(U)) |
| 63 | + return !TheLoop->contains(I); |
| 64 | + return true; |
| 65 | + }); |
| 66 | + }; |
| 67 | + auto *Sel = cast<SelectInst>(SelectInstIt->get()); |
| 68 | + auto *LoopInv = LoopInvIt->get(); |
| 69 | + if (!IsOnlyUsedOutsideLoop(Phi, Sel) || !IsOnlyUsedOutsideLoop(Sel, Phi)) |
| 70 | + return CSADescriptor(); |
| 71 | + |
| 72 | + return CSADescriptor(Phi, Sel, LoopInv); |
| 73 | +} |
0 commit comments