1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
//===-- binops.cpp --------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/binops.h"
#include "dmd/declaration.h"
#include "dmd/expression.h"
#include "gen/complex.h"
#include "gen/dvalue.h"
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/tollvm.h"
//////////////////////////////////////////////////////////////////////////////
dinteger_t undoStrideMul(const Loc &loc, Type *t, dinteger_t offset) {
assert(t->ty == TY::Tpointer);
const auto elemSize = t->nextOf()->size(loc);
assert((offset % elemSize) == 0 &&
"Expected offset by an integer amount of elements");
return offset / elemSize;
}
//////////////////////////////////////////////////////////////////////////////
namespace {
struct RVals {
DRValue *lhs, *rhs;
};
RVals evalSides(DValue *lhs, Expression *rhs, bool loadLhsAfterRhs) {
RVals rvals;
if (!loadLhsAfterRhs) {
rvals.lhs = lhs->getRVal();
rvals.rhs = toElem(rhs)->getRVal();
} else {
rvals.rhs = toElem(rhs)->getRVal();
rvals.lhs = lhs->getRVal();
}
return rvals;
}
/// Tries to remove a MulExp by a constant value of baseSize from e. Returns
/// NULL if not possible.
Expression *extractNoStrideInc(Expression *e, dinteger_t baseSize, bool &negate) {
MulExp *mul;
while (true) {
if (auto ne = e->isNegExp()) {
negate = !negate;
e = ne->e1;
continue;
}
if (auto me = e->isMulExp()) {
mul = me;
break;
}
return nullptr;
}
if (!mul->e2->isConst()) {
return nullptr;
}
dinteger_t stride = mul->e2->toInteger();
if (stride != baseSize) {
return nullptr;
}
return mul->e1;
}
DValue *emitPointerOffset(Loc loc, DValue *base, Expression *offset,
bool negateOffset, Type *resultType,
bool loadLhsAfterRhs) {
// The operand emitted by the frontend is in units of bytes, and not
// pointer elements. We try to undo this before resorting to
// temporarily bitcasting the pointer to i8.
LLValue *llBase = nullptr;
LLValue *llOffset = nullptr;
LLValue *llResult = nullptr;
if (offset->isConst()) {
llBase = DtoRVal(base);
dinteger_t byteOffset = offset->toInteger();
if (byteOffset == 0) {
llResult = llBase;
} else {
llOffset = DtoConstSize_t(undoStrideMul(loc, base->type, byteOffset));
}
} else {
Expression *noStrideInc = extractNoStrideInc(
offset, base->type->nextOf()->size(loc), negateOffset);
auto rvals =
evalSides(base, noStrideInc ? noStrideInc : offset, loadLhsAfterRhs);
llBase = DtoRVal(rvals.lhs);
llOffset = DtoRVal(rvals.rhs);
if (!noStrideInc) // byte offset => cast base to i8*
llBase = DtoBitCast(llBase, getVoidPtrType());
}
if (!llResult) {
if (negateOffset)
llOffset = gIR->ir->CreateNeg(llOffset);
llResult = DtoGEP1(llBase, llOffset);
}
return new DImValue(resultType, DtoBitCast(llResult, DtoType(resultType)));
}
// LDC issue #2537 / DMD issue #18317: associative arrays can be
// added/subtracted via `typeof(null)` (implicitly cast to the AA type).
// If the specified type is an AA type, this function makes sure one operand is
// a null constant and returns the other operand (AA) as new DImValue.
// Returns null if type is not an AA.
DValue *isAssociativeArrayAndNull(Type *type, LLValue *lhs, LLValue *rhs) {
if (type->ty != TY::Taarray)
return nullptr;
if (auto constantL = isaConstant(lhs)) {
if (constantL->isNullValue())
return new DImValue(type, rhs);
};
if (auto constantR = isaConstant(rhs)) {
if (constantR->isNullValue())
return new DImValue(type, lhs);
}
llvm_unreachable(
"associative array addition/subtraction without null operand");
}
}
//////////////////////////////////////////////////////////////////////////////
DValue *binAdd(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
Type *lhsType = lhs->type->toBasetype();
Type *rhsType = rhs->type->toBasetype();
if (lhsType != rhsType && lhsType->ty == TY::Tpointer &&
rhsType->isintegral()) {
Logger::println("Adding integer to pointer");
return emitPointerOffset(loc, lhs, rhs, false, type, loadLhsAfterRhs);
}
auto rvals = evalSides(lhs, rhs, loadLhsAfterRhs);
if (type->ty == TY::Tnull)
return DtoNullValue(type, loc);
if (type->iscomplex())
return DtoComplexAdd(loc, type, rvals.lhs, rvals.rhs);
LLValue *l = DtoRVal(DtoCast(loc, rvals.lhs, type));
LLValue *r = DtoRVal(DtoCast(loc, rvals.rhs, type));
if (auto aa = isAssociativeArrayAndNull(type, l, r))
return aa;
LLValue *res = (type->isfloating() ? gIR->ir->CreateFAdd(l, r)
: gIR->ir->CreateAdd(l, r));
return new DImValue(type, res);
}
//////////////////////////////////////////////////////////////////////////////
DValue *binMin(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
Type *lhsType = lhs->type->toBasetype();
Type *rhsType = rhs->type->toBasetype();
if (lhsType != rhsType && lhsType->ty == TY::Tpointer &&
rhsType->isintegral()) {
Logger::println("Subtracting integer from pointer");
return emitPointerOffset(loc, lhs, rhs, true, type, loadLhsAfterRhs);
}
auto rvals = evalSides(lhs, rhs, loadLhsAfterRhs);
if (lhsType->ty == TY::Tpointer && rhsType->ty == TY::Tpointer) {
LLValue *l = DtoRVal(rvals.lhs);
LLValue *r = DtoRVal(rvals.rhs);
LLType *llSizeT = DtoSize_t();
l = gIR->ir->CreatePtrToInt(l, llSizeT);
r = gIR->ir->CreatePtrToInt(r, llSizeT);
LLValue *diff = gIR->ir->CreateSub(l, r);
LLType *llType = DtoType(type);
if (diff->getType() != llType)
diff = gIR->ir->CreateIntToPtr(diff, llType);
return new DImValue(type, diff);
}
if (type->ty == TY::Tnull)
return DtoNullValue(type, loc);
if (type->iscomplex())
return DtoComplexMin(loc, type, rvals.lhs, rvals.rhs);
LLValue *l = DtoRVal(DtoCast(loc, rvals.lhs, type));
LLValue *r = DtoRVal(DtoCast(loc, rvals.rhs, type));
if (auto aa = isAssociativeArrayAndNull(type, l, r))
return aa;
LLValue *res = (type->isfloating() ? gIR->ir->CreateFSub(l, r)
: gIR->ir->CreateSub(l, r));
return new DImValue(type, res);
}
//////////////////////////////////////////////////////////////////////////////
DValue *binMul(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
auto rvals = evalSides(lhs, rhs, loadLhsAfterRhs);
if (type->iscomplex())
return DtoComplexMul(loc, type, rvals.lhs, rvals.rhs);
LLValue *l = DtoRVal(DtoCast(loc, rvals.lhs, type));
LLValue *r = DtoRVal(DtoCast(loc, rvals.rhs, type));
LLValue *res = (type->isfloating() ? gIR->ir->CreateFMul(l, r)
: gIR->ir->CreateMul(l, r));
return new DImValue(type, res);
}
//////////////////////////////////////////////////////////////////////////////
DValue *binDiv(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
auto rvals = evalSides(lhs, rhs, loadLhsAfterRhs);
if (type->iscomplex())
return DtoComplexDiv(loc, type, rvals.lhs, rvals.rhs);
LLValue *l = DtoRVal(DtoCast(loc, rvals.lhs, type));
LLValue *r = DtoRVal(DtoCast(loc, rvals.rhs, type));
LLValue *res;
if (type->isfloating()) {
res = gIR->ir->CreateFDiv(l, r);
} else if (!isLLVMUnsigned(type)) {
res = gIR->ir->CreateSDiv(l, r);
} else {
res = gIR->ir->CreateUDiv(l, r);
}
return new DImValue(type, res);
}
//////////////////////////////////////////////////////////////////////////////
DValue *binMod(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
auto rvals = evalSides(lhs, rhs, loadLhsAfterRhs);
if (type->iscomplex())
return DtoComplexMod(loc, type, rvals.lhs, rvals.rhs);
LLValue *l = DtoRVal(DtoCast(loc, rvals.lhs, type));
LLValue *r = DtoRVal(DtoCast(loc, rvals.rhs, type));
LLValue *res;
if (type->isfloating()) {
res = gIR->ir->CreateFRem(l, r);
} else if (!isLLVMUnsigned(type)) {
res = gIR->ir->CreateSRem(l, r);
} else {
res = gIR->ir->CreateURem(l, r);
}
return new DImValue(type, res);
}
//////////////////////////////////////////////////////////////////////////////
namespace {
DValue *binBitwise(llvm::Instruction::BinaryOps binOp, const Loc &loc,
Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
auto rvals = evalSides(lhs, rhs, loadLhsAfterRhs);
LLValue *l = DtoRVal(DtoCast(loc, rvals.lhs, type));
LLValue *r = DtoRVal(DtoCast(loc, rvals.rhs, type));
LLValue *res = llvm::BinaryOperator::Create(binOp, l, r, "", gIR->scopebb());
return new DImValue(type, res);
}
}
DValue *binAnd(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
return binBitwise(llvm::Instruction::And, loc, type, lhs, rhs,
loadLhsAfterRhs);
}
DValue *binOr(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
return binBitwise(llvm::Instruction::Or, loc, type, lhs, rhs,
loadLhsAfterRhs);
}
DValue *binXor(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
return binBitwise(llvm::Instruction::Xor, loc, type, lhs, rhs,
loadLhsAfterRhs);
}
DValue *binShl(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
return binBitwise(llvm::Instruction::Shl, loc, type, lhs, rhs,
loadLhsAfterRhs);
}
DValue *binShr(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
auto op = (isLLVMUnsigned(type) ? llvm::Instruction::LShr
: llvm::Instruction::AShr);
return binBitwise(op, loc, type, lhs, rhs, loadLhsAfterRhs);
}
DValue *binUshr(const Loc &loc, Type *type, DValue *lhs, Expression *rhs,
bool loadLhsAfterRhs) {
return binBitwise(llvm::Instruction::LShr, loc, type, lhs, rhs,
loadLhsAfterRhs);
}
//////////////////////////////////////////////////////////////////////////////
LLValue *DtoBinNumericEquals(const Loc &loc, DValue *lhs, DValue *rhs, EXP op) {
assert(op == EXP::equal || op == EXP::notEqual || op == EXP::identity ||
op == EXP::notIdentity);
Type *t = lhs->type->toBasetype();
assert(t->isfloating());
Logger::println("numeric equality");
LLValue *res = nullptr;
if (t->iscomplex()) {
Logger::println("complex");
res = DtoComplexEquals(loc, op, lhs, rhs);
} else if (t->isfloating()) {
Logger::println("floating");
res = DtoBinFloatsEquals(loc, lhs, rhs, op);
}
assert(res);
return res;
}
//////////////////////////////////////////////////////////////////////////////
LLValue *DtoBinFloatsEquals(const Loc &loc, DValue *lhs, DValue *rhs, EXP op) {
LLValue *res = nullptr;
if (op == EXP::equal || op == EXP::notEqual) {
LLValue *l = DtoRVal(lhs);
LLValue *r = DtoRVal(rhs);
res = (op == EXP::equal ? gIR->ir->CreateFCmpOEQ(l, r)
: gIR->ir->CreateFCmpUNE(l, r));
if (lhs->type->toBasetype()->ty == TY::Tvector) {
res = mergeVectorEquals(res, op);
}
} else {
const auto cmpop =
op == EXP::identity ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE;
LLValue *sz = DtoConstSize_t(getTypeStoreSize(DtoType(lhs->type)));
LLValue *val = DtoMemCmp(makeLValue(loc, lhs), makeLValue(loc, rhs), sz);
res = gIR->ir->CreateICmp(cmpop, val,
LLConstantInt::get(val->getType(), 0, false));
}
assert(res);
return res;
}
//////////////////////////////////////////////////////////////////////////////
LLValue *mergeVectorEquals(LLValue *resultsVector, EXP op) {
// `resultsVector` is a vector of i1 values, the pair-wise results.
// Bitcast to an integer and check the bits via additional integer
// comparison.
const auto sizeInBits = getTypeBitSize(resultsVector->getType());
LLType *integerType = LLType::getIntNTy(gIR->context(), sizeInBits);
LLValue *v = DtoBitCast(resultsVector, integerType);
if (op == EXP::equal) {
// all pairs must be equal for the vectors to be equal
LLConstant *allEqual = LLConstant::getAllOnesValue(integerType);
return gIR->ir->CreateICmpEQ(v, allEqual);
} else if (op == EXP::notEqual) {
// any not-equal pair suffices for the vectors to be not-equal
LLConstant *noneNotEqual = LLConstantInt::get(integerType, 0);
return gIR->ir->CreateICmpNE(v, noneNotEqual);
}
llvm_unreachable("Unsupported operator.");
return nullptr;
}
|