Blame SOURCES/bpftrace-0.16.0-IR-builder-get-rid-of-getPointerElementType-calls.patch

b69d1d
From 7afe3ced2b91d940a8d72755043ac2468687f1ee Mon Sep 17 00:00:00 2001
b69d1d
From: Viktor Malik <viktor.malik@gmail.com>
b69d1d
Date: Mon, 10 Oct 2022 14:26:38 +0200
b69d1d
Subject: [PATCH] IR builder: get rid of getPointerElementType calls
b69d1d
b69d1d
Usage of Value::getPointerElementType is deprecated and will be dropped
b69d1d
in LLVM 16 [1].
b69d1d
b69d1d
There are several places where we use this method:
b69d1d
- function (value) calls - the called function type is usually
b69d1d
  available, so just pass it to createCall, the only exception is
b69d1d
  CreateProbeReadStr which must have been refactored
b69d1d
- getting the type of alloca instruction - there is a dedicated
b69d1d
  AllocaInst::getAllocatedType method that can be used instead
b69d1d
- strncmp - pass sizes of the strings to CreateStrncmp to be able to get
b69d1d
  the correct string type (which is array of uint8)
b69d1d
b69d1d
[1] https://llvm.org/docs/OpaquePointers.html
b69d1d
---
b69d1d
 src/ast/irbuilderbpf.cpp        | 143 ++++++++++++--------------------
b69d1d
 src/ast/irbuilderbpf.h          |  23 +++--
b69d1d
 src/ast/passes/codegen_llvm.cpp |  30 +++++--
b69d1d
 3 files changed, 86 insertions(+), 110 deletions(-)
b69d1d
b69d1d
diff --git a/src/ast/irbuilderbpf.cpp b/src/ast/irbuilderbpf.cpp
b69d1d
index d49883f7..4036b2df 100644
b69d1d
--- a/src/ast/irbuilderbpf.cpp
b69d1d
+++ b/src/ast/irbuilderbpf.cpp
b69d1d
@@ -288,17 +288,16 @@ CallInst *IRBuilderBPF::CreateHelperCall(libbpf::bpf_func_id func_id,
b69d1d
   Constant *helper_func = ConstantExpr::getCast(Instruction::IntToPtr,
b69d1d
                                                 getInt64(func_id),
b69d1d
                                                 helper_ptr_type);
b69d1d
-  return createCall(helper_func, args, Name);
b69d1d
+  return createCall(helper_type, helper_func, args, Name);
b69d1d
 }
b69d1d
 
