blob: d23473a64c4084e1390960c47103078862dbbf76 [file] [log] [blame]
Chris Lattner9fba49a2007-08-24 05:35:26 +00001//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
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 Carlsson36760332007-10-15 20:28:48 +000019#include "llvm/Intrinsics.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000020#include "llvm/Support/Compiler.h"
21using namespace clang;
22using namespace CodeGen;
23using llvm::Value;
24
25//===----------------------------------------------------------------------===//
26// Scalar Expression Emitter
27//===----------------------------------------------------------------------===//
28
29struct BinOpInfo {
30 Value *LHS;
31 Value *RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +000032 QualType Ty; // Computation Type.
Chris Lattner9fba49a2007-08-24 05:35:26 +000033 const BinaryOperator *E;
34};
35
36namespace {
37class VISIBILITY_HIDDEN ScalarExprEmitter
38 : public StmtVisitor<ScalarExprEmitter, Value*> {
39 CodeGenFunction &CGF;
Devang Patel638b64c2007-10-09 19:49:58 +000040 llvm::LLVMFoldingBuilder &Builder;
Chris Lattner9fba49a2007-08-24 05:35:26 +000041public:
42
43 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) {
44 }
45
46
47 //===--------------------------------------------------------------------===//
48 // Utilities
49 //===--------------------------------------------------------------------===//
50
51 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
52 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
53
54 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +000055 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +000056 }
57
58 /// EmitLoadOfLValue - Given an expression with complex type that represents a
59 /// value l-value, this method emits the address of the l-value, then loads
60 /// and returns the result.
61 Value *EmitLoadOfLValue(const Expr *E) {
62 // FIXME: Volatile
63 return EmitLoadOfLValue(EmitLValue(E), E->getType());
64 }
65
Chris Lattnerd8d44222007-08-26 16:42:57 +000066 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +000067 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +000068 Value *EmitConversionToBool(Value *Src, QualType DstTy);
69
Chris Lattner4e05d1e2007-08-26 06:48:56 +000070 /// EmitScalarConversion - Emit a conversion from the specified type to the
71 /// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +000072 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
73
74 /// EmitComplexToScalarConversion - Emit a conversion from the specified
75 /// complex type to the specified destination type, where the destination
76 /// type is an LLVM scalar type.
77 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
78 QualType SrcTy, QualType DstTy);
Chris Lattner4e05d1e2007-08-26 06:48:56 +000079
Chris Lattner9fba49a2007-08-24 05:35:26 +000080 //===--------------------------------------------------------------------===//
81 // Visitor Methods
82 //===--------------------------------------------------------------------===//
83
84 Value *VisitStmt(Stmt *S) {
Chris Lattner1aef6212007-09-13 01:17:29 +000085 S->dump(CGF.getContext().SourceMgr);
Chris Lattner9fba49a2007-08-24 05:35:26 +000086 assert(0 && "Stmt can't have complex result type!");
87 return 0;
88 }
89 Value *VisitExpr(Expr *S);
90 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
91
92 // Leaves.
93 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
94 return llvm::ConstantInt::get(E->getValue());
95 }
96 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner7f298762007-09-22 18:47:25 +000097 return llvm::ConstantFP::get(ConvertType(E->getType()), E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +000098 }
99 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
100 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
101 }
102 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
103 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroff85f0dc52007-10-15 20:41:53 +0000104 CGF.getContext().typesAreCompatible(
105 E->getArgType1(), E->getArgType2()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000106 }
107 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
108 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
109 }
110
111 // l-values.
112 Value *VisitDeclRefExpr(DeclRefExpr *E) {
113 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
114 return llvm::ConstantInt::get(EC->getInitVal());
115 return EmitLoadOfLValue(E);
116 }
117 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
118 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
119 Value *VisitOCUVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
120 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
121 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel01ab1302007-10-24 17:18:43 +0000122
123 Value *VisitInitListExpr(InitListExpr *E) {
124 std::vector<llvm::Constant *> Elements;
125 unsigned N = E->getNumInits();
126 for (unsigned i = 0; i < N; ++i) {
127 Value *V = Visit(E->getInit(i));
128 llvm::Constant * C = cast<llvm::Constant>(V);
129 Elements.push_back(C);
130 }
131 return llvm::ConstantVector::get(Elements);
132 }
133
134 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
135 return Visit(E->getInitializer());
136 }
137
Chris Lattner9fba49a2007-08-24 05:35:26 +0000138 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
139 Value *VisitCastExpr(const CastExpr *E) {
140 return EmitCastExpr(E->getSubExpr(), E->getType());
141 }
142 Value *EmitCastExpr(const Expr *E, QualType T);
143
144 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000145 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000146 }
147
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000148 Value *VisitStmtExpr(const StmtExpr *E);
149
Chris Lattner9fba49a2007-08-24 05:35:26 +0000150 // Unary Operators.
151 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
152 Value *VisitUnaryPostDec(const UnaryOperator *E) {
153 return VisitPrePostIncDec(E, false, false);
154 }
155 Value *VisitUnaryPostInc(const UnaryOperator *E) {
156 return VisitPrePostIncDec(E, true, false);
157 }
158 Value *VisitUnaryPreDec(const UnaryOperator *E) {
159 return VisitPrePostIncDec(E, false, true);
160 }
161 Value *VisitUnaryPreInc(const UnaryOperator *E) {
162 return VisitPrePostIncDec(E, true, true);
163 }
164 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
165 return EmitLValue(E->getSubExpr()).getAddress();
166 }
167 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
168 Value *VisitUnaryPlus(const UnaryOperator *E) {
169 return Visit(E->getSubExpr());
170 }
171 Value *VisitUnaryMinus (const UnaryOperator *E);
172 Value *VisitUnaryNot (const UnaryOperator *E);
173 Value *VisitUnaryLNot (const UnaryOperator *E);
174 Value *VisitUnarySizeOf (const UnaryOperator *E) {
175 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
176 }
177 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
178 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
179 }
180 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
181 bool isSizeOf);
Chris Lattner01211af2007-08-24 21:20:17 +0000182 Value *VisitUnaryReal (const UnaryOperator *E);
183 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000184 Value *VisitUnaryExtension(const UnaryOperator *E) {
185 return Visit(E->getSubExpr());
186 }
187
188 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000189 Value *EmitMul(const BinOpInfo &Ops) {
190 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
191 }
192 Value *EmitDiv(const BinOpInfo &Ops);
193 Value *EmitRem(const BinOpInfo &Ops);
194 Value *EmitAdd(const BinOpInfo &Ops);
195 Value *EmitSub(const BinOpInfo &Ops);
196 Value *EmitShl(const BinOpInfo &Ops);
197 Value *EmitShr(const BinOpInfo &Ops);
198 Value *EmitAnd(const BinOpInfo &Ops) {
199 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
200 }
201 Value *EmitXor(const BinOpInfo &Ops) {
202 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
203 }
204 Value *EmitOr (const BinOpInfo &Ops) {
205 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
206 }
207
Chris Lattner660e31d2007-08-24 21:00:35 +0000208 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000209 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000210 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
211
212 // Binary operators and binary compound assignment operators.
213#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000214 Value *VisitBin ## OP(const BinaryOperator *E) { \
215 return Emit ## OP(EmitBinOps(E)); \
216 } \
217 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
218 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000219 }
220 HANDLEBINOP(Mul);
221 HANDLEBINOP(Div);
222 HANDLEBINOP(Rem);
223 HANDLEBINOP(Add);
224 // (Sub) - Sub is handled specially below for ptr-ptr subtract.
225 HANDLEBINOP(Shl);
226 HANDLEBINOP(Shr);
227 HANDLEBINOP(And);
228 HANDLEBINOP(Xor);
229 HANDLEBINOP(Or);
230#undef HANDLEBINOP
231 Value *VisitBinSub(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000232 Value *VisitBinSubAssign(const CompoundAssignOperator *E) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000233 return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub);
234 }
235
Chris Lattner9fba49a2007-08-24 05:35:26 +0000236 // Comparisons.
237 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
238 unsigned SICmpOpc, unsigned FCmpOpc);
239#define VISITCOMP(CODE, UI, SI, FP) \
240 Value *VisitBin##CODE(const BinaryOperator *E) { \
241 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
242 llvm::FCmpInst::FP); }
243 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
244 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
245 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
246 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
247 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
248 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
249#undef VISITCOMP
250
251 Value *VisitBinAssign (const BinaryOperator *E);
252
253 Value *VisitBinLAnd (const BinaryOperator *E);
254 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000255 Value *VisitBinComma (const BinaryOperator *E);
256
257 // Other Operators.
258 Value *VisitConditionalOperator(const ConditionalOperator *CO);
259 Value *VisitChooseExpr(ChooseExpr *CE);
Anders Carlsson36760332007-10-15 20:28:48 +0000260 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000261 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
262 return CGF.EmitObjCStringLiteral(E);
263 }
264};
265} // end anonymous namespace.
266
267//===----------------------------------------------------------------------===//
268// Utilities
269//===----------------------------------------------------------------------===//
270
Chris Lattnerd8d44222007-08-26 16:42:57 +0000271/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000272/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000273Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
274 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
275
276 if (SrcType->isRealFloatingType()) {
277 // Compare against 0.0 for fp scalars.
278 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000279 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
280 }
281
282 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
283 "Unknown scalar type to convert");
284
285 // Because of the type rules of C, we often end up computing a logical value,
286 // then zero extending it to int, then wanting it as a logical value again.
287 // Optimize this common case.
288 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
289 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
290 Value *Result = ZI->getOperand(0);
291 ZI->eraseFromParent();
292 return Result;
293 }
294 }
295
296 // Compare against an integer or pointer null.
297 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
298 return Builder.CreateICmpNE(Src, Zero, "tobool");
299}
300
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000301/// EmitScalarConversion - Emit a conversion from the specified type to the
302/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000303Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
304 QualType DstType) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000305 SrcType = SrcType.getCanonicalType();
306 DstType = DstType.getCanonicalType();
307 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000308
309 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000310
311 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000312 if (DstType->isBooleanType())
313 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000314
315 const llvm::Type *DstTy = ConvertType(DstType);
316
317 // Ignore conversions like int -> uint.
318 if (Src->getType() == DstTy)
319 return Src;
320
321 // Handle pointer conversions next: pointers can only be converted to/from
322 // other pointers and integers.
323 if (isa<PointerType>(DstType)) {
324 // The source value may be an integer, or a pointer.
325 if (isa<llvm::PointerType>(Src->getType()))
326 return Builder.CreateBitCast(Src, DstTy, "conv");
327 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
328 return Builder.CreateIntToPtr(Src, DstTy, "conv");
329 }
330
331 if (isa<PointerType>(SrcType)) {
332 // Must be an ptr to int cast.
333 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
334 return Builder.CreateIntToPtr(Src, DstTy, "conv");
335 }
336
337 // Finally, we have the arithmetic types: real int/float.
338 if (isa<llvm::IntegerType>(Src->getType())) {
339 bool InputSigned = SrcType->isSignedIntegerType();
340 if (isa<llvm::IntegerType>(DstTy))
341 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
342 else if (InputSigned)
343 return Builder.CreateSIToFP(Src, DstTy, "conv");
344 else
345 return Builder.CreateUIToFP(Src, DstTy, "conv");
346 }
347
348 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
349 if (isa<llvm::IntegerType>(DstTy)) {
350 if (DstType->isSignedIntegerType())
351 return Builder.CreateFPToSI(Src, DstTy, "conv");
352 else
353 return Builder.CreateFPToUI(Src, DstTy, "conv");
354 }
355
356 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
357 if (DstTy->getTypeID() < Src->getType()->getTypeID())
358 return Builder.CreateFPTrunc(Src, DstTy, "conv");
359 else
360 return Builder.CreateFPExt(Src, DstTy, "conv");
361}
362
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000363/// EmitComplexToScalarConversion - Emit a conversion from the specified
364/// complex type to the specified destination type, where the destination
365/// type is an LLVM scalar type.
366Value *ScalarExprEmitter::
367EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
368 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000369 // Get the source element type.
370 SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType();
371
372 // Handle conversions to bool first, they are special: comparisons against 0.
373 if (DstTy->isBooleanType()) {
374 // Complex != 0 -> (Real != 0) | (Imag != 0)
375 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
376 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
377 return Builder.CreateOr(Src.first, Src.second, "tobool");
378 }
379
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000380 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
381 // the imaginary part of the complex value is discarded and the value of the
382 // real part is converted according to the conversion rules for the
383 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000384 return EmitScalarConversion(Src.first, SrcTy, DstTy);
385}
386
387
Chris Lattner9fba49a2007-08-24 05:35:26 +0000388//===----------------------------------------------------------------------===//
389// Visitor Methods
390//===----------------------------------------------------------------------===//
391
392Value *ScalarExprEmitter::VisitExpr(Expr *E) {
393 fprintf(stderr, "Unimplemented scalar expr!\n");
Chris Lattner1aef6212007-09-13 01:17:29 +0000394 E->dump(CGF.getContext().SourceMgr);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000395 if (E->getType()->isVoidType())
396 return 0;
397 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
398}
399
400Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
401 // Emit subscript expressions in rvalue context's. For most cases, this just
402 // loads the lvalue formed by the subscript expr. However, we have to be
403 // careful, because the base of a vector subscript is occasionally an rvalue,
404 // so we can't get it as an lvalue.
405 if (!E->getBase()->getType()->isVectorType())
406 return EmitLoadOfLValue(E);
407
408 // Handle the vector case. The base must be a vector, the index must be an
409 // integer value.
410 Value *Base = Visit(E->getBase());
411 Value *Idx = Visit(E->getIdx());
412
413 // FIXME: Convert Idx to i32 type.
414 return Builder.CreateExtractElement(Base, Idx, "vecext");
415}
416
417/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
418/// also handle things like function to pointer-to-function decay, and array to
419/// pointer decay.
420Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
421 const Expr *Op = E->getSubExpr();
422
423 // If this is due to array->pointer conversion, emit the array expression as
424 // an l-value.
425 if (Op->getType()->isArrayType()) {
426 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
427 // will not true when we add support for VLAs.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000428 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000429
430 assert(isa<llvm::PointerType>(V->getType()) &&
431 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
432 ->getElementType()) &&
433 "Doesn't support VLAs yet!");
434 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Ted Kremenek7f6f4a42007-09-04 17:20:08 +0000435
436 llvm::Value *Ops[] = {Idx0, Idx0};
437 return Builder.CreateGEP(V, Ops, Ops+2, "arraydecay");
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000438 } else if (E->getType()->isReferenceType()) {
Anders Carlsson88842452007-10-13 05:52:34 +0000439 assert(cast<ReferenceType>(E->getType().getCanonicalType())->
440 getReferenceeType() ==
441 Op->getType().getCanonicalType() && "Incompatible types!");
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000442
443 return EmitLValue(Op).getAddress();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000444 }
445
446 return EmitCastExpr(Op, E->getType());
447}
448
449
450// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
451// have to handle a more broad range of conversions than explicit casts, as they
452// handle things like function to ptr-to-function decay etc.
453Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000454 // Handle cases where the source is an non-complex type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000455 if (!E->getType()->isComplexType()) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000456 Value *Src = Visit(const_cast<Expr*>(E));
457
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000458 // Use EmitScalarConversion to perform the conversion.
459 return EmitScalarConversion(Src, E->getType(), DestTy);
460 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000461
Chris Lattner82e10392007-08-26 07:26:12 +0000462 // Handle cases where the source is a complex type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000463 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
464 DestTy);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000465}
466
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000467Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000468 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000469}
470
471
Chris Lattner9fba49a2007-08-24 05:35:26 +0000472//===----------------------------------------------------------------------===//
473// Unary Operators
474//===----------------------------------------------------------------------===//
475
476Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000477 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000478 LValue LV = EmitLValue(E->getSubExpr());
479 // FIXME: Handle volatile!
Chris Lattner0dc11f62007-08-26 05:10:16 +0000480 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000481 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000482
483 int AmountVal = isInc ? 1 : -1;
484
485 Value *NextVal;
Chris Lattner0dc11f62007-08-26 05:10:16 +0000486 if (isa<llvm::PointerType>(InVal->getType())) {
487 // FIXME: This isn't right for VLAs.
488 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
489 NextVal = Builder.CreateGEP(InVal, NextVal);
490 } else {
491 // Add the inc/dec to the real part.
492 if (isa<llvm::IntegerType>(InVal->getType()))
493 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000494 else if (InVal->getType() == llvm::Type::FloatTy)
495 // FIXME: Handle long double.
496 NextVal = llvm::ConstantFP::get(InVal->getType(),
497 llvm::APFloat(static_cast<float>(AmountVal)));
498 else {
499 // FIXME: Handle long double.
500 assert(InVal->getType() == llvm::Type::DoubleTy);
501 NextVal = llvm::ConstantFP::get(InVal->getType(),
502 llvm::APFloat(static_cast<double>(AmountVal)));
503 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000504 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
505 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000506
507 // Store the updated result through the lvalue.
508 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
509 E->getSubExpr()->getType());
510
511 // If this is a postinc, return the value read from memory, otherwise use the
512 // updated value.
513 return isPre ? NextVal : InVal;
514}
515
516
517Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
518 Value *Op = Visit(E->getSubExpr());
519 return Builder.CreateNeg(Op, "neg");
520}
521
522Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
523 Value *Op = Visit(E->getSubExpr());
524 return Builder.CreateNot(Op, "neg");
525}
526
527Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
528 // Compare operand to zero.
529 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
530
531 // Invert value.
532 // TODO: Could dynamically modify easy computations here. For example, if
533 // the operand is an icmp ne, turn into icmp eq.
534 BoolVal = Builder.CreateNot(BoolVal, "lnot");
535
536 // ZExt result to int.
537 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
538}
539
540/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
541/// an integer (RetType).
542Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner01211af2007-08-24 21:20:17 +0000543 QualType RetType,bool isSizeOf){
Chris Lattner9fba49a2007-08-24 05:35:26 +0000544 /// FIXME: This doesn't handle VLAs yet!
545 std::pair<uint64_t, unsigned> Info =
546 CGF.getContext().getTypeInfo(TypeToSize, SourceLocation());
547
548 uint64_t Val = isSizeOf ? Info.first : Info.second;
549 Val /= 8; // Return size in bytes, not bits.
550
551 assert(RetType->isIntegerType() && "Result type must be an integer!");
552
Hartmut Kaiserff08d2c2007-10-17 15:00:17 +0000553 uint32_t ResultWidth = static_cast<uint32_t>(
554 CGF.getContext().getTypeSize(RetType, SourceLocation()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000555 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
556}
557
Chris Lattner01211af2007-08-24 21:20:17 +0000558Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
559 Expr *Op = E->getSubExpr();
560 if (Op->getType()->isComplexType())
561 return CGF.EmitComplexExpr(Op).first;
562 return Visit(Op);
563}
564Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
565 Expr *Op = E->getSubExpr();
566 if (Op->getType()->isComplexType())
567 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000568
569 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
570 // effects are evaluated.
571 CGF.EmitScalarExpr(Op);
572 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000573}
574
575
Chris Lattner9fba49a2007-08-24 05:35:26 +0000576//===----------------------------------------------------------------------===//
577// Binary Operators
578//===----------------------------------------------------------------------===//
579
580BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
581 BinOpInfo Result;
582 Result.LHS = Visit(E->getLHS());
583 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000584 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000585 Result.E = E;
586 return Result;
587}
588
Chris Lattner0d965302007-08-26 21:41:21 +0000589Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000590 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
591 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
592
593 BinOpInfo OpInfo;
594
595 // Load the LHS and RHS operands.
596 LValue LHSLV = EmitLValue(E->getLHS());
597 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000598
599 // Determine the computation type. If the RHS is complex, then this is one of
600 // the add/sub/mul/div operators. All of these operators can be computed in
601 // with just their real component even though the computation domain really is
602 // complex.
Chris Lattner0d965302007-08-26 21:41:21 +0000603 QualType ComputeType = E->getComputationType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000604
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000605 // If the computation type is complex, then the RHS is complex. Emit the RHS.
606 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
607 ComputeType = CT->getElementType();
608
609 // Emit the RHS, only keeping the real component.
610 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
611 RHSTy = RHSTy->getAsComplexType()->getElementType();
612 } else {
613 // Otherwise the RHS is a simple scalar value.
614 OpInfo.RHS = Visit(E->getRHS());
615 }
616
617 // Convert the LHS/RHS values to the computation type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000618 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000619
620 // Do not merge types for -= where the LHS is a pointer.
Chris Lattner42330c32007-08-25 21:56:20 +0000621 if (E->getOpcode() != BinaryOperator::SubAssign ||
622 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnerb1497062007-08-26 07:08:39 +0000623 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000624 }
625 OpInfo.Ty = ComputeType;
626 OpInfo.E = E;
627
628 // Expand the binary operator.
629 Value *Result = (this->*Func)(OpInfo);
630
631 // Truncate the result back to the LHS type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000632 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000633
634 // Store the result value into the LHS lvalue.
635 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
636
637 return Result;
638}
639
640
Chris Lattner9fba49a2007-08-24 05:35:26 +0000641Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
642 if (Ops.LHS->getType()->isFloatingPoint())
643 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000644 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000645 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
646 else
647 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
648}
649
650Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
651 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000652 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000653 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
654 else
655 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
656}
657
658
659Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000660 if (!Ops.Ty->isPointerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000661 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner660e31d2007-08-24 21:00:35 +0000662
663 // FIXME: What about a pointer to a VLA?
Chris Lattner9fba49a2007-08-24 05:35:26 +0000664 if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int
665 return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr");
666 // int + pointer
667 return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr");
668}
669
670Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
671 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
672 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
673
Chris Lattner660e31d2007-08-24 21:00:35 +0000674 // pointer - int
675 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
676 "ptr-ptr shouldn't get here");
677 // FIXME: The pointer could point to a VLA.
678 Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
679 return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr");
680}
681
682Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
683 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
684 // the compound assignment case it is invalid, so just handle it here.
685 if (!E->getRHS()->getType()->isPointerType())
686 return EmitSub(EmitBinOps(E));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000687
688 // pointer - pointer
Chris Lattner660e31d2007-08-24 21:00:35 +0000689 Value *LHS = Visit(E->getLHS());
690 Value *RHS = Visit(E->getRHS());
691
692 const PointerType *LHSPtrType = E->getLHS()->getType()->getAsPointerType();
693 assert(LHSPtrType == E->getRHS()->getType()->getAsPointerType() &&
694 "Can't subtract different pointer types");
695
Chris Lattner9fba49a2007-08-24 05:35:26 +0000696 QualType LHSElementType = LHSPtrType->getPointeeType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000697 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType,
698 SourceLocation()) / 8;
Chris Lattner660e31d2007-08-24 21:00:35 +0000699
700 const llvm::Type *ResultType = ConvertType(E->getType());
701 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
702 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
703 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000704
705 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
706 // remainder. As such, we handle common power-of-two cases here to generate
707 // better code.
708 if (llvm::isPowerOf2_64(ElementSize)) {
709 Value *ShAmt =
710 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
711 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
712 }
Chris Lattner660e31d2007-08-24 21:00:35 +0000713
Chris Lattner9fba49a2007-08-24 05:35:26 +0000714 // Otherwise, do a full sdiv.
715 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
716 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
717}
718
Chris Lattner660e31d2007-08-24 21:00:35 +0000719
Chris Lattner9fba49a2007-08-24 05:35:26 +0000720Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
721 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
722 // RHS to the same size as the LHS.
723 Value *RHS = Ops.RHS;
724 if (Ops.LHS->getType() != RHS->getType())
725 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
726
727 return Builder.CreateShl(Ops.LHS, RHS, "shl");
728}
729
730Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
731 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
732 // RHS to the same size as the LHS.
733 Value *RHS = Ops.RHS;
734 if (Ops.LHS->getType() != RHS->getType())
735 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
736
Chris Lattner660e31d2007-08-24 21:00:35 +0000737 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000738 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
739 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
740}
741
742Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
743 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000744 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000745 QualType LHSTy = E->getLHS()->getType();
746 if (!LHSTy->isComplexType()) {
747 Value *LHS = Visit(E->getLHS());
748 Value *RHS = Visit(E->getRHS());
749
750 if (LHS->getType()->isFloatingPoint()) {
751 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
752 LHS, RHS, "cmp");
753 } else if (LHSTy->isUnsignedIntegerType()) {
754 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
755 LHS, RHS, "cmp");
756 } else {
757 // Signed integers and pointers.
758 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
759 LHS, RHS, "cmp");
760 }
761 } else {
762 // Complex Comparison: can only be an equality comparison.
763 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
764 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
765
766 QualType CETy =
767 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
768
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000769 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000770 if (CETy->isRealFloatingType()) {
771 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
772 LHS.first, RHS.first, "cmp.r");
773 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
774 LHS.second, RHS.second, "cmp.i");
775 } else {
776 // Complex comparisons can only be equality comparisons. As such, signed
777 // and unsigned opcodes are the same.
778 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
779 LHS.first, RHS.first, "cmp.r");
780 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
781 LHS.second, RHS.second, "cmp.i");
782 }
783
784 if (E->getOpcode() == BinaryOperator::EQ) {
785 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
786 } else {
787 assert(E->getOpcode() == BinaryOperator::NE &&
788 "Complex comparison other than == or != ?");
789 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
790 }
791 }
792
793 // ZExt result to int.
794 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
795}
796
797Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
798 LValue LHS = EmitLValue(E->getLHS());
799 Value *RHS = Visit(E->getRHS());
800
801 // Store the value into the LHS.
802 // FIXME: Volatility!
803 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
804
805 // Return the RHS.
806 return RHS;
807}
808
809Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
810 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
811
812 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
813 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
814
815 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
816 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
817
818 CGF.EmitBlock(RHSBlock);
819 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
820
821 // Reaquire the RHS block, as there may be subblocks inserted.
822 RHSBlock = Builder.GetInsertBlock();
823 CGF.EmitBlock(ContBlock);
824
825 // Create a PHI node. If we just evaluted the LHS condition, the result is
826 // false. If we evaluated both, the result is the RHS condition.
827 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
828 PN->reserveOperandSpace(2);
829 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
830 PN->addIncoming(RHSCond, RHSBlock);
831
832 // ZExt result to int.
833 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
834}
835
836Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
837 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
838
839 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
840 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
841
842 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
843 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
844
845 CGF.EmitBlock(RHSBlock);
846 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
847
848 // Reaquire the RHS block, as there may be subblocks inserted.
849 RHSBlock = Builder.GetInsertBlock();
850 CGF.EmitBlock(ContBlock);
851
852 // Create a PHI node. If we just evaluted the LHS condition, the result is
853 // true. If we evaluated both, the result is the RHS condition.
854 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
855 PN->reserveOperandSpace(2);
856 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
857 PN->addIncoming(RHSCond, RHSBlock);
858
859 // ZExt result to int.
860 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
861}
862
863Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
864 CGF.EmitStmt(E->getLHS());
865 return Visit(E->getRHS());
866}
867
868//===----------------------------------------------------------------------===//
869// Other Operators
870//===----------------------------------------------------------------------===//
871
872Value *ScalarExprEmitter::
873VisitConditionalOperator(const ConditionalOperator *E) {
874 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
875 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
876 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
877
878 Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
879 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
880
881 CGF.EmitBlock(LHSBlock);
882
883 // Handle the GNU extension for missing LHS.
884 Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond;
885 Builder.CreateBr(ContBlock);
886 LHSBlock = Builder.GetInsertBlock();
887
888 CGF.EmitBlock(RHSBlock);
889
890 Value *RHS = Visit(E->getRHS());
891 Builder.CreateBr(ContBlock);
892 RHSBlock = Builder.GetInsertBlock();
893
894 CGF.EmitBlock(ContBlock);
895
896 // Create a PHI node for the real part.
897 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
898 PN->reserveOperandSpace(2);
899 PN->addIncoming(LHS, LHSBlock);
900 PN->addIncoming(RHS, RHSBlock);
901 return PN;
902}
903
904Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
905 llvm::APSInt CondVal(32);
906 bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext());
907 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
908
909 // Emit the LHS or RHS as appropriate.
910 return Visit(CondVal != 0 ? E->getLHS() : E->getRHS());
911}
912
Anders Carlsson36760332007-10-15 20:28:48 +0000913Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE)
914{
915 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
916
917 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
918 return V;
919}
920
Chris Lattner9fba49a2007-08-24 05:35:26 +0000921//===----------------------------------------------------------------------===//
922// Entry Point into this File
923//===----------------------------------------------------------------------===//
924
925/// EmitComplexExpr - Emit the computation of the specified expression of
926/// complex type, ignoring the result.
927Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
928 assert(E && !hasAggregateLLVMType(E->getType()) &&
929 "Invalid scalar expression to emit");
930
931 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
932}
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000933
934/// EmitScalarConversion - Emit a conversion from the specified type to the
935/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000936Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
937 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000938 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
939 "Invalid scalar expression to emit");
940 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
941}
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000942
943/// EmitComplexToScalarConversion - Emit a conversion from the specified
944/// complex type to the specified destination type, where the destination
945/// type is an LLVM scalar type.
946Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
947 QualType SrcTy,
948 QualType DstTy) {
949 assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) &&
950 "Invalid complex -> scalar conversion");
951 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
952 DstTy);
953}