diff --git a/transform/func-lowering.go b/transform/func-lowering.go index 89c9f692..02341a83 100644 --- a/transform/func-lowering.go +++ b/transform/func-lowering.go @@ -118,12 +118,18 @@ func LowerFuncValues(mod llvm.Module) { continue } for _, funcValueWithSignatureGlobal := range getUses(funcValueWithSignatureConstant) { + id := llvm.ConstInt(uintptrType, uint64(fn.id), false) for _, use := range getUses(funcValueWithSignatureGlobal) { - if ptrtoint.IsAConstantExpr().IsNil() || ptrtoint.Opcode() != llvm.PtrToInt { - panic("expected const ptrtoint") + // Try to replace uses directly: most will be + // ptrtoint instructions. + if !use.IsAConstantExpr().IsNil() && use.Opcode() == llvm.PtrToInt { + use.ReplaceAllUsesWith(id) } - use.ReplaceAllUsesWith(llvm.ConstInt(uintptrType, uint64(fn.id), false)) } + // Remaining uses can be replaced using a ptrtoint. + // In my quick testing, this doesn't really happen in + // practice. + funcValueWithSignatureGlobal.ReplaceAllUsesWith(llvm.ConstIntToPtr(id, funcValueWithSignatureGlobal.Type())) } } }