unsigned Align = cast<ConstantInt>(CI.getOperand(4))->getZExtValue();
PointerType *NewPtrTy = NULL;
// Destination pointer type is always i8 *
- if (Size == 8)
- NewPtrTy = PointerType::get(Type::Int64Ty);
- else if (Size == 4)
- NewPtrTy = PointerType::get(Type::Int32Ty);
- else if (Size == 2)
- NewPtrTy = PointerType::get(Type::Int16Ty);
- else if (Size == 1)
- NewPtrTy = PointerType::get(Type::Int8Ty);
+ // If Size is 8 then use Int64Ty
+ // If Size is 4 then use Int32Ty
+ // If Size is 2 then use Int16Ty
+ // If Size is 1 then use Int8Ty
+ if (Size && Size <=8 && !(Size&(Size-1)))
+ NewPtrTy = PointerType::get(IntegerType::get(Size<<3));
+
if (NewPtrTy) {
Value *Src = InsertCastBefore(Instruction::BitCast, CI.getOperand(2), NewPtrTy, CI);
Value *Dest = InsertCastBefore(Instruction::BitCast, CI.getOperand(1), NewPtrTy, CI);
Value *L = new LoadInst(Src, "tmp", false, Align, &CI);
Value *NS = new StoreInst(L, Dest, false, Align, &CI);
+ AddToWorkList(cast<Instruction>(L));
+ AddToWorkList(cast<Instruction>(NS));
CI.replaceAllUsesWith(NS);
Changed = true;
return EraseInstFromFunction(CI);
}
} else if (CE->isCast()) {
+ // Instead of loading constant c string, use corresponding integer value
+ // directly if string length is small enough.
+ const std::string &Str = CE->getOperand(0)->getStringValue();
+ if (!Str.empty()) {
+ unsigned len = Str.length();
+ const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
+ unsigned numBits = Ty->getPrimitiveSizeInBits();
+ if ((numBits >> 3) == len + 1) {
+ // Replace LI with immediate integer store.
+ APInt StrVal(numBits, 0);
+ APInt SingleChar(numBits, 0);
+ for (unsigned i = 0; i < len; i++) {
+ SingleChar = (uint64_t) Str[i];
+ StrVal = (StrVal << 8) | SingleChar;
+ }
+ // Append NULL at the end.
+ SingleChar = 0;
+ StrVal = (StrVal << 8) | SingleChar;
+ Value *NL = ConstantInt::get(StrVal);
+ return ReplaceInstUsesWith(LI, NL);
+ }
+ }
+
if (Instruction *Res = InstCombineLoadCast(*this, LI))
return Res;
}