Ignore:
Timestamp:
Jul 1, 2017, 2:08:30 PM (23 months ago)
Author:
cameron
Message:

Dynamic Buffers - initial check-in

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

Legend:

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

    r5502 r5541  
    4141
    4242using namespace llvm;
     43
     44
     45Value * CBuilder::CreateURem(Value * number, Value * divisor, const Twine &Name) {
     46    if (ConstantInt * c = dyn_cast<ConstantInt>(divisor)) {
     47        uint64_t d = c->getZExtValue();
     48        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    }
     52    return Insert(BinaryOperator::CreateURem(number, divisor), Name);
     53}
     54
     55Value * CBuilder::CreateUDiv(Value * number, Value * divisor, const Twine &Name) {
     56    if (ConstantInt * c = dyn_cast<ConstantInt>(divisor)) {
     57        uint64_t d = c->getZExtValue();
     58        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    }
     63    return Insert(BinaryOperator::CreateUDiv(number, divisor), Name);
     64}
     65
     66Value * CBuilder::CreateUDivCeil(Value * number, Value * divisor, const Twine &Name) {
     67    if (ConstantInt * c = dyn_cast<ConstantInt>(divisor)) {
     68        uint64_t d = c->getZExtValue();
     69        if ((d & (d - 1)) == 0) { // is a power of 2 or 0
     70            if (d > 1) {
     71                Value * n = CreateAdd(number, ConstantInt::get(divisor->getType(), d - 1));
     72                return CreateLShr(n, ConstantInt::get(divisor->getType(), std::log2(d)), Name);
     73            }
     74            else if (d == 1) return number;
     75        }
     76    }
     77    return CreateUDiv(CreateAdd(number, CreateSub(divisor, ConstantInt::get(divisor->getType(), 1))), divisor, Name);
     78}
     79
     80
    4381
    4482Value * CBuilder::CreateOpenCall(Value * filename, Value * oflag, Value * mode) {
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5510 r5541  
    4848        ClearInsertionPoint();
    4949    }
    50 
     50   
     51    // UDiv and URem with optimization for division by power-of-2 constants
     52    llvm::Value * CreateUDiv(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
     53    llvm::Value * CreateURem(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
     54
     55    // Division with rounding up to the ceiling
     56    // Equivalent to CreateUDiv(CreateAdd(number, CreateSub(divisor, ConstantInt::get(divisor->getType(), 1))), divisor)
     57    llvm::Value * CreateUDivCeil(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
     58       
    5159    llvm::Value * CreateMalloc(llvm::Value * size);
    5260
Note: See TracChangeset for help on using the changeset viewer.