Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1 | //===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Expr nodes with scalar LLVM types as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Function.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 20 | #include "llvm/Intrinsics.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Chris Lattner | c89bf69 | 2008-01-03 07:05:49 +0000 | [diff] [blame] | 22 | #include <cstdarg> |
Ted Kremenek | 6aad91a | 2007-12-10 23:44:32 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | using llvm::Value; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Scalar Expression Emitter |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | struct BinOpInfo { |
| 33 | Value *LHS; |
| 34 | Value *RHS; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 35 | QualType Ty; // Computation Type. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 36 | const BinaryOperator *E; |
| 37 | }; |
| 38 | |
| 39 | namespace { |
| 40 | class VISIBILITY_HIDDEN ScalarExprEmitter |
| 41 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 42 | CodeGenFunction &CGF; |
Devang Patel | 50c9034 | 2007-10-09 19:49:58 +0000 | [diff] [blame] | 43 | llvm::LLVMFoldingBuilder &Builder; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 44 | public: |
| 45 | |
| 46 | ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) { |
| 47 | } |
| 48 | |
| 49 | |
| 50 | //===--------------------------------------------------------------------===// |
| 51 | // Utilities |
| 52 | //===--------------------------------------------------------------------===// |
| 53 | |
| 54 | const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } |
| 55 | LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } |
| 56 | |
| 57 | Value *EmitLoadOfLValue(LValue LV, QualType T) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 58 | return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 62 | /// value l-value, this method emits the address of the l-value, then loads |
| 63 | /// and returns the result. |
| 64 | Value *EmitLoadOfLValue(const Expr *E) { |
| 65 | // FIXME: Volatile |
| 66 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 67 | } |
| 68 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 69 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 70 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 71 | Value *EmitConversionToBool(Value *Src, QualType DstTy); |
| 72 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 73 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 74 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 75 | Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); |
| 76 | |
| 77 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 78 | /// complex type to the specified destination type, where the destination |
| 79 | /// type is an LLVM scalar type. |
| 80 | Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 81 | QualType SrcTy, QualType DstTy); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 83 | //===--------------------------------------------------------------------===// |
| 84 | // Visitor Methods |
| 85 | //===--------------------------------------------------------------------===// |
| 86 | |
| 87 | Value *VisitStmt(Stmt *S) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 88 | S->dump(CGF.getContext().getSourceManager()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 89 | assert(0 && "Stmt can't have complex result type!"); |
| 90 | return 0; |
| 91 | } |
| 92 | Value *VisitExpr(Expr *S); |
| 93 | Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); } |
| 94 | |
| 95 | // Leaves. |
| 96 | Value *VisitIntegerLiteral(const IntegerLiteral *E) { |
| 97 | return llvm::ConstantInt::get(E->getValue()); |
| 98 | } |
| 99 | Value *VisitFloatingLiteral(const FloatingLiteral *E) { |
Chris Lattner | c9bec4b | 2007-09-22 18:47:25 +0000 | [diff] [blame] | 100 | return llvm::ConstantFP::get(ConvertType(E->getType()), E->getValue()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 101 | } |
| 102 | Value *VisitCharacterLiteral(const CharacterLiteral *E) { |
| 103 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 104 | } |
Nate Begeman | e7579b5 | 2007-11-15 05:40:03 +0000 | [diff] [blame] | 105 | Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
| 106 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 107 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 108 | Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 109 | return llvm::ConstantInt::get(ConvertType(E->getType()), |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 110 | CGF.getContext().typesAreCompatible( |
| 111 | E->getArgType1(), E->getArgType2())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 112 | } |
| 113 | Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
| 114 | return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf()); |
| 115 | } |
| 116 | |
| 117 | // l-values. |
| 118 | Value *VisitDeclRefExpr(DeclRefExpr *E) { |
| 119 | if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl())) |
| 120 | return llvm::ConstantInt::get(EC->getInitVal()); |
| 121 | return EmitLoadOfLValue(E); |
| 122 | } |
| 123 | Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
| 124 | Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } |
| 125 | Value *VisitOCUVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } |
| 126 | Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } |
| 127 | Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 128 | |
| 129 | Value *VisitInitListExpr(InitListExpr *E) { |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 130 | unsigned NumInitElements = E->getNumInits(); |
| 131 | |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 132 | const llvm::VectorType *VType = |
Anders Carlsson | f6884ac | 2008-01-29 01:15:48 +0000 | [diff] [blame] | 133 | dyn_cast<llvm::VectorType>(ConvertType(E->getType())); |
| 134 | |
| 135 | // We have a scalar in braces. Just use the first element. |
| 136 | if (!VType) |
| 137 | return Visit(E->getInit(0)); |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 139 | unsigned NumVectorElements = VType->getNumElements(); |
| 140 | const llvm::Type *ElementType = VType->getElementType(); |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 141 | |
| 142 | // Emit individual vector element stores. |
| 143 | llvm::Value *V = llvm::UndefValue::get(VType); |
| 144 | |
Anders Carlsson | 222d2c8 | 2007-12-18 02:45:33 +0000 | [diff] [blame] | 145 | // Emit initializers |
| 146 | unsigned i; |
| 147 | for (i = 0; i < NumInitElements; ++i) { |
Devang Patel | a83cc33 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 148 | Value *NewV = Visit(E->getInit(i)); |
| 149 | Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 150 | V = Builder.CreateInsertElement(V, NewV, Idx); |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 151 | } |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 152 | |
| 153 | // Emit remaining default initializers |
| 154 | for (/* Do not initialize i*/; i < NumVectorElements; ++i) { |
| 155 | Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 156 | llvm::Value *NewV = llvm::Constant::getNullValue(ElementType); |
| 157 | V = Builder.CreateInsertElement(V, NewV, Idx); |
| 158 | } |
| 159 | |
Devang Patel | a83cc33 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 160 | return V; |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 164 | return Visit(E->getInitializer()); |
| 165 | } |
| 166 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 167 | Value *VisitImplicitCastExpr(const ImplicitCastExpr *E); |
| 168 | Value *VisitCastExpr(const CastExpr *E) { |
| 169 | return EmitCastExpr(E->getSubExpr(), E->getType()); |
| 170 | } |
| 171 | Value *EmitCastExpr(const Expr *E, QualType T); |
| 172 | |
| 173 | Value *VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 174 | return CGF.EmitCallExpr(E).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 177 | Value *VisitStmtExpr(const StmtExpr *E); |
| 178 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 179 | // Unary Operators. |
| 180 | Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre); |
| 181 | Value *VisitUnaryPostDec(const UnaryOperator *E) { |
| 182 | return VisitPrePostIncDec(E, false, false); |
| 183 | } |
| 184 | Value *VisitUnaryPostInc(const UnaryOperator *E) { |
| 185 | return VisitPrePostIncDec(E, true, false); |
| 186 | } |
| 187 | Value *VisitUnaryPreDec(const UnaryOperator *E) { |
| 188 | return VisitPrePostIncDec(E, false, true); |
| 189 | } |
| 190 | Value *VisitUnaryPreInc(const UnaryOperator *E) { |
| 191 | return VisitPrePostIncDec(E, true, true); |
| 192 | } |
| 193 | Value *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 194 | return EmitLValue(E->getSubExpr()).getAddress(); |
| 195 | } |
| 196 | Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
| 197 | Value *VisitUnaryPlus(const UnaryOperator *E) { |
| 198 | return Visit(E->getSubExpr()); |
| 199 | } |
| 200 | Value *VisitUnaryMinus (const UnaryOperator *E); |
| 201 | Value *VisitUnaryNot (const UnaryOperator *E); |
| 202 | Value *VisitUnaryLNot (const UnaryOperator *E); |
| 203 | Value *VisitUnarySizeOf (const UnaryOperator *E) { |
| 204 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true); |
| 205 | } |
| 206 | Value *VisitUnaryAlignOf (const UnaryOperator *E) { |
| 207 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false); |
| 208 | } |
| 209 | Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType, |
| 210 | bool isSizeOf); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 211 | Value *VisitUnaryReal (const UnaryOperator *E); |
| 212 | Value *VisitUnaryImag (const UnaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 213 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 214 | return Visit(E->getSubExpr()); |
| 215 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 216 | Value *VisitUnaryOffsetOf(const UnaryOperator *E); |
| 217 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 218 | // Binary Operators. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 219 | Value *EmitMul(const BinOpInfo &Ops) { |
| 220 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 221 | } |
| 222 | Value *EmitDiv(const BinOpInfo &Ops); |
| 223 | Value *EmitRem(const BinOpInfo &Ops); |
| 224 | Value *EmitAdd(const BinOpInfo &Ops); |
| 225 | Value *EmitSub(const BinOpInfo &Ops); |
| 226 | Value *EmitShl(const BinOpInfo &Ops); |
| 227 | Value *EmitShr(const BinOpInfo &Ops); |
| 228 | Value *EmitAnd(const BinOpInfo &Ops) { |
| 229 | return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); |
| 230 | } |
| 231 | Value *EmitXor(const BinOpInfo &Ops) { |
| 232 | return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); |
| 233 | } |
| 234 | Value *EmitOr (const BinOpInfo &Ops) { |
| 235 | return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); |
| 236 | } |
| 237 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 238 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 239 | Value *EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 240 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); |
| 241 | |
| 242 | // Binary operators and binary compound assignment operators. |
| 243 | #define HANDLEBINOP(OP) \ |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 244 | Value *VisitBin ## OP(const BinaryOperator *E) { \ |
| 245 | return Emit ## OP(EmitBinOps(E)); \ |
| 246 | } \ |
| 247 | Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ |
| 248 | return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 249 | } |
| 250 | HANDLEBINOP(Mul); |
| 251 | HANDLEBINOP(Div); |
| 252 | HANDLEBINOP(Rem); |
| 253 | HANDLEBINOP(Add); |
| 254 | // (Sub) - Sub is handled specially below for ptr-ptr subtract. |
| 255 | HANDLEBINOP(Shl); |
| 256 | HANDLEBINOP(Shr); |
| 257 | HANDLEBINOP(And); |
| 258 | HANDLEBINOP(Xor); |
| 259 | HANDLEBINOP(Or); |
| 260 | #undef HANDLEBINOP |
| 261 | Value *VisitBinSub(const BinaryOperator *E); |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 262 | Value *VisitBinSubAssign(const CompoundAssignOperator *E) { |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 263 | return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub); |
| 264 | } |
| 265 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 266 | // Comparisons. |
| 267 | Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 268 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 269 | #define VISITCOMP(CODE, UI, SI, FP) \ |
| 270 | Value *VisitBin##CODE(const BinaryOperator *E) { \ |
| 271 | return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ |
| 272 | llvm::FCmpInst::FP); } |
| 273 | VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT); |
| 274 | VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT); |
| 275 | VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE); |
| 276 | VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE); |
| 277 | VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ); |
| 278 | VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE); |
| 279 | #undef VISITCOMP |
| 280 | |
| 281 | Value *VisitBinAssign (const BinaryOperator *E); |
| 282 | |
| 283 | Value *VisitBinLAnd (const BinaryOperator *E); |
| 284 | Value *VisitBinLOr (const BinaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 285 | Value *VisitBinComma (const BinaryOperator *E); |
| 286 | |
| 287 | // Other Operators. |
| 288 | Value *VisitConditionalOperator(const ConditionalOperator *CO); |
| 289 | Value *VisitChooseExpr(ChooseExpr *CE); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 290 | Value *VisitOverloadExpr(OverloadExpr *OE); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 291 | Value *VisitVAArgExpr(VAArgExpr *VE); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 292 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 293 | return CGF.EmitObjCStringLiteral(E); |
| 294 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 295 | Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 296 | }; |
| 297 | } // end anonymous namespace. |
| 298 | |
| 299 | //===----------------------------------------------------------------------===// |
| 300 | // Utilities |
| 301 | //===----------------------------------------------------------------------===// |
| 302 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 303 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 304 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 305 | Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { |
| 306 | assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs"); |
| 307 | |
| 308 | if (SrcType->isRealFloatingType()) { |
| 309 | // Compare against 0.0 for fp scalars. |
| 310 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 311 | return Builder.CreateFCmpUNE(Src, Zero, "tobool"); |
| 312 | } |
| 313 | |
| 314 | assert((SrcType->isIntegerType() || SrcType->isPointerType()) && |
| 315 | "Unknown scalar type to convert"); |
| 316 | |
| 317 | // Because of the type rules of C, we often end up computing a logical value, |
| 318 | // then zero extending it to int, then wanting it as a logical value again. |
| 319 | // Optimize this common case. |
| 320 | if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) { |
| 321 | if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) { |
| 322 | Value *Result = ZI->getOperand(0); |
Eli Friedman | 356916e | 2008-01-29 18:13:51 +0000 | [diff] [blame] | 323 | // If there aren't any more uses, zap the instruction to save space. |
| 324 | // Note that there can be more uses, for example if this |
| 325 | // is the result of an assignment. |
| 326 | if (ZI->use_empty()) |
| 327 | ZI->eraseFromParent(); |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 328 | return Result; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // Compare against an integer or pointer null. |
| 333 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 334 | return Builder.CreateICmpNE(Src, Zero, "tobool"); |
| 335 | } |
| 336 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 337 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 338 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 339 | Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, |
| 340 | QualType DstType) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 341 | SrcType = SrcType.getCanonicalType(); |
| 342 | DstType = DstType.getCanonicalType(); |
| 343 | if (SrcType == DstType) return Src; |
Chris Lattner | cf28908 | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 344 | |
| 345 | if (DstType->isVoidType()) return 0; |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 346 | |
| 347 | // Handle conversions to bool first, they are special: comparisons against 0. |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 348 | if (DstType->isBooleanType()) |
| 349 | return EmitConversionToBool(Src, SrcType); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 350 | |
| 351 | const llvm::Type *DstTy = ConvertType(DstType); |
| 352 | |
| 353 | // Ignore conversions like int -> uint. |
| 354 | if (Src->getType() == DstTy) |
| 355 | return Src; |
| 356 | |
| 357 | // Handle pointer conversions next: pointers can only be converted to/from |
| 358 | // other pointers and integers. |
| 359 | if (isa<PointerType>(DstType)) { |
| 360 | // The source value may be an integer, or a pointer. |
| 361 | if (isa<llvm::PointerType>(Src->getType())) |
| 362 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
| 363 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
| 364 | return Builder.CreateIntToPtr(Src, DstTy, "conv"); |
| 365 | } |
| 366 | |
| 367 | if (isa<PointerType>(SrcType)) { |
| 368 | // Must be an ptr to int cast. |
| 369 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
Anders Carlsson | 50b5a30 | 2007-10-31 23:18:02 +0000 | [diff] [blame] | 370 | return Builder.CreatePtrToInt(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 373 | // A scalar source can be splatted to a vector of the same element type |
| 374 | if (isa<llvm::VectorType>(DstTy) && !isa<VectorType>(SrcType)) { |
| 375 | const llvm::VectorType *VT = cast<llvm::VectorType>(DstTy); |
| 376 | assert((VT->getElementType() == Src->getType()) && |
| 377 | "Vector element type must match scalar type to splat."); |
| 378 | return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(), |
| 379 | true); |
| 380 | } |
| 381 | |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 382 | if (isa<llvm::VectorType>(Src->getType()) || |
| 383 | isa<llvm::VectorType>(DstTy)) { |
| 384 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
| 385 | } |
| 386 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 387 | // Finally, we have the arithmetic types: real int/float. |
| 388 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 389 | bool InputSigned = SrcType->isSignedIntegerType(); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 390 | if (isa<llvm::IntegerType>(DstTy)) |
| 391 | return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); |
| 392 | else if (InputSigned) |
| 393 | return Builder.CreateSIToFP(Src, DstTy, "conv"); |
| 394 | else |
| 395 | return Builder.CreateUIToFP(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 399 | if (isa<llvm::IntegerType>(DstTy)) { |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 400 | if (DstType->isSignedIntegerType()) |
| 401 | return Builder.CreateFPToSI(Src, DstTy, "conv"); |
| 402 | else |
| 403 | return Builder.CreateFPToUI(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 407 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 408 | return Builder.CreateFPTrunc(Src, DstTy, "conv"); |
| 409 | else |
| 410 | return Builder.CreateFPExt(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 413 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 414 | /// complex type to the specified destination type, where the destination |
| 415 | /// type is an LLVM scalar type. |
| 416 | Value *ScalarExprEmitter:: |
| 417 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 418 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 419 | // Get the source element type. |
| 420 | SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType(); |
| 421 | |
| 422 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 423 | if (DstTy->isBooleanType()) { |
| 424 | // Complex != 0 -> (Real != 0) | (Imag != 0) |
| 425 | Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 426 | Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); |
| 427 | return Builder.CreateOr(Src.first, Src.second, "tobool"); |
| 428 | } |
| 429 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 430 | // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, |
| 431 | // the imaginary part of the complex value is discarded and the value of the |
| 432 | // real part is converted according to the conversion rules for the |
| 433 | // corresponding real type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 434 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 435 | } |
| 436 | |
| 437 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 438 | //===----------------------------------------------------------------------===// |
| 439 | // Visitor Methods |
| 440 | //===----------------------------------------------------------------------===// |
| 441 | |
| 442 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 443 | CGF.WarnUnsupported(E, "scalar expression"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 444 | if (E->getType()->isVoidType()) |
| 445 | return 0; |
| 446 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
| 447 | } |
| 448 | |
| 449 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 450 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 451 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 452 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 453 | // so we can't get it as an lvalue. |
| 454 | if (!E->getBase()->getType()->isVectorType()) |
| 455 | return EmitLoadOfLValue(E); |
| 456 | |
| 457 | // Handle the vector case. The base must be a vector, the index must be an |
| 458 | // integer value. |
| 459 | Value *Base = Visit(E->getBase()); |
| 460 | Value *Idx = Visit(E->getIdx()); |
| 461 | |
| 462 | // FIXME: Convert Idx to i32 type. |
| 463 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 464 | } |
| 465 | |
| 466 | /// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but |
| 467 | /// also handle things like function to pointer-to-function decay, and array to |
| 468 | /// pointer decay. |
| 469 | Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) { |
| 470 | const Expr *Op = E->getSubExpr(); |
| 471 | |
| 472 | // If this is due to array->pointer conversion, emit the array expression as |
| 473 | // an l-value. |
| 474 | if (Op->getType()->isArrayType()) { |
| 475 | // FIXME: For now we assume that all source arrays map to LLVM arrays. This |
| 476 | // will not true when we add support for VLAs. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 477 | Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 478 | |
| 479 | assert(isa<llvm::PointerType>(V->getType()) && |
| 480 | isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 481 | ->getElementType()) && |
| 482 | "Doesn't support VLAs yet!"); |
| 483 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
Ted Kremenek | d627889 | 2007-09-04 17:20:08 +0000 | [diff] [blame] | 484 | |
| 485 | llvm::Value *Ops[] = {Idx0, Idx0}; |
Chris Lattner | a9e6372 | 2007-12-12 04:13:20 +0000 | [diff] [blame] | 486 | V = Builder.CreateGEP(V, Ops, Ops+2, "arraydecay"); |
| 487 | |
| 488 | // The resultant pointer type can be implicitly casted to other pointer |
| 489 | // types as well, for example void*. |
| 490 | const llvm::Type *DestPTy = ConvertType(E->getType()); |
| 491 | assert(isa<llvm::PointerType>(DestPTy) && |
| 492 | "Only expect implicit cast to pointer"); |
| 493 | if (V->getType() != DestPTy) |
| 494 | V = Builder.CreateBitCast(V, DestPTy, "ptrconv"); |
| 495 | return V; |
| 496 | |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 497 | } else if (E->getType()->isReferenceType()) { |
Anders Carlsson | 23af9f2 | 2007-10-13 05:52:34 +0000 | [diff] [blame] | 498 | assert(cast<ReferenceType>(E->getType().getCanonicalType())-> |
| 499 | getReferenceeType() == |
| 500 | Op->getType().getCanonicalType() && "Incompatible types!"); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 501 | |
| 502 | return EmitLValue(Op).getAddress(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | return EmitCastExpr(Op, E->getType()); |
| 506 | } |
| 507 | |
| 508 | |
| 509 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 510 | // have to handle a more broad range of conversions than explicit casts, as they |
| 511 | // handle things like function to ptr-to-function decay etc. |
| 512 | Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) { |
Chris Lattner | 58a2e94 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 513 | // Handle cases where the source is an non-complex type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 514 | if (!E->getType()->isComplexType()) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 515 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 516 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 517 | // Use EmitScalarConversion to perform the conversion. |
| 518 | return EmitScalarConversion(Src, E->getType(), DestTy); |
| 519 | } |
Chris Lattner | 10b00cf | 2007-08-26 07:16:41 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 58a2e94 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 521 | // Handle cases where the source is a complex type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 522 | return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(), |
| 523 | DestTy); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 526 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 527 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal(); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 531 | //===----------------------------------------------------------------------===// |
| 532 | // Unary Operators |
| 533 | //===----------------------------------------------------------------------===// |
| 534 | |
| 535 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | dfce2a5 | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 536 | bool isInc, bool isPre) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 537 | LValue LV = EmitLValue(E->getSubExpr()); |
| 538 | // FIXME: Handle volatile! |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 539 | Value *InVal = CGF.EmitLoadOfLValue(LV, // false |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 540 | E->getSubExpr()->getType()).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 541 | |
| 542 | int AmountVal = isInc ? 1 : -1; |
| 543 | |
| 544 | Value *NextVal; |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 545 | if (isa<llvm::PointerType>(InVal->getType())) { |
| 546 | // FIXME: This isn't right for VLAs. |
| 547 | NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal); |
| 548 | NextVal = Builder.CreateGEP(InVal, NextVal); |
| 549 | } else { |
| 550 | // Add the inc/dec to the real part. |
| 551 | if (isa<llvm::IntegerType>(InVal->getType())) |
| 552 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 553 | else if (InVal->getType() == llvm::Type::FloatTy) |
| 554 | // FIXME: Handle long double. |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 555 | NextVal = |
| 556 | llvm::ConstantFP::get(InVal->getType(), |
| 557 | llvm::APFloat(static_cast<float>(AmountVal))); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 558 | else { |
| 559 | // FIXME: Handle long double. |
| 560 | assert(InVal->getType() == llvm::Type::DoubleTy); |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 561 | NextVal = |
| 562 | llvm::ConstantFP::get(InVal->getType(), |
| 563 | llvm::APFloat(static_cast<double>(AmountVal))); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 564 | } |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 565 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 566 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 567 | |
| 568 | // Store the updated result through the lvalue. |
| 569 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, |
| 570 | E->getSubExpr()->getType()); |
| 571 | |
| 572 | // If this is a postinc, return the value read from memory, otherwise use the |
| 573 | // updated value. |
| 574 | return isPre ? NextVal : InVal; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 579 | Value *Op = Visit(E->getSubExpr()); |
| 580 | return Builder.CreateNeg(Op, "neg"); |
| 581 | } |
| 582 | |
| 583 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 584 | Value *Op = Visit(E->getSubExpr()); |
| 585 | return Builder.CreateNot(Op, "neg"); |
| 586 | } |
| 587 | |
| 588 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 589 | // Compare operand to zero. |
| 590 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
| 591 | |
| 592 | // Invert value. |
| 593 | // TODO: Could dynamically modify easy computations here. For example, if |
| 594 | // the operand is an icmp ne, turn into icmp eq. |
| 595 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 596 | |
| 597 | // ZExt result to int. |
| 598 | return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext"); |
| 599 | } |
| 600 | |
| 601 | /// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as |
| 602 | /// an integer (RetType). |
| 603 | Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize, |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 604 | QualType RetType,bool isSizeOf){ |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 605 | /// FIXME: This doesn't handle VLAs yet! |
| 606 | std::pair<uint64_t, unsigned> Info = |
| 607 | CGF.getContext().getTypeInfo(TypeToSize, SourceLocation()); |
| 608 | |
| 609 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 610 | Val /= 8; // Return size in bytes, not bits. |
| 611 | |
| 612 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 613 | |
Hartmut Kaiser | 7b66000 | 2007-10-17 15:00:17 +0000 | [diff] [blame] | 614 | uint32_t ResultWidth = static_cast<uint32_t>( |
| 615 | CGF.getContext().getTypeSize(RetType, SourceLocation())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 616 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 617 | } |
| 618 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 619 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 620 | Expr *Op = E->getSubExpr(); |
| 621 | if (Op->getType()->isComplexType()) |
| 622 | return CGF.EmitComplexExpr(Op).first; |
| 623 | return Visit(Op); |
| 624 | } |
| 625 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 626 | Expr *Op = E->getSubExpr(); |
| 627 | if (Op->getType()->isComplexType()) |
| 628 | return CGF.EmitComplexExpr(Op).second; |
Chris Lattner | 36f8406 | 2007-08-26 05:29:21 +0000 | [diff] [blame] | 629 | |
| 630 | // __imag on a scalar returns zero. Emit it the subexpr to ensure side |
| 631 | // effects are evaluated. |
| 632 | CGF.EmitScalarExpr(Op); |
| 633 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 636 | Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) |
| 637 | { |
| 638 | int64_t Val = E->evaluateOffsetOf(CGF.getContext()); |
| 639 | |
| 640 | assert(E->getType()->isIntegerType() && "Result type must be an integer!"); |
| 641 | |
| 642 | uint32_t ResultWidth = static_cast<uint32_t>( |
| 643 | CGF.getContext().getTypeSize(E->getType(), SourceLocation())); |
| 644 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 645 | } |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 646 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 647 | //===----------------------------------------------------------------------===// |
| 648 | // Binary Operators |
| 649 | //===----------------------------------------------------------------------===// |
| 650 | |
| 651 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
| 652 | BinOpInfo Result; |
| 653 | Result.LHS = Visit(E->getLHS()); |
| 654 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 655 | Result.Ty = E->getType(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 656 | Result.E = E; |
| 657 | return Result; |
| 658 | } |
| 659 | |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 660 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 661 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
| 662 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 663 | |
| 664 | BinOpInfo OpInfo; |
| 665 | |
| 666 | // Load the LHS and RHS operands. |
| 667 | LValue LHSLV = EmitLValue(E->getLHS()); |
| 668 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Chris Lattner | 04dc764 | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 669 | |
| 670 | // Determine the computation type. If the RHS is complex, then this is one of |
| 671 | // the add/sub/mul/div operators. All of these operators can be computed in |
| 672 | // with just their real component even though the computation domain really is |
| 673 | // complex. |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 674 | QualType ComputeType = E->getComputationType(); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 04dc764 | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 676 | // If the computation type is complex, then the RHS is complex. Emit the RHS. |
| 677 | if (const ComplexType *CT = ComputeType->getAsComplexType()) { |
| 678 | ComputeType = CT->getElementType(); |
| 679 | |
| 680 | // Emit the RHS, only keeping the real component. |
| 681 | OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first; |
| 682 | RHSTy = RHSTy->getAsComplexType()->getElementType(); |
| 683 | } else { |
| 684 | // Otherwise the RHS is a simple scalar value. |
| 685 | OpInfo.RHS = Visit(E->getRHS()); |
| 686 | } |
| 687 | |
| 688 | // Convert the LHS/RHS values to the computation type. |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 689 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 690 | |
Devang Patel | f86206f | 2007-10-25 22:19:13 +0000 | [diff] [blame] | 691 | // Do not merge types for -= or += where the LHS is a pointer. |
| 692 | if (!(E->getOpcode() == BinaryOperator::SubAssign || |
Devang Patel | 03f7c03 | 2007-10-30 18:31:12 +0000 | [diff] [blame] | 693 | E->getOpcode() == BinaryOperator::AddAssign) || |
Chris Lattner | 3b44b57 | 2007-08-25 21:56:20 +0000 | [diff] [blame] | 694 | !E->getLHS()->getType()->isPointerType()) { |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 695 | OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 696 | } |
| 697 | OpInfo.Ty = ComputeType; |
| 698 | OpInfo.E = E; |
| 699 | |
| 700 | // Expand the binary operator. |
| 701 | Value *Result = (this->*Func)(OpInfo); |
| 702 | |
| 703 | // Truncate the result back to the LHS type. |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 704 | Result = EmitScalarConversion(Result, ComputeType, LHSTy); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 705 | |
| 706 | // Store the result value into the LHS lvalue. |
| 707 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType()); |
| 708 | |
| 709 | return Result; |
| 710 | } |
| 711 | |
| 712 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 713 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
Nate Begeman | b3ab8dc | 2007-12-30 01:28:16 +0000 | [diff] [blame] | 714 | if (Ops.LHS->getType()->isFPOrFPVector()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 715 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 716 | else if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 717 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 718 | else |
| 719 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 720 | } |
| 721 | |
| 722 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 723 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 724 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 725 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 726 | else |
| 727 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 728 | } |
| 729 | |
| 730 | |
| 731 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 732 | if (!Ops.Ty->isPointerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 733 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 734 | |
| 735 | // FIXME: What about a pointer to a VLA? |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 736 | Value *Ptr, *Idx; |
| 737 | Expr *IdxExp; |
| 738 | if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int |
| 739 | Ptr = Ops.LHS; |
| 740 | Idx = Ops.RHS; |
| 741 | IdxExp = Ops.E->getRHS(); |
| 742 | } else { // int + pointer |
| 743 | Ptr = Ops.RHS; |
| 744 | Idx = Ops.LHS; |
| 745 | IdxExp = Ops.E->getLHS(); |
| 746 | } |
| 747 | |
| 748 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 749 | if (Width < CGF.LLVMPointerWidth) { |
| 750 | // Zero or sign extend the pointer value based on whether the index is |
| 751 | // signed or not. |
| 752 | const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 753 | if (IdxExp->getType().getCanonicalType()->isSignedIntegerType()) |
| 754 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 755 | else |
| 756 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 757 | } |
| 758 | |
| 759 | return Builder.CreateGEP(Ptr, Idx, "add.ptr"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
| 763 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) |
| 764 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
| 765 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 766 | // pointer - int |
| 767 | assert(!isa<llvm::PointerType>(Ops.RHS->getType()) && |
| 768 | "ptr-ptr shouldn't get here"); |
| 769 | // FIXME: The pointer could point to a VLA. |
Chris Lattner | 6860f3c | 2008-01-31 04:12:50 +0000 | [diff] [blame] | 770 | Value *Idx = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg"); |
| 771 | |
| 772 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 773 | if (Width < CGF.LLVMPointerWidth) { |
| 774 | // Zero or sign extend the pointer value based on whether the index is |
| 775 | // signed or not. |
| 776 | const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 777 | if (Ops.E->getRHS()->getType().getCanonicalType()->isSignedIntegerType()) |
| 778 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 779 | else |
| 780 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 781 | } |
| 782 | |
| 783 | return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) { |
| 787 | // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In |
| 788 | // the compound assignment case it is invalid, so just handle it here. |
| 789 | if (!E->getRHS()->getType()->isPointerType()) |
| 790 | return EmitSub(EmitBinOps(E)); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 791 | |
| 792 | // pointer - pointer |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 793 | Value *LHS = Visit(E->getLHS()); |
| 794 | Value *RHS = Visit(E->getRHS()); |
| 795 | |
Seo Sanghyeon | ec86b97 | 2007-12-03 06:23:43 +0000 | [diff] [blame] | 796 | const QualType LHSType = E->getLHS()->getType().getCanonicalType(); |
Seo Sanghyeon | 9bb947a | 2007-12-26 05:21:37 +0000 | [diff] [blame] | 797 | const QualType LHSElementType = cast<PointerType>(LHSType)->getPointeeType(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 798 | uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType, |
| 799 | SourceLocation()) / 8; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 800 | |
| 801 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 802 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 803 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 804 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 805 | |
| 806 | // HACK: LLVM doesn't have an divide instruction that 'knows' there is no |
| 807 | // remainder. As such, we handle common power-of-two cases here to generate |
| 808 | // better code. |
| 809 | if (llvm::isPowerOf2_64(ElementSize)) { |
| 810 | Value *ShAmt = |
| 811 | llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize)); |
| 812 | return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr"); |
| 813 | } |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 814 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 815 | // Otherwise, do a full sdiv. |
| 816 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
| 817 | return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
| 818 | } |
| 819 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 821 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 822 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 823 | // RHS to the same size as the LHS. |
| 824 | Value *RHS = Ops.RHS; |
| 825 | if (Ops.LHS->getType() != RHS->getType()) |
| 826 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 827 | |
| 828 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 829 | } |
| 830 | |
| 831 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 832 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 833 | // RHS to the same size as the LHS. |
| 834 | Value *RHS = Ops.RHS; |
| 835 | if (Ops.LHS->getType() != RHS->getType()) |
| 836 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 837 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 838 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 839 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 840 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 841 | } |
| 842 | |
| 843 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 844 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 845 | Value *Result; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 846 | QualType LHSTy = E->getLHS()->getType(); |
| 847 | if (!LHSTy->isComplexType()) { |
| 848 | Value *LHS = Visit(E->getLHS()); |
| 849 | Value *RHS = Visit(E->getRHS()); |
| 850 | |
| 851 | if (LHS->getType()->isFloatingPoint()) { |
| 852 | Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 853 | LHS, RHS, "cmp"); |
| 854 | } else if (LHSTy->isUnsignedIntegerType()) { |
| 855 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 856 | LHS, RHS, "cmp"); |
| 857 | } else { |
| 858 | // Signed integers and pointers. |
| 859 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
| 860 | LHS, RHS, "cmp"); |
| 861 | } |
| 862 | } else { |
| 863 | // Complex Comparison: can only be an equality comparison. |
| 864 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 865 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
| 866 | |
| 867 | QualType CETy = |
| 868 | cast<ComplexType>(LHSTy.getCanonicalType())->getElementType(); |
| 869 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 870 | Value *ResultR, *ResultI; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 871 | if (CETy->isRealFloatingType()) { |
| 872 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 873 | LHS.first, RHS.first, "cmp.r"); |
| 874 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 875 | LHS.second, RHS.second, "cmp.i"); |
| 876 | } else { |
| 877 | // Complex comparisons can only be equality comparisons. As such, signed |
| 878 | // and unsigned opcodes are the same. |
| 879 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 880 | LHS.first, RHS.first, "cmp.r"); |
| 881 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 882 | LHS.second, RHS.second, "cmp.i"); |
| 883 | } |
| 884 | |
| 885 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 886 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 887 | } else { |
| 888 | assert(E->getOpcode() == BinaryOperator::NE && |
| 889 | "Complex comparison other than == or != ?"); |
| 890 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | // ZExt result to int. |
| 895 | return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext"); |
| 896 | } |
| 897 | |
| 898 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 899 | LValue LHS = EmitLValue(E->getLHS()); |
| 900 | Value *RHS = Visit(E->getRHS()); |
| 901 | |
| 902 | // Store the value into the LHS. |
| 903 | // FIXME: Volatility! |
| 904 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
| 905 | |
| 906 | // Return the RHS. |
| 907 | return RHS; |
| 908 | } |
| 909 | |
| 910 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
| 911 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 912 | |
| 913 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont"); |
| 914 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs"); |
| 915 | |
| 916 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 917 | Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock); |
| 918 | |
| 919 | CGF.EmitBlock(RHSBlock); |
| 920 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 921 | |
| 922 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 923 | RHSBlock = Builder.GetInsertBlock(); |
| 924 | CGF.EmitBlock(ContBlock); |
| 925 | |
| 926 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 927 | // false. If we evaluated both, the result is the RHS condition. |
| 928 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land"); |
| 929 | PN->reserveOperandSpace(2); |
| 930 | PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock); |
| 931 | PN->addIncoming(RHSCond, RHSBlock); |
| 932 | |
| 933 | // ZExt result to int. |
| 934 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext"); |
| 935 | } |
| 936 | |
| 937 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
| 938 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 939 | |
| 940 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont"); |
| 941 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs"); |
| 942 | |
| 943 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 944 | Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock); |
| 945 | |
| 946 | CGF.EmitBlock(RHSBlock); |
| 947 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 948 | |
| 949 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 950 | RHSBlock = Builder.GetInsertBlock(); |
| 951 | CGF.EmitBlock(ContBlock); |
| 952 | |
| 953 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 954 | // true. If we evaluated both, the result is the RHS condition. |
| 955 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor"); |
| 956 | PN->reserveOperandSpace(2); |
| 957 | PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock); |
| 958 | PN->addIncoming(RHSCond, RHSBlock); |
| 959 | |
| 960 | // ZExt result to int. |
| 961 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext"); |
| 962 | } |
| 963 | |
| 964 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 965 | CGF.EmitStmt(E->getLHS()); |
| 966 | return Visit(E->getRHS()); |
| 967 | } |
| 968 | |
| 969 | //===----------------------------------------------------------------------===// |
| 970 | // Other Operators |
| 971 | //===----------------------------------------------------------------------===// |
| 972 | |
| 973 | Value *ScalarExprEmitter:: |
| 974 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 975 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 976 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 977 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 978 | |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 979 | // Evaluate the conditional, then convert it to bool. We do this explicitly |
| 980 | // because we need the unconverted value if this is a GNU ?: expression with |
| 981 | // missing middle value. |
| 982 | Value *CondVal = CGF.EmitScalarExpr(E->getCond()); |
Chris Lattner | c89bf69 | 2008-01-03 07:05:49 +0000 | [diff] [blame] | 983 | Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), |
| 984 | CGF.getContext().BoolTy); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 985 | Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 986 | |
| 987 | CGF.EmitBlock(LHSBlock); |
| 988 | |
| 989 | // Handle the GNU extension for missing LHS. |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 990 | Value *LHS; |
| 991 | if (E->getLHS()) |
| 992 | LHS = Visit(E->getLHS()); |
| 993 | else // Perform promotions, to handle cases like "short ?: int" |
| 994 | LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType()); |
| 995 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 996 | Builder.CreateBr(ContBlock); |
| 997 | LHSBlock = Builder.GetInsertBlock(); |
| 998 | |
| 999 | CGF.EmitBlock(RHSBlock); |
| 1000 | |
| 1001 | Value *RHS = Visit(E->getRHS()); |
| 1002 | Builder.CreateBr(ContBlock); |
| 1003 | RHSBlock = Builder.GetInsertBlock(); |
| 1004 | |
| 1005 | CGF.EmitBlock(ContBlock); |
| 1006 | |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1007 | if (!LHS) { |
| 1008 | assert(E->getType()->isVoidType() && "Non-void value should have a value"); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1012 | // Create a PHI node for the real part. |
| 1013 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 1014 | PN->reserveOperandSpace(2); |
| 1015 | PN->addIncoming(LHS, LHSBlock); |
| 1016 | PN->addIncoming(RHS, RHSBlock); |
| 1017 | return PN; |
| 1018 | } |
| 1019 | |
| 1020 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1021 | // Emit the LHS or RHS as appropriate. |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 1022 | return |
| 1023 | Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1026 | Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) { |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 1027 | return CGF.EmitCallExpr(E->getFn(), E->arg_begin(), |
| 1028 | E->getNumArgs(CGF.getContext())).getScalarVal(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1031 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1032 | llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress(); |
| 1033 | |
| 1034 | llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 1035 | return V; |
| 1036 | } |
| 1037 | |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1038 | Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1039 | std::string str; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1040 | llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; |
| 1041 | CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str, |
| 1042 | EncodingRecordTypes); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1043 | |
| 1044 | llvm::Constant *C = llvm::ConstantArray::get(str); |
| 1045 | C = new llvm::GlobalVariable(C->getType(), true, |
| 1046 | llvm::GlobalValue::InternalLinkage, |
| 1047 | C, ".str", &CGF.CGM.getModule()); |
| 1048 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 1049 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 1050 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 1051 | |
| 1052 | return C; |
| 1053 | } |
| 1054 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1055 | //===----------------------------------------------------------------------===// |
| 1056 | // Entry Point into this File |
| 1057 | //===----------------------------------------------------------------------===// |
| 1058 | |
| 1059 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 1060 | /// complex type, ignoring the result. |
| 1061 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E) { |
| 1062 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 1063 | "Invalid scalar expression to emit"); |
| 1064 | |
| 1065 | return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
| 1066 | } |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1067 | |
| 1068 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 1069 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1070 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 1071 | QualType DstTy) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1072 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 1073 | "Invalid scalar expression to emit"); |
| 1074 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 1075 | } |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1076 | |
| 1077 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 1078 | /// complex type to the specified destination type, where the destination |
| 1079 | /// type is an LLVM scalar type. |
| 1080 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 1081 | QualType SrcTy, |
| 1082 | QualType DstTy) { |
| 1083 | assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) && |
| 1084 | "Invalid complex -> scalar conversion"); |
| 1085 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 1086 | DstTy); |
| 1087 | } |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1088 | |
| 1089 | Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) { |
| 1090 | assert(V1->getType() == V2->getType() && |
| 1091 | "Vector operands must be of the same type"); |
| 1092 | |
| 1093 | unsigned NumElements = |
| 1094 | cast<llvm::VectorType>(V1->getType())->getNumElements(); |
| 1095 | |
| 1096 | va_list va; |
| 1097 | va_start(va, V2); |
| 1098 | |
| 1099 | llvm::SmallVector<llvm::Constant*, 16> Args; |
| 1100 | |
| 1101 | for (unsigned i = 0; i < NumElements; i++) { |
| 1102 | int n = va_arg(va, int); |
| 1103 | |
| 1104 | assert(n >= 0 && n < (int)NumElements * 2 && |
| 1105 | "Vector shuffle index out of bounds!"); |
| 1106 | |
| 1107 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n)); |
| 1108 | } |
| 1109 | |
| 1110 | const char *Name = va_arg(va, const char *); |
| 1111 | va_end(va); |
| 1112 | |
| 1113 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
| 1114 | |
| 1115 | return Builder.CreateShuffleVector(V1, V2, Mask, Name); |
| 1116 | } |
| 1117 | |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1118 | llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals, |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1119 | unsigned NumVals, bool isSplat) |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1120 | { |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1121 | llvm::Value *Vec |
| 1122 | = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals)); |
| 1123 | |
| 1124 | for (unsigned i = 0, e = NumVals ; i != e; ++i) { |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1125 | llvm::Value *Val = isSplat ? Vals[0] : Vals[i]; |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1126 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1127 | Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp"); |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | return Vec; |
| 1131 | } |