@@ -563,42 +563,6 @@ func (c *Compiler) getLLVMType(goType types.Type) llvm.Type {
563563 }
564564}
565565
566- // Return a zero LLVM value for any LLVM type. Setting this value as an
567- // initializer has the same effect as setting 'zeroinitializer' on a value.
568- // Sadly, I haven't found a way to do it directly with the Go API but this works
569- // just fine.
570- func (c * Compiler ) getZeroValue (typ llvm.Type ) llvm.Value {
571- switch typ .TypeKind () {
572- case llvm .ArrayTypeKind :
573- subTyp := typ .ElementType ()
574- subVal := c .getZeroValue (subTyp )
575- vals := make ([]llvm.Value , typ .ArrayLength ())
576- for i := range vals {
577- vals [i ] = subVal
578- }
579- return llvm .ConstArray (subTyp , vals )
580- case llvm .FloatTypeKind , llvm .DoubleTypeKind :
581- return llvm .ConstFloat (typ , 0.0 )
582- case llvm .IntegerTypeKind :
583- return llvm .ConstInt (typ , 0 , false )
584- case llvm .PointerTypeKind :
585- return llvm .ConstPointerNull (typ )
586- case llvm .StructTypeKind :
587- types := typ .StructElementTypes ()
588- vals := make ([]llvm.Value , len (types ))
589- for i , subTyp := range types {
590- vals [i ] = c .getZeroValue (subTyp )
591- }
592- if typ .StructName () != "" {
593- return llvm .ConstNamedStruct (typ , vals )
594- } else {
595- return c .ctx .ConstStruct (vals , false )
596- }
597- default :
598- panic ("unknown LLVM zero inititializer: " + typ .String ())
599- }
600- }
601-
602566// Is this a pointer type of some sort? Can be unsafe.Pointer or any *T pointer.
603567func isPointer (typ types.Type ) bool {
604568 if _ , ok := typ .(* types.Pointer ); ok {
@@ -1132,7 +1096,7 @@ func (c *Compiler) parseInstr(frame *Frame, instr ssa.Instruction) {
11321096 c .builder .CreateRet (c .getValue (frame , instr .Results [0 ]))
11331097 } else {
11341098 // Multiple return values. Put them all in a struct.
1135- retVal := c . getZeroValue (frame .fn .LLVMFn .Type ().ElementType ().ReturnType ())
1099+ retVal := llvm . ConstNull (frame .fn .LLVMFn .Type ().ElementType ().ReturnType ())
11361100 for i , result := range instr .Results {
11371101 val := c .getValue (frame , result )
11381102 retVal = c .builder .CreateInsertValue (retVal , val , i , "" )
@@ -1460,7 +1424,7 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) {
14601424 } else {
14611425 buf := c .createEntryBlockAlloca (typ , expr .Comment )
14621426 if c .targetData .TypeAllocSize (typ ) != 0 {
1463- c .builder .CreateStore (c . getZeroValue (typ ), buf ) // zero-initialize var
1427+ c .builder .CreateStore (llvm . ConstNull (typ ), buf ) // zero-initialize var
14641428 }
14651429 return buf , nil
14661430 }
@@ -1767,7 +1731,7 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) {
17671731 panic ("unknown type in range: " + typ .String ())
17681732 }
17691733 it , _ , _ := c .createTemporaryAlloca (iteratorType , "range.it" )
1770- c .builder .CreateStore (c . getZeroValue (iteratorType ), it )
1734+ c .builder .CreateStore (llvm . ConstNull (iteratorType ), it )
17711735 return it , nil
17721736 case * ssa.Select :
17731737 return c .emitSelect (frame , expr ), nil
@@ -2349,12 +2313,12 @@ func (c *Compiler) parseConst(prefix string, expr *ssa.Const) llvm.Value {
23492313 if expr .Value != nil {
23502314 panic ("expected nil chan constant" )
23512315 }
2352- return c . getZeroValue (c .getLLVMType (expr .Type ()))
2316+ return llvm . ConstNull (c .getLLVMType (expr .Type ()))
23532317 case * types.Signature :
23542318 if expr .Value != nil {
23552319 panic ("expected nil signature constant" )
23562320 }
2357- return c . getZeroValue (c .getLLVMType (expr .Type ()))
2321+ return llvm . ConstNull (c .getLLVMType (expr .Type ()))
23582322 case * types.Interface :
23592323 if expr .Value != nil {
23602324 panic ("expected nil interface constant" )
@@ -2389,7 +2353,7 @@ func (c *Compiler) parseConst(prefix string, expr *ssa.Const) llvm.Value {
23892353 panic ("non-nil map constant" )
23902354 }
23912355 llvmType := c .getLLVMType (typ )
2392- return c . getZeroValue (llvmType )
2356+ return llvm . ConstNull (llvmType )
23932357 default :
23942358 panic ("unknown constant: " + expr .String ())
23952359 }
@@ -2581,7 +2545,7 @@ func (c *Compiler) parseUnOp(frame *Frame, unop *ssa.UnOp) (llvm.Value, error) {
25812545 unop .X .Type ().Underlying ().(* types.Pointer ).Elem ()
25822546 if c .targetData .TypeAllocSize (x .Type ().ElementType ()) == 0 {
25832547 // zero-length data
2584- return c . getZeroValue (x .Type ().ElementType ()), nil
2548+ return llvm . ConstNull (x .Type ().ElementType ()), nil
25852549 } else if strings .HasSuffix (unop .X .String (), "$funcaddr" ) {
25862550 // CGo function pointer. The cgo part has rewritten CGo function
25872551 // pointers as stub global variables of the form:
0 commit comments