summaryrefslogtreecommitdiff
path: root/lib/CodeGen/IntrinsicLowering.cpp
diff options
context:
space:
mode:
authorJay Foad <jay.foad@gmail.com>2009-05-11 11:32:25 +0000
committerJay Foad <jay.foad@gmail.com>2009-05-11 11:32:25 +0000
commit589b1efe1b83a43cf89a41841664ce0b4ae3e838 (patch)
treef4dfe4f1d0bf2609f8b96e7976d2be9656dfb56a /lib/CodeGen/IntrinsicLowering.cpp
parent7524b5906267e8ea8322c2ef4334f6e3bf0e6d24 (diff)
downloadllvm-589b1efe1b83a43cf89a41841664ce0b4ae3e838.tar.gz
llvm-589b1efe1b83a43cf89a41841664ce0b4ae3e838.tar.bz2
llvm-589b1efe1b83a43cf89a41841664ce0b4ae3e838.tar.xz
Don't generate redundant casts of constant values when lowering calls to
memcpy, memmove and memset. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71427 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/IntrinsicLowering.cpp')
-rw-r--r--lib/CodeGen/IntrinsicLowering.cpp67
1 files changed, 29 insertions, 38 deletions
diff --git a/lib/CodeGen/IntrinsicLowering.cpp b/lib/CodeGen/IntrinsicLowering.cpp
index 56b1736b41..ce68c29612 100644
--- a/lib/CodeGen/IntrinsicLowering.cpp
+++ b/lib/CodeGen/IntrinsicLowering.cpp
@@ -23,6 +23,26 @@
#include "llvm/ADT/STLExtras.h"
using namespace llvm;
+// Return the integer value Val zero-extended or truncated (if necessary) to
+// type ITy. Any new instructions are inserted at InsertBefore.
+template<typename InsertType>
+static Value *getZExtOrTrunc(Value *Val, const IntegerType *ITy,
+ InsertType InsertPoint) {
+ const IntegerType *ValTy = cast<IntegerType>(Val->getType());
+ if (ValTy == ITy)
+ return Val;
+ Constant *CVal = dyn_cast<Constant>(Val);
+ if (ValTy->getBitWidth() < ITy->getBitWidth()) {
+ if (CVal)
+ return ConstantExpr::getZExt(CVal, ITy);
+ return new ZExtInst(Val, ITy, "", InsertPoint);
+ } else {
+ if (CVal)
+ return ConstantExpr::getTrunc(CVal, ITy);
+ return new TruncInst(Val, ITy, "", InsertPoint);
+ }
+}
+
template <class ArgIt>
static void EnsureFunctionExists(Module &M, const char *Name,
ArgIt ArgBegin, ArgIt ArgEnd,
@@ -504,7 +524,6 @@ static Instruction *LowerPartSet(CallInst *CI) {
// Get some types we need
const IntegerType* ValTy = cast<IntegerType>(Val->getType());
const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
- uint32_t ValBits = ValTy->getBitWidth();
uint32_t RepBits = RepTy->getBitWidth();
// Constant Definitions
@@ -532,13 +551,7 @@ static Instruction *LowerPartSet(CallInst *CI) {
BinaryOperator* NumBits = BinaryOperator::CreateSub(Hi_pn, Lo_pn, "",entry);
NumBits = BinaryOperator::CreateAdd(NumBits, One, "", entry);
// Now, convert Lo and Hi to ValTy bit width
- if (ValBits > 32) {
- Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
- } else if (ValBits < 32) {
- Lo = new TruncInst(Lo_pn, ValTy, "", entry);
- } else {
- Lo = Lo_pn;
- }
+ Lo = getZExtOrTrunc(Lo_pn, ValTy, entry);
// Determine if the replacement bits are larger than the number of bits we
// are replacing and deal with it.
ICmpInst* is_large =
@@ -560,11 +573,7 @@ static Instruction *LowerPartSet(CallInst *CI) {
Rep3->reserveOperandSpace(2);
Rep3->addIncoming(Rep2, large);
Rep3->addIncoming(Rep, entry);
- Value* Rep4 = Rep3;
- if (ValBits > RepBits)
- Rep4 = new ZExtInst(Rep3, ValTy, "", small);
- else if (ValBits < RepBits)
- Rep4 = new TruncInst(Rep3, ValTy, "", small);
+ Value* Rep4 = getZExtOrTrunc(Rep3, ValTy, small);
BranchInst::Create(result, reverse, is_forward, small);
// BASIC BLOCK: reverse (reverses the bits of the replacement)
@@ -788,14 +797,8 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
case Intrinsic::memcpy: {
static Constant *MemcpyFCache = 0;
- Value *Size = CI->getOperand(3);
- const Type *IntPtr = TD.getIntPtrType();
- if (Size->getType()->getPrimitiveSizeInBits() <
- IntPtr->getPrimitiveSizeInBits())
- Size = new ZExtInst(Size, IntPtr, "", CI);
- else if (Size->getType()->getPrimitiveSizeInBits() >
- IntPtr->getPrimitiveSizeInBits())
- Size = new TruncInst(Size, IntPtr, "", CI);
+ const IntegerType *IntPtr = TD.getIntPtrType();
+ Value *Size = getZExtOrTrunc(CI->getOperand(3), IntPtr, CI);
Value *Ops[3];
Ops[0] = CI->getOperand(1);
Ops[1] = CI->getOperand(2);
@@ -806,14 +809,8 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
}
case Intrinsic::memmove: {
static Constant *MemmoveFCache = 0;
- Value *Size = CI->getOperand(3);
- const Type *IntPtr = TD.getIntPtrType();
- if (Size->getType()->getPrimitiveSizeInBits() <
- IntPtr->getPrimitiveSizeInBits())
- Size = new ZExtInst(Size, IntPtr, "", CI);
- else if (Size->getType()->getPrimitiveSizeInBits() >
- IntPtr->getPrimitiveSizeInBits())
- Size = new TruncInst(Size, IntPtr, "", CI);
+ const IntegerType *IntPtr = TD.getIntPtrType();
+ Value *Size = getZExtOrTrunc(CI->getOperand(3), IntPtr, CI);
Value *Ops[3];
Ops[0] = CI->getOperand(1);
Ops[1] = CI->getOperand(2);
@@ -824,18 +821,12 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
}
case Intrinsic::memset: {
static Constant *MemsetFCache = 0;
- Value *Size = CI->getOperand(3);
- const Type *IntPtr = TD.getIntPtrType();
- if (Size->getType()->getPrimitiveSizeInBits() <
- IntPtr->getPrimitiveSizeInBits())
- Size = new ZExtInst(Size, IntPtr, "", CI);
- else if (Size->getType()->getPrimitiveSizeInBits() >
- IntPtr->getPrimitiveSizeInBits())
- Size = new TruncInst(Size, IntPtr, "", CI);
+ const IntegerType *IntPtr = TD.getIntPtrType();
+ Value *Size = getZExtOrTrunc(CI->getOperand(3), IntPtr, CI);
Value *Ops[3];
Ops[0] = CI->getOperand(1);
// Extend the amount to i32.
- Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
+ Ops[1] = getZExtOrTrunc(CI->getOperand(2), Type::Int32Ty, CI);
Ops[2] = Size;
ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
MemsetFCache);