b69d1d
-CallInst *IRBuilderBPF::createCall(Value *callee,
b69d1d
+CallInst *IRBuilderBPF::createCall(FunctionType *callee_type,
b69d1d
+                                   Value *callee,
b69d1d
                                    ArrayRef<Value *> args,
b69d1d
                                    const Twine &Name)
b69d1d
 {
b69d1d
 #if LLVM_VERSION_MAJOR >= 11
b69d1d
-  auto *calleePtrType = cast<PointerType>(callee->getType());
b69d1d
-  auto *calleeType = cast<FunctionType>(calleePtrType->getPointerElementType());
b69d1d
-  return CreateCall(calleeType, callee, args, Name);
b69d1d
+  return CreateCall(callee_type, callee, args, Name);
b69d1d
 #else
b69d1d
   return CreateCall(callee, args, Name);
b69d1d
 #endif
b69d1d
@@ -307,7 +306,7 @@ CallInst *IRBuilderBPF::createCall(Value *callee,
b69d1d
 CallInst *IRBuilderBPF::CreateBpfPseudoCallId(int mapid)
b69d1d
 {
b69d1d
   Function *pseudo_func = module_.getFunction("llvm.bpf.pseudo");
b69d1d
-  return createCall(pseudo_func,
b69d1d
+  return CreateCall(pseudo_func,
b69d1d
                     { getInt64(BPF_PSEUDO_MAP_FD), getInt64(mapid) },
b69d1d
                     "pseudo");
b69d1d
 }
b69d1d
@@ -346,7 +345,8 @@ CallInst *IRBuilderBPF::createMapLookup(int mapid, Value *key)
b69d1d
       Instruction::IntToPtr,
b69d1d
       getInt64(libbpf::BPF_FUNC_map_lookup_elem),
b69d1d
       lookup_func_ptr_type);
b69d1d
-  return createCall(lookup_func, { map_ptr, key }, "lookup_elem");
b69d1d
+  return createCall(
b69d1d
+      lookup_func_type, lookup_func, { map_ptr, key }, "lookup_elem");
b69d1d
 }
b69d1d
 
b69d1d
 CallInst *IRBuilderBPF::CreateGetJoinMap(Value *ctx, const location &loc)
b69d1d
@@ -397,8 +397,7 @@ Value *IRBuilderBPF::CreateMapLookupElem(Value *ctx,
b69d1d
     CREATE_MEMCPY(value, call, type.GetSize(), 1);
b69d1d
   else
b69d1d
   {
b69d1d
-    assert(value->getType()->isPointerTy() &&
b69d1d
-           (value->getType()->getPointerElementType() == getInt64Ty()));
b69d1d
+    assert(value->getAllocatedType() == getInt64Ty());
b69d1d
     // createMapLookup  returns an u8*
b69d1d
     auto *cast = CreatePointerCast(call, value->getType(), "cast");
b69d1d
     CreateStore(CreateLoad(getInt64Ty(), cast), value);
b69d1d
@@ -448,7 +447,8 @@ void IRBuilderBPF::CreateMapUpdateElem(Value *ctx,
b69d1d
       Instruction::IntToPtr,
b69d1d
       getInt64(libbpf::BPF_FUNC_map_update_elem),
b69d1d
       update_func_ptr_type);
b69d1d
-  CallInst *call = createCall(update_func,
b69d1d
+  CallInst *call = createCall(update_func_type,
b69d1d
+                              update_func,
b69d1d
                               { map_ptr, key, val, flags },
b69d1d
                               "update_elem");
b69d1d
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_update_elem, loc);
b69d1d
@@ -472,7 +472,8 @@ void IRBuilderBPF::CreateMapDeleteElem(Value *ctx,
b69d1d
       Instruction::IntToPtr,
b69d1d
       getInt64(libbpf::BPF_FUNC_map_delete_elem),
b69d1d
       delete_func_ptr_type);
b69d1d
-  CallInst *call = createCall(delete_func, { map_ptr, key }, "delete_elem");
b69d1d
+  CallInst *call = createCall(
b69d1d
+      delete_func_type, delete_func, { map_ptr, key }, "delete_elem");
b69d1d
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_delete_elem, loc);
b69d1d
 }
b69d1d
 
b69d1d
@@ -508,72 +509,53 @@ void IRBuilderBPF::CreateProbeRead(Value *ctx,
b69d1d
   Constant *proberead_func = ConstantExpr::getCast(Instruction::IntToPtr,
b69d1d
                                                    getInt64(read_fn),
b69d1d
                                                    proberead_func_ptr_type);
b69d1d
-  CallInst *call = createCall(proberead_func,
b69d1d
+  CallInst *call = createCall(proberead_func_type,
b69d1d
+                              proberead_func,
b69d1d
                               { dst, size, src },
b69d1d
                               probeReadHelperName(read_fn));
b69d1d
   CreateHelperErrorCond(ctx, call, read_fn, loc);
b69d1d
 }
b69d1d
 
b69d1d
-Constant *IRBuilderBPF::createProbeReadStrFn(llvm::Type *dst,
b69d1d
-                                             llvm::Type *src,
b69d1d
-                                             AddrSpace as)
b69d1d
-{
b69d1d
-  assert(src && (src->isIntegerTy() || src->isPointerTy()));
b69d1d
-  // int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
b69d1d
-  FunctionType *probereadstr_func_type = FunctionType::get(
b69d1d
-      getInt64Ty(), { dst, getInt32Ty(), src }, false);
b69d1d
-  PointerType *probereadstr_func_ptr_type = PointerType::get(
b69d1d
-      probereadstr_func_type, 0);
b69d1d
-  return ConstantExpr::getCast(Instruction::IntToPtr,
b69d1d
-                               getInt64(selectProbeReadHelper(as, true)),
b69d1d
-                               probereadstr_func_ptr_type);
b69d1d
-}
b69d1d
-
b69d1d
 CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
b69d1d
-                                           AllocaInst *dst,
b69d1d
+                                           Value *dst,
b69d1d
                                            size_t size,
b69d1d
                                            Value *src,
b69d1d
                                            AddrSpace as,
b69d1d
                                            const location &loc)
b69d1d
 {
b69d1d
-  assert(ctx && ctx->getType() == getInt8PtrTy());
b69d1d
   return CreateProbeReadStr(ctx, dst, getInt32(size), src, as, loc);
b69d1d
 }
b69d1d
 
b69d1d
 CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
b69d1d
                                            Value *dst,
b69d1d
-                                           size_t size,
b69d1d
-                                           Value *src,
b69d1d
-                                           AddrSpace as,
b69d1d
-                                           const location &loc)
b69d1d
-{
b69d1d
-  assert(ctx && ctx->getType() == getInt8PtrTy());
b69d1d
-  Constant *fn = createProbeReadStrFn(dst->getType(), src->getType(), as);
b69d1d
-  auto read_fn = selectProbeReadHelper(as, true);
b69d1d
-  CallInst *call = createCall(fn,
b69d1d
-                              { dst, getInt32(size), src },
b69d1d
-                              probeReadHelperName(read_fn));
b69d1d
-  CreateHelperErrorCond(ctx, call, read_fn, loc);
b69d1d
-  return call;
b69d1d
-}
b69d1d
-
b69d1d
-CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
b69d1d
-                                           AllocaInst *dst,
b69d1d
                                            llvm::Value *size,
b69d1d
                                            Value *src,
b69d1d
                                            AddrSpace as,
b69d1d
                                            const location &loc)
