#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Statistic.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
return
- B.CreateBitCast(V, Context->getPointerTypeUnqual(Type::Int8Ty), "cstr");
+ B.CreateBitCast(V, PointerType::getUnqual(Type::Int8Ty), "cstr");
}
/// EmitStrLen - Emit a call to the strlen function to the builder, for the
Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
TD->getIntPtrType(),
- Context->getPointerTypeUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
NULL);
CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
- Context->getPointerTypeUnqual(Type::Int8Ty),
- Context->getPointerTypeUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
Type::Int32Ty, TD->getIntPtrType(),
NULL);
CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Type::Int32Ty,
- Context->getPointerTypeUnqual(Type::Int8Ty),
- Context->getPointerTypeUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
TD->getIntPtrType(), NULL);
CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
Len, "memcmp");
Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Type::Int32Ty,
- Context->getPointerTypeUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
NULL);
CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
Constant *F;
if (isa<PointerType>(File->getType()))
F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3), Type::Int32Ty,
- Context->getPointerTypeUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
File->getType(), NULL);
else
F = M->getOrInsertFunction("fputs", Type::Int32Ty,
- Context->getPointerTypeUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
File->getType(), NULL);
CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
if (isa<PointerType>(File->getType()))
F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
TD->getIntPtrType(),
- Context->getPointerTypeUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
TD->getIntPtrType(), TD->getIntPtrType(),
File->getType(), NULL);
else
F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
- Context->getPointerTypeUnqual(Type::Int8Ty),
+ PointerType::getUnqual(Type::Int8Ty),
TD->getIntPtrType(), TD->getIntPtrType(),
File->getType(), NULL);
CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
return 0;
TerminatorInst *OldTI = CI->getParent()->getTerminator();
-
+
// Drop all successor phi node entries.
for (unsigned i = 0, e = OldTI->getNumSuccessors(); i != e; ++i)
OldTI->getSuccessor(i)->removePredecessor(CI->getParent());
-
- // Split the basic block after the call to exit.
- BasicBlock::iterator FirstDead = CI; ++FirstDead;
- CI->getParent()->splitBasicBlock(FirstDead);
- B.SetInsertPoint(B.GetInsertBlock());
- // Remove the branch that splitBB created and insert a return instead.
- CI->getParent()->getTerminator()->eraseFromParent();
+ // Remove all instructions after the exit.
+ BasicBlock::iterator Dead = CI, E = OldTI; ++Dead;
+ while (Dead != E) {
+ BasicBlock::iterator Next = next(Dead);
+ if (Dead->getType() != Type::VoidTy)
+ Dead->replaceAllUsesWith(UndefValue::get(Dead->getType()));
+ Dead->eraseFromParent();
+ Dead = Next;
+ }
+
+ // Insert a return instruction.
+ OldTI->eraseFromParent();
+ B.SetInsertPoint(B.GetInsertBlock());
B.CreateRet(CI->getOperand(1));
return CI;
// Verify the "strcat" function prototype.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 ||
- FT->getReturnType() != Context->getPointerTypeUnqual(Type::Int8Ty) ||
+ FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
FT->getParamType(0) != FT->getReturnType() ||
FT->getParamType(1) != FT->getReturnType())
return 0;
// Verify the "strncat" function prototype.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 ||
- FT->getReturnType() != Context->getPointerTypeUnqual(Type::Int8Ty) ||
+ FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
FT->getParamType(0) != FT->getReturnType() ||
FT->getParamType(1) != FT->getReturnType() ||
!isa<IntegerType>(FT->getParamType(2)))
// Verify the "strchr" function prototype.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 ||
- FT->getReturnType() != Context->getPointerTypeUnqual(Type::Int8Ty) ||
+ FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
FT->getParamType(0) != FT->getReturnType())
return 0;
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 || FT->getReturnType() != Type::Int32Ty ||
FT->getParamType(0) != FT->getParamType(1) ||
- FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty))
+ FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
return 0;
Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 || FT->getReturnType() != Type::Int32Ty ||
FT->getParamType(0) != FT->getParamType(1) ||
- FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty) ||
+ FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
!isa<IntegerType>(FT->getParamType(2)))
return 0;
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
FT->getParamType(0) != FT->getParamType(1) ||
- FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty))
+ FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
return 0;
Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
FT->getParamType(0) != FT->getParamType(1) ||
- FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty) ||
+ FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
!isa<IntegerType>(FT->getParamType(2)))
return 0;
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 1 ||
- FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty) ||
+ FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
!isa<IntegerType>(FT->getReturnType()))
return 0;
// memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
// memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
- const Type *PTy = Context->getPointerTypeUnqual(Len == 2 ?
+ const Type *PTy = PointerType::getUnqual(Len == 2 ?
Type::Int16Ty : Type::Int32Ty);
LHS = B.CreateBitCast(LHS, PTy, "tmp");
RHS = B.CreateBitCast(RHS, PTy, "tmp");