Home | History | Annotate | Line # | Download | only in IR
      1 //===- ReplaceConstant.cpp - Replace LLVM constant expression--------------===//
      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 implements a utility function for replacing LLVM constant
     10 // expressions by instructions.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/IR/ReplaceConstant.h"
     15 #include "llvm/IR/IRBuilder.h"
     16 #include "llvm/IR/Instructions.h"
     17 #include "llvm/IR/NoFolder.h"
     18 
     19 namespace llvm {
     20 // Replace a constant expression by instructions with equivalent operations at
     21 // a specified location.
     22 Instruction *createReplacementInstr(ConstantExpr *CE, Instruction *Instr) {
     23   IRBuilder<NoFolder> Builder(Instr);
     24   unsigned OpCode = CE->getOpcode();
     25   switch (OpCode) {
     26   case Instruction::GetElementPtr: {
     27     SmallVector<Value *, 4> CEOpVec(CE->operands());
     28     ArrayRef<Value *> CEOps(CEOpVec);
     29     return dyn_cast<Instruction>(
     30         Builder.CreateInBoundsGEP(cast<GEPOperator>(CE)->getSourceElementType(),
     31                                   CEOps[0], CEOps.slice(1)));
     32   }
     33   case Instruction::Add:
     34   case Instruction::Sub:
     35   case Instruction::Mul:
     36   case Instruction::UDiv:
     37   case Instruction::SDiv:
     38   case Instruction::FDiv:
     39   case Instruction::URem:
     40   case Instruction::SRem:
     41   case Instruction::FRem:
     42   case Instruction::Shl:
     43   case Instruction::LShr:
     44   case Instruction::AShr:
     45   case Instruction::And:
     46   case Instruction::Or:
     47   case Instruction::Xor:
     48     return dyn_cast<Instruction>(
     49         Builder.CreateBinOp((Instruction::BinaryOps)OpCode, CE->getOperand(0),
     50                             CE->getOperand(1), CE->getName()));
     51   case Instruction::Trunc:
     52   case Instruction::ZExt:
     53   case Instruction::SExt:
     54   case Instruction::FPToUI:
     55   case Instruction::FPToSI:
     56   case Instruction::UIToFP:
     57   case Instruction::SIToFP:
     58   case Instruction::FPTrunc:
     59   case Instruction::FPExt:
     60   case Instruction::PtrToInt:
     61   case Instruction::IntToPtr:
     62   case Instruction::BitCast:
     63   case Instruction::AddrSpaceCast:
     64     return dyn_cast<Instruction>(
     65         Builder.CreateCast((Instruction::CastOps)OpCode, CE->getOperand(0),
     66                            CE->getType(), CE->getName()));
     67   default:
     68     llvm_unreachable("Unhandled constant expression!\n");
     69   }
     70 }
     71 } // namespace llvm
     72