b69d1d
 {
b69d1d
   assert(ctx && ctx->getType() == getInt8PtrTy());
b69d1d
-  assert(dst && dst->getAllocatedType()->isArrayTy() &&
b69d1d
-         dst->getAllocatedType()->getArrayElementType() == getInt8Ty());
b69d1d
   assert(size && size->getType()->isIntegerTy());
b69d1d
+  if (auto *dst_alloca = dyn_cast<AllocaInst>(dst))
b69d1d
+  {
b69d1d
+    assert(dst_alloca->getAllocatedType()->isArrayTy() &&
b69d1d
+           dst_alloca->getAllocatedType()->getArrayElementType() ==
b69d1d
+               getInt8Ty());
b69d1d
+  }
b69d1d
 
b69d1d
-  auto *size_i32 = CreateIntCast(size, getInt32Ty(), false);
b69d1d
+  auto *size_i32 = size;
b69d1d
+  if (size_i32->getType()->getScalarSizeInBits() != 32)
b69d1d
+    size_i32 = CreateIntCast(size_i32, getInt32Ty(), false);
b69d1d
 
b69d1d
-  Constant *fn = createProbeReadStrFn(dst->getType(), src->getType(), as);
b69d1d
   auto read_fn = selectProbeReadHelper(as, true);
b69d1d
-  CallInst *call = createCall(fn,
b69d1d
+  // int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
b69d1d
+  FunctionType *probereadstr_func_type = FunctionType::get(
b69d1d
+      getInt64Ty(), { dst->getType(), getInt32Ty(), src->getType() }, false);
b69d1d
+  PointerType *probereadstr_func_ptr_type = PointerType::get(
b69d1d
+      probereadstr_func_type, 0);
b69d1d
+  Constant *probereadstr_callee = ConstantExpr::getCast(
b69d1d
+      Instruction::IntToPtr, getInt64(read_fn), probereadstr_func_ptr_type);
b69d1d
+  CallInst *call = createCall(probereadstr_func_type,
b69d1d
+                              probereadstr_callee,
b69d1d
                               { dst, size_i32, src },
b69d1d
                               probeReadHelperName(read_fn));
b69d1d
   CreateHelperErrorCond(ctx, call, read_fn, loc);
b69d1d
@@ -732,8 +714,10 @@ Value *IRBuilderBPF::CreateUSDTReadArgument(Value *ctx,
b69d1d
   return result;
b69d1d
 }
b69d1d
 
b69d1d
-Value *IRBuilderBPF::CreateStrncmp(Value *val1,
b69d1d
-                                   Value *val2,
b69d1d
+Value *IRBuilderBPF::CreateStrncmp(Value *str1,
b69d1d
+                                   uint64_t str1_size,
b69d1d
+                                   Value *str2,
b69d1d
+                                   uint64_t str2_size,
b69d1d
                                    uint64_t n,
b69d1d
                                    bool inverse)
b69d1d
 {
b69d1d
@@ -762,40 +746,21 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
b69d1d
   // Check if the compared strings are literals.
b69d1d
   // If so, we can avoid storing the literal in memory.
b69d1d
   std::optional<std::string> literal1;
b69d1d
-  if (auto constString1 = dyn_cast<ConstantDataArray>(val1))
b69d1d
+  if (auto constString1 = dyn_cast<ConstantDataArray>(str1))
b69d1d
     literal1 = constString1->getAsString();
b69d1d
-  else if (isa<ConstantAggregateZero>(val1))
b69d1d
+  else if (isa<ConstantAggregateZero>(str1))
b69d1d
     literal1 = "";
b69d1d
   else
b69d1d
     literal1 = std::nullopt;
b69d1d
 
b69d1d
   std::optional<std::string> literal2;
b69d1d
-  if (auto constString2 = dyn_cast<ConstantDataArray>(val2))
b69d1d
+  if (auto constString2 = dyn_cast<ConstantDataArray>(str2))
b69d1d
     literal2 = constString2->getAsString();
b69d1d
-  else if (isa<ConstantAggregateZero>(val2))
b69d1d
+  else if (isa<ConstantAggregateZero>(str2))
b69d1d
     literal2 = "";
b69d1d
   else
b69d1d
     literal2 = std::nullopt;
b69d1d
 
b69d1d
-  auto *val1p = dyn_cast<PointerType>(val1->getType());
b69d1d
-  auto *val2p = dyn_cast<PointerType>(val2->getType());
b69d1d
-#ifndef NDEBUG
b69d1d
-  if (!literal1)
b69d1d
-  {
b69d1d
-    assert(val1p);
b69d1d
-    assert(val1p->getPointerElementType()->isArrayTy() &&
b69d1d
-           val1p->getPointerElementType()->getArrayElementType() ==
b69d1d
-               getInt8Ty());
b69d1d
-  }
b69d1d
-  if (!literal2)
b69d1d
-  {
b69d1d
-    assert(val2p);
b69d1d
-    assert(val2p->getPointerElementType()->isArrayTy() &&
b69d1d
-           val2p->getPointerElementType()->getArrayElementType() ==
b69d1d
-               getInt8Ty());
b69d1d
-  }
b69d1d
-#endif
b69d1d
-
b69d1d
   Function *parent = GetInsertBlock()->getParent();
b69d1d
   AllocaInst *store = CreateAllocaBPF(getInt1Ty(), "strcmp.result");
b69d1d
   BasicBlock *str_ne = BasicBlock::Create(module_.getContext(),
b69d1d
@@ -822,8 +787,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
b69d1d
       l = getInt8(literal1->c_str()[i]);
b69d1d
     else
b69d1d
     {
b69d1d
-      auto *ptr_l = CreateGEP(val1p->getPointerElementType(),
b69d1d
-                              val1,
b69d1d
+      auto *ptr_l = CreateGEP(ArrayType::get(getInt8Ty(), str1_size),
b69d1d
+                              str1,
b69d1d
                               { getInt32(0), getInt32(i) });
b69d1d
       l = CreateLoad(getInt8Ty(), ptr_l);
b69d1d
     }
b69d1d
@@ -833,8 +798,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
b69d1d
       r = getInt8(literal2->c_str()[i]);
b69d1d
     else
b69d1d
     {
b69d1d
-      auto *ptr_r = CreateGEP(val2p->getPointerElementType(),
b69d1d
-                              val2,
b69d1d
+      auto *ptr_r = CreateGEP(ArrayType::get(getInt8Ty(), str2_size),
b69d1d
+                              str2,
b69d1d
                               { getInt32(0), getInt32(i) });
b69d1d
       r = CreateLoad(getInt8Ty(), ptr_r);
b69d1d
     }
b69d1d
@@ -994,11 +959,9 @@ void IRBuilderBPF::CreateGetCurrentComm(Value *ctx,
b69d1d
                                         size_t size,
b69d1d
                                         const location &loc)
b69d1d
 {
b69d1d
-  assert(buf->getType()->getPointerElementType()->isArrayTy() &&
b69d1d
-         buf->getType()->getPointerElementType()->getArrayNumElements() >=
b69d1d
-             size &&
b69d1d
-         buf->getType()->getPointerElementType()->getArrayElementType() ==
b69d1d
-             getInt8Ty());
b69d1d
+  assert(buf->getAllocatedType()->isArrayTy() &&
b69d1d
+         buf->getAllocatedType()->getArrayNumElements() >= size &&
b69d1d
+         buf->getAllocatedType()->getArrayElementType() == getInt8Ty());
b69d1d
 
b69d1d
   // long bpf_get_current_comm(char *buf, int size_of_buf)
b69d1d
   // Return: 0 on success or negative error
b69d1d
@@ -1077,7 +1040,7 @@ void IRBuilderBPF::CreateSignal(Value *ctx, Value *sig, const location &loc)
b69d1d
       Instruction::IntToPtr,
b69d1d
       getInt64(libbpf::BPF_FUNC_send_signal),
b69d1d
       signal_func_ptr_type);
b69d1d
-  CallInst *call = createCall(signal_func, { sig }, "signal");
b69d1d
+  CallInst *call = createCall(signal_func_type, signal_func, { sig }, "signal");
b69d1d
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_send_signal, loc);
b69d1d
 }
b69d1d
 
b69d1d
@@ -1091,7 +1054,7 @@ void IRBuilderBPF::CreateOverrideReturn(Value *ctx, Value *rc)
b69d1d
   Constant *override_func = ConstantExpr::getCast(Instruction::IntToPtr,
b69d1d
       getInt64(libbpf::BPF_FUNC_override_return),
b69d1d
       override_func_ptr_type);
b69d1d
-  createCall(override_func, { ctx, rc }, "override");
b69d1d
+  createCall(override_func_type, override_func, { ctx, rc }, "override");
b69d1d
 }
b69d1d
 
b69d1d
 CallInst *IRBuilderBPF::CreateSkbOutput(Value *skb,
b69d1d
@@ -1126,7 +1089,8 @@ CallInst *IRBuilderBPF::CreateSkbOutput(Value *skb,
b69d1d
       Instruction::IntToPtr,
b69d1d
       getInt64(libbpf::BPF_FUNC_skb_output),
b69d1d
       skb_output_func_ptr_type);
b69d1d
-  CallInst *call = createCall(skb_output_func,
b69d1d
+  CallInst *call = createCall(skb_output_func_type,
b69d1d
+                              skb_output_func,
b69d1d
                               { skb, map_ptr, flags, data, size_val },
b69d1d
                               "skb_output");
b69d1d
   return call;
b69d1d
@@ -1320,7 +1284,8 @@ void IRBuilderBPF::CreateSeqPrintf(Value *ctx,
b69d1d
                           CreateGEP(getInt64Ty(), meta, getInt64(0)),
b69d1d
                           "seq");
b69d1d
 
b69d1d
-  CallInst *call = createCall(seq_printf_func,
b69d1d
+  CallInst *call = createCall(seq_printf_func_type,
b69d1d
+                              seq_printf_func,
b69d1d
                               { seq, fmt, fmt_size, data, data_len },
b69d1d
                               "seq_printf");
b69d1d
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_seq_printf, loc);
b69d1d
diff --git a/src/ast/irbuilderbpf.h b/src/ast/irbuilderbpf.h
b69d1d
index e124911b..c9ffb545 100644
b69d1d
--- a/src/ast/irbuilderbpf.h
b69d1d
+++ b/src/ast/irbuilderbpf.h
b69d1d
@@ -90,17 +90,11 @@ public:
b69d1d
                        AddrSpace as,
b69d1d
                        const location &loc;;
b69d1d
   CallInst *CreateProbeReadStr(Value *ctx,
b69d1d
-                               AllocaInst *dst,
b69d1d
+                               Value *dst,
b69d1d
                                llvm::Value *size,
b69d1d
                                Value *src,
b69d1d
                                AddrSpace as,
b69d1d
                                const location &loc;;
b69d1d
-  CallInst *CreateProbeReadStr(Value *ctx,
b69d1d
-                               AllocaInst *dst,
b69d1d
-                               size_t size,
b69d1d
-                               Value *src,
b69d1d
-                               AddrSpace as,
b69d1d
-                               const location &loc;;
b69d1d
   CallInst *CreateProbeReadStr(Value *ctx,
b69d1d
                                Value *dst,
b69d1d
                                size_t size,
b69d1d
@@ -115,7 +109,12 @@ public:
b69d1d
                                 pid_t pid,
b69d1d
                                 AddrSpace as,
b69d1d
                                 const location &loc;;
b69d1d
-  Value *CreateStrncmp(Value *val1, Value *val2, uint64_t n, bool inverse);
b69d1d
+  Value *CreateStrncmp(Value *str1,
b69d1d
+                       uint64_t str1_size,
b69d1d
+                       Value *str2,
b69d1d
+                       uint64_t str2_size,
b69d1d
+                       uint64_t n,
b69d1d
+                       bool inverse);
b69d1d
   CallInst *CreateGetNs(bool boot_time, const location &loc;;
b69d1d
   CallInst *CreateGetPidTgid(const location &loc;;
b69d1d
   CallInst *CreateGetCurrentCgroupId(const location &loc;;
b69d1d
@@ -131,7 +130,10 @@ public:
b69d1d
                              ArrayRef<Value *> args,
b69d1d
                              const Twine &Name,
b69d1d
                              const location *loc = nullptr);
b69d1d
-  CallInst   *createCall(Value *callee, ArrayRef<Value *> args, const Twine &Name);
b69d1d
+  CallInst *createCall(FunctionType *callee_type,
b69d1d
+                       Value *callee,
b69d1d
+                       ArrayRef<Value *> args,
b69d1d
+                       const Twine &Name);
b69d1d
   void        CreateGetCurrentComm(Value *ctx, AllocaInst *buf, size_t size, const location& loc);
b69d1d
   void CreatePerfEventOutput(Value *ctx,
b69d1d
                              Value *data,
b69d1d
@@ -185,9 +187,6 @@ private:
b69d1d
                                 AddrSpace as,
b69d1d
                                 const location &loc;;
b69d1d
   CallInst *createMapLookup(int mapid, Value *key);
b69d1d
-  Constant *createProbeReadStrFn(llvm::Type *dst,
b69d1d
-                                 llvm::Type *src,
b69d1d
-                                 AddrSpace as);
b69d1d
   libbpf::bpf_func_id selectProbeReadHelper(AddrSpace as, bool str);
b69d1d
 
b69d1d
   std::map<std::string, StructType *> structs_;
b69d1d
diff --git a/src/ast/passes/codegen_llvm.cpp b/src/ast/passes/codegen_llvm.cpp
b69d1d
index a818ca0b..2b888087 100644
b69d1d
--- a/src/ast/passes/codegen_llvm.cpp
b69d1d
+++ b/src/ast/passes/codegen_llvm.cpp
b69d1d
@@ -1133,8 +1133,12 @@ void CodegenLLVM::visit(Call &call)
b69d1d
     auto left_string = getString(left_arg);
b69d1d
     auto right_string = getString(right_arg);
b69d1d
 
b69d1d
-    expr_ = b_.CreateStrncmp(
b69d1d
-        left_string.first, right_string.first, size, false);
b69d1d
+    expr_ = b_.CreateStrncmp(left_string.first,
b69d1d
+                             left_string.second,
b69d1d
+                             right_string.first,
b69d1d
+                             right_string.second,
b69d1d
+                             size,
b69d1d
+                             false);
b69d1d
   }
b69d1d
   else if (call.func == "override")
b69d1d
   {
b69d1d
@@ -1269,8 +1273,7 @@ void CodegenLLVM::visit(Variable &var)
b69d1d
   else
b69d1d
   {
b69d1d
     auto *var_alloca = variables_[var.ident];
b69d1d
-    expr_ = b_.CreateLoad(var_alloca->getType()->getPointerElementType(),
b69d1d
-                          var_alloca);
b69d1d
+    expr_ = b_.CreateLoad(var_alloca->getAllocatedType(), var_alloca);
b69d1d
   }
b69d1d
 }
b69d1d
 
b69d1d
@@ -1310,7 +1313,12 @@ void CodegenLLVM::binop_string(Binop &binop)
b69d1d
   auto right_string = getString(binop.right);
b69d1d
 
b69d1d
   size_t len = std::min(left_string.second, right_string.second);
b69d1d
-  expr_ = b_.CreateStrncmp(left_string.first, right_string.first, len, inverse);
b69d1d
+  expr_ = b_.CreateStrncmp(left_string.first,
b69d1d
+                           left_string.second,
b69d1d
+                           right_string.first,
b69d1d
+                           right_string.second,
b69d1d
+                           len,
b69d1d
+                           inverse);
b69d1d
 }
b69d1d
 
b69d1d
 void CodegenLLVM::binop_buf(Binop &binop)
b69d1d
@@ -1334,7 +1342,12 @@ void CodegenLLVM::binop_buf(Binop &binop)
b69d1d
 
b69d1d
   size_t len = std::min(binop.left->type.GetSize(),
b69d1d
                         binop.right->type.GetSize());
b69d1d
-  expr_ = b_.CreateStrncmp(left_string, right_string, len, inverse);
b69d1d
+  expr_ = b_.CreateStrncmp(left_string,
b69d1d
+                           binop.left->type.GetSize(),
b69d1d
+                           right_string,
b69d1d
+                           binop.right->type.GetSize(),
b69d1d
+                           len,
b69d1d
+                           inverse);
b69d1d
 }
b69d1d
 
b69d1d
 void CodegenLLVM::binop_int(Binop &binop)
b69d1d
@@ -3528,9 +3541,8 @@ void CodegenLLVM::createIncDec(Unop &unop)
b69d1d
   else if (unop.expr->is_variable)
b69d1d
   {
b69d1d
     Variable &var = static_cast<Variable &>(*unop.expr);
b69d1d
-    Value *oldval = b_.CreateLoad(
b69d1d
-        variables_[var.ident]->getType()->getPointerElementType(),
b69d1d
-        variables_[var.ident]);
b69d1d
+    Value *oldval = b_.CreateLoad(variables_[var.ident]->getAllocatedType(),
b69d1d
+                                  variables_[var.ident]);
b69d1d
     Value *newval;
b69d1d
     if (is_increment)
b69d1d
       newval = b_.CreateAdd(oldval, b_.GetIntSameSize(step, oldval));
b69d1d
-- 
b69d1d
2.38.1
b69d1d