blob: 319ca82bf89ed5bbe832495a61b7aae57c139ca6 [file] [log] [blame]
Chris Lattner7f02f722007-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"
19#include "llvm/Support/Compiler.h"
20using namespace clang;
21using namespace CodeGen;
22using llvm::Value;
23
24//===----------------------------------------------------------------------===//
25// Scalar Expression Emitter
26//===----------------------------------------------------------------------===//
27
28struct BinOpInfo {
29 Value *LHS;
30 Value *RHS;
Chris Lattner1f1ded92007-08-24 21:00:35 +000031 QualType Ty; // Computation Type.
Chris Lattner7f02f722007-08-24 05:35:26 +000032 const BinaryOperator *E;
33};
34
35namespace {
36class VISIBILITY_HIDDEN ScalarExprEmitter
37 : public StmtVisitor<ScalarExprEmitter, Value*> {
38 CodeGenFunction &CGF;
39 llvm::LLVMBuilder &Builder;
40public:
41
42 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) {
43 }
44
45
46 //===--------------------------------------------------------------------===//
47 // Utilities
48 //===--------------------------------------------------------------------===//
49
50 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
51 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
52
53 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattner9b655512007-08-31 22:49:20 +000054 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +000055 }
56
57 /// EmitLoadOfLValue - Given an expression with complex type that represents a
58 /// value l-value, this method emits the address of the l-value, then loads
59 /// and returns the result.
60 Value *EmitLoadOfLValue(const Expr *E) {
61 // FIXME: Volatile
62 return EmitLoadOfLValue(EmitLValue(E), E->getType());
63 }
64
Chris Lattner9abc84e2007-08-26 16:42:57 +000065 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner3420d0d2007-08-26 17:25:57 +000066 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattner9abc84e2007-08-26 16:42:57 +000067 Value *EmitConversionToBool(Value *Src, QualType DstTy);
68
Chris Lattner3707b252007-08-26 06:48:56 +000069 /// EmitScalarConversion - Emit a conversion from the specified type to the
70 /// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +000071 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
72
73 /// EmitComplexToScalarConversion - Emit a conversion from the specified
74 /// complex type to the specified destination type, where the destination
75 /// type is an LLVM scalar type.
76 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
77 QualType SrcTy, QualType DstTy);
Chris Lattner3707b252007-08-26 06:48:56 +000078
Chris Lattner7f02f722007-08-24 05:35:26 +000079 //===--------------------------------------------------------------------===//
80 // Visitor Methods
81 //===--------------------------------------------------------------------===//
82
83 Value *VisitStmt(Stmt *S) {
Chris Lattner419ea7e2007-09-13 01:17:29 +000084 S->dump(CGF.getContext().SourceMgr);
Chris Lattner7f02f722007-08-24 05:35:26 +000085 assert(0 && "Stmt can't have complex result type!");
86 return 0;
87 }
88 Value *VisitExpr(Expr *S);
89 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
90
91 // Leaves.
92 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
93 return llvm::ConstantInt::get(E->getValue());
94 }
95 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner191396c2007-09-13 01:26:11 +000096 double V = E->getValue();
97 // FIXME: Change this when FloatingLiteral uses an APFloat internally.
98 const llvm::Type *Ty = ConvertType(E->getType());
99 if (Ty == llvm::Type::FloatTy)
100 return llvm::ConstantFP::get(Ty, llvm::APFloat((float)V));
101 assert(Ty == llvm::Type::DoubleTy && "Unknown float type!");
102 return llvm::ConstantFP::get(Ty, llvm::APFloat((double)V));
Chris Lattner7f02f722007-08-24 05:35:26 +0000103 }
104 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
105 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
106 }
107 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
108 return llvm::ConstantInt::get(ConvertType(E->getType()),
109 E->typesAreCompatible());
110 }
111 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
112 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
113 }
114
115 // l-values.
116 Value *VisitDeclRefExpr(DeclRefExpr *E) {
117 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
118 return llvm::ConstantInt::get(EC->getInitVal());
119 return EmitLoadOfLValue(E);
120 }
121 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
122 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
123 Value *VisitOCUVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
124 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
125 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
126
127 // FIXME: CompoundLiteralExpr
128 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
129 Value *VisitCastExpr(const CastExpr *E) {
130 return EmitCastExpr(E->getSubExpr(), E->getType());
131 }
132 Value *EmitCastExpr(const Expr *E, QualType T);
133
134 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattner9b655512007-08-31 22:49:20 +0000135 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +0000136 }
137
Chris Lattner33793202007-08-31 22:09:40 +0000138 Value *VisitStmtExpr(const StmtExpr *E);
139
Chris Lattner7f02f722007-08-24 05:35:26 +0000140 // Unary Operators.
141 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
142 Value *VisitUnaryPostDec(const UnaryOperator *E) {
143 return VisitPrePostIncDec(E, false, false);
144 }
145 Value *VisitUnaryPostInc(const UnaryOperator *E) {
146 return VisitPrePostIncDec(E, true, false);
147 }
148 Value *VisitUnaryPreDec(const UnaryOperator *E) {
149 return VisitPrePostIncDec(E, false, true);
150 }
151 Value *VisitUnaryPreInc(const UnaryOperator *E) {
152 return VisitPrePostIncDec(E, true, true);
153 }
154 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
155 return EmitLValue(E->getSubExpr()).getAddress();
156 }
157 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
158 Value *VisitUnaryPlus(const UnaryOperator *E) {
159 return Visit(E->getSubExpr());
160 }
161 Value *VisitUnaryMinus (const UnaryOperator *E);
162 Value *VisitUnaryNot (const UnaryOperator *E);
163 Value *VisitUnaryLNot (const UnaryOperator *E);
164 Value *VisitUnarySizeOf (const UnaryOperator *E) {
165 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
166 }
167 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
168 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
169 }
170 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
171 bool isSizeOf);
Chris Lattner46f93d02007-08-24 21:20:17 +0000172 Value *VisitUnaryReal (const UnaryOperator *E);
173 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000174 Value *VisitUnaryExtension(const UnaryOperator *E) {
175 return Visit(E->getSubExpr());
176 }
177
178 // Binary Operators.
Chris Lattner7f02f722007-08-24 05:35:26 +0000179 Value *EmitMul(const BinOpInfo &Ops) {
180 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
181 }
182 Value *EmitDiv(const BinOpInfo &Ops);
183 Value *EmitRem(const BinOpInfo &Ops);
184 Value *EmitAdd(const BinOpInfo &Ops);
185 Value *EmitSub(const BinOpInfo &Ops);
186 Value *EmitShl(const BinOpInfo &Ops);
187 Value *EmitShr(const BinOpInfo &Ops);
188 Value *EmitAnd(const BinOpInfo &Ops) {
189 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
190 }
191 Value *EmitXor(const BinOpInfo &Ops) {
192 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
193 }
194 Value *EmitOr (const BinOpInfo &Ops) {
195 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
196 }
197
Chris Lattner1f1ded92007-08-24 21:00:35 +0000198 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner3ccf7742007-08-26 21:41:21 +0000199 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner1f1ded92007-08-24 21:00:35 +0000200 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
201
202 // Binary operators and binary compound assignment operators.
203#define HANDLEBINOP(OP) \
Chris Lattner3ccf7742007-08-26 21:41:21 +0000204 Value *VisitBin ## OP(const BinaryOperator *E) { \
205 return Emit ## OP(EmitBinOps(E)); \
206 } \
207 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
208 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner1f1ded92007-08-24 21:00:35 +0000209 }
210 HANDLEBINOP(Mul);
211 HANDLEBINOP(Div);
212 HANDLEBINOP(Rem);
213 HANDLEBINOP(Add);
214 // (Sub) - Sub is handled specially below for ptr-ptr subtract.
215 HANDLEBINOP(Shl);
216 HANDLEBINOP(Shr);
217 HANDLEBINOP(And);
218 HANDLEBINOP(Xor);
219 HANDLEBINOP(Or);
220#undef HANDLEBINOP
221 Value *VisitBinSub(const BinaryOperator *E);
Chris Lattner3ccf7742007-08-26 21:41:21 +0000222 Value *VisitBinSubAssign(const CompoundAssignOperator *E) {
Chris Lattner1f1ded92007-08-24 21:00:35 +0000223 return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub);
224 }
225
Chris Lattner7f02f722007-08-24 05:35:26 +0000226 // Comparisons.
227 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
228 unsigned SICmpOpc, unsigned FCmpOpc);
229#define VISITCOMP(CODE, UI, SI, FP) \
230 Value *VisitBin##CODE(const BinaryOperator *E) { \
231 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
232 llvm::FCmpInst::FP); }
233 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
234 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
235 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
236 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
237 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
238 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
239#undef VISITCOMP
240
241 Value *VisitBinAssign (const BinaryOperator *E);
242
243 Value *VisitBinLAnd (const BinaryOperator *E);
244 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000245 Value *VisitBinComma (const BinaryOperator *E);
246
247 // Other Operators.
248 Value *VisitConditionalOperator(const ConditionalOperator *CO);
249 Value *VisitChooseExpr(ChooseExpr *CE);
250 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
251 return CGF.EmitObjCStringLiteral(E);
252 }
253};
254} // end anonymous namespace.
255
256//===----------------------------------------------------------------------===//
257// Utilities
258//===----------------------------------------------------------------------===//
259
Chris Lattner9abc84e2007-08-26 16:42:57 +0000260/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner3420d0d2007-08-26 17:25:57 +0000261/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattner9abc84e2007-08-26 16:42:57 +0000262Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
263 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
264
265 if (SrcType->isRealFloatingType()) {
266 // Compare against 0.0 for fp scalars.
267 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattner9abc84e2007-08-26 16:42:57 +0000268 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
269 }
270
271 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
272 "Unknown scalar type to convert");
273
274 // Because of the type rules of C, we often end up computing a logical value,
275 // then zero extending it to int, then wanting it as a logical value again.
276 // Optimize this common case.
277 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
278 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
279 Value *Result = ZI->getOperand(0);
280 ZI->eraseFromParent();
281 return Result;
282 }
283 }
284
285 // Compare against an integer or pointer null.
286 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
287 return Builder.CreateICmpNE(Src, Zero, "tobool");
288}
289
Chris Lattner3707b252007-08-26 06:48:56 +0000290/// EmitScalarConversion - Emit a conversion from the specified type to the
291/// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000292Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
293 QualType DstType) {
Chris Lattner3707b252007-08-26 06:48:56 +0000294 SrcType = SrcType.getCanonicalType();
295 DstType = DstType.getCanonicalType();
296 if (SrcType == DstType) return Src;
Chris Lattnercf289082007-08-26 07:21:11 +0000297
298 if (DstType->isVoidType()) return 0;
Chris Lattner3707b252007-08-26 06:48:56 +0000299
300 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnered70f0a2007-08-26 16:52:28 +0000301 if (DstType->isBooleanType())
302 return EmitConversionToBool(Src, SrcType);
Chris Lattner3707b252007-08-26 06:48:56 +0000303
304 const llvm::Type *DstTy = ConvertType(DstType);
305
306 // Ignore conversions like int -> uint.
307 if (Src->getType() == DstTy)
308 return Src;
309
310 // Handle pointer conversions next: pointers can only be converted to/from
311 // other pointers and integers.
312 if (isa<PointerType>(DstType)) {
313 // The source value may be an integer, or a pointer.
314 if (isa<llvm::PointerType>(Src->getType()))
315 return Builder.CreateBitCast(Src, DstTy, "conv");
316 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
317 return Builder.CreateIntToPtr(Src, DstTy, "conv");
318 }
319
320 if (isa<PointerType>(SrcType)) {
321 // Must be an ptr to int cast.
322 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
323 return Builder.CreateIntToPtr(Src, DstTy, "conv");
324 }
325
326 // Finally, we have the arithmetic types: real int/float.
327 if (isa<llvm::IntegerType>(Src->getType())) {
328 bool InputSigned = SrcType->isSignedIntegerType();
329 if (isa<llvm::IntegerType>(DstTy))
330 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
331 else if (InputSigned)
332 return Builder.CreateSIToFP(Src, DstTy, "conv");
333 else
334 return Builder.CreateUIToFP(Src, DstTy, "conv");
335 }
336
337 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
338 if (isa<llvm::IntegerType>(DstTy)) {
339 if (DstType->isSignedIntegerType())
340 return Builder.CreateFPToSI(Src, DstTy, "conv");
341 else
342 return Builder.CreateFPToUI(Src, DstTy, "conv");
343 }
344
345 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
346 if (DstTy->getTypeID() < Src->getType()->getTypeID())
347 return Builder.CreateFPTrunc(Src, DstTy, "conv");
348 else
349 return Builder.CreateFPExt(Src, DstTy, "conv");
350}
351
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000352/// EmitComplexToScalarConversion - Emit a conversion from the specified
353/// complex type to the specified destination type, where the destination
354/// type is an LLVM scalar type.
355Value *ScalarExprEmitter::
356EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
357 QualType SrcTy, QualType DstTy) {
Chris Lattnered70f0a2007-08-26 16:52:28 +0000358 // Get the source element type.
359 SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType();
360
361 // Handle conversions to bool first, they are special: comparisons against 0.
362 if (DstTy->isBooleanType()) {
363 // Complex != 0 -> (Real != 0) | (Imag != 0)
364 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
365 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
366 return Builder.CreateOr(Src.first, Src.second, "tobool");
367 }
368
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000369 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
370 // the imaginary part of the complex value is discarded and the value of the
371 // real part is converted according to the conversion rules for the
372 // corresponding real type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000373 return EmitScalarConversion(Src.first, SrcTy, DstTy);
374}
375
376
Chris Lattner7f02f722007-08-24 05:35:26 +0000377//===----------------------------------------------------------------------===//
378// Visitor Methods
379//===----------------------------------------------------------------------===//
380
381Value *ScalarExprEmitter::VisitExpr(Expr *E) {
382 fprintf(stderr, "Unimplemented scalar expr!\n");
Chris Lattner419ea7e2007-09-13 01:17:29 +0000383 E->dump(CGF.getContext().SourceMgr);
Chris Lattner7f02f722007-08-24 05:35:26 +0000384 if (E->getType()->isVoidType())
385 return 0;
386 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
387}
388
389Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
390 // Emit subscript expressions in rvalue context's. For most cases, this just
391 // loads the lvalue formed by the subscript expr. However, we have to be
392 // careful, because the base of a vector subscript is occasionally an rvalue,
393 // so we can't get it as an lvalue.
394 if (!E->getBase()->getType()->isVectorType())
395 return EmitLoadOfLValue(E);
396
397 // Handle the vector case. The base must be a vector, the index must be an
398 // integer value.
399 Value *Base = Visit(E->getBase());
400 Value *Idx = Visit(E->getIdx());
401
402 // FIXME: Convert Idx to i32 type.
403 return Builder.CreateExtractElement(Base, Idx, "vecext");
404}
405
406/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
407/// also handle things like function to pointer-to-function decay, and array to
408/// pointer decay.
409Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
410 const Expr *Op = E->getSubExpr();
411
412 // If this is due to array->pointer conversion, emit the array expression as
413 // an l-value.
414 if (Op->getType()->isArrayType()) {
415 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
416 // will not true when we add support for VLAs.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000417 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner7f02f722007-08-24 05:35:26 +0000418
419 assert(isa<llvm::PointerType>(V->getType()) &&
420 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
421 ->getElementType()) &&
422 "Doesn't support VLAs yet!");
423 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Ted Kremenekd6278892007-09-04 17:20:08 +0000424
425 llvm::Value *Ops[] = {Idx0, Idx0};
426 return Builder.CreateGEP(V, Ops, Ops+2, "arraydecay");
Chris Lattner7f02f722007-08-24 05:35:26 +0000427 }
428
429 return EmitCastExpr(Op, E->getType());
430}
431
432
433// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
434// have to handle a more broad range of conversions than explicit casts, as they
435// handle things like function to ptr-to-function decay etc.
436Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner58a2e942007-08-26 07:26:12 +0000437 // Handle cases where the source is an non-complex type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000438 if (!E->getType()->isComplexType()) {
Chris Lattner3707b252007-08-26 06:48:56 +0000439 Value *Src = Visit(const_cast<Expr*>(E));
440
Chris Lattner3707b252007-08-26 06:48:56 +0000441 // Use EmitScalarConversion to perform the conversion.
442 return EmitScalarConversion(Src, E->getType(), DestTy);
443 }
Chris Lattner10b00cf2007-08-26 07:16:41 +0000444
Chris Lattner58a2e942007-08-26 07:26:12 +0000445 // Handle cases where the source is a complex type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000446 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
447 DestTy);
Chris Lattner7f02f722007-08-24 05:35:26 +0000448}
449
Chris Lattner33793202007-08-31 22:09:40 +0000450Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner9b655512007-08-31 22:49:20 +0000451 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal();
Chris Lattner33793202007-08-31 22:09:40 +0000452}
453
454
Chris Lattner7f02f722007-08-24 05:35:26 +0000455//===----------------------------------------------------------------------===//
456// Unary Operators
457//===----------------------------------------------------------------------===//
458
459Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattnerdfce2a52007-08-24 16:24:49 +0000460 bool isInc, bool isPre) {
Chris Lattner7f02f722007-08-24 05:35:26 +0000461 LValue LV = EmitLValue(E->getSubExpr());
462 // FIXME: Handle volatile!
Chris Lattnere936cc82007-08-26 05:10:16 +0000463 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattner9b655512007-08-31 22:49:20 +0000464 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +0000465
466 int AmountVal = isInc ? 1 : -1;
467
468 Value *NextVal;
Chris Lattnere936cc82007-08-26 05:10:16 +0000469 if (isa<llvm::PointerType>(InVal->getType())) {
470 // FIXME: This isn't right for VLAs.
471 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
472 NextVal = Builder.CreateGEP(InVal, NextVal);
473 } else {
474 // Add the inc/dec to the real part.
475 if (isa<llvm::IntegerType>(InVal->getType()))
476 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerca2617c2007-09-13 06:19:18 +0000477 else if (InVal->getType() == llvm::Type::FloatTy)
478 // FIXME: Handle long double.
479 NextVal = llvm::ConstantFP::get(InVal->getType(),
480 llvm::APFloat(static_cast<float>(AmountVal)));
481 else {
482 // FIXME: Handle long double.
483 assert(InVal->getType() == llvm::Type::DoubleTy);
484 NextVal = llvm::ConstantFP::get(InVal->getType(),
485 llvm::APFloat(static_cast<double>(AmountVal)));
486 }
Chris Lattnere936cc82007-08-26 05:10:16 +0000487 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
488 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000489
490 // Store the updated result through the lvalue.
491 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
492 E->getSubExpr()->getType());
493
494 // If this is a postinc, return the value read from memory, otherwise use the
495 // updated value.
496 return isPre ? NextVal : InVal;
497}
498
499
500Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
501 Value *Op = Visit(E->getSubExpr());
502 return Builder.CreateNeg(Op, "neg");
503}
504
505Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
506 Value *Op = Visit(E->getSubExpr());
507 return Builder.CreateNot(Op, "neg");
508}
509
510Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
511 // Compare operand to zero.
512 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
513
514 // Invert value.
515 // TODO: Could dynamically modify easy computations here. For example, if
516 // the operand is an icmp ne, turn into icmp eq.
517 BoolVal = Builder.CreateNot(BoolVal, "lnot");
518
519 // ZExt result to int.
520 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
521}
522
523/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
524/// an integer (RetType).
525Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner46f93d02007-08-24 21:20:17 +0000526 QualType RetType,bool isSizeOf){
Chris Lattner7f02f722007-08-24 05:35:26 +0000527 /// FIXME: This doesn't handle VLAs yet!
528 std::pair<uint64_t, unsigned> Info =
529 CGF.getContext().getTypeInfo(TypeToSize, SourceLocation());
530
531 uint64_t Val = isSizeOf ? Info.first : Info.second;
532 Val /= 8; // Return size in bytes, not bits.
533
534 assert(RetType->isIntegerType() && "Result type must be an integer!");
535
Chris Lattner47f7dbf2007-09-04 02:34:27 +0000536 unsigned ResultWidth = static_cast<unsigned>(CGF.getContext().getTypeSize(RetType,SourceLocation()));
Chris Lattner7f02f722007-08-24 05:35:26 +0000537 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
538}
539
Chris Lattner46f93d02007-08-24 21:20:17 +0000540Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
541 Expr *Op = E->getSubExpr();
542 if (Op->getType()->isComplexType())
543 return CGF.EmitComplexExpr(Op).first;
544 return Visit(Op);
545}
546Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
547 Expr *Op = E->getSubExpr();
548 if (Op->getType()->isComplexType())
549 return CGF.EmitComplexExpr(Op).second;
Chris Lattner36f84062007-08-26 05:29:21 +0000550
551 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
552 // effects are evaluated.
553 CGF.EmitScalarExpr(Op);
554 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner46f93d02007-08-24 21:20:17 +0000555}
556
557
Chris Lattner7f02f722007-08-24 05:35:26 +0000558//===----------------------------------------------------------------------===//
559// Binary Operators
560//===----------------------------------------------------------------------===//
561
562BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
563 BinOpInfo Result;
564 Result.LHS = Visit(E->getLHS());
565 Result.RHS = Visit(E->getRHS());
Chris Lattner1f1ded92007-08-24 21:00:35 +0000566 Result.Ty = E->getType();
Chris Lattner7f02f722007-08-24 05:35:26 +0000567 Result.E = E;
568 return Result;
569}
570
Chris Lattner3ccf7742007-08-26 21:41:21 +0000571Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner1f1ded92007-08-24 21:00:35 +0000572 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
573 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
574
575 BinOpInfo OpInfo;
576
577 // Load the LHS and RHS operands.
578 LValue LHSLV = EmitLValue(E->getLHS());
579 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner04dc7642007-08-26 22:37:40 +0000580
581 // Determine the computation type. If the RHS is complex, then this is one of
582 // the add/sub/mul/div operators. All of these operators can be computed in
583 // with just their real component even though the computation domain really is
584 // complex.
Chris Lattner3ccf7742007-08-26 21:41:21 +0000585 QualType ComputeType = E->getComputationType();
Chris Lattner1f1ded92007-08-24 21:00:35 +0000586
Chris Lattner04dc7642007-08-26 22:37:40 +0000587 // If the computation type is complex, then the RHS is complex. Emit the RHS.
588 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
589 ComputeType = CT->getElementType();
590
591 // Emit the RHS, only keeping the real component.
592 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
593 RHSTy = RHSTy->getAsComplexType()->getElementType();
594 } else {
595 // Otherwise the RHS is a simple scalar value.
596 OpInfo.RHS = Visit(E->getRHS());
597 }
598
599 // Convert the LHS/RHS values to the computation type.
Chris Lattnere9377122007-08-26 07:08:39 +0000600 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000601
602 // Do not merge types for -= where the LHS is a pointer.
Chris Lattner3b44b572007-08-25 21:56:20 +0000603 if (E->getOpcode() != BinaryOperator::SubAssign ||
604 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnere9377122007-08-26 07:08:39 +0000605 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000606 }
607 OpInfo.Ty = ComputeType;
608 OpInfo.E = E;
609
610 // Expand the binary operator.
611 Value *Result = (this->*Func)(OpInfo);
612
613 // Truncate the result back to the LHS type.
Chris Lattnere9377122007-08-26 07:08:39 +0000614 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000615
616 // Store the result value into the LHS lvalue.
617 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
618
619 return Result;
620}
621
622
Chris Lattner7f02f722007-08-24 05:35:26 +0000623Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
624 if (Ops.LHS->getType()->isFloatingPoint())
625 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000626 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000627 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
628 else
629 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
630}
631
632Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
633 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner1f1ded92007-08-24 21:00:35 +0000634 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000635 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
636 else
637 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
638}
639
640
641Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner1f1ded92007-08-24 21:00:35 +0000642 if (!Ops.Ty->isPointerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000643 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000644
645 // FIXME: What about a pointer to a VLA?
Chris Lattner7f02f722007-08-24 05:35:26 +0000646 if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int
647 return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr");
648 // int + pointer
649 return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr");
650}
651
652Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
653 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
654 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
655
Chris Lattner1f1ded92007-08-24 21:00:35 +0000656 // pointer - int
657 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
658 "ptr-ptr shouldn't get here");
659 // FIXME: The pointer could point to a VLA.
660 Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
661 return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr");
662}
663
664Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
665 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
666 // the compound assignment case it is invalid, so just handle it here.
667 if (!E->getRHS()->getType()->isPointerType())
668 return EmitSub(EmitBinOps(E));
Chris Lattner7f02f722007-08-24 05:35:26 +0000669
670 // pointer - pointer
Chris Lattner1f1ded92007-08-24 21:00:35 +0000671 Value *LHS = Visit(E->getLHS());
672 Value *RHS = Visit(E->getRHS());
673
674 const PointerType *LHSPtrType = E->getLHS()->getType()->getAsPointerType();
675 assert(LHSPtrType == E->getRHS()->getType()->getAsPointerType() &&
676 "Can't subtract different pointer types");
677
Chris Lattner7f02f722007-08-24 05:35:26 +0000678 QualType LHSElementType = LHSPtrType->getPointeeType();
Chris Lattner7f02f722007-08-24 05:35:26 +0000679 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType,
680 SourceLocation()) / 8;
Chris Lattner1f1ded92007-08-24 21:00:35 +0000681
682 const llvm::Type *ResultType = ConvertType(E->getType());
683 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
684 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
685 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner7f02f722007-08-24 05:35:26 +0000686
687 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
688 // remainder. As such, we handle common power-of-two cases here to generate
689 // better code.
690 if (llvm::isPowerOf2_64(ElementSize)) {
691 Value *ShAmt =
692 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
693 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
694 }
Chris Lattner1f1ded92007-08-24 21:00:35 +0000695
Chris Lattner7f02f722007-08-24 05:35:26 +0000696 // Otherwise, do a full sdiv.
697 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
698 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
699}
700
Chris Lattner1f1ded92007-08-24 21:00:35 +0000701
Chris Lattner7f02f722007-08-24 05:35:26 +0000702Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
703 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
704 // RHS to the same size as the LHS.
705 Value *RHS = Ops.RHS;
706 if (Ops.LHS->getType() != RHS->getType())
707 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
708
709 return Builder.CreateShl(Ops.LHS, RHS, "shl");
710}
711
712Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
713 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
714 // RHS to the same size as the LHS.
715 Value *RHS = Ops.RHS;
716 if (Ops.LHS->getType() != RHS->getType())
717 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
718
Chris Lattner1f1ded92007-08-24 21:00:35 +0000719 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000720 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
721 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
722}
723
724Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
725 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000726 Value *Result;
Chris Lattner7f02f722007-08-24 05:35:26 +0000727 QualType LHSTy = E->getLHS()->getType();
728 if (!LHSTy->isComplexType()) {
729 Value *LHS = Visit(E->getLHS());
730 Value *RHS = Visit(E->getRHS());
731
732 if (LHS->getType()->isFloatingPoint()) {
733 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
734 LHS, RHS, "cmp");
735 } else if (LHSTy->isUnsignedIntegerType()) {
736 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
737 LHS, RHS, "cmp");
738 } else {
739 // Signed integers and pointers.
740 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
741 LHS, RHS, "cmp");
742 }
743 } else {
744 // Complex Comparison: can only be an equality comparison.
745 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
746 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
747
748 QualType CETy =
749 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
750
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000751 Value *ResultR, *ResultI;
Chris Lattner7f02f722007-08-24 05:35:26 +0000752 if (CETy->isRealFloatingType()) {
753 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
754 LHS.first, RHS.first, "cmp.r");
755 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
756 LHS.second, RHS.second, "cmp.i");
757 } else {
758 // Complex comparisons can only be equality comparisons. As such, signed
759 // and unsigned opcodes are the same.
760 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
761 LHS.first, RHS.first, "cmp.r");
762 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
763 LHS.second, RHS.second, "cmp.i");
764 }
765
766 if (E->getOpcode() == BinaryOperator::EQ) {
767 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
768 } else {
769 assert(E->getOpcode() == BinaryOperator::NE &&
770 "Complex comparison other than == or != ?");
771 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
772 }
773 }
774
775 // ZExt result to int.
776 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
777}
778
779Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
780 LValue LHS = EmitLValue(E->getLHS());
781 Value *RHS = Visit(E->getRHS());
782
783 // Store the value into the LHS.
784 // FIXME: Volatility!
785 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
786
787 // Return the RHS.
788 return RHS;
789}
790
791Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
792 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
793
794 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
795 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
796
797 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
798 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
799
800 CGF.EmitBlock(RHSBlock);
801 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
802
803 // Reaquire the RHS block, as there may be subblocks inserted.
804 RHSBlock = Builder.GetInsertBlock();
805 CGF.EmitBlock(ContBlock);
806
807 // Create a PHI node. If we just evaluted the LHS condition, the result is
808 // false. If we evaluated both, the result is the RHS condition.
809 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
810 PN->reserveOperandSpace(2);
811 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
812 PN->addIncoming(RHSCond, RHSBlock);
813
814 // ZExt result to int.
815 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
816}
817
818Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
819 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
820
821 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
822 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
823
824 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
825 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
826
827 CGF.EmitBlock(RHSBlock);
828 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
829
830 // Reaquire the RHS block, as there may be subblocks inserted.
831 RHSBlock = Builder.GetInsertBlock();
832 CGF.EmitBlock(ContBlock);
833
834 // Create a PHI node. If we just evaluted the LHS condition, the result is
835 // true. If we evaluated both, the result is the RHS condition.
836 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
837 PN->reserveOperandSpace(2);
838 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
839 PN->addIncoming(RHSCond, RHSBlock);
840
841 // ZExt result to int.
842 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
843}
844
845Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
846 CGF.EmitStmt(E->getLHS());
847 return Visit(E->getRHS());
848}
849
850//===----------------------------------------------------------------------===//
851// Other Operators
852//===----------------------------------------------------------------------===//
853
854Value *ScalarExprEmitter::
855VisitConditionalOperator(const ConditionalOperator *E) {
856 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
857 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
858 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
859
860 Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
861 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
862
863 CGF.EmitBlock(LHSBlock);
864
865 // Handle the GNU extension for missing LHS.
866 Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond;
867 Builder.CreateBr(ContBlock);
868 LHSBlock = Builder.GetInsertBlock();
869
870 CGF.EmitBlock(RHSBlock);
871
872 Value *RHS = Visit(E->getRHS());
873 Builder.CreateBr(ContBlock);
874 RHSBlock = Builder.GetInsertBlock();
875
876 CGF.EmitBlock(ContBlock);
877
878 // Create a PHI node for the real part.
879 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
880 PN->reserveOperandSpace(2);
881 PN->addIncoming(LHS, LHSBlock);
882 PN->addIncoming(RHS, RHSBlock);
883 return PN;
884}
885
886Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
887 llvm::APSInt CondVal(32);
888 bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext());
889 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
890
891 // Emit the LHS or RHS as appropriate.
892 return Visit(CondVal != 0 ? E->getLHS() : E->getRHS());
893}
894
895//===----------------------------------------------------------------------===//
896// Entry Point into this File
897//===----------------------------------------------------------------------===//
898
899/// EmitComplexExpr - Emit the computation of the specified expression of
900/// complex type, ignoring the result.
901Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
902 assert(E && !hasAggregateLLVMType(E->getType()) &&
903 "Invalid scalar expression to emit");
904
905 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
906}
Chris Lattner3707b252007-08-26 06:48:56 +0000907
908/// EmitScalarConversion - Emit a conversion from the specified type to the
909/// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000910Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
911 QualType DstTy) {
Chris Lattner3707b252007-08-26 06:48:56 +0000912 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
913 "Invalid scalar expression to emit");
914 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
915}
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000916
917/// EmitComplexToScalarConversion - Emit a conversion from the specified
918/// complex type to the specified destination type, where the destination
919/// type is an LLVM scalar type.
920Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
921 QualType SrcTy,
922 QualType DstTy) {
923 assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) &&
924 "Invalid complex -> scalar conversion");
925 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
926 DstTy);
927}