1 //===-- ConstantFolding.h - Fold instructions into constants ----*- 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 declares routines for folding instructions into constants when all 10 // operands are constants, for example "sub i32 1, 0" -> "1". 11 // 12 // Also, to supplement the basic VMCore ConstantExpr simplifications, 13 // this file declares some additional folding routines that can make use of 14 // DataLayout information. These functions cannot go in VMCore due to library 15 // dependency issues. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H 20 #define LLVM_ANALYSIS_CONSTANTFOLDING_H 21 22 namespace llvm { 23 class APInt; 24 template <typename T> class ArrayRef; 25 class CallBase; 26 class Constant; 27 class ConstantExpr; 28 class DataLayout; 29 class Function; 30 class GlobalValue; 31 class Instruction; 32 class TargetLibraryInfo; 33 class Type; 34 35 /// If this constant is a constant offset from a global, return the global and 36 /// the constant. Because of constantexprs, this function is recursive. 37 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, 38 const DataLayout &DL, bool LookThroughAddrSpaces); 39 40 /// ConstantFoldInstruction - Try to constant fold the specified instruction. 41 /// If successful, the constant result is returned, if not, null is returned. 42 /// Note that this fails if not all of the operands are constant. Otherwise, 43 /// this function can only fail when attempting to fold instructions like loads 44 /// and stores, which have no constant expression form. 45 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL, 46 const TargetLibraryInfo *TLI = nullptr); 47 48 /// ConstantFoldConstant - Fold the constant using the specified DataLayout. 49 /// This function always returns a non-null constant: Either the folding result, 50 /// or the original constant if further folding is not possible. 51 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL, 52 const TargetLibraryInfo *TLI = nullptr); 53 54 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the 55 /// specified operands. If successful, the constant result is returned, if not, 56 /// null is returned. Note that this function can fail when attempting to 57 /// fold instructions like loads and stores, which have no constant expression 58 /// form. 59 /// 60 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops, 61 const DataLayout &DL, 62 const TargetLibraryInfo *TLI = nullptr); 63 64 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare 65 /// instruction (icmp/fcmp) with the specified operands. If it fails, it 66 /// returns a constant expression of the specified operands. 67 /// 68 Constant * 69 ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, 70 Constant *RHS, const DataLayout &DL, 71 const TargetLibraryInfo *TLI = nullptr); 72 73 /// Attempt to constant fold a unary operation with the specified 74 /// operand. If it fails, it returns a constant expression of the specified 75 /// operands. 76 Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, 77 const DataLayout &DL); 78 79 /// Attempt to constant fold a binary operation with the specified 80 /// operands. If it fails, it returns a constant expression of the specified 81 /// operands. 82 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, 83 Constant *RHS, const DataLayout &DL); 84 85 /// Attempt to constant fold a select instruction with the specified 86 /// operands. The constant result is returned if successful; if not, null is 87 /// returned. 88 Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, 89 Constant *V2); 90 91 /// Attempt to constant fold a cast with the specified operand. If it 92 /// fails, it returns a constant expression of the specified operand. 93 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, 94 const DataLayout &DL); 95 96 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue 97 /// instruction with the specified operands and indices. The constant result is 98 /// returned if successful; if not, null is returned. 99 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, 100 ArrayRef<unsigned> Idxs); 101 102 /// Attempt to constant fold an extractvalue instruction with the 103 /// specified operands and indices. The constant result is returned if 104 /// successful; if not, null is returned. 105 Constant *ConstantFoldExtractValueInstruction(Constant *Agg, 106 ArrayRef<unsigned> Idxs); 107 108 /// Attempt to constant fold an insertelement instruction with the 109 /// specified operands and indices. The constant result is returned if 110 /// successful; if not, null is returned. 111 Constant *ConstantFoldInsertElementInstruction(Constant *Val, 112 Constant *Elt, 113 Constant *Idx); 114 115 /// Attempt to constant fold an extractelement instruction with the 116 /// specified operands and indices. The constant result is returned if 117 /// successful; if not, null is returned. 118 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx); 119 120 /// Attempt to constant fold a shufflevector instruction with the 121 /// specified operands and mask. See class ShuffleVectorInst for a description 122 /// of the mask representation. The constant result is returned if successful; 123 /// if not, null is returned. 124 Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, 125 ArrayRef<int> Mask); 126 127 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would 128 /// produce if it is constant and determinable. If this is not determinable, 129 /// return null. 130 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); 131 132 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a 133 /// getelementptr constantexpr, return the constant value being addressed by the 134 /// constant expression, or null if something is funny and we can't decide. 135 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); 136 137 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr 138 /// indices (with an *implied* zero pointer index that is not in the list), 139 /// return the constant value being addressed by a virtual load, or null if 140 /// something is funny and we can't decide. 141 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, 142 ArrayRef<Constant *> Indices); 143 144 /// canConstantFoldCallTo - Return true if its even possible to fold a call to 145 /// the specified function. 146 bool canConstantFoldCallTo(const CallBase *Call, const Function *F); 147 148 /// ConstantFoldCall - Attempt to constant fold a call to the specified function 149 /// with the specified arguments, returning null if unsuccessful. 150 Constant *ConstantFoldCall(const CallBase *Call, Function *F, 151 ArrayRef<Constant *> Operands, 152 const TargetLibraryInfo *TLI = nullptr); 153 154 /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type 155 /// returning null if unsuccessful. Can cast pointer to pointer or pointer to 156 /// integer and vice versa if their sizes are equal. 157 Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, 158 const DataLayout &DL); 159 160 /// Check whether the given call has no side-effects. 161 /// Specifically checks for math routimes which sometimes set errno. 162 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI); 163 } 164 165 #endif 166