+/// IsOnlyUsedInEqualsComparison - Return true if it only matters that the value
+/// is equal or not-equal to zero.
+static bool IsOnlyUsedInEqualsZeroComparison(Instruction *I) {
+ for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
+ UI != E; ++UI) {
+ if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
+ if (IC->isEquality())
+ if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
+ if (C->isNullValue())
+ continue;
+ // Unknown instruction.
+ return false;
+ }
+ return true;
+}
+
+/// This memcmpOptimization will simplify a call to the memcmp library
+/// function.
+struct VISIBILITY_HIDDEN memcmpOptimization : public LibCallOptimization {
+ /// @brief Default Constructor
+ memcmpOptimization()
+ : LibCallOptimization("memcmp", "Number of 'memcmp' calls simplified") {}
+
+ /// @brief Make sure that the "memcmp" function has the right prototype
+ virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &TD) {
+ Function::const_arg_iterator AI = F->arg_begin();
+ if (F->arg_size() != 3 || !isa<PointerType>(AI->getType())) return false;
+ if (!isa<PointerType>((++AI)->getType())) return false;
+ if (!(++AI)->getType()->isInteger()) return false;
+ if (!F->getReturnType()->isInteger()) return false;
+ return true;
+ }
+
+ /// Because of alignment and instruction information that we don't have, we
+ /// leave the bulk of this to the code generators.
+ ///
+ /// Note that we could do much more if we could force alignment on otherwise
+ /// small aligned allocas, or if we could indicate that loads have a small
+ /// alignment.
+ virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &TD) {
+ Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
+
+ // If the two operands are the same, return zero.
+ if (LHS == RHS) {
+ // memcmp(s,s,x) -> 0
+ return ReplaceCallWith(CI, Constant::getNullValue(CI->getType()));
+ }
+
+ // Make sure we have a constant length.
+ ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
+ if (!LenC) return false;
+ uint64_t Len = LenC->getZExtValue();
+
+ // If the length is zero, this returns 0.
+ switch (Len) {
+ case 0:
+ // memcmp(s1,s2,0) -> 0
+ return ReplaceCallWith(CI, Constant::getNullValue(CI->getType()));
+ case 1: {
+ // memcmp(S1,S2,1) -> *(ubyte*)S1 - *(ubyte*)S2
+ const Type *UCharPtr = PointerType::getUnqual(Type::Int8Ty);
+ CastInst *Op1Cast = CastInst::create(
+ Instruction::BitCast, LHS, UCharPtr, LHS->getName(), CI);
+ CastInst *Op2Cast = CastInst::create(
+ Instruction::BitCast, RHS, UCharPtr, RHS->getName(), CI);
+ Value *S1V = new LoadInst(Op1Cast, LHS->getName()+".val", CI);
+ Value *S2V = new LoadInst(Op2Cast, RHS->getName()+".val", CI);
+ Value *RV = BinaryOperator::createSub(S1V, S2V, CI->getName()+".diff",CI);
+ if (RV->getType() != CI->getType())
+ RV = CastInst::createIntegerCast(RV, CI->getType(), false,
+ RV->getName(), CI);
+ return ReplaceCallWith(CI, RV);
+ }
+ case 2:
+ if (IsOnlyUsedInEqualsZeroComparison(CI)) {
+ // TODO: IF both are aligned, use a short load/compare.
+
+ // memcmp(S1,S2,2) -> S1[0]-S2[0] | S1[1]-S2[1] iff only ==/!= 0 matters
+ const Type *UCharPtr = PointerType::getUnqual(Type::Int8Ty);
+ CastInst *Op1Cast = CastInst::create(
+ Instruction::BitCast, LHS, UCharPtr, LHS->getName(), CI);
+ CastInst *Op2Cast = CastInst::create(
+ Instruction::BitCast, RHS, UCharPtr, RHS->getName(), CI);
+ Value *S1V1 = new LoadInst(Op1Cast, LHS->getName()+".val1", CI);
+ Value *S2V1 = new LoadInst(Op2Cast, RHS->getName()+".val1", CI);
+ Value *D1 = BinaryOperator::createSub(S1V1, S2V1,
+ CI->getName()+".d1", CI);
+ Constant *One = ConstantInt::get(Type::Int32Ty, 1);
+ Value *G1 = GetElementPtrInst::Create(Op1Cast, One, "next1v", CI);
+ Value *G2 = GetElementPtrInst::Create(Op2Cast, One, "next2v", CI);
+ Value *S1V2 = new LoadInst(G1, LHS->getName()+".val2", CI);
+ Value *S2V2 = new LoadInst(G2, RHS->getName()+".val2", CI);
+ Value *D2 = BinaryOperator::createSub(S1V2, S2V2,
+ CI->getName()+".d1", CI);
+ Value *Or = BinaryOperator::createOr(D1, D2, CI->getName()+".res", CI);
+ if (Or->getType() != CI->getType())
+ Or = CastInst::createIntegerCast(Or, CI->getType(), false /*ZExt*/,
+ Or->getName(), CI);
+ return ReplaceCallWith(CI, Or);
+ }
+ break;
+ default:
+ break;
+ }
+
+ return false;
+ }
+} memcmpOptimizer;
+
+/// This LibCallOptimization will simplify a call to the memcpy library
+/// function. It simply converts them into calls to llvm.memcpy.*;
+/// the resulting call should be optimized later.
+/// @brief Simplify the memcpy library function.
+struct VISIBILITY_HIDDEN MemCpyOptimization : public LibCallOptimization {
+public:
+ MemCpyOptimization() : LibCallOptimization("memcpy",
+ "Number of 'memcpy' calls simplified") {}
+
+ /// @brief Make sure that the "memcpy" function has the right prototype
+ virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
+ const FunctionType *FT = F->getFunctionType();
+ const Type* voidPtr = PointerType::getUnqual(Type::Int8Ty);
+ return FT->getReturnType() == voidPtr && FT->getNumParams() == 3 &&
+ FT->getParamType(0) == voidPtr &&
+ FT->getParamType(1) == voidPtr &&
+ FT->getParamType(2) == SLC.getIntPtrType();
+ }
+
+ /// @brief Perform the memcpy optimization
+ virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
+ Value *MemcpyOps[] = {
+ CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
+ ConstantInt::get(Type::Int32Ty, 1) // align = 1 always.
+ };
+ CallInst::Create(SLC.get_memcpy(), MemcpyOps, MemcpyOps + 4, "", CI);
+ // memcpy always returns the destination
+ return ReplaceCallWith(CI, CI->getOperand(1));
+ }
+} MemCpyOptimizer;
+