diff --git a/gen/arrays.cpp b/gen/arrays.cpp index 07eb2ed6..6ddaa007 100644 --- a/gen/arrays.cpp +++ b/gen/arrays.cpp @@ -758,7 +758,7 @@ static LLValue* DtoArrayEqCmp_impl(const char* func, DValue* l, DValue* r, bool LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r) { LLValue* res = DtoArrayEqCmp_impl("_adEq", l, r, true); - res = new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, res, DtoConstInt(0), "tmp", gIR->scopebb()); + res = gIR->ir->CreateICmpNE(res, DtoConstInt(0), "tmp"); if (op == TOKnotequal) res = gIR->ir->CreateNot(res, "tmp"); @@ -817,7 +817,7 @@ LLValue* DtoArrayCompare(TOK op, DValue* l, DValue* r) res = DtoArrayEqCmp_impl("_adCmpChar", l, r, false); else res = DtoArrayEqCmp_impl("_adCmp", l, r, true); - res = new llvm::ICmpInst(cmpop, res, DtoConstInt(0), "tmp", gIR->scopebb()); + res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp"); } assert(res); @@ -859,12 +859,12 @@ LLValue* DtoDynArrayIs(TOK op, DValue* l, DValue* r) // compare lengths len1 = DtoArrayLen(l); len2 = DtoArrayLen(r); - LLValue* b1 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,len1,len2,"tmp"); + LLValue* b1 = gIR->ir->CreateICmpEQ(len1,len2,"tmp"); // compare pointers ptr1 = DtoArrayPtr(l); ptr2 = DtoArrayPtr(r); - LLValue* b2 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,ptr1,ptr2,"tmp"); + LLValue* b2 = gIR->ir->CreateICmpEQ(ptr1,ptr2,"tmp"); // combine LLValue* res = gIR->ir->CreateAnd(b1,b2,"tmp"); diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index b7413c01..c9735c50 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -4,6 +4,7 @@ #include "mars.h" #include "init.h" #include "id.h" +#include "expression.h" #include "gen/tollvm.h" #include "gen/llvmhelpers.h" @@ -1312,3 +1313,58 @@ void findDefaultTarget() global.params.llvmArch = const_cast(e->Name); } } + +////////////////////////////////////////////////////////////////////////////////////////// + +LLValue* DtoBoolean(DValue* dval) +{ + Type* dtype = dval->getType()->toBasetype(); + TY ty = dtype->ty; + + // integer + if (dtype->isintegral()) + { + LLValue* val = dval->getRVal(); + if (val->getType() == LLType::Int1Ty) + return val; + else { + LLValue* zero = LLConstantInt::get(val->getType(), 0, false); + return gIR->ir->CreateICmpNE(val, zero, "tmp"); + } + } + // complex + else if (dtype->iscomplex()) + { + // huh? + return DtoComplexEquals(TOKnotequal, dval, DtoComplex(dtype, new DNullValue(Type::tint8, llvm::ConstantInt::get(LLType::Int8Ty, 0)))); + } + // floating point + else if (dtype->isfloating()) + { + LLValue* val = dval->getRVal(); + LLValue* zero = LLConstant::getNullValue(val->getType()); + return gIR->ir->CreateFCmpONE(val, zero, "tmp"); + } + // pointer/class + else if (ty == Tpointer || ty == Tclass) { + LLValue* val = dval->getRVal(); + LLValue* zero = LLConstant::getNullValue(val->getType()); + return gIR->ir->CreateICmpNE(val, zero, "tmp"); + } + // dynamic array + else if (ty == Tarray) + { + // return (arr.length != 0) + return gIR->ir->CreateICmpNE(DtoArrayLen(dval), DtoConstSize_t(0), "tmp"); + } + // delegate + else if (ty == Tdelegate) + { + // return (dg !is null) + return DtoDelegateEquals(TOKnotequal, dval->getRVal(), NULL); + } + // unknown + std::cout << "unsupported -> bool : " << dtype->toChars() << '\n'; + assert(0); + return 0; +} diff --git a/gen/llvmhelpers.h b/gen/llvmhelpers.h index a607fad4..e8244fc0 100644 --- a/gen/llvmhelpers.h +++ b/gen/llvmhelpers.h @@ -98,4 +98,7 @@ void findDefaultTarget(); */ DValue* DtoCallDFunc(FuncDeclaration* fdecl, Array* arguments, TypeClass* type=0, LLValue* thismem=0); +/// Converts any value to a boolean (llvm i1) +LLValue* DtoBoolean(DValue* dval); + #endif diff --git a/gen/statements.cpp b/gen/statements.cpp index 88b08b6a..24395c9d 100644 --- a/gen/statements.cpp +++ b/gen/statements.cpp @@ -967,12 +967,12 @@ void ForeachStatement::toIR(IRState* p) LLValue* done = 0; LLValue* load = DtoLoad(keyvar); if (op == TOKforeach) { - done = new llvm::ICmpInst(llvm::ICmpInst::ICMP_ULT, load, niters, "tmp", p->scopebb()); + done = p->ir->CreateICmpULT(load, niters, "tmp"); } else if (op == TOKforeach_reverse) { - done = new llvm::ICmpInst(llvm::ICmpInst::ICMP_UGT, load, zerokey, "tmp", p->scopebb()); - load = llvm::BinaryOperator::createSub(load,llvm::ConstantInt::get(keytype, 1, false),"tmp",p->scopebb()); - new llvm::StoreInst(load, keyvar, p->scopebb()); + done = p->ir->CreateICmpUGT(load, zerokey, "tmp"); + load = p->ir->CreateSub(load, llvm::ConstantInt::get(keytype, 1, false), "tmp"); + DtoStore(load, keyvar); } llvm::BranchInst::Create(bodybb, endbb, done, p->scopebb()); diff --git a/gen/toir.cpp b/gen/toir.cpp index 9e6608ef..c1c6b9ef 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1610,7 +1610,7 @@ DValue* CmpExp::toElem(IRState* p) LLValue* b = r->getRVal(); Logger::cout() << "type 1: " << *a << '\n'; Logger::cout() << "type 2: " << *b << '\n'; - eval = new llvm::ICmpInst(cmpop, a, b, "tmp", p->scopebb()); + eval = p->ir->CreateICmp(cmpop, a, b, "tmp"); } } else if (t->isfloating()) @@ -1646,7 +1646,7 @@ DValue* CmpExp::toElem(IRState* p) default: assert(0); } - eval = new llvm::FCmpInst(cmpop, l->getRVal(), r->getRVal(), "tmp", p->scopebb()); + eval = p->ir->CreateFCmp(cmpop, l->getRVal(), r->getRVal(), "tmp"); } else if (t->ty == Tsarray || t->ty == Tarray) { @@ -1697,7 +1697,7 @@ DValue* EqualExp::toElem(IRState* p) if (rv->getType() != lv->getType()) { rv = DtoBitCast(rv, lv->getType()); } - eval = new llvm::ICmpInst(cmpop, lv, rv, "tmp", p->scopebb()); + eval = p->ir->CreateICmp(cmpop, lv, rv, "tmp"); } else if (t->iscomplex()) { @@ -1719,7 +1719,7 @@ DValue* EqualExp::toElem(IRState* p) default: assert(0); } - eval = new llvm::FCmpInst(cmpop, l->getRVal(), r->getRVal(), "tmp", p->scopebb()); + eval = p->ir->CreateFCmp(cmpop, l->getRVal(), r->getRVal(), "tmp"); } else if (t->ty == Tsarray || t->ty == Tarray) { @@ -1996,7 +1996,7 @@ DValue* NotExp::toElem(IRState* p) LLValue* b = DtoBoolean(u); - LLConstant* zero = llvm::ConstantInt::get(LLType::Int1Ty, 0, true); + LLConstant* zero = DtoConstBool(false); b = p->ir->CreateICmpEQ(b,zero); return new DImValue(type, b); @@ -2262,8 +2262,9 @@ DValue* IdentityExp::toElem(IRState* p) } else if (t1->isfloating()) { - llvm::FCmpInst::Predicate pred = (op == TOKidentity) ? llvm::FCmpInst::FCMP_OEQ : llvm::FCmpInst::FCMP_ONE; - eval = new llvm::FCmpInst(pred, l, r, "tmp", p->scopebb()); + eval = (op == TOKidentity) + ? p->ir->CreateFCmpOEQ(l,r,"tmp") + : p->ir->CreateFCmpONE(l,r,"tmp"); } else if (t1->ty == Tpointer) { @@ -2273,13 +2274,14 @@ DValue* IdentityExp::toElem(IRState* p) else r = DtoBitCast(r, l->getType()); } - llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; - eval = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); + eval = (op == TOKidentity) + ? p->ir->CreateICmpEQ(l,r,"tmp") + : p->ir->CreateICmpNE(l,r,"tmp"); } else { - llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; - //Logger::cout() << "l = " << *l << " r = " << *r << '\n'; - eval = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); + eval = (op == TOKidentity) + ? p->ir->CreateICmpEQ(l,r,"tmp") + : p->ir->CreateICmpNE(l,r,"tmp"); } return new DImValue(type, eval); } diff --git a/gen/tollvm.cpp b/gen/tollvm.cpp index b9692c82..7170a222 100644 --- a/gen/tollvm.cpp +++ b/gen/tollvm.cpp @@ -326,41 +326,6 @@ LLValue* DtoPointedType(LLValue* ptr, LLValue* val) ////////////////////////////////////////////////////////////////////////////////////////// -LLValue* DtoBoolean(DValue* dval) -{ - Type* dtype = dval->getType()->toBasetype(); - LLValue* val = dval->getRVal(); - const LLType* t = val->getType(); - - if (dtype->isintegral()) - { - if (t == LLType::Int1Ty) - return val; - else { - LLValue* zero = llvm::ConstantInt::get(t, 0, false); - return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); - } - } - else if (dtype->iscomplex()) - { - return DtoComplexEquals(TOKnotequal, dval, DtoComplex(dtype, new DNullValue(Type::tint8, llvm::ConstantInt::get(LLType::Int8Ty, 0)))); - } - else if (dtype->isfloating()) - { - LLValue* zero = llvm::Constant::getNullValue(t); - return new llvm::FCmpInst(llvm::FCmpInst::FCMP_ONE, val, zero, "tmp", gIR->scopebb()); - } - else if (dtype->ty == Tpointer) { - LLValue* zero = llvm::Constant::getNullValue(t); - return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); - } - std::cout << "unsupported -> bool : " << dtype->toChars() << " " << *t << '\n'; - assert(0); - return 0; -} - -////////////////////////////////////////////////////////////////////////////////////////// - const LLType* DtoSize_t() { // the type of size_t does not change once set diff --git a/gen/tollvm.h b/gen/tollvm.h index a3fee834..99f93dc6 100644 --- a/gen/tollvm.h +++ b/gen/tollvm.h @@ -40,9 +40,6 @@ unsigned DtoCallingConv(LINK l); // TODO: this one should be removed!!! LLValue* DtoPointedType(LLValue* ptr, LLValue* val); -// casts any value to a boolean -LLValue* DtoBoolean(DValue* dval); - // some types const LLType* DtoSize_t(); const LLStructType* DtoInterfaceInfoType();