| 1 | //===- VNCoercion.h - Value Numbering Coercion Utilities --------*- 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 | /// \file / This file provides routines used by LLVM's value numbering passes to |
| 9 | /// perform various forms of value extraction from memory when the types are not |
| 10 | /// identical. For example, given |
| 11 | /// |
| 12 | /// store i32 8, i32 *%foo |
| 13 | /// %a = bitcast i32 *%foo to i16 |
| 14 | /// %val = load i16, i16 *%a |
| 15 | /// |
| 16 | /// It possible to extract the value of the load of %a from the store to %foo. |
| 17 | /// These routines know how to tell whether they can do that (the analyze* |
| 18 | /// routines), and can also insert the necessary IR to do it (the get* |
| 19 | /// routines). |
| 20 | |
| 21 | #ifndef LLVM_TRANSFORMS_UTILS_VNCOERCION_H |
| 22 | #define LLVM_TRANSFORMS_UTILS_VNCOERCION_H |
| 23 | |
| 24 | namespace llvm { |
| 25 | class Constant; |
| 26 | class Function; |
| 27 | class StoreInst; |
| 28 | class LoadInst; |
| 29 | class MemIntrinsic; |
| 30 | class Instruction; |
| 31 | class IRBuilderBase; |
| 32 | class Value; |
| 33 | class Type; |
| 34 | class DataLayout; |
| 35 | namespace VNCoercion { |
| 36 | /// Return true if CoerceAvailableValueToLoadType would succeed if it was |
| 37 | /// called. |
| 38 | bool canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy, |
| 39 | Function *F); |
| 40 | |
| 41 | /// If we saw a store of a value to memory, and then a load from a must-aliased |
| 42 | /// pointer of a different type, try to coerce the stored value to the loaded |
| 43 | /// type. LoadedTy is the type of the load we want to replace. IRB is |
| 44 | /// IRBuilder used to insert new instructions. |
| 45 | /// |
| 46 | /// If we can't do it, return null. |
| 47 | Value *coerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy, |
| 48 | IRBuilderBase &IRB, Function *F); |
| 49 | |
| 50 | /// This function determines whether a value for the pointer LoadPtr can be |
| 51 | /// extracted from the store at DepSI. |
| 52 | /// |
| 53 | /// On success, it returns the offset into DepSI that extraction would start. |
| 54 | /// On failure, it returns -1. |
| 55 | int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr, |
| 56 | StoreInst *DepSI, const DataLayout &DL); |
| 57 | |
| 58 | /// This function determines whether a value for the pointer LoadPtr can be |
| 59 | /// extracted from the load at DepLI. |
| 60 | /// |
| 61 | /// On success, it returns the offset into DepLI that extraction would start. |
| 62 | /// On failure, it returns -1. |
| 63 | int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI, |
| 64 | const DataLayout &DL); |
| 65 | |
| 66 | /// This function determines whether a value for the pointer LoadPtr can be |
| 67 | /// extracted from the memory intrinsic at DepMI. |
| 68 | /// |
| 69 | /// On success, it returns the offset into DepMI that extraction would start. |
| 70 | /// On failure, it returns -1. |
| 71 | int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, |
| 72 | MemIntrinsic *DepMI, const DataLayout &DL); |
| 73 | |
| 74 | /// If analyzeLoadFromClobberingStore/Load returned an offset, this function |
| 75 | /// can be used to actually perform the extraction of the bits from the store. |
| 76 | /// It inserts instructions to do so at InsertPt, and returns the extracted |
| 77 | /// value. |
| 78 | Value *getValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy, |
| 79 | Instruction *InsertPt, Function *F); |
| 80 | // This is the same as getValueForLoad, except it performs no insertion. |
| 81 | // It only allows constant inputs. |
| 82 | Constant *getConstantValueForLoad(Constant *SrcVal, unsigned Offset, |
| 83 | Type *LoadTy, const DataLayout &DL); |
| 84 | |
| 85 | /// If analyzeLoadFromClobberingMemInst returned an offset, this function can be |
| 86 | /// used to actually perform the extraction of the bits from the memory |
| 87 | /// intrinsic. It inserts instructions to do so at InsertPt, and returns the |
| 88 | /// extracted value. |
| 89 | Value *getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, |
| 90 | Type *LoadTy, Instruction *InsertPt, |
| 91 | const DataLayout &DL); |
| 92 | // This is the same as getStoreValueForLoad, except it performs no insertion. |
| 93 | // It returns nullptr if it cannot produce a constant. |
| 94 | Constant *getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, |
| 95 | Type *LoadTy, const DataLayout &DL); |
| 96 | } |
| 97 | } |
| 98 | #endif |
| 99 | |