Ignore:
Timestamp:
Dec 3, 2017, 12:40:40 PM (17 months ago)
Author:
nmedfort
Message:

Bug fixes and simplified MultiBlockKernel? logic

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.cpp

    r5746 r5755  
    5252}
    5353
     54#ifdef HAS_ADDRESS_SANITIZER
     55Value * checkHeapAddress(CBuilder * const b, Value * const Ptr, Value * const Size) {
     56    Module * const m = b->getModule();
     57    PointerType * const voidPtrTy = b->getVoidPtrTy();
     58    IntegerType * const sizeTy = b->getSizeTy();
     59    Function * isPoisoned = m->getFunction("__asan_region_is_poisoned");
     60    if (LLVM_UNLIKELY(isPoisoned == nullptr)) {
     61        isPoisoned = Function::Create(FunctionType::get(voidPtrTy, {voidPtrTy, sizeTy}, false), Function::ExternalLinkage, "__asan_region_is_poisoned", m);
     62        isPoisoned->setCallingConv(CallingConv::C);
     63        isPoisoned->setReturnDoesNotAlias();
     64        #if LLVM_VERSION_INTEGER < LLVM_5_0_0
     65        isPoisoned->setDoesNotAlias(1);
     66        #endif
     67    }
     68    Value * const addr = b->CreatePointerCast(Ptr, voidPtrTy);
     69    Value * check = b->CreateCall(isPoisoned, { addr, b->CreateTrunc(Size, sizeTy) });
     70    return b->CreateICmpEQ(check, ConstantPointerNull::get(cast<PointerType>(isPoisoned->getReturnType())));
     71}
     72#define CHECK_HEAP_ADDRESS(Ptr, Size, Name) \
     73if (LLVM_UNLIKELY(hasAddressSanitizer())) { \
     74    CreateAssert(checkHeapAddress(this, Ptr, Size), Name " was given unallocated memory address"); \
     75}
     76#else
     77#define CHECK_HEAP_ADDRESS(Ptr, Size, Name)
     78#endif
     79
     80static AllocaInst * resolveStackAddress(Value * Ptr) {
     81    for (;;) {
     82        if (GetElementPtrInst * gep = dyn_cast<GetElementPtrInst>(Ptr)) {
     83            Ptr = gep->getPointerOperand();
     84        } else if (CastInst * ci = dyn_cast<CastInst>(Ptr)) {
     85            Ptr = ci->getOperand(0);
     86        } else {
     87            return dyn_cast<AllocaInst>(Ptr);
     88        }
     89    }
     90}
     91
     92static Value * checkStackAddress(CBuilder * const b, Value * const Ptr, Value * const Size, AllocaInst * const Base) {
     93    DataLayout DL(b->getModule());
     94    IntegerType * const intPtrTy = cast<IntegerType>(DL.getIntPtrType(Ptr->getType()));
     95    Value * sz = ConstantExpr::getTrunc(ConstantExpr::getSizeOf(Base->getAllocatedType()), intPtrTy);
     96    if (dyn_cast_or_null<Constant>(Base->getArraySize()) && !cast<Constant>(Base->getArraySize())->isNullValue()) {
     97        sz = b->CreateMul(sz, b->CreateZExtOrTrunc(Base->getArraySize(), intPtrTy));
     98    }
     99    Value * const p = b->CreatePtrToInt(Ptr, intPtrTy);
     100    Value * const s = b->CreatePtrToInt(Base, intPtrTy);
     101    Value * const e = b->CreateAdd(s, b->CreateSub(sz, b->CreateZExtOrTrunc(Size, intPtrTy)));
     102    return b->CreateAnd(b->CreateICmpUGE(p, s), b->CreateICmpULE(p, e));
     103}
     104
     105#define CHECK_ADDRESS(Ptr, Size, Name) \
     106    CreateAssert(Ptr, Name " was given a null address"); \
     107    if (AllocaInst * Base = resolveStackAddress(Ptr)) { \
     108        CreateAssert(checkStackAddress(this, Ptr, Size, Base), Name " was given an invalid stack address"); \
     109    } else { \
     110        CHECK_HEAP_ADDRESS(Ptr, Size, Name) \
     111    }
     112
    54113Value * CBuilder::CreateURem(Value * const number, Value * const divisor, const Twine & Name) {
    55114    if (ConstantInt * c = dyn_cast<ConstantInt>(divisor)) {
     
    59118            return CreateAnd(number, ConstantInt::get(divisor->getType(), d - 1), Name);
    60119        }
    61     } else {
    62         CreateAssert(divisor, "CreateURem divisor cannot be 0!");
    63     }
     120    }
     121    CreateAssert(divisor, "CreateURem divisor cannot be 0!");
    64122    return Insert(BinaryOperator::CreateURem(number, divisor), Name);
    65123}
     
    83141Value * CBuilder::CreateUDivCeil(Value * const number, Value * const divisor, const Twine & Name) {
    84142    assert (number->getType() == divisor->getType());
     143    Type * const t = number->getType();
     144    Value * const n = CreateAdd(number, CreateSub(divisor, ConstantInt::get(t, 1)));
    85145    if (isa<ConstantInt>(divisor)) {
    86         return CreateUDivCeil(number, cast<ConstantInt>(divisor)->getZExtValue(), Name);
     146        const auto d = cast<ConstantInt>(divisor)->getZExtValue();
     147        if (is_power_2(d)) {
     148            if (d > 1) {
     149                return CreateLShr(n, ConstantInt::get(t, std::log2(d)), Name);
     150            } else {
     151                return number;
     152            }
     153        }
    87154    }
    88155    CreateAssert(divisor, "CreateUDivCeil divisor cannot be 0!");
    89     Constant * const one = ConstantInt::get(divisor->getType(), 1);
    90     return CreateUDiv(CreateAdd(number, CreateSub(divisor, one)), divisor, Name);
    91 }
    92 
    93 Value * CBuilder::CreateUDivCeil(Value * const number, const uint64_t divisor, const Twine & Name) {
    94     assert ("CreateUDivCeil divisor cannot be 0!" && divisor);
    95     Type * const t = number->getType();
    96     Value * const n = CreateAdd(number, ConstantInt::get(t, divisor - 1));
    97     if (is_power_2(divisor)) {
    98         if (divisor > 1) {
    99             return CreateLShr(n, ConstantInt::get(t, std::log2(divisor)), Name);
    100         } else {
    101             return number;
    102         }
    103     }
    104     Constant * const d = ConstantInt::get(t, divisor);
    105     CreateAssert(d, "CreateUDivCeil divisor cannot be 0!");
    106     return CreateUDiv(n, d, Name);
     156    return CreateUDiv(n, divisor, Name);
    107157}
    108158
     
    115165    Function * openFn = m->getFunction("open");
    116166    if (openFn == nullptr) {
    117         IntegerType * int32Ty = getInt32Ty();
    118         PointerType * int8PtrTy = getInt8PtrTy();
     167        IntegerType * const int32Ty = getInt32Ty();
     168        PointerType * const int8PtrTy = getInt8PtrTy();
    119169        openFn = cast<Function>(m->getOrInsertFunction("open",
    120170                                                         int32Ty, int8PtrTy, int32Ty, int32Ty, nullptr));
     
    125175// ssize_t write(int fildes, const void *buf, size_t nbyte);
    126176Value * CBuilder::CreateWriteCall(Value * fileDescriptor, Value * buf, Value * nbyte) {
    127     PointerType * voidPtrTy = getVoidPtrTy();
     177    PointerType * const voidPtrTy = getVoidPtrTy();
    128178    Module * const m = getModule();
    129179    Function * write = m->getFunction("write");
    130180    if (write == nullptr) {
    131         IntegerType * sizeTy = getSizeTy();
    132         IntegerType * int32Ty = getInt32Ty();
     181        IntegerType * const sizeTy = getSizeTy();
     182        IntegerType * const int32Ty = getInt32Ty();
    133183        write = cast<Function>(m->getOrInsertFunction("write",
    134184#if LLVM_VERSION_INTEGER < LLVM_5_0_0
    135                                                         AttributeSet().addAttribute(getContext(), 2U, Attribute::NoAlias),
     185        AttributeSet().addAttribute(getContext(), 2U, Attribute::NoAlias),
    136186#else
    137                                                         AttributeList().addAttribute(getContext(), 2U, Attribute::NoAlias),
     187        AttributeList().addAttribute(getContext(), 2U, Attribute::NoAlias),
    138188#endif
    139                                                         sizeTy, int32Ty, voidPtrTy, sizeTy, nullptr));
     189        sizeTy, int32Ty, voidPtrTy, sizeTy, nullptr));
    140190    }
    141191    buf = CreatePointerCast(buf, voidPtrTy);
     192    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     193        CHECK_ADDRESS(buf, nbyte, "CreateWriteCall");
     194    }
    142195    return CreateCall(write, {fileDescriptor, buf, nbyte});
    143196}
    144197
    145198Value * CBuilder::CreateReadCall(Value * fileDescriptor, Value * buf, Value * nbyte) {
    146     PointerType * voidPtrTy = getVoidPtrTy();
     199    PointerType * const voidPtrTy = getVoidPtrTy();
    147200    Module * const m = getModule();
    148201    Function * readFn = m->getFunction("read");
    149202    if (readFn == nullptr) {
    150         IntegerType * sizeTy = getSizeTy();
    151         IntegerType * int32Ty = getInt32Ty();
     203        IntegerType * const sizeTy = getSizeTy();
     204        IntegerType * const int32Ty = getInt32Ty();
    152205        readFn = cast<Function>(m->getOrInsertFunction("read",
    153206#if LLVM_VERSION_INTEGER < LLVM_5_0_0
    154                                                          AttributeSet().addAttribute(getContext(), 2U, Attribute::NoAlias),
     207        AttributeSet().addAttribute(getContext(), 2U, Attribute::NoAlias),
    155208#else
    156                                                          AttributeList().addAttribute(getContext(), 2U, Attribute::NoAlias),
     209        AttributeList().addAttribute(getContext(), 2U, Attribute::NoAlias),
    157210#endif
    158                                                          sizeTy, int32Ty, voidPtrTy, sizeTy, nullptr));
     211        sizeTy, int32Ty, voidPtrTy, sizeTy, nullptr));
    159212    }
    160213    buf = CreatePointerCast(buf, voidPtrTy);
     214    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     215        CHECK_ADDRESS(buf, nbyte, "CreateReadCall");
     216    }
    161217    return CreateCall(readFn, {fileDescriptor, buf, nbyte});
    162218}
     
    332388}
    333389
    334 Value * CBuilder::CreateCacheAlignedMalloc(Value * size) {
    335     const auto alignment = getCacheAlignment();
    336     if (LLVM_LIKELY(isa<Constant>(size))) {
    337         Constant * const align = ConstantInt::get(size->getType(), alignment, false);
    338         Constant * offset = ConstantExpr::getURem(cast<Constant>(size), align);
    339         if (!offset->isNullValue()) {
    340             size = ConstantExpr::getAdd(cast<Constant>(size), ConstantExpr::getSub(align, offset));
    341         }
    342     }
    343     return CreateAlignedMalloc(size, alignment);
    344 }
    345 
    346390Value * CBuilder::CreateAlignedMalloc(Value * size, const unsigned alignment) {
    347     if (LLVM_UNLIKELY((alignment & (alignment - 1)) != 0)) {
     391    if (LLVM_UNLIKELY(!is_power_2(alignment))) {
    348392        report_fatal_error("CreateAlignedMalloc: alignment must be a power of 2");
    349393    }
     
    351395    IntegerType * const sizeTy = getSizeTy();
    352396    PointerType * const voidPtrTy = getVoidPtrTy();
    353 
     397    ConstantInt * const align = ConstantInt::get(sizeTy, alignment);
     398    ConstantInt * const alignMask = ConstantInt::get(sizeTy, alignment - 1);
    354399    size = CreateZExtOrTrunc(size, sizeTy);
    355     ConstantInt * const align = ConstantInt::get(sizeTy, alignment);
    356     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
    357         CreateAssertZero(CreateURem(size, align), "CreateAlignedMalloc: size must be an integral multiple of alignment.");
    358     }
     400    Value * const offset = CreateAnd(size, alignMask);
     401    size = CreateSelect(CreateIsNull(offset), size, CreateAdd(size, CreateXor(offset, alignMask)));
     402    CreateAssertZero(CreateURem(size, align), "CreateAlignedMalloc: size must be an integral multiple of alignment.");
    359403    Value * ptr = nullptr;
    360404    if (hasAlignedAlloc()) {
     
    374418            f->setCallingConv(CallingConv::C);
    375419            f->setReturnDoesNotAlias();
    376 #if LLVM_VERSION_INTEGER < LLVM_5_0_0
     420            #if LLVM_VERSION_INTEGER < LLVM_5_0_0
    377421            f->setDoesNotAlias(1);
    378 #endif
     422            #endif
    379423        }
    380424        Value * handle = CreateAlloca(voidPtrTy);
     
    466510    }
    467511    Value * ptr = CreateCall(fMMap, {addr, size, prot, flags, fd, offset});
    468     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
     512    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    469513        DataLayout DL(m);
    470514        IntegerType * const intTy = getIntPtrTy(DL);
     
    556600        ConstantInt * const flags = ConstantInt::get(intTy, MREMAP_MAYMOVE);
    557601        ptr = CreateCall(fMRemap, {addr, oldSize, newSize, flags});
    558         if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
     602        if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    559603            Value * success = CreateICmpNE(CreatePtrToInt(addr, intTy), ConstantInt::getAllOnesValue(intTy)); // MAP_FAILED = -1
    560604            CreateAssert(success, "CreateMRemap: mremap failed to allocate memory");
     
    578622    }
    579623    len = CreateZExtOrTrunc(len, sizeTy);
    580     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
     624    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    581625        DataLayout DL(getModule());
    582626        IntegerType * const intPtrTy = getIntPtrTy(DL);
     
    592636}
    593637
     638Value * CBuilder::CreateMProtect(Value * addr, Value * size, const int protect) {
     639    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     640        // mprotect() changes the access protections for the calling process's
     641        // memory pages containing any part of the address range in the interval
     642        // [addr, addr+len-1].  addr must be aligned to a page boundary.
     643
     644        // mprotect(): POSIX.1-2001, POSIX.1-2008, SVr4.  POSIX says that the
     645        // behavior of mprotect() is unspecified if it is applied to a region of
     646        // memory that was not obtained via mmap(2).
     647
     648        // On Linux, it is always permissible to call mprotect() on any address
     649        // in a process's address space (except for the kernel vsyscall area).
     650        // In particular, it can be used to change existing code mappings to be
     651        // writable.
     652
     653//        Triple T(mTriple);
     654//        if (!T.isOSLinux()) {
     655//            DataLayout DL(getModule());
     656//            IntegerType * const intPtrTy = getIntPtrTy(DL);
     657//            Value * a = CreatePtrToInt(addr, intPtrTy);
     658//            Constant * const pageSize = ConstantInt::get(intPtrTy, getpagesize());
     659//            CreateAssertZero(CreateURem(a, pageSize), "CreateMProtect: addr must be aligned to page boundary on non-Linux architectures");
     660//        }
     661    }
     662
     663    IntegerType * const sizeTy = getSizeTy();
     664    PointerType * const voidPtrTy = getVoidPtrTy();
     665    IntegerType * const int32Ty = getInt32Ty();
     666
     667    Module * const m = getModule();
     668    Function * mprotectFunc = m->getFunction("mprotect");
     669    if (LLVM_UNLIKELY(mprotectFunc == nullptr)) {
     670        FunctionType * const fty = FunctionType::get(sizeTy, {voidPtrTy, sizeTy, int32Ty}, false);
     671        mprotectFunc = Function::Create(fty, Function::ExternalLinkage, "mprotect", m);
     672    }
     673    addr = CreatePointerCast(addr, voidPtrTy);
     674    size = CreateZExtOrTrunc(size, sizeTy);
     675    return CreateCall(mprotectFunc, {addr, size, ConstantInt::get(int32Ty, (int)protect)});
     676
     677}
     678
    594679IntegerType * CBuilder::getIntAddrTy() const {
    595680    return TypeBuilder<intptr_t, false>::get(getContext());
    596681}
    597682
    598 PointerType * CBuilder::getVoidPtrTy() const {
    599     return TypeBuilder<void *, true>::get(getContext());
     683PointerType * CBuilder::getVoidPtrTy(const unsigned AddressSpace) const {
     684    return PointerType::get(Type::getVoidTy(getContext()), AddressSpace);
    600685}
    601686
     
    644729    }
    645730    ptr = CreatePointerCast(ptr, voidPtrTy);
     731    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     732        CHECK_ADDRESS(ptr, CreateMul(size, nitems), "CreateFReadCall");
     733    }
    646734    return CreateCall(fReadFunc, {ptr, size, nitems, stream});
    647735}
     
    658746    }
    659747    ptr = CreatePointerCast(ptr, voidPtrTy);
     748    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     749        CHECK_ADDRESS(ptr, CreateMul(size, nitems), "CreateFReadCall");
     750    }
    660751    return CreateCall(fWriteFunc, {ptr, size, nitems, stream});
    661752}
     
    9921083        }
    9931084        IRBuilder<>::CreateCall(function, {assertion, GetString(failureMessage), trace, depth});
     1085    } else { // if assertions are not enabled, make it a compiler assumption.
     1086        IRBuilder<>::CreateAssumption(assertion);
    9941087    }
    9951088}
     
    10291122
    10301123Value * CBuilder::CreateCountForwardZeroes(Value * value, const bool isZeroUndefined) {
    1031     if (isZeroUndefined) {
     1124    if (LLVM_UNLIKELY(isZeroUndefined && codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    10321125        CreateAssert(value, "CreateCountForwardZeroes: value cannot be zero!");
    10331126    }
     
    10371130
    10381131Value * CBuilder::CreateCountReverseZeroes(Value * value, const bool isZeroUndefined) {
    1039     if (isZeroUndefined) {
     1132    if (LLVM_UNLIKELY(isZeroUndefined && codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    10401133        CreateAssert(value, "CreateCountReverseZeroes: value cannot be zero!");
    10411134    }
     
    10921185}
    10931186
    1094 #ifdef HAS_ADDRESS_SANITIZER
    1095 Value * checkHeapAddress(CBuilder * const b, Value * const Ptr) {
    1096     Module * const m = b->getModule();
    1097     PointerType * const voidPtrTy = b->getVoidPtrTy();
    1098     IntegerType * const sizeTy = b->getSizeTy();
    1099     Function * isPoisoned = m->getFunction("__asan_region_is_poisoned");
    1100     if (LLVM_UNLIKELY(isPoisoned == nullptr)) {
    1101         isPoisoned = Function::Create(FunctionType::get(voidPtrTy, {voidPtrTy, sizeTy}, false), Function::ExternalLinkage, "__asan_region_is_poisoned", m);
    1102         isPoisoned->setCallingConv(CallingConv::C);
    1103         isPoisoned->setReturnDoesNotAlias();
    1104         isPoisoned->setDoesNotAlias(1);
    1105     } \
    1106     Value * const addr = b->CreatePointerCast(Ptr, voidPtrTy);
    1107     ConstantInt * const size = ConstantInt::get(sizeTy, Ptr->getType()->getPointerElementType()->getPrimitiveSizeInBits() / 8);
    1108     Value * check = b->CreateCall(isPoisoned, { addr, size });
    1109     return b->CreateICmpEQ(check, ConstantPointerNull::get(cast<PointerType>(isPoisoned->getReturnType())));
    1110 }
    1111 #define CHECK_HEAP_ADDRESS(Ptr, Name) \
    1112 if (LLVM_UNLIKELY(hasAddressSanitizer())) { \
    1113     CreateAssert(checkHeapAddress(this, Ptr), Name " was given unallocated memory address"); \
    1114 }
    1115 #else
    1116 #define CHECK_HEAP_ADDRESS(Ptr, Name)
    1117 #endif
    1118 
    1119 static AllocaInst * resolveStackAddress(Value * Ptr) {
    1120     while (isa<GetElementPtrInst>(Ptr)) {
    1121         Ptr = cast<GetElementPtrInst>(Ptr)->getPointerOperand();
    1122     }
    1123     return dyn_cast<AllocaInst>(Ptr);
    1124 }
    1125 
    1126 static Value * checkStackAddress(CBuilder * const b, Value * const Ptr, AllocaInst * const Base) {
    1127     DataLayout DL(b->getModule());
    1128     IntegerType * const intPtrTy = cast<IntegerType>(DL.getIntPtrType(Ptr->getType()));
    1129     Value * sz = ConstantExpr::getSizeOf(Base->getAllocatedType());
    1130     sz = b->CreateZExtOrTrunc(sz, intPtrTy);
    1131     if (dyn_cast_or_null<Constant>(Base->getArraySize()) && !cast<Constant>(Base->getArraySize())->isNullValue()) {
    1132         sz = b->CreateMul(sz, b->CreateZExtOrTrunc(Base->getArraySize(), intPtrTy));
    1133     }
    1134     Value * const p = b->CreatePtrToInt(Ptr, intPtrTy);
    1135     Value * const s = b->CreatePtrToInt(Base, intPtrTy);
    1136     Value * const e = b->CreateAdd(s, sz);
    1137     return b->CreateAnd(b->CreateICmpUGE(p, s), b->CreateICmpULT(p, e));
    1138 }
    1139 
    1140 #define CHECK_ADDRESS(Ptr, Name) \
    1141 if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) { \
    1142     CreateAssert(Ptr, Name " was given a null address"); \
    1143     if (AllocaInst * Base = resolveStackAddress(Ptr)) { \
    1144         CreateAssert(checkStackAddress(this, Ptr, Base), Name " was given an invalid stack address"); \
    1145     } else { \
    1146         CHECK_HEAP_ADDRESS(Ptr, Name) \
    1147     } \
    1148 }
    1149 
    11501187LoadInst * CBuilder::CreateLoad(Value *Ptr, const char * Name) {
    1151     CHECK_ADDRESS(Ptr, "CreateLoad");
     1188    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     1189        CHECK_ADDRESS(Ptr, ConstantExpr::getSizeOf(Ptr->getType()->getPointerElementType()), "CreateLoad");
     1190    }
    11521191    return IRBuilder<>::CreateLoad(Ptr, Name);
    11531192}
    11541193
    11551194LoadInst * CBuilder::CreateLoad(Value * Ptr, const Twine & Name) {
    1156     CHECK_ADDRESS(Ptr, "CreateLoad");
     1195    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     1196        CHECK_ADDRESS(Ptr, ConstantExpr::getSizeOf(Ptr->getType()->getPointerElementType()), "CreateLoad");
     1197    }
    11571198    return IRBuilder<>::CreateLoad(Ptr, Name);
    11581199}
    11591200
    1160 LoadInst * CBuilder::CreateLoad(Type *Ty, Value *Ptr, const Twine & Name) {
    1161     CHECK_ADDRESS(Ptr, "CreateLoad");
     1201LoadInst * CBuilder::CreateLoad(Type * Ty, Value *Ptr, const Twine & Name) {
     1202    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     1203        CHECK_ADDRESS(Ptr, ConstantExpr::getSizeOf(Ty), "CreateLoad");
     1204    }
    11621205    return IRBuilder<>::CreateLoad(Ty, Ptr, Name);
    11631206}
    11641207
    1165 LoadInst * CBuilder::CreateLoad(Value *Ptr, bool isVolatile, const Twine & Name) {   
    1166     CHECK_ADDRESS(Ptr, "CreateLoad");
     1208LoadInst * CBuilder::CreateLoad(Value * Ptr, bool isVolatile, const Twine & Name) {
     1209    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     1210        CHECK_ADDRESS(Ptr, ConstantExpr::getSizeOf(Ptr->getType()->getPointerElementType()), "CreateLoad");
     1211    }
    11671212    return IRBuilder<>::CreateLoad(Ptr, isVolatile, Name);
    11681213}
    11691214
    11701215StoreInst * CBuilder::CreateStore(Value * Val, Value * Ptr, bool isVolatile) {
    1171     assert (Val->getType()->getPointerTo() == Ptr->getType());
    1172     CHECK_ADDRESS(Ptr, "CreateStore");
     1216    assert (Val->getType() == Ptr->getType()->getPointerElementType());
     1217    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     1218        CHECK_ADDRESS(Ptr, ConstantExpr::getSizeOf(Val->getType()), "CreateStore");
     1219    }
    11731220    return IRBuilder<>::CreateStore(Val, Ptr, isVolatile);
    11741221}
    11751222
    11761223inline bool CBuilder::hasAddressSanitizer() const {
    1177     return codegen::DebugOptionIsSet(codegen::EnableAsserts) && mDriver && mDriver->hasExternalFunction("__asan_region_is_poisoned");
     1224    return mDriver && mDriver->hasExternalFunction("__asan_region_is_poisoned");
    11781225}
    11791226
    11801227LoadInst * CBuilder::CreateAlignedLoad(Value * Ptr, unsigned Align, const char * Name) {
    1181     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
     1228    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    11821229        DataLayout DL(getModule());
    11831230        IntegerType * const intPtrTy = cast<IntegerType>(DL.getIntPtrType(Ptr->getType()));
     
    11911238
    11921239LoadInst * CBuilder::CreateAlignedLoad(Value * Ptr, unsigned Align, const Twine & Name) {
    1193     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
     1240    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    11941241        DataLayout DL(getModule());
    11951242        IntegerType * const intPtrTy = cast<IntegerType>(DL.getIntPtrType(Ptr->getType()));
     
    12031250
    12041251LoadInst * CBuilder::CreateAlignedLoad(Value * Ptr, unsigned Align, bool isVolatile, const Twine & Name) {
    1205     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
     1252    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    12061253        DataLayout DL(getModule());
    12071254        IntegerType * const intPtrTy = cast<IntegerType>(DL.getIntPtrType(Ptr->getType()));
     
    12151262
    12161263StoreInst * CBuilder::CreateAlignedStore(Value * Val, Value * Ptr, unsigned Align, bool isVolatile) {
    1217     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
     1264    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
    12181265        DataLayout DL(getModule());
    12191266        IntegerType * const intPtrTy = cast<IntegerType>(DL.getIntPtrType(Ptr->getType()));
     
    12281275CallInst * CBuilder::CreateMemMove(Value * Dst, Value * Src, Value *Size, unsigned Align, bool isVolatile,
    12291276                                   MDNode *TBAATag, MDNode *ScopeTag, MDNode *NoAliasTag) {
    1230     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
     1277    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     1278        CHECK_ADDRESS(Src, Size, "CreateMemMove: Src");
     1279        CHECK_ADDRESS(Dst, Size, "CreateMemMove: Dst");
     1280        // If the call to this intrinisic has an alignment value that is not 0 or 1, then the caller
     1281        // guarantees that both the source and destination pointers are aligned to that boundary.
     1282        if (Align > 1) {
     1283            DataLayout DL(getModule());
     1284            IntegerType * const intPtrTy = DL.getIntPtrType(getContext());
     1285            Value * intSrc = CreatePtrToInt(Src, intPtrTy);
     1286            Value * intDst = CreatePtrToInt(Dst, intPtrTy);
     1287            ConstantInt * align = ConstantInt::get(intPtrTy, Align);
     1288            CreateAssertZero(CreateURem(intSrc, align), "CreateMemMove: Src pointer is misaligned");
     1289            CreateAssertZero(CreateURem(intDst, align), "CreateMemMove: Dst pointer is misaligned");
     1290
     1291        }
     1292    }
     1293    return IRBuilder<>::CreateMemMove(Dst, Src, Size, Align, isVolatile, TBAATag, ScopeTag, NoAliasTag);
     1294}
     1295
     1296CallInst * CBuilder::CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align, bool isVolatile,
     1297                                  MDNode *TBAATag, MDNode *TBAAStructTag, MDNode *ScopeTag, MDNode *NoAliasTag) {
     1298    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     1299        CHECK_ADDRESS(Src, Size, "CreateMemCpy: Src");
     1300        CHECK_ADDRESS(Dst, Size, "CreateMemCpy: Dst");
    12311301        DataLayout DL(getModule());
    12321302        IntegerType * const intPtrTy = DL.getIntPtrType(getContext());
     1303        Value * intSrc = CreatePtrToInt(Src, intPtrTy);
    12331304        Value * intDst = CreatePtrToInt(Dst, intPtrTy);
    1234         Value * intSrc = CreatePtrToInt(Src, intPtrTy);
    12351305        // If the call to this intrinisic has an alignment value that is not 0 or 1, then the caller
    12361306        // guarantees that both the source and destination pointers are aligned to that boundary.
    12371307        if (Align > 1) {
    12381308            ConstantInt * align = ConstantInt::get(intPtrTy, Align);
    1239             CreateAssertZero(CreateURem(intDst, align), "CreateMemMove: Dst pointer is misaligned");
    1240             CreateAssertZero(CreateURem(intSrc, align), "CreateMemMove: Src pointer is misaligned");
    1241         }
    1242     }
    1243     return IRBuilder<>::CreateMemMove(Dst, Src, Size, Align, isVolatile, TBAATag, ScopeTag, NoAliasTag);
    1244 }
    1245 
    1246 CallInst * CBuilder::CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align, bool isVolatile,
    1247                                   MDNode *TBAATag, MDNode *TBAAStructTag, MDNode *ScopeTag, MDNode *NoAliasTag) {
    1248     if (codegen::DebugOptionIsSet(codegen::EnableAsserts)) {
    1249         DataLayout DL(getModule());
    1250         IntegerType * const intPtrTy = DL.getIntPtrType(getContext());
    1251         Value * intDst = CreatePtrToInt(Dst, intPtrTy);
    1252         Value * intSrc = CreatePtrToInt(Src, intPtrTy);
    1253         // If the call to this intrinisic has an alignment value that is not 0 or 1, then the caller
    1254         // guarantees that both the source and destination pointers are aligned to that boundary.
    1255         if (Align > 1) {
    1256             ConstantInt * align = ConstantInt::get(intPtrTy, Align);
     1309            CreateAssertZero(CreateURem(intSrc, align), "CreateMemCpy: Src pointer is misaligned");
    12571310            CreateAssertZero(CreateURem(intDst, align), "CreateMemCpy: Dst pointer is misaligned");
    1258             CreateAssertZero(CreateURem(intSrc, align), "CreateMemCpy: Src pointer is misaligned");
    1259         }
    1260         Value * intSize = CreateZExtOrTrunc(Size, intSrc->getType());
     1311        }
     1312        Value * intSize = CreateZExtOrTrunc(Size, intPtrTy);
    12611313        Value * nonOverlapping = CreateOr(CreateICmpULT(CreateAdd(intSrc, intSize), intDst),
    12621314                                          CreateICmpULT(CreateAdd(intDst, intSize), intSrc));
     
    12641316    }
    12651317    return IRBuilder<>::CreateMemCpy(Dst, Src, Size, Align, isVolatile, TBAATag, TBAAStructTag, ScopeTag, NoAliasTag);
     1318}
     1319
     1320llvm::CallInst * CBuilder::CreateMemSet(llvm::Value * Ptr, llvm::Value * Val, llvm::Value * Size, unsigned Align,
     1321                       bool isVolatile, llvm::MDNode * TBAATag, llvm::MDNode * ScopeTag, llvm::MDNode * NoAliasTag) {
     1322    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::EnableAsserts))) {
     1323        CHECK_ADDRESS(Ptr, Size, "CreateMemSet");
     1324    }
     1325    return IRBuilder<>::CreateMemSet(Ptr, Val, Size, Align, isVolatile, TBAATag, ScopeTag, NoAliasTag);
    12661326}
    12671327
Note: See TracChangeset for help on using the changeset viewer.