blob: e73ce616683d992983230c1101a11a0797cd6755 [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()),
104 E->typesAreCompatible());
105 }
106 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
107 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
108 }
109
110 // l-values.
111 Value *VisitDeclRefExpr(DeclRefExpr *E) {
112 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
113 return llvm::ConstantInt::get(EC->getInitVal());
114 return EmitLoadOfLValue(E);
115 }
116 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
117 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
118 Value *VisitOCUVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
119 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
120 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
121
122 // FIXME: CompoundLiteralExpr
123 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
124 Value *VisitCastExpr(const CastExpr *E) {
125 return EmitCastExpr(E->getSubExpr(), E->getType());
126 }
127 Value *EmitCastExpr(const Expr *E, QualType T);
128
129 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000130 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000131 }
132
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000133 Value *VisitStmtExpr(const StmtExpr *E);
134
Chris Lattner9fba49a2007-08-24 05:35:26 +0000135 // Unary Operators.
136 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
137 Value *VisitUnaryPostDec(const UnaryOperator *E) {
138 return VisitPrePostIncDec(E, false, false);
139 }
140 Value *VisitUnaryPostInc(const UnaryOperator *E) {
141 return VisitPrePostIncDec(E, true, false);
142 }
143 Value *VisitUnaryPreDec(const UnaryOperator *E) {
144 return VisitPrePostIncDec(E, false, true);
145 }
146 Value *VisitUnaryPreInc(const UnaryOperator *E) {
147 return VisitPrePostIncDec(E, true, true);
148 }
149 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
150 return EmitLValue(E->getSubExpr()).getAddress();
151 }
152 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
153 Value *VisitUnaryPlus(const UnaryOperator *E) {
154 return Visit(E->getSubExpr());
155 }
156 Value *VisitUnaryMinus (const UnaryOperator *E);
157 Value *VisitUnaryNot (const UnaryOperator *E);
158 Value *VisitUnaryLNot (const UnaryOperator *E);
159 Value *VisitUnarySizeOf (const UnaryOperator *E) {
160 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
161 }
162 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
163 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
164 }
165 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
166 bool isSizeOf);
Chris Lattner01211af2007-08-24 21:20:17 +0000167 Value *VisitUnaryReal (const UnaryOperator *E);
168 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000169 Value *VisitUnaryExtension(const UnaryOperator *E) {
170 return Visit(E->getSubExpr());
171 }
172
173 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000174 Value *EmitMul(const BinOpInfo &Ops) {
175 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
176 }
177 Value *EmitDiv(const BinOpInfo &Ops);
178 Value *EmitRem(const BinOpInfo &Ops);
179 Value *EmitAdd(const BinOpInfo &Ops);
180 Value *EmitSub(const BinOpInfo &Ops);
181 Value *EmitShl(const BinOpInfo &Ops);
182 Value *EmitShr(const BinOpInfo &Ops);
183 Value *EmitAnd(const BinOpInfo &Ops) {
184 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
185 }
186 Value *EmitXor(const BinOpInfo &Ops) {
187 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
188 }
189 Value *EmitOr (const BinOpInfo &Ops) {
190 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
191 }
192
Chris Lattner660e31d2007-08-24 21:00:35 +0000193 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000194 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000195 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
196
197 // Binary operators and binary compound assignment operators.
198#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000199 Value *VisitBin ## OP(const BinaryOperator *E) { \
200 return Emit ## OP(EmitBinOps(E)); \
201 } \
202 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
203 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000204 }
205 HANDLEBINOP(Mul);
206 HANDLEBINOP(Div);
207 HANDLEBINOP(Rem);
208 HANDLEBINOP(Add);
209 // (Sub) - Sub is handled specially below for ptr-ptr subtract.
210 HANDLEBINOP(Shl);
211 HANDLEBINOP(Shr);
212 HANDLEBINOP(And);
213 HANDLEBINOP(Xor);
214 HANDLEBINOP(Or);
215#undef HANDLEBINOP
216 Value *VisitBinSub(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000217 Value *VisitBinSubAssign(const CompoundAssignOperator *E) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000218 return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub);
219 }
220
Chris Lattner9fba49a2007-08-24 05:35:26 +0000221 // Comparisons.
222 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
223 unsigned SICmpOpc, unsigned FCmpOpc);
224#define VISITCOMP(CODE, UI, SI, FP) \
225 Value *VisitBin##CODE(const BinaryOperator *E) { \
226 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
227 llvm::FCmpInst::FP); }
228 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
229 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
230 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
231 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
232 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
233 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
234#undef VISITCOMP
235
236 Value *VisitBinAssign (const BinaryOperator *E);
237
238 Value *VisitBinLAnd (const BinaryOperator *E);
239 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000240 Value *VisitBinComma (const BinaryOperator *E);
241
242 // Other Operators.
243 Value *VisitConditionalOperator(const ConditionalOperator *CO);
244 Value *VisitChooseExpr(ChooseExpr *CE);
Anders Carlsson36760332007-10-15 20:28:48 +0000245 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000246 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
247 return CGF.EmitObjCStringLiteral(E);
248 }
249};
250} // end anonymous namespace.
251
252//===----------------------------------------------------------------------===//
253// Utilities
254//===----------------------------------------------------------------------===//
255
Chris Lattnerd8d44222007-08-26 16:42:57 +0000256/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000257/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000258Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
259 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
260
261 if (SrcType->isRealFloatingType()) {
262 // Compare against 0.0 for fp scalars.
263 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000264 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
265 }
266
267 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
268 "Unknown scalar type to convert");
269
270 // Because of the type rules of C, we often end up computing a logical value,
271 // then zero extending it to int, then wanting it as a logical value again.
272 // Optimize this common case.
273 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
274 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
275 Value *Result = ZI->getOperand(0);
276 ZI->eraseFromParent();
277 return Result;
278 }
279 }
280
281 // Compare against an integer or pointer null.
282 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
283 return Builder.CreateICmpNE(Src, Zero, "tobool");
284}
285
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000286/// EmitScalarConversion - Emit a conversion from the specified type to the
287/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000288Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
289 QualType DstType) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000290 SrcType = SrcType.getCanonicalType();
291 DstType = DstType.getCanonicalType();
292 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000293
294 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000295
296 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000297 if (DstType->isBooleanType())
298 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000299
300 const llvm::Type *DstTy = ConvertType(DstType);
301
302 // Ignore conversions like int -> uint.
303 if (Src->getType() == DstTy)
304 return Src;
305
306 // Handle pointer conversions next: pointers can only be converted to/from
307 // other pointers and integers.
308 if (isa<PointerType>(DstType)) {
309 // The source value may be an integer, or a pointer.
310 if (isa<llvm::PointerType>(Src->getType()))
311 return Builder.CreateBitCast(Src, DstTy, "conv");
312 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
313 return Builder.CreateIntToPtr(Src, DstTy, "conv");
314 }
315
316 if (isa<PointerType>(SrcType)) {
317 // Must be an ptr to int cast.
318 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
319 return Builder.CreateIntToPtr(Src, DstTy, "conv");
320 }
321
322 // Finally, we have the arithmetic types: real int/float.
323 if (isa<llvm::IntegerType>(Src->getType())) {
324 bool InputSigned = SrcType->isSignedIntegerType();
325 if (isa<llvm::IntegerType>(DstTy))
326 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
327 else if (InputSigned)
328 return Builder.CreateSIToFP(Src, DstTy, "conv");
329 else
330 return Builder.CreateUIToFP(Src, DstTy, "conv");
331 }
332
333 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
334 if (isa<llvm::IntegerType>(DstTy)) {
335 if (DstType->isSignedIntegerType())
336 return Builder.CreateFPToSI(Src, DstTy, "conv");
337 else
338 return Builder.CreateFPToUI(Src, DstTy, "conv");
339 }
340
341 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
342 if (DstTy->getTypeID() < Src->getType()->getTypeID())
343 return Builder.CreateFPTrunc(Src, DstTy, "conv");
344 else
345 return Builder.CreateFPExt(Src, DstTy, "conv");
346}
347
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000348/// EmitComplexToScalarConversion - Emit a conversion from the specified
349/// complex type to the specified destination type, where the destination
350/// type is an LLVM scalar type.
351Value *ScalarExprEmitter::
352EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
353 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000354 // Get the source element type.
355 SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType();
356
357 // Handle conversions to bool first, they are special: comparisons against 0.
358 if (DstTy->isBooleanType()) {
359 // Complex != 0 -> (Real != 0) | (Imag != 0)
360 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
361 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
362 return Builder.CreateOr(Src.first, Src.second, "tobool");
363 }
364
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000365 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
366 // the imaginary part of the complex value is discarded and the value of the
367 // real part is converted according to the conversion rules for the
368 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000369 return EmitScalarConversion(Src.first, SrcTy, DstTy);
370}
371
372
Chris Lattner9fba49a2007-08-24 05:35:26 +0000373//===----------------------------------------------------------------------===//
374// Visitor Methods
375//===----------------------------------------------------------------------===//
376
377Value *ScalarExprEmitter::VisitExpr(Expr *E) {
378 fprintf(stderr, "Unimplemented scalar expr!\n");
Chris Lattner1aef6212007-09-13 01:17:29 +0000379 E->dump(CGF.getContext().SourceMgr);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000380 if (E->getType()->isVoidType())
381 return 0;
382 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
383}
384
385Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
386 // Emit subscript expressions in rvalue context's. For most cases, this just
387 // loads the lvalue formed by the subscript expr. However, we have to be
388 // careful, because the base of a vector subscript is occasionally an rvalue,
389 // so we can't get it as an lvalue.
390 if (!E->getBase()->getType()->isVectorType())
391 return EmitLoadOfLValue(E);
392
393 // Handle the vector case. The base must be a vector, the index must be an
394 // integer value.
395 Value *Base = Visit(E->getBase());
396 Value *Idx = Visit(E->getIdx());
397
398 // FIXME: Convert Idx to i32 type.
399 return Builder.CreateExtractElement(Base, Idx, "vecext");
400}
401
402/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
403/// also handle things like function to pointer-to-function decay, and array to
404/// pointer decay.
405Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
406 const Expr *Op = E->getSubExpr();
407
408 // If this is due to array->pointer conversion, emit the array expression as
409 // an l-value.
410 if (Op->getType()->isArrayType()) {
411 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
412 // will not true when we add support for VLAs.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000413 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000414
415 assert(isa<llvm::PointerType>(V->getType()) &&
416 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
417 ->getElementType()) &&
418 "Doesn't support VLAs yet!");
419 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Ted Kremenek7f6f4a42007-09-04 17:20:08 +0000420
421 llvm::Value *Ops[] = {Idx0, Idx0};
422 return Builder.CreateGEP(V, Ops, Ops+2, "arraydecay");
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000423 } else if (E->getType()->isReferenceType()) {
Anders Carlsson88842452007-10-13 05:52:34 +0000424 assert(cast<ReferenceType>(E->getType().getCanonicalType())->
425 getReferenceeType() ==
426 Op->getType().getCanonicalType() && "Incompatible types!");
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000427
428 return EmitLValue(Op).getAddress();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000429 }
430
431 return EmitCastExpr(Op, E->getType());
432}
433
434
435// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
436// have to handle a more broad range of conversions than explicit casts, as they
437// handle things like function to ptr-to-function decay etc.
438Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000439 // Handle cases where the source is an non-complex type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000440 if (!E->getType()->isComplexType()) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000441 Value *Src = Visit(const_cast<Expr*>(E));
442
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000443 // Use EmitScalarConversion to perform the conversion.
444 return EmitScalarConversion(Src, E->getType(), DestTy);
445 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000446
Chris Lattner82e10392007-08-26 07:26:12 +0000447 // Handle cases where the source is a complex type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000448 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
449 DestTy);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000450}
451
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000452Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000453 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000454}
455
456
Chris Lattner9fba49a2007-08-24 05:35:26 +0000457//===----------------------------------------------------------------------===//
458// Unary Operators
459//===----------------------------------------------------------------------===//
460
461Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000462 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000463 LValue LV = EmitLValue(E->getSubExpr());
464 // FIXME: Handle volatile!
Chris Lattner0dc11f62007-08-26 05:10:16 +0000465 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000466 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000467
468 int AmountVal = isInc ? 1 : -1;
469
470 Value *NextVal;
Chris Lattner0dc11f62007-08-26 05:10:16 +0000471 if (isa<llvm::PointerType>(InVal->getType())) {
472 // FIXME: This isn't right for VLAs.
473 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
474 NextVal = Builder.CreateGEP(InVal, NextVal);
475 } else {
476 // Add the inc/dec to the real part.
477 if (isa<llvm::IntegerType>(InVal->getType()))
478 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000479 else if (InVal->getType() == llvm::Type::FloatTy)
480 // FIXME: Handle long double.
481 NextVal = llvm::ConstantFP::get(InVal->getType(),
482 llvm::APFloat(static_cast<float>(AmountVal)));
483 else {
484 // FIXME: Handle long double.
485 assert(InVal->getType() == llvm::Type::DoubleTy);
486 NextVal = llvm::ConstantFP::get(InVal->getType(),
487 llvm::APFloat(static_cast<double>(AmountVal)));
488 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000489 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
490 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000491
492 // Store the updated result through the lvalue.
493 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
494 E->getSubExpr()->getType());
495
496 // If this is a postinc, return the value read from memory, otherwise use the
497 // updated value.
498 return isPre ? NextVal : InVal;
499}
500
501
502Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
503 Value *Op = Visit(E->getSubExpr());
504 return Builder.CreateNeg(Op, "neg");
505}
506
507Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
508 Value *Op = Visit(E->getSubExpr());
509 return Builder.CreateNot(Op, "neg");
510}
511
512Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
513 // Compare operand to zero.
514 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
515
516 // Invert value.
517 // TODO: Could dynamically modify easy computations here. For example, if
518 // the operand is an icmp ne, turn into icmp eq.
519 BoolVal = Builder.CreateNot(BoolVal, "lnot");
520
521 // ZExt result to int.
522 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
523}
524
525/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
526/// an integer (RetType).
527Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner01211af2007-08-24 21:20:17 +0000528 QualType RetType,bool isSizeOf){
Chris Lattner9fba49a2007-08-24 05:35:26 +0000529 /// FIXME: This doesn't handle VLAs yet!
530 std::pair<uint64_t, unsigned> Info =
531 CGF.getContext().getTypeInfo(TypeToSize, SourceLocation());
532
533 uint64_t Val = isSizeOf ? Info.first : Info.second;
534 Val /= 8; // Return size in bytes, not bits.
535
536 assert(RetType->isIntegerType() && "Result type must be an integer!");
537
Chris Lattnera96e0d82007-09-04 02:34:27 +0000538 unsigned ResultWidth = static_cast<unsigned>(CGF.getContext().getTypeSize(RetType,SourceLocation()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000539 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
540}
541
Chris Lattner01211af2007-08-24 21:20:17 +0000542Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
543 Expr *Op = E->getSubExpr();
544 if (Op->getType()->isComplexType())
545 return CGF.EmitComplexExpr(Op).first;
546 return Visit(Op);
547}
548Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
549 Expr *Op = E->getSubExpr();
550 if (Op->getType()->isComplexType())
551 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000552
553 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
554 // effects are evaluated.
555 CGF.EmitScalarExpr(Op);
556 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000557}
558
559
Chris Lattner9fba49a2007-08-24 05:35:26 +0000560//===----------------------------------------------------------------------===//
561// Binary Operators
562//===----------------------------------------------------------------------===//
563
564BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
565 BinOpInfo Result;
566 Result.LHS = Visit(E->getLHS());
567 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000568 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000569 Result.E = E;
570 return Result;
571}
572
Chris Lattner0d965302007-08-26 21:41:21 +0000573Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000574 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
575 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
576
577 BinOpInfo OpInfo;
578
579 // Load the LHS and RHS operands.
580 LValue LHSLV = EmitLValue(E->getLHS());
581 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000582
583 // Determine the computation type. If the RHS is complex, then this is one of
584 // the add/sub/mul/div operators. All of these operators can be computed in
585 // with just their real component even though the computation domain really is
586 // complex.
Chris Lattner0d965302007-08-26 21:41:21 +0000587 QualType ComputeType = E->getComputationType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000588
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000589 // If the computation type is complex, then the RHS is complex. Emit the RHS.
590 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
591 ComputeType = CT->getElementType();
592
593 // Emit the RHS, only keeping the real component.
594 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
595 RHSTy = RHSTy->getAsComplexType()->getElementType();
596 } else {
597 // Otherwise the RHS is a simple scalar value.
598 OpInfo.RHS = Visit(E->getRHS());
599 }
600
601 // Convert the LHS/RHS values to the computation type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000602 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000603
604 // Do not merge types for -= where the LHS is a pointer.
Chris Lattner42330c32007-08-25 21:56:20 +0000605 if (E->getOpcode() != BinaryOperator::SubAssign ||
606 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnerb1497062007-08-26 07:08:39 +0000607 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000608 }
609 OpInfo.Ty = ComputeType;
610 OpInfo.E = E;
611
612 // Expand the binary operator.
613 Value *Result = (this->*Func)(OpInfo);
614
615 // Truncate the result back to the LHS type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000616 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000617
618 // Store the result value into the LHS lvalue.
619 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
620
621 return Result;
622}
623
624
Chris Lattner9fba49a2007-08-24 05:35:26 +0000625Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
626 if (Ops.LHS->getType()->isFloatingPoint())
627 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000628 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000629 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
630 else
631 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
632}
633
634Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
635 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000636 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000637 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
638 else
639 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
640}
641
642
643Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000644 if (!Ops.Ty->isPointerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000645 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner660e31d2007-08-24 21:00:35 +0000646
647 // FIXME: What about a pointer to a VLA?
Chris Lattner9fba49a2007-08-24 05:35:26 +0000648 if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int
649 return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr");
650 // int + pointer
651 return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr");
652}
653
654Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
655 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
656 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
657
Chris Lattner660e31d2007-08-24 21:00:35 +0000658 // pointer - int
659 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
660 "ptr-ptr shouldn't get here");
661 // FIXME: The pointer could point to a VLA.
662 Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
663 return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr");
664}
665
666Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
667 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
668 // the compound assignment case it is invalid, so just handle it here.
669 if (!E->getRHS()->getType()->isPointerType())
670 return EmitSub(EmitBinOps(E));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000671
672 // pointer - pointer
Chris Lattner660e31d2007-08-24 21:00:35 +0000673 Value *LHS = Visit(E->getLHS());
674 Value *RHS = Visit(E->getRHS());
675
676 const PointerType *LHSPtrType = E->getLHS()->getType()->getAsPointerType();
677 assert(LHSPtrType == E->getRHS()->getType()->getAsPointerType() &&
678 "Can't subtract different pointer types");
679
Chris Lattner9fba49a2007-08-24 05:35:26 +0000680 QualType LHSElementType = LHSPtrType->getPointeeType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000681 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType,
682 SourceLocation()) / 8;
Chris Lattner660e31d2007-08-24 21:00:35 +0000683
684 const llvm::Type *ResultType = ConvertType(E->getType());
685 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
686 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
687 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000688
689 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
690 // remainder. As such, we handle common power-of-two cases here to generate
691 // better code.
692 if (llvm::isPowerOf2_64(ElementSize)) {
693 Value *ShAmt =
694 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
695 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
696 }
Chris Lattner660e31d2007-08-24 21:00:35 +0000697
Chris Lattner9fba49a2007-08-24 05:35:26 +0000698 // Otherwise, do a full sdiv.
699 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
700 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
701}
702
Chris Lattner660e31d2007-08-24 21:00:35 +0000703
Chris Lattner9fba49a2007-08-24 05:35:26 +0000704Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
705 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
706 // RHS to the same size as the LHS.
707 Value *RHS = Ops.RHS;
708 if (Ops.LHS->getType() != RHS->getType())
709 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
710
711 return Builder.CreateShl(Ops.LHS, RHS, "shl");
712}
713
714Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
715 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
716 // RHS to the same size as the LHS.
717 Value *RHS = Ops.RHS;
718 if (Ops.LHS->getType() != RHS->getType())
719 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
720
Chris Lattner660e31d2007-08-24 21:00:35 +0000721 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000722 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
723 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
724}
725
726Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
727 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000728 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000729 QualType LHSTy = E->getLHS()->getType();
730 if (!LHSTy->isComplexType()) {
731 Value *LHS = Visit(E->getLHS());
732 Value *RHS = Visit(E->getRHS());
733
734 if (LHS->getType()->isFloatingPoint()) {
735 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
736 LHS, RHS, "cmp");
737 } else if (LHSTy->isUnsignedIntegerType()) {
738 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
739 LHS, RHS, "cmp");
740 } else {
741 // Signed integers and pointers.
742 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
743 LHS, RHS, "cmp");
744 }
745 } else {
746 // Complex Comparison: can only be an equality comparison.
747 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
748 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
749
750 QualType CETy =
751 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
752
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000753 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000754 if (CETy->isRealFloatingType()) {
755 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
756 LHS.first, RHS.first, "cmp.r");
757 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
758 LHS.second, RHS.second, "cmp.i");
759 } else {
760 // Complex comparisons can only be equality comparisons. As such, signed
761 // and unsigned opcodes are the same.
762 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
763 LHS.first, RHS.first, "cmp.r");
764 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
765 LHS.second, RHS.second, "cmp.i");
766 }
767
768 if (E->getOpcode() == BinaryOperator::EQ) {
769 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
770 } else {
771 assert(E->getOpcode() == BinaryOperator::NE &&
772 "Complex comparison other than == or != ?");
773 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
774 }
775 }
776
777 // ZExt result to int.
778 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
779}
780
781Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
782 LValue LHS = EmitLValue(E->getLHS());
783 Value *RHS = Visit(E->getRHS());
784
785 // Store the value into the LHS.
786 // FIXME: Volatility!
787 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
788
789 // Return the RHS.
790 return RHS;
791}
792
793Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
794 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
795
796 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
797 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
798
799 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
800 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
801
802 CGF.EmitBlock(RHSBlock);
803 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
804
805 // Reaquire the RHS block, as there may be subblocks inserted.
806 RHSBlock = Builder.GetInsertBlock();
807 CGF.EmitBlock(ContBlock);
808
809 // Create a PHI node. If we just evaluted the LHS condition, the result is
810 // false. If we evaluated both, the result is the RHS condition.
811 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
812 PN->reserveOperandSpace(2);
813 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
814 PN->addIncoming(RHSCond, RHSBlock);
815
816 // ZExt result to int.
817 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
818}
819
820Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
821 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
822
823 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
824 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
825
826 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
827 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
828
829 CGF.EmitBlock(RHSBlock);
830 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
831
832 // Reaquire the RHS block, as there may be subblocks inserted.
833 RHSBlock = Builder.GetInsertBlock();
834 CGF.EmitBlock(ContBlock);
835
836 // Create a PHI node. If we just evaluted the LHS condition, the result is
837 // true. If we evaluated both, the result is the RHS condition.
838 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
839 PN->reserveOperandSpace(2);
840 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
841 PN->addIncoming(RHSCond, RHSBlock);
842
843 // ZExt result to int.
844 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
845}
846
847Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
848 CGF.EmitStmt(E->getLHS());
849 return Visit(E->getRHS());
850}
851
852//===----------------------------------------------------------------------===//
853// Other Operators
854//===----------------------------------------------------------------------===//
855
856Value *ScalarExprEmitter::
857VisitConditionalOperator(const ConditionalOperator *E) {
858 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
859 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
860 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
861
862 Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
863 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
864
865 CGF.EmitBlock(LHSBlock);
866
867 // Handle the GNU extension for missing LHS.
868 Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond;
869 Builder.CreateBr(ContBlock);
870 LHSBlock = Builder.GetInsertBlock();
871
872 CGF.EmitBlock(RHSBlock);
873
874 Value *RHS = Visit(E->getRHS());
875 Builder.CreateBr(ContBlock);
876 RHSBlock = Builder.GetInsertBlock();
877
878 CGF.EmitBlock(ContBlock);
879
880 // Create a PHI node for the real part.
881 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
882 PN->reserveOperandSpace(2);
883 PN->addIncoming(LHS, LHSBlock);
884 PN->addIncoming(RHS, RHSBlock);
885 return PN;
886}
887
888Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
889 llvm::APSInt CondVal(32);
890 bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext());
891 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
892
893 // Emit the LHS or RHS as appropriate.
894 return Visit(CondVal != 0 ? E->getLHS() : E->getRHS());
895}
896
Anders Carlsson36760332007-10-15 20:28:48 +0000897Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE)
898{
899 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
900
901 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
902 return V;
903}
904
Chris Lattner9fba49a2007-08-24 05:35:26 +0000905//===----------------------------------------------------------------------===//
906// Entry Point into this File
907//===----------------------------------------------------------------------===//
908
909/// EmitComplexExpr - Emit the computation of the specified expression of
910/// complex type, ignoring the result.
911Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
912 assert(E && !hasAggregateLLVMType(E->getType()) &&
913 "Invalid scalar expression to emit");
914
915 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
916}
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000917
918/// EmitScalarConversion - Emit a conversion from the specified type to the
919/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000920Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
921 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000922 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
923 "Invalid scalar expression to emit");
924 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
925}
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000926
927/// EmitComplexToScalarConversion - Emit a conversion from the specified
928/// complex type to the specified destination type, where the destination
929/// type is an LLVM scalar type.
930Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
931 QualType SrcTy,
932 QualType DstTy) {
933 assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) &&
934 "Invalid complex -> scalar conversion");
935 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
936 DstTy);
937}