Changeset 5622


Ignore:
Timestamp:
Aug 29, 2017, 5:07:20 PM (3 weeks ago)
Author:
nmedfort
Message:

More CBuilder assertions.

Location:
icGREP/icgrep-devel/icgrep/IR_Gen
Files:
2 edited

Legend:

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

    r5597 r5622  
    4343
    4444
    45 Value * CBuilder::CreateURem(Value * number, Value * divisor, const Twine &Name) {
     45Value * CBuilder::CreateURem(Value * number, Value * divisor, const Twine &Name) {   
    4646    if (ConstantInt * c = dyn_cast<ConstantInt>(divisor)) {
    47         uint64_t d = c->getZExtValue();
     47        const auto d = c->getZExtValue();
    4848        if ((d & (d - 1)) == 0) { // is a power of 2 or 0
    49             if (d > 0) return CreateAnd(number, ConstantInt::get(divisor->getType(), d - 1), Name);
    50         }
    51     }
     49            if (d > 0) {
     50                return CreateAnd(number, ConstantInt::get(divisor->getType(), d - 1), Name);
     51            }
     52        }
     53    }
     54    CreateAssert(divisor, "CreateURem divisor cannot be 0!");
    5255    return Insert(BinaryOperator::CreateURem(number, divisor), Name);
    5356}
    5457
    55 Value * CBuilder::CreateUDiv(Value * number, Value * divisor, const Twine &Name) {
     58Value * CBuilder::CreateUDiv(Value * number, Value * divisor, const Twine &Name) {   
    5659    if (ConstantInt * c = dyn_cast<ConstantInt>(divisor)) {
    57         uint64_t d = c->getZExtValue();
     60        const auto d = c->getZExtValue();
    5861        if ((d & (d - 1)) == 0) { // is a power of 2 or 0
    59             if (d > 1) return CreateLShr(number, ConstantInt::get(divisor->getType(), std::log2(d)), Name);
    60             else if (d == 1) return number;
    61         }
    62     }
     62            if (d > 1) {
     63                return CreateLShr(number, ConstantInt::get(divisor->getType(), std::log2(d)), Name);
     64            } else if (d == 1) {
     65                return number;
     66            }
     67        }
     68    }
     69    CreateAssert(divisor, "CreateUDiv divisor cannot be 0!");
    6370    return Insert(BinaryOperator::CreateUDiv(number, divisor), Name);
    6471}
    6572
    66 Value * CBuilder::CreateUDivCeil(Value * number, Value * divisor, const Twine &Name) {
     73Value * CBuilder::CreateUDivCeil(Value * number, Value * divisor, const Twine &Name) {   
    6774    if (ConstantInt * c = dyn_cast<ConstantInt>(divisor)) {
    68         uint64_t d = c->getZExtValue();
     75        const auto d = c->getZExtValue();
    6976        if ((d & (d - 1)) == 0) { // is a power of 2 or 0
    7077            if (d > 1) {
    7178                Value * n = CreateAdd(number, ConstantInt::get(divisor->getType(), d - 1));
    7279                return CreateLShr(n, ConstantInt::get(divisor->getType(), std::log2(d)), Name);
     80            } else if (d == 1) {
     81                return number;
    7382            }
    74             else if (d == 1) return number;
    75         }
    76     }
     83        }
     84    }
     85    CreateAssert(divisor, "CreateUDivCeil divisor cannot be 0!");
    7786    return CreateUDiv(CreateAdd(number, CreateSub(divisor, ConstantInt::get(divisor->getType(), 1))), divisor, Name);
    7887}
     
    295304}
    296305
    297 llvm::Value * CBuilder::CreateCacheAlignedMalloc(llvm::Value * size) {
     306Value * CBuilder::CreateCacheAlignedMalloc(Value * size) {
    298307    const auto alignment = getCacheAlignment();
    299308    if (LLVM_LIKELY(isa<Constant>(size))) {
     
    978987
    979988Value * CBuilder::CreateCountForwardZeroes(Value * value) {
     989    CreateAssert(value, "CreateCountForwardZeroes: value cannot be zero!");
    980990    Value * cttzFunc = Intrinsic::getDeclaration(getModule(), Intrinsic::cttz, value->getType());
    981991    return CreateCall(cttzFunc, {value, ConstantInt::getFalse(getContext())});
     
    983993
    984994Value * CBuilder::CreateCountReverseZeroes(Value * value) {
     995    CreateAssert(value, "CreateCountReverseZeroes: value cannot be zero!");
    985996    Value * ctlzFunc = Intrinsic::getDeclaration(getModule(), Intrinsic::ctlz, value->getType());
    986997    return CreateCall(ctlzFunc, {value, ConstantInt::getFalse(getContext())});
     
    10101021Value * CBuilder::CreateCeilLog2(Value * value) {
    10111022    IntegerType * ty = cast<IntegerType>(value->getType());
    1012     CreateAssert(value, "CreateCeilLog2: value cannot be zero");
    10131023    Value * m = CreateCountReverseZeroes(CreateSub(value, ConstantInt::get(ty, 1)));
    10141024    return CreateSub(ConstantInt::get(m->getType(), ty->getBitWidth()), m);
     
    10361046}
    10371047
     1048#define CONCAT(a__, b__) a__##b__
     1049#define STRINGIFY(a__) #a__
     1050
    10381051#ifdef HAS_ADDRESS_SANITIZER
    1039 
    1040 #define CHECK_ADDRESS(Ptr) \
    1041     if (LLVM_UNLIKELY(hasAddressSanitizer())) { \
    1042         Module * const m = getModule(); \
    1043         PointerType * const voidPtrTy = getVoidPtrTy(); \
    1044         IntegerType * const sizeTy = getSizeTy(); \
    1045         Function * isPoisoned = m->getFunction("__asan_region_is_poisoned"); \
    1046         if (LLVM_UNLIKELY(isPoisoned == nullptr)) { \
    1047             isPoisoned = Function::Create(FunctionType::get(voidPtrTy, {voidPtrTy, sizeTy}, false), Function::ExternalLinkage, "__asan_region_is_poisoned", m); \
    1048             isPoisoned->setCallingConv(CallingConv::C); \
    1049             isPoisoned->setDoesNotAlias(0); \
    1050             isPoisoned->setDoesNotAlias(1); \
    1051         } \
    1052         Value * const addr = CreatePointerCast(Ptr, voidPtrTy); \
    1053         ConstantInt * const size = ConstantInt::get(sizeTy, Ptr->getType()->getPointerElementType()->getPrimitiveSizeInBits() / 8); \
    1054         Value * check = CreateCall(isPoisoned, { addr, size }); \
    1055         check = CreateICmpEQ(check, ConstantPointerNull::get(cast<PointerType>(isPoisoned->getReturnType()))); \
    1056         CreateAssert(check, "Valid memory address"); \
     1052#define CHECK_ADDRESS_SANITIZER(Ptr, Name) \
     1053if (LLVM_UNLIKELY(hasAddressSanitizer())) { \
     1054    Module * const m = getModule(); \
     1055    PointerType * const voidPtrTy = getVoidPtrTy(); \
     1056    IntegerType * const sizeTy = getSizeTy(); \
     1057    Function * isPoisoned = m->getFunction("__asan_region_is_poisoned"); \
     1058    if (LLVM_UNLIKELY(isPoisoned == nullptr)) { \
     1059        isPoisoned = Function::Create(FunctionType::get(voidPtrTy, {voidPtrTy, sizeTy}, false), Function::ExternalLinkage, "__asan_region_is_poisoned", m); \
     1060        isPoisoned->setCallingConv(CallingConv::C); \
     1061        isPoisoned->setDoesNotAlias(0); \
     1062        isPoisoned->setDoesNotAlias(1); \
     1063    } \
     1064    Value * const addr = CreatePointerCast(Ptr, voidPtrTy); \
     1065    ConstantInt * const size = ConstantInt::get(sizeTy, Ptr->getType()->getPointerElementType()->getPrimitiveSizeInBits() / 8); \
     1066    Value * check = CreateCall(isPoisoned, { addr, size }); \
     1067    check = CreateICmpEQ(check, ConstantPointerNull::get(cast<PointerType>(isPoisoned->getReturnType()))); \
     1068    CreateAssert(check, STRINGIFY(CONCAT(Name, ": invalid memory address"))); \
     1069}
     1070#else
     1071#define CHECK_ADDRESS_SANITIZER(Ptr, Name)
     1072#endif
     1073
     1074#define CHECK_ADDRESS(Ptr, Name) \
     1075    if (codegen::EnableAsserts) { \
     1076        CreateAssert(Ptr, STRINGIFY(CONCAT(Name, ": null pointer address"))); \
     1077        CHECK_ADDRESS_SANITIZER(Ptr, Name) \
    10571078    }
    10581079
    10591080LoadInst * CBuilder::CreateLoad(Value *Ptr, const char * Name) {
    1060     CHECK_ADDRESS(Ptr);
     1081    CHECK_ADDRESS(Ptr, "CreateLoad");
    10611082    return IRBuilder<>::CreateLoad(Ptr, Name);
    10621083}
    10631084
    10641085LoadInst * CBuilder::CreateLoad(Value * Ptr, const Twine & Name) {
    1065     CHECK_ADDRESS(Ptr);
     1086    CHECK_ADDRESS(Ptr, "CreateLoad");
    10661087    return IRBuilder<>::CreateLoad(Ptr, Name);
    10671088}
    10681089
    10691090LoadInst * CBuilder::CreateLoad(Type *Ty, Value *Ptr, const Twine & Name) {
    1070     CHECK_ADDRESS(Ptr);
     1091    CHECK_ADDRESS(Ptr, "CreateLoad");
    10711092    return IRBuilder<>::CreateLoad(Ty, Ptr, Name);
    10721093}
    10731094
    10741095LoadInst * CBuilder::CreateLoad(Value *Ptr, bool isVolatile, const Twine & Name) {
    1075     CHECK_ADDRESS(Ptr);
     1096    CHECK_ADDRESS(Ptr, "CreateLoad");
    10761097    return IRBuilder<>::CreateLoad(Ptr, isVolatile, Name);
    10771098}
    10781099
    10791100StoreInst * CBuilder::CreateStore(Value * Val, Value * Ptr, bool isVolatile) {
    1080     CHECK_ADDRESS(Ptr);
     1101    CHECK_ADDRESS(Ptr, "CreateStore");
    10811102    return IRBuilder<>::CreateStore(Val, Ptr, isVolatile);
    10821103}
    10831104
    10841105#undef CHECK_ADDRESS
    1085 
    1086 #endif
    10871106
    10881107inline bool CBuilder::hasAddressSanitizer() const {
     
    11381157}
    11391158
     1159CallInst * CBuilder::CreateMemMove(Value * Dst, Value * Src, Value *Size, unsigned Align, bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag, MDNode *NoAliasTag) {
     1160    if (codegen::EnableAsserts) {
     1161        DataLayout DL(getModule());
     1162        IntegerType * const intPtrTy = DL.getIntPtrType(getContext());
     1163        Value * intDst = CreatePtrToInt(Dst, intPtrTy);
     1164        Value * intSrc = CreatePtrToInt(Src, intPtrTy);
     1165        if (Align > 1) {
     1166            ConstantInt * align = ConstantInt::get(intPtrTy, Align);
     1167            CreateAssertZero(CreateURem(intDst, align), "CreateMemMove: Dst pointer is misaligned");
     1168            CreateAssertZero(CreateURem(intSrc, align), "CreateMemMove: Src pointer is misaligned");
     1169        }
     1170    }
     1171    return IRBuilder<>::CreateMemMove(Dst, Src, Size, Align, isVolatile, TBAATag, ScopeTag, NoAliasTag);
     1172}
     1173
    11401174CBuilder::CBuilder(LLVMContext & C)
    11411175: IRBuilder<>(C)
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5597 r5622  
    239239    llvm::Function * LinkFunction(llvm::StringRef name, ExternalFunctionType * functionPtr) const;
    240240
    241     #ifdef HAS_ADDRESS_SANITIZER
     241
    242242    virtual llvm::LoadInst * CreateLoad(llvm::Value * Ptr, const char * Name);
    243243
     
    249249
    250250    virtual llvm::StoreInst * CreateStore(llvm::Value * Val, llvm::Value * Ptr, bool isVolatile = false);
    251     #endif
    252251
    253252    llvm::LoadInst * CreateAlignedLoad(llvm::Value * Ptr, unsigned Align, const char * Name);
     
    258257
    259258    llvm::StoreInst * CreateAlignedStore(llvm::Value * Val, llvm::Value * Ptr, unsigned Align, bool isVolatile = false);
     259
     260    llvm::CallInst * CreateMemMove(llvm::Value *Dst, llvm::Value *Src, llvm::Value *Size, unsigned Align,
     261                            bool isVolatile = false, llvm::MDNode *TBAATag = nullptr,
     262                            llvm::MDNode *ScopeTag = nullptr,
     263                            llvm::MDNode *NoAliasTag = nullptr);
    260264
    261265    void setDriver(Driver * const driver) {
Note: See TracChangeset for help on using the changeset viewer.