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