blob: 62137b03b7d42d19424c5d8494be18ba27c7b592 [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);
477 else
Hartmut Kaiser124dd7b2007-09-07 14:34:20 +0000478 NextVal = llvm::ConstantFP::get(InVal->getType(),
479 static_cast<double>(AmountVal));
Chris Lattnere936cc82007-08-26 05:10:16 +0000480 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
481 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000482
483 // Store the updated result through the lvalue.
484 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
485 E->getSubExpr()->getType());
486
487 // If this is a postinc, return the value read from memory, otherwise use the
488 // updated value.
489 return isPre ? NextVal : InVal;
490}
491
492
493Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
494 Value *Op = Visit(E->getSubExpr());
495 return Builder.CreateNeg(Op, "neg");
496}
497
498Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
499 Value *Op = Visit(E->getSubExpr());
500 return Builder.CreateNot(Op, "neg");
501}
502
503Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
504 // Compare operand to zero.
505 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
506
507 // Invert value.
508 // TODO: Could dynamically modify easy computations here. For example, if
509 // the operand is an icmp ne, turn into icmp eq.
510 BoolVal = Builder.CreateNot(BoolVal, "lnot");
511
512 // ZExt result to int.
513 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
514}
515
516/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
517/// an integer (RetType).
518Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner46f93d02007-08-24 21:20:17 +0000519 QualType RetType,bool isSizeOf){
Chris Lattner7f02f722007-08-24 05:35:26 +0000520 /// FIXME: This doesn't handle VLAs yet!
521 std::pair<uint64_t, unsigned> Info =
522 CGF.getContext().getTypeInfo(TypeToSize, SourceLocation());
523
524 uint64_t Val = isSizeOf ? Info.first : Info.second;
525 Val /= 8; // Return size in bytes, not bits.
526
527 assert(RetType->isIntegerType() && "Result type must be an integer!");
528
Chris Lattner47f7dbf2007-09-04 02:34:27 +0000529 unsigned ResultWidth = static_cast<unsigned>(CGF.getContext().getTypeSize(RetType,SourceLocation()));
Chris Lattner7f02f722007-08-24 05:35:26 +0000530 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
531}
532
Chris Lattner46f93d02007-08-24 21:20:17 +0000533Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
534 Expr *Op = E->getSubExpr();
535 if (Op->getType()->isComplexType())
536 return CGF.EmitComplexExpr(Op).first;
537 return Visit(Op);
538}
539Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
540 Expr *Op = E->getSubExpr();
541 if (Op->getType()->isComplexType())
542 return CGF.EmitComplexExpr(Op).second;
Chris Lattner36f84062007-08-26 05:29:21 +0000543
544 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
545 // effects are evaluated.
546 CGF.EmitScalarExpr(Op);
547 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner46f93d02007-08-24 21:20:17 +0000548}
549
550
Chris Lattner7f02f722007-08-24 05:35:26 +0000551//===----------------------------------------------------------------------===//
552// Binary Operators
553//===----------------------------------------------------------------------===//
554
555BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
556 BinOpInfo Result;
557 Result.LHS = Visit(E->getLHS());
558 Result.RHS = Visit(E->getRHS());
Chris Lattner1f1ded92007-08-24 21:00:35 +0000559 Result.Ty = E->getType();
Chris Lattner7f02f722007-08-24 05:35:26 +0000560 Result.E = E;
561 return Result;
562}
563
Chris Lattner3ccf7742007-08-26 21:41:21 +0000564Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner1f1ded92007-08-24 21:00:35 +0000565 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
566 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
567
568 BinOpInfo OpInfo;
569
570 // Load the LHS and RHS operands.
571 LValue LHSLV = EmitLValue(E->getLHS());
572 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner04dc7642007-08-26 22:37:40 +0000573
574 // Determine the computation type. If the RHS is complex, then this is one of
575 // the add/sub/mul/div operators. All of these operators can be computed in
576 // with just their real component even though the computation domain really is
577 // complex.
Chris Lattner3ccf7742007-08-26 21:41:21 +0000578 QualType ComputeType = E->getComputationType();
Chris Lattner1f1ded92007-08-24 21:00:35 +0000579
Chris Lattner04dc7642007-08-26 22:37:40 +0000580 // If the computation type is complex, then the RHS is complex. Emit the RHS.
581 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
582 ComputeType = CT->getElementType();
583
584 // Emit the RHS, only keeping the real component.
585 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
586 RHSTy = RHSTy->getAsComplexType()->getElementType();
587 } else {
588 // Otherwise the RHS is a simple scalar value.
589 OpInfo.RHS = Visit(E->getRHS());
590 }
591
592 // Convert the LHS/RHS values to the computation type.
Chris Lattnere9377122007-08-26 07:08:39 +0000593 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000594
595 // Do not merge types for -= where the LHS is a pointer.
Chris Lattner3b44b572007-08-25 21:56:20 +0000596 if (E->getOpcode() != BinaryOperator::SubAssign ||
597 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnere9377122007-08-26 07:08:39 +0000598 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000599 }
600 OpInfo.Ty = ComputeType;
601 OpInfo.E = E;
602
603 // Expand the binary operator.
604 Value *Result = (this->*Func)(OpInfo);
605
606 // Truncate the result back to the LHS type.
Chris Lattnere9377122007-08-26 07:08:39 +0000607 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000608
609 // Store the result value into the LHS lvalue.
610 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
611
612 return Result;
613}
614
615
Chris Lattner7f02f722007-08-24 05:35:26 +0000616Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
617 if (Ops.LHS->getType()->isFloatingPoint())
618 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000619 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000620 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
621 else
622 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
623}
624
625Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
626 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner1f1ded92007-08-24 21:00:35 +0000627 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000628 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
629 else
630 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
631}
632
633
634Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner1f1ded92007-08-24 21:00:35 +0000635 if (!Ops.Ty->isPointerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000636 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000637
638 // FIXME: What about a pointer to a VLA?
Chris Lattner7f02f722007-08-24 05:35:26 +0000639 if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int
640 return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr");
641 // int + pointer
642 return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr");
643}
644
645Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
646 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
647 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
648
Chris Lattner1f1ded92007-08-24 21:00:35 +0000649 // pointer - int
650 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
651 "ptr-ptr shouldn't get here");
652 // FIXME: The pointer could point to a VLA.
653 Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
654 return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr");
655}
656
657Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
658 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
659 // the compound assignment case it is invalid, so just handle it here.
660 if (!E->getRHS()->getType()->isPointerType())
661 return EmitSub(EmitBinOps(E));
Chris Lattner7f02f722007-08-24 05:35:26 +0000662
663 // pointer - pointer
Chris Lattner1f1ded92007-08-24 21:00:35 +0000664 Value *LHS = Visit(E->getLHS());
665 Value *RHS = Visit(E->getRHS());
666
667 const PointerType *LHSPtrType = E->getLHS()->getType()->getAsPointerType();
668 assert(LHSPtrType == E->getRHS()->getType()->getAsPointerType() &&
669 "Can't subtract different pointer types");
670
Chris Lattner7f02f722007-08-24 05:35:26 +0000671 QualType LHSElementType = LHSPtrType->getPointeeType();
Chris Lattner7f02f722007-08-24 05:35:26 +0000672 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType,
673 SourceLocation()) / 8;
Chris Lattner1f1ded92007-08-24 21:00:35 +0000674
675 const llvm::Type *ResultType = ConvertType(E->getType());
676 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
677 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
678 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner7f02f722007-08-24 05:35:26 +0000679
680 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
681 // remainder. As such, we handle common power-of-two cases here to generate
682 // better code.
683 if (llvm::isPowerOf2_64(ElementSize)) {
684 Value *ShAmt =
685 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
686 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
687 }
Chris Lattner1f1ded92007-08-24 21:00:35 +0000688
Chris Lattner7f02f722007-08-24 05:35:26 +0000689 // Otherwise, do a full sdiv.
690 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
691 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
692}
693
Chris Lattner1f1ded92007-08-24 21:00:35 +0000694
Chris Lattner7f02f722007-08-24 05:35:26 +0000695Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
696 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
697 // RHS to the same size as the LHS.
698 Value *RHS = Ops.RHS;
699 if (Ops.LHS->getType() != RHS->getType())
700 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
701
702 return Builder.CreateShl(Ops.LHS, RHS, "shl");
703}
704
705Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
706 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
707 // RHS to the same size as the LHS.
708 Value *RHS = Ops.RHS;
709 if (Ops.LHS->getType() != RHS->getType())
710 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
711
Chris Lattner1f1ded92007-08-24 21:00:35 +0000712 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000713 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
714 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
715}
716
717Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
718 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000719 Value *Result;
Chris Lattner7f02f722007-08-24 05:35:26 +0000720 QualType LHSTy = E->getLHS()->getType();
721 if (!LHSTy->isComplexType()) {
722 Value *LHS = Visit(E->getLHS());
723 Value *RHS = Visit(E->getRHS());
724
725 if (LHS->getType()->isFloatingPoint()) {
726 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
727 LHS, RHS, "cmp");
728 } else if (LHSTy->isUnsignedIntegerType()) {
729 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
730 LHS, RHS, "cmp");
731 } else {
732 // Signed integers and pointers.
733 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
734 LHS, RHS, "cmp");
735 }
736 } else {
737 // Complex Comparison: can only be an equality comparison.
738 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
739 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
740
741 QualType CETy =
742 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
743
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000744 Value *ResultR, *ResultI;
Chris Lattner7f02f722007-08-24 05:35:26 +0000745 if (CETy->isRealFloatingType()) {
746 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
747 LHS.first, RHS.first, "cmp.r");
748 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
749 LHS.second, RHS.second, "cmp.i");
750 } else {
751 // Complex comparisons can only be equality comparisons. As such, signed
752 // and unsigned opcodes are the same.
753 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
754 LHS.first, RHS.first, "cmp.r");
755 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
756 LHS.second, RHS.second, "cmp.i");
757 }
758
759 if (E->getOpcode() == BinaryOperator::EQ) {
760 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
761 } else {
762 assert(E->getOpcode() == BinaryOperator::NE &&
763 "Complex comparison other than == or != ?");
764 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
765 }
766 }
767
768 // ZExt result to int.
769 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
770}
771
772Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
773 LValue LHS = EmitLValue(E->getLHS());
774 Value *RHS = Visit(E->getRHS());
775
776 // Store the value into the LHS.
777 // FIXME: Volatility!
778 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
779
780 // Return the RHS.
781 return RHS;
782}
783
784Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
785 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
786
787 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
788 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
789
790 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
791 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
792
793 CGF.EmitBlock(RHSBlock);
794 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
795
796 // Reaquire the RHS block, as there may be subblocks inserted.
797 RHSBlock = Builder.GetInsertBlock();
798 CGF.EmitBlock(ContBlock);
799
800 // Create a PHI node. If we just evaluted the LHS condition, the result is
801 // false. If we evaluated both, the result is the RHS condition.
802 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
803 PN->reserveOperandSpace(2);
804 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
805 PN->addIncoming(RHSCond, RHSBlock);
806
807 // ZExt result to int.
808 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
809}
810
811Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
812 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
813
814 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
815 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
816
817 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
818 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
819
820 CGF.EmitBlock(RHSBlock);
821 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
822
823 // Reaquire the RHS block, as there may be subblocks inserted.
824 RHSBlock = Builder.GetInsertBlock();
825 CGF.EmitBlock(ContBlock);
826
827 // Create a PHI node. If we just evaluted the LHS condition, the result is
828 // true. If we evaluated both, the result is the RHS condition.
829 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
830 PN->reserveOperandSpace(2);
831 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
832 PN->addIncoming(RHSCond, RHSBlock);
833
834 // ZExt result to int.
835 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
836}
837
838Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
839 CGF.EmitStmt(E->getLHS());
840 return Visit(E->getRHS());
841}
842
843//===----------------------------------------------------------------------===//
844// Other Operators
845//===----------------------------------------------------------------------===//
846
847Value *ScalarExprEmitter::
848VisitConditionalOperator(const ConditionalOperator *E) {
849 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
850 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
851 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
852
853 Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
854 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
855
856 CGF.EmitBlock(LHSBlock);
857
858 // Handle the GNU extension for missing LHS.
859 Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond;
860 Builder.CreateBr(ContBlock);
861 LHSBlock = Builder.GetInsertBlock();
862
863 CGF.EmitBlock(RHSBlock);
864
865 Value *RHS = Visit(E->getRHS());
866 Builder.CreateBr(ContBlock);
867 RHSBlock = Builder.GetInsertBlock();
868
869 CGF.EmitBlock(ContBlock);
870
871 // Create a PHI node for the real part.
872 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
873 PN->reserveOperandSpace(2);
874 PN->addIncoming(LHS, LHSBlock);
875 PN->addIncoming(RHS, RHSBlock);
876 return PN;
877}
878
879Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
880 llvm::APSInt CondVal(32);
881 bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext());
882 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
883
884 // Emit the LHS or RHS as appropriate.
885 return Visit(CondVal != 0 ? E->getLHS() : E->getRHS());
886}
887
888//===----------------------------------------------------------------------===//
889// Entry Point into this File
890//===----------------------------------------------------------------------===//
891
892/// EmitComplexExpr - Emit the computation of the specified expression of
893/// complex type, ignoring the result.
894Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
895 assert(E && !hasAggregateLLVMType(E->getType()) &&
896 "Invalid scalar expression to emit");
897
898 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
899}
Chris Lattner3707b252007-08-26 06:48:56 +0000900
901/// EmitScalarConversion - Emit a conversion from the specified type to the
902/// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000903Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
904 QualType DstTy) {
Chris Lattner3707b252007-08-26 06:48:56 +0000905 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
906 "Invalid scalar expression to emit");
907 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
908}
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000909
910/// EmitComplexToScalarConversion - Emit a conversion from the specified
911/// complex type to the specified destination type, where the destination
912/// type is an LLVM scalar type.
913Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
914 QualType SrcTy,
915 QualType DstTy) {
916 assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) &&
917 "Invalid complex -> scalar conversion");
918 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
919 DstTy);
920}