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