Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- NoFolder.h - Constant folding helper ---------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the NoFolder class, a helper for IRBuilder. It provides |
| 10 | // IRBuilder with a set of methods for creating unfolded constants. This is |
| 11 | // useful for learners trying to understand how LLVM IR works, and who don't |
| 12 | // want details to be hidden by the constant folder. For general constant |
| 13 | // creation and folding, use ConstantExpr and the routines in |
| 14 | // llvm/Analysis/ConstantFolding.h. |
| 15 | // |
| 16 | // Note: since it is not actually possible to create unfolded constants, this |
| 17 | // class returns instructions rather than constants. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #ifndef LLVM_IR_NOFOLDER_H |
| 22 | #define LLVM_IR_NOFOLDER_H |
| 23 | |
| 24 | #include "llvm/ADT/ArrayRef.h" |
| 25 | #include "llvm/IR/Constants.h" |
| 26 | #include "llvm/IR/InstrTypes.h" |
| 27 | #include "llvm/IR/Instruction.h" |
| 28 | #include "llvm/IR/Instructions.h" |
| 29 | |
| 30 | namespace llvm { |
| 31 | |
| 32 | /// NoFolder - Create "constants" (actually, instructions) with no folding. |
| 33 | class NoFolder { |
| 34 | public: |
| 35 | explicit NoFolder() = default; |
| 36 | |
| 37 | //===--------------------------------------------------------------------===// |
| 38 | // Binary Operators |
| 39 | //===--------------------------------------------------------------------===// |
| 40 | |
| 41 | Instruction *CreateAdd(Constant *LHS, Constant *RHS, |
| 42 | bool HasNUW = false, bool HasNSW = false) const { |
| 43 | BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS); |
| 44 | if (HasNUW) BO->setHasNoUnsignedWrap(); |
| 45 | if (HasNSW) BO->setHasNoSignedWrap(); |
| 46 | return BO; |
| 47 | } |
| 48 | |
| 49 | Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const { |
| 50 | return BinaryOperator::CreateNSWAdd(LHS, RHS); |
| 51 | } |
| 52 | |
| 53 | Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const { |
| 54 | return BinaryOperator::CreateNUWAdd(LHS, RHS); |
| 55 | } |
| 56 | |
| 57 | Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const { |
| 58 | return BinaryOperator::CreateFAdd(LHS, RHS); |
| 59 | } |
| 60 | |
| 61 | Instruction *CreateSub(Constant *LHS, Constant *RHS, |
| 62 | bool HasNUW = false, bool HasNSW = false) const { |
| 63 | BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS); |
| 64 | if (HasNUW) BO->setHasNoUnsignedWrap(); |
| 65 | if (HasNSW) BO->setHasNoSignedWrap(); |
| 66 | return BO; |
| 67 | } |
| 68 | |
| 69 | Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const { |
| 70 | return BinaryOperator::CreateNSWSub(LHS, RHS); |
| 71 | } |
| 72 | |
| 73 | Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const { |
| 74 | return BinaryOperator::CreateNUWSub(LHS, RHS); |
| 75 | } |
| 76 | |
| 77 | Instruction *CreateFSub(Constant *LHS, Constant *RHS) const { |
| 78 | return BinaryOperator::CreateFSub(LHS, RHS); |
| 79 | } |
| 80 | |
| 81 | Instruction *CreateMul(Constant *LHS, Constant *RHS, |
| 82 | bool HasNUW = false, bool HasNSW = false) const { |
| 83 | BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS); |
| 84 | if (HasNUW) BO->setHasNoUnsignedWrap(); |
| 85 | if (HasNSW) BO->setHasNoSignedWrap(); |
| 86 | return BO; |
| 87 | } |
| 88 | |
| 89 | Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const { |
| 90 | return BinaryOperator::CreateNSWMul(LHS, RHS); |
| 91 | } |
| 92 | |
| 93 | Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const { |
| 94 | return BinaryOperator::CreateNUWMul(LHS, RHS); |
| 95 | } |
| 96 | |
| 97 | Instruction *CreateFMul(Constant *LHS, Constant *RHS) const { |
| 98 | return BinaryOperator::CreateFMul(LHS, RHS); |
| 99 | } |
| 100 | |
| 101 | Instruction *CreateUDiv(Constant *LHS, Constant *RHS, |
| 102 | bool isExact = false) const { |
| 103 | if (!isExact) |
| 104 | return BinaryOperator::CreateUDiv(LHS, RHS); |
| 105 | return BinaryOperator::CreateExactUDiv(LHS, RHS); |
| 106 | } |
| 107 | |
| 108 | Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const { |
| 109 | return BinaryOperator::CreateExactUDiv(LHS, RHS); |
| 110 | } |
| 111 | |
| 112 | Instruction *CreateSDiv(Constant *LHS, Constant *RHS, |
| 113 | bool isExact = false) const { |
| 114 | if (!isExact) |
| 115 | return BinaryOperator::CreateSDiv(LHS, RHS); |
| 116 | return BinaryOperator::CreateExactSDiv(LHS, RHS); |
| 117 | } |
| 118 | |
| 119 | Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const { |
| 120 | return BinaryOperator::CreateExactSDiv(LHS, RHS); |
| 121 | } |
| 122 | |
| 123 | Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const { |
| 124 | return BinaryOperator::CreateFDiv(LHS, RHS); |
| 125 | } |
| 126 | |
| 127 | Instruction *CreateURem(Constant *LHS, Constant *RHS) const { |
| 128 | return BinaryOperator::CreateURem(LHS, RHS); |
| 129 | } |
| 130 | |
| 131 | Instruction *CreateSRem(Constant *LHS, Constant *RHS) const { |
| 132 | return BinaryOperator::CreateSRem(LHS, RHS); |
| 133 | } |
| 134 | |
| 135 | Instruction *CreateFRem(Constant *LHS, Constant *RHS) const { |
| 136 | return BinaryOperator::CreateFRem(LHS, RHS); |
| 137 | } |
| 138 | |
| 139 | Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false, |
| 140 | bool HasNSW = false) const { |
| 141 | BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS); |
| 142 | if (HasNUW) BO->setHasNoUnsignedWrap(); |
| 143 | if (HasNSW) BO->setHasNoSignedWrap(); |
| 144 | return BO; |
| 145 | } |
| 146 | |
| 147 | Instruction *CreateLShr(Constant *LHS, Constant *RHS, |
| 148 | bool isExact = false) const { |
| 149 | if (!isExact) |
| 150 | return BinaryOperator::CreateLShr(LHS, RHS); |
| 151 | return BinaryOperator::CreateExactLShr(LHS, RHS); |
| 152 | } |
| 153 | |
| 154 | Instruction *CreateAShr(Constant *LHS, Constant *RHS, |
| 155 | bool isExact = false) const { |
| 156 | if (!isExact) |
| 157 | return BinaryOperator::CreateAShr(LHS, RHS); |
| 158 | return BinaryOperator::CreateExactAShr(LHS, RHS); |
| 159 | } |
| 160 | |
| 161 | Instruction *CreateAnd(Constant *LHS, Constant *RHS) const { |
| 162 | return BinaryOperator::CreateAnd(LHS, RHS); |
| 163 | } |
| 164 | |
| 165 | Instruction *CreateOr(Constant *LHS, Constant *RHS) const { |
| 166 | return BinaryOperator::CreateOr(LHS, RHS); |
| 167 | } |
| 168 | |
| 169 | Instruction *CreateXor(Constant *LHS, Constant *RHS) const { |
| 170 | return BinaryOperator::CreateXor(LHS, RHS); |
| 171 | } |
| 172 | |
| 173 | Instruction *CreateBinOp(Instruction::BinaryOps Opc, |
| 174 | Constant *LHS, Constant *RHS) const { |
| 175 | return BinaryOperator::Create(Opc, LHS, RHS); |
| 176 | } |
| 177 | |
| 178 | //===--------------------------------------------------------------------===// |
| 179 | // Unary Operators |
| 180 | //===--------------------------------------------------------------------===// |
| 181 | |
| 182 | Instruction *CreateNeg(Constant *C, |
| 183 | bool HasNUW = false, bool HasNSW = false) const { |
| 184 | BinaryOperator *BO = BinaryOperator::CreateNeg(C); |
| 185 | if (HasNUW) BO->setHasNoUnsignedWrap(); |
| 186 | if (HasNSW) BO->setHasNoSignedWrap(); |
| 187 | return BO; |
| 188 | } |
| 189 | |
| 190 | Instruction *CreateNSWNeg(Constant *C) const { |
| 191 | return BinaryOperator::CreateNSWNeg(C); |
| 192 | } |
| 193 | |
| 194 | Instruction *CreateNUWNeg(Constant *C) const { |
| 195 | return BinaryOperator::CreateNUWNeg(C); |
| 196 | } |
| 197 | |
| 198 | Instruction *CreateFNeg(Constant *C) const { |
| 199 | return BinaryOperator::CreateFNeg(C); |
| 200 | } |
| 201 | |
| 202 | Instruction *CreateNot(Constant *C) const { |
| 203 | return BinaryOperator::CreateNot(C); |
| 204 | } |
| 205 | |
| 206 | //===--------------------------------------------------------------------===// |
| 207 | // Memory Instructions |
| 208 | //===--------------------------------------------------------------------===// |
| 209 | |
| 210 | Constant *CreateGetElementPtr(Type *Ty, Constant *C, |
| 211 | ArrayRef<Constant *> IdxList) const { |
| 212 | return ConstantExpr::getGetElementPtr(Ty, C, IdxList); |
| 213 | } |
| 214 | |
| 215 | Constant *CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const { |
| 216 | // This form of the function only exists to avoid ambiguous overload |
| 217 | // warnings about whether to convert Idx to ArrayRef<Constant *> or |
| 218 | // ArrayRef<Value *>. |
| 219 | return ConstantExpr::getGetElementPtr(Ty, C, Idx); |
| 220 | } |
| 221 | |
| 222 | Instruction *CreateGetElementPtr(Type *Ty, Constant *C, |
| 223 | ArrayRef<Value *> IdxList) const { |
| 224 | return GetElementPtrInst::Create(Ty, C, IdxList); |
| 225 | } |
| 226 | |
| 227 | Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C, |
| 228 | ArrayRef<Constant *> IdxList) const { |
| 229 | return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList); |
| 230 | } |
| 231 | |
| 232 | Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C, |
| 233 | Constant *Idx) const { |
| 234 | // This form of the function only exists to avoid ambiguous overload |
| 235 | // warnings about whether to convert Idx to ArrayRef<Constant *> or |
| 236 | // ArrayRef<Value *>. |
| 237 | return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx); |
| 238 | } |
| 239 | |
| 240 | Instruction *CreateInBoundsGetElementPtr(Type *Ty, Constant *C, |
| 241 | ArrayRef<Value *> IdxList) const { |
| 242 | return GetElementPtrInst::CreateInBounds(Ty, C, IdxList); |
| 243 | } |
| 244 | |
| 245 | //===--------------------------------------------------------------------===// |
| 246 | // Cast/Conversion Operators |
| 247 | //===--------------------------------------------------------------------===// |
| 248 | |
| 249 | Instruction *CreateCast(Instruction::CastOps Op, Constant *C, |
| 250 | Type *DestTy) const { |
| 251 | return CastInst::Create(Op, C, DestTy); |
| 252 | } |
| 253 | |
| 254 | Instruction *CreatePointerCast(Constant *C, Type *DestTy) const { |
| 255 | return CastInst::CreatePointerCast(C, DestTy); |
| 256 | } |
| 257 | |
| 258 | Instruction *CreateIntCast(Constant *C, Type *DestTy, |
| 259 | bool isSigned) const { |
| 260 | return CastInst::CreateIntegerCast(C, DestTy, isSigned); |
| 261 | } |
| 262 | |
| 263 | Instruction *CreateFPCast(Constant *C, Type *DestTy) const { |
| 264 | return CastInst::CreateFPCast(C, DestTy); |
| 265 | } |
| 266 | |
| 267 | Instruction *CreateBitCast(Constant *C, Type *DestTy) const { |
| 268 | return CreateCast(Instruction::BitCast, C, DestTy); |
| 269 | } |
| 270 | |
| 271 | Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const { |
| 272 | return CreateCast(Instruction::IntToPtr, C, DestTy); |
| 273 | } |
| 274 | |
| 275 | Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const { |
| 276 | return CreateCast(Instruction::PtrToInt, C, DestTy); |
| 277 | } |
| 278 | |
| 279 | Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const { |
| 280 | return CastInst::CreateZExtOrBitCast(C, DestTy); |
| 281 | } |
| 282 | |
| 283 | Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const { |
| 284 | return CastInst::CreateSExtOrBitCast(C, DestTy); |
| 285 | } |
| 286 | |
| 287 | Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const { |
| 288 | return CastInst::CreateTruncOrBitCast(C, DestTy); |
| 289 | } |
| 290 | |
| 291 | //===--------------------------------------------------------------------===// |
| 292 | // Compare Instructions |
| 293 | //===--------------------------------------------------------------------===// |
| 294 | |
| 295 | Instruction *CreateICmp(CmpInst::Predicate P, |
| 296 | Constant *LHS, Constant *RHS) const { |
| 297 | return new ICmpInst(P, LHS, RHS); |
| 298 | } |
| 299 | |
| 300 | Instruction *CreateFCmp(CmpInst::Predicate P, |
| 301 | Constant *LHS, Constant *RHS) const { |
| 302 | return new FCmpInst(P, LHS, RHS); |
| 303 | } |
| 304 | |
| 305 | //===--------------------------------------------------------------------===// |
| 306 | // Other Instructions |
| 307 | //===--------------------------------------------------------------------===// |
| 308 | |
| 309 | Instruction *CreateSelect(Constant *C, |
| 310 | Constant *True, Constant *False) const { |
| 311 | return SelectInst::Create(C, True, False); |
| 312 | } |
| 313 | |
| 314 | Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const { |
| 315 | return ExtractElementInst::Create(Vec, Idx); |
| 316 | } |
| 317 | |
| 318 | Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt, |
| 319 | Constant *Idx) const { |
| 320 | return InsertElementInst::Create(Vec, NewElt, Idx); |
| 321 | } |
| 322 | |
| 323 | Instruction *CreateShuffleVector(Constant *V1, Constant *V2, |
| 324 | Constant *Mask) const { |
| 325 | return new ShuffleVectorInst(V1, V2, Mask); |
| 326 | } |
| 327 | |
| 328 | Instruction *CreateExtractValue(Constant *Agg, |
| 329 | ArrayRef<unsigned> IdxList) const { |
| 330 | return ExtractValueInst::Create(Agg, IdxList); |
| 331 | } |
| 332 | |
| 333 | Instruction *CreateInsertValue(Constant *Agg, Constant *Val, |
| 334 | ArrayRef<unsigned> IdxList) const { |
| 335 | return InsertValueInst::Create(Agg, Val, IdxList); |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | } // end namespace llvm |
| 340 | |
| 341 | #endif // LLVM_IR_NOFOLDER_H |