[llvm] 157473a - [IR] Simplify createReplacementInstr

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 23 02:50:16 PDT 2021


Author: Jay Foad
Date: 2021-06-23T10:47:43+01:00
New Revision: 157473a58f02b8f2ad12ecbaaa1af32d0342257b

URL: https://github.com/llvm/llvm-project/commit/157473a58f02b8f2ad12ecbaaa1af32d0342257b
DIFF: https://github.com/llvm/llvm-project/commit/157473a58f02b8f2ad12ecbaaa1af32d0342257b.diff

LOG: [IR] Simplify createReplacementInstr

NFCI, although the test change shows that ConstantExpr::getAsInstruction
is better than the old implementation of createReplacementInstr because
it propagates things like the sdiv "exact" flag.

Differential Revision: https://reviews.llvm.org/D104124

Added: 
    

Modified: 
    clang/test/CodeGenCUDA/managed-var.cu
    llvm/lib/IR/ReplaceConstant.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/CodeGenCUDA/managed-var.cu b/clang/test/CodeGenCUDA/managed-var.cu
index 99bbad924ea54..05a7a69387690 100644
--- a/clang/test/CodeGenCUDA/managed-var.cu
+++ b/clang/test/CodeGenCUDA/managed-var.cu
@@ -146,7 +146,7 @@ float load3() {
 // HOST:  %3 = getelementptr inbounds [100 x %struct.vec], [100 x %struct.vec]* %2, i64 0, i64 1, i32 1
 // HOST:  %4 = ptrtoint float* %3 to i64
 // HOST:  %5 = sub i64 %4, %1
-// HOST:  %6 = sdiv i64 %5, 4
+// HOST:  %6 = sdiv exact i64 %5, 4
 // HOST:  %7 = sitofp i64 %6 to float
 // HOST:  ret float %7
 float addr_taken2() {

diff  --git a/llvm/lib/IR/ReplaceConstant.cpp b/llvm/lib/IR/ReplaceConstant.cpp
index 9382f820a7f29..fd73a1a8e5afe 100644
--- a/llvm/lib/IR/ReplaceConstant.cpp
+++ b/llvm/lib/IR/ReplaceConstant.cpp
@@ -20,53 +20,9 @@ namespace llvm {
 // Replace a constant expression by instructions with equivalent operations at
 // a specified location.
 Instruction *createReplacementInstr(ConstantExpr *CE, Instruction *Instr) {
-  IRBuilder<NoFolder> Builder(Instr);
-  unsigned OpCode = CE->getOpcode();
-  switch (OpCode) {
-  case Instruction::GetElementPtr: {
-    SmallVector<Value *, 4> CEOpVec(CE->operands());
-    ArrayRef<Value *> CEOps(CEOpVec);
-    return dyn_cast<Instruction>(
-        Builder.CreateInBoundsGEP(cast<GEPOperator>(CE)->getSourceElementType(),
-                                  CEOps[0], CEOps.slice(1)));
-  }
-  case Instruction::Add:
-  case Instruction::Sub:
-  case Instruction::Mul:
-  case Instruction::UDiv:
-  case Instruction::SDiv:
-  case Instruction::FDiv:
-  case Instruction::URem:
-  case Instruction::SRem:
-  case Instruction::FRem:
-  case Instruction::Shl:
-  case Instruction::LShr:
-  case Instruction::AShr:
-  case Instruction::And:
-  case Instruction::Or:
-  case Instruction::Xor:
-    return dyn_cast<Instruction>(
-        Builder.CreateBinOp((Instruction::BinaryOps)OpCode, CE->getOperand(0),
-                            CE->getOperand(1), CE->getName()));
-  case Instruction::Trunc:
-  case Instruction::ZExt:
-  case Instruction::SExt:
-  case Instruction::FPToUI:
-  case Instruction::FPToSI:
-  case Instruction::UIToFP:
-  case Instruction::SIToFP:
-  case Instruction::FPTrunc:
-  case Instruction::FPExt:
-  case Instruction::PtrToInt:
-  case Instruction::IntToPtr:
-  case Instruction::BitCast:
-  case Instruction::AddrSpaceCast:
-    return dyn_cast<Instruction>(
-        Builder.CreateCast((Instruction::CastOps)OpCode, CE->getOperand(0),
-                           CE->getType(), CE->getName()));
-  default:
-    llvm_unreachable("Unhandled constant expression!\n");
-  }
+  auto *CEInstr = CE->getAsInstruction();
+  CEInstr->insertBefore(Instr);
+  return CEInstr;
 }
 
 void convertConstantExprsToInstructions(Instruction *I, ConstantExpr *CE,


        


More information about the llvm-commits mailing list