Skip some more superfluous bitcasts NOT using the DtoBitCast() helper

This commit is contained in:
Martin Kinkelin 2024-05-20 16:01:53 +02:00
parent d140f2a283
commit 7fa7f35b2a
9 changed files with 11 additions and 49 deletions

View file

@ -453,10 +453,7 @@ LLConstant *DtoConstArrayInitializer(ArrayInitializer *arrinit,
return gvar; return gvar;
} }
LLConstant *gep = DtoGEP(gvar->getValueType(), gvar, 0u, 0u); return DtoConstSlice(DtoConstSize_t(arrlen), gvar, arrty);
gep = llvm::ConstantExpr::getBitCast(gvar, getPtrToType(llelemty));
return DtoConstSlice(DtoConstSize_t(arrlen), gep, arrty);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -1490,11 +1490,7 @@ DValue *DtoSymbolAddress(const Loc &loc, Type *type, Declaration *decl) {
// typeinfo // typeinfo
if (TypeInfoDeclaration *tid = vd->isTypeInfoDeclaration()) { if (TypeInfoDeclaration *tid = vd->isTypeInfoDeclaration()) {
Logger::println("TypeInfoDeclaration"); Logger::println("TypeInfoDeclaration");
LLType *vartype = DtoType(type);
LLValue *m = DtoResolveTypeInfo(tid); LLValue *m = DtoResolveTypeInfo(tid);
if (m->getType() != getPtrToType(vartype)) {
m = gIR->ir->CreateBitCast(m, vartype);
}
return new DImValue(type, m); return new DImValue(type, m);
} }
// special vtbl symbol, used by LDC as alias to the actual vtbl (with // special vtbl symbol, used by LDC as alias to the actual vtbl (with

View file

@ -136,10 +136,8 @@ LLFunction *build_module_reference_and_ctor(const char *moduleMangle,
// provide the default initializer // provide the default initializer
LLStructType *modulerefTy = DtoModuleReferenceType(); LLStructType *modulerefTy = DtoModuleReferenceType();
LLConstant *mrefvalues[] = { LLConstant *mrefvalues[] = {LLConstant::getNullValue(getVoidPtrType()),
LLConstant::getNullValue(modulerefTy->getContainedType(0)), moduleinfo};
llvm::ConstantExpr::getBitCast(moduleinfo,
modulerefTy->getContainedType(1))};
LLConstant *thismrefinit = LLConstantStruct::get( LLConstant *thismrefinit = LLConstantStruct::get(
modulerefTy, llvm::ArrayRef<LLConstant *>(mrefvalues)); modulerefTy, llvm::ArrayRef<LLConstant *>(mrefvalues));

View file

@ -69,8 +69,6 @@ struct G2StackAnalysis {
void EmitMemSet(IRBuilder<> &B, Value *Dst, Value *Val, Value *Len, void EmitMemSet(IRBuilder<> &B, Value *Dst, Value *Val, Value *Len,
const G2StackAnalysis &A) { const G2StackAnalysis &A) {
Dst = B.CreateBitCast(Dst, PointerType::getUnqual(B.getInt8Ty()));
MaybeAlign Align(1); MaybeAlign Align(1);
auto CS = B.CreateMemSet(Dst, Val, Len, Align, false /*isVolatile*/); auto CS = B.CreateMemSet(Dst, Val, Len, Align, false /*isVolatile*/);
@ -197,9 +195,7 @@ Value* ArrayFI::promote(CallBase *CB, IRBuilder<> &B, const G2StackAnalysis &A)
if (ReturnType == ReturnType::Array) { if (ReturnType == ReturnType::Array) {
Value *arrStruct = llvm::UndefValue::get(CB->getType()); Value *arrStruct = llvm::UndefValue::get(CB->getType());
arrStruct = Builder.CreateInsertValue(arrStruct, arrSize, 0); arrStruct = Builder.CreateInsertValue(arrStruct, arrSize, 0);
Value *memPtr = arrStruct = Builder.CreateInsertValue(arrStruct, alloca, 1);
Builder.CreateBitCast(alloca, PointerType::getUnqual(B.getInt8Ty()));
arrStruct = Builder.CreateInsertValue(arrStruct, memPtr, 1);
return arrStruct; return arrStruct;
} }
@ -284,7 +280,7 @@ Value* UntypedMemoryFI::promote(CallBase *CB, IRBuilder<> &B, const G2StackAnaly
AllocaInst *alloca = AllocaInst *alloca =
B.CreateAlloca(Ty, count, ".nongc_mem"); // FIXME: align? B.CreateAlloca(Ty, count, ".nongc_mem"); // FIXME: align?
return B.CreateBitCast(alloca, CB->getType()); return alloca;
} }
//} //}
@ -454,9 +450,7 @@ bool GarbageCollect2Stack::run(Function &F, std::function<DominatorTree& ()> get
// Make sure the type is the same as it was before, and replace all // Make sure the type is the same as it was before, and replace all
// uses of the runtime call with the alloca. // uses of the runtime call with the alloca.
if (newVal->getType() != CB->getType()) { assert(newVal->getType() == CB->getType());
newVal = Builder.CreateBitCast(newVal, CB->getType());
}
static_cast<Instruction *>(CB)->replaceAllUsesWith(newVal); static_cast<Instruction *>(CB)->replaceAllUsesWith(newVal);
RemoveCall(CB, A); RemoveCall(CB, A);

View file

@ -51,18 +51,12 @@ Value *LibCallOptimization::OptimizeCall(CallInst *CI, bool &Changed, const Data
return CallOptimizer(CI->getCalledFunction(), CI, B); return CallOptimizer(CI->getCalledFunction(), CI, B);
} }
/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
return B.CreateBitCast(V, PointerType::getUnqual(B.getInt8Ty()), "cstr");
}
/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always /// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
/// expects that the size has type 'intptr_t' and Dst/Src are pointers. /// expects that the size has type 'intptr_t' and Dst/Src are pointers.
Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len, Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
unsigned Align, IRBuilder<> &B) { unsigned Align, IRBuilder<> &B) {
auto A = llvm::MaybeAlign(Align); auto A = llvm::MaybeAlign(Align);
return B.CreateMemCpy(CastToCStr(Dst, B), A, CastToCStr(Src, B), A, Len, return B.CreateMemCpy(Dst, A, Src, A, Len, false);
false);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View file

@ -18,9 +18,6 @@ protected:
llvm::AliasAnalysis *AA; llvm::AliasAnalysis *AA;
llvm::LLVMContext *Context; llvm::LLVMContext *Context;
/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
llvm::Value *CastToCStr(llvm::Value *V, llvm::IRBuilder<> &B);
/// EmitMemCpy - Emit a call to the memcpy function to the builder. This /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
/// always expects that the size has type 'intptr_t' and Dst/Src are pointers. /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
llvm::Value *EmitMemCpy(llvm::Value *Dst, llvm::Value *Src, llvm::Value *Len, unsigned Align, llvm::Value *EmitMemCpy(llvm::Value *Dst, llvm::Value *Src, llvm::Value *Len, unsigned Align,

View file

@ -909,10 +909,8 @@ void CodeGenPGO::emitCounterIncrement(const RootObject *S) const {
assert(counter_it != (*RegionCounterMap).end() && assert(counter_it != (*RegionCounterMap).end() &&
"Statement not found in PGO counter map!"); "Statement not found in PGO counter map!");
unsigned counter = counter_it->second; unsigned counter = counter_it->second;
auto *I8PtrTy = getVoidPtrType();
gIR->ir->CreateCall(GET_INTRINSIC_DECL(instrprof_increment), gIR->ir->CreateCall(GET_INTRINSIC_DECL(instrprof_increment),
{llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy), {FuncNameVar, gIR->ir->getInt64(FunctionHash),
gIR->ir->getInt64(FunctionHash),
gIR->ir->getInt32(NumRegionCounters), gIR->ir->getInt32(NumRegionCounters),
gIR->ir->getInt32(counter)}); gIR->ir->getInt32(counter)});
} }
@ -1117,11 +1115,9 @@ void CodeGenPGO::valueProfile(uint32_t valueKind, llvm::Instruction *valueSite,
if (ptrCastNeeded) if (ptrCastNeeded)
value = gIR->ir->CreatePtrToInt(value, gIR->ir->getInt64Ty()); value = gIR->ir->CreatePtrToInt(value, gIR->ir->getInt64Ty());
auto *i8PtrTy = getVoidPtrType(); llvm::Value *Args[5] = {FuncNameVar, gIR->ir->getInt64(FunctionHash), value,
llvm::Value *Args[5] = { gIR->ir->getInt32(valueKind),
llvm::ConstantExpr::getBitCast(FuncNameVar, i8PtrTy), gIR->ir->getInt32(NumValueSites[valueKind])};
gIR->ir->getInt64(FunctionHash), value, gIR->ir->getInt32(valueKind),
gIR->ir->getInt32(NumValueSites[valueKind])};
gIR->ir->CreateCall(GET_INTRINSIC_DECL(instrprof_value_profile), Args); gIR->ir->CreateCall(GET_INTRINSIC_DECL(instrprof_value_profile), Args);
gIR->ir->restoreIP(savedInsertPoint); gIR->ir->restoreIP(savedInsertPoint);

View file

@ -262,14 +262,6 @@ public:
Logger::println("Loading value for return"); Logger::println("Loading value for return");
returnValue = DtoLoad(funcType->getReturnType(), returnValue); returnValue = DtoLoad(funcType->getReturnType(), returnValue);
} }
// can happen for classes
if (returnValue->getType() != funcType->getReturnType()) {
returnValue =
irs->ir->CreateBitCast(returnValue, funcType->getReturnType());
IF_LOG Logger::cout()
<< "return value after cast: " << *returnValue << '\n';
}
} }
} else { } else {
// no return value expression means it's a void function. // no return value expression means it's a void function.

View file

@ -51,7 +51,6 @@ void LocalVariableLifetimeAnnotator::addLocalVariable(llvm::Value *address,
scopes.back().variables.emplace_back(size, address); scopes.back().variables.emplace_back(size, address);
// Emit lifetime start // Emit lifetime start
address = irs.ir->CreateBitCast(address, allocaType);
irs.CreateCallOrInvoke(getLLVMLifetimeStartFn(), {size, address}, "", irs.CreateCallOrInvoke(getLLVMLifetimeStartFn(), {size, address}, "",
true /*nothrow*/); true /*nothrow*/);
} }
@ -65,7 +64,6 @@ void LocalVariableLifetimeAnnotator::popScope() {
auto size = var.first; auto size = var.first;
auto address = var.second; auto address = var.second;
address = irs.ir->CreateBitCast(address, allocaType);
assert(address); assert(address);
irs.CreateCallOrInvoke(getLLVMLifetimeEndFn(), {size, address}, "", irs.CreateCallOrInvoke(getLLVMLifetimeEndFn(), {size, address}, "",