blob: 755fd56de66007f61f6fb1316f1a2942ea0a694a [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"
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 Lattner660e31d2007-08-24 21:00:35 +000031 QualType Ty; // Computation Type.
Chris Lattner9fba49a2007-08-24 05:35:26 +000032 const BinaryOperator *E;
33};
34
35namespace {
36class VISIBILITY_HIDDEN ScalarExprEmitter
37 : public StmtVisitor<ScalarExprEmitter, Value*> {
38 CodeGenFunction &CGF;
Devang Patel638b64c2007-10-09 19:49:58 +000039 llvm::LLVMFoldingBuilder &Builder;
Chris Lattner9fba49a2007-08-24 05:35:26 +000040public:
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 Lattnere24c4cf2007-08-31 22:49:20 +000054 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner9fba49a2007-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 Lattnerd8d44222007-08-26 16:42:57 +000065 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +000066 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +000067 Value *EmitConversionToBool(Value *Src, QualType DstTy);
68
Chris Lattner4e05d1e2007-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 Lattnerfb182ee2007-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 Lattner4e05d1e2007-08-26 06:48:56 +000078
Chris Lattner9fba49a2007-08-24 05:35:26 +000079 //===--------------------------------------------------------------------===//
80 // Visitor Methods
81 //===--------------------------------------------------------------------===//
82
83 Value *VisitStmt(Stmt *S) {
Chris Lattner1aef6212007-09-13 01:17:29 +000084 S->dump(CGF.getContext().SourceMgr);
Chris Lattner9fba49a2007-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 Lattner7f298762007-09-22 18:47:25 +000096 return llvm::ConstantFP::get(ConvertType(E->getType()), E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +000097 }
98 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
99 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
100 }
101 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
102 return llvm::ConstantInt::get(ConvertType(E->getType()),
103 E->typesAreCompatible());
104 }
105 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
106 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
107 }
108
109 // l-values.
110 Value *VisitDeclRefExpr(DeclRefExpr *E) {
111 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
112 return llvm::ConstantInt::get(EC->getInitVal());
113 return EmitLoadOfLValue(E);
114 }
115 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
116 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
117 Value *VisitOCUVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
118 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
119 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
120
121 // FIXME: CompoundLiteralExpr
122 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
123 Value *VisitCastExpr(const CastExpr *E) {
124 return EmitCastExpr(E->getSubExpr(), E->getType());
125 }
126 Value *EmitCastExpr(const Expr *E, QualType T);
127
128 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000129 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000130 }
131
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000132 Value *VisitStmtExpr(const StmtExpr *E);
133
Chris Lattner9fba49a2007-08-24 05:35:26 +0000134 // Unary Operators.
135 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
136 Value *VisitUnaryPostDec(const UnaryOperator *E) {
137 return VisitPrePostIncDec(E, false, false);
138 }
139 Value *VisitUnaryPostInc(const UnaryOperator *E) {
140 return VisitPrePostIncDec(E, true, false);
141 }
142 Value *VisitUnaryPreDec(const UnaryOperator *E) {
143 return VisitPrePostIncDec(E, false, true);
144 }
145 Value *VisitUnaryPreInc(const UnaryOperator *E) {
146 return VisitPrePostIncDec(E, true, true);
147 }
148 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
149 return EmitLValue(E->getSubExpr()).getAddress();
150 }
151 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
152 Value *VisitUnaryPlus(const UnaryOperator *E) {
153 return Visit(E->getSubExpr());
154 }
155 Value *VisitUnaryMinus (const UnaryOperator *E);
156 Value *VisitUnaryNot (const UnaryOperator *E);
157 Value *VisitUnaryLNot (const UnaryOperator *E);
158 Value *VisitUnarySizeOf (const UnaryOperator *E) {
159 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
160 }
161 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
162 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
163 }
164 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
165 bool isSizeOf);
Chris Lattner01211af2007-08-24 21:20:17 +0000166 Value *VisitUnaryReal (const UnaryOperator *E);
167 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000168 Value *VisitUnaryExtension(const UnaryOperator *E) {
169 return Visit(E->getSubExpr());
170 }
171
172 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000173 Value *EmitMul(const BinOpInfo &Ops) {
174 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
175 }
176 Value *EmitDiv(const BinOpInfo &Ops);
177 Value *EmitRem(const BinOpInfo &Ops);
178 Value *EmitAdd(const BinOpInfo &Ops);
179 Value *EmitSub(const BinOpInfo &Ops);
180 Value *EmitShl(const BinOpInfo &Ops);
181 Value *EmitShr(const BinOpInfo &Ops);
182 Value *EmitAnd(const BinOpInfo &Ops) {
183 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
184 }
185 Value *EmitXor(const BinOpInfo &Ops) {
186 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
187 }
188 Value *EmitOr (const BinOpInfo &Ops) {
189 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
190 }
191
Chris Lattner660e31d2007-08-24 21:00:35 +0000192 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000193 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000194 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
195
196 // Binary operators and binary compound assignment operators.
197#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000198 Value *VisitBin ## OP(const BinaryOperator *E) { \
199 return Emit ## OP(EmitBinOps(E)); \
200 } \
201 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
202 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000203 }
204 HANDLEBINOP(Mul);
205 HANDLEBINOP(Div);
206 HANDLEBINOP(Rem);
207 HANDLEBINOP(Add);
208 // (Sub) - Sub is handled specially below for ptr-ptr subtract.
209 HANDLEBINOP(Shl);
210 HANDLEBINOP(Shr);
211 HANDLEBINOP(And);
212 HANDLEBINOP(Xor);
213 HANDLEBINOP(Or);
214#undef HANDLEBINOP
215 Value *VisitBinSub(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000216 Value *VisitBinSubAssign(const CompoundAssignOperator *E) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000217 return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub);
218 }
219
Chris Lattner9fba49a2007-08-24 05:35:26 +0000220 // Comparisons.
221 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
222 unsigned SICmpOpc, unsigned FCmpOpc);
223#define VISITCOMP(CODE, UI, SI, FP) \
224 Value *VisitBin##CODE(const BinaryOperator *E) { \
225 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
226 llvm::FCmpInst::FP); }
227 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
228 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
229 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
230 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
231 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
232 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
233#undef VISITCOMP
234
235 Value *VisitBinAssign (const BinaryOperator *E);
236
237 Value *VisitBinLAnd (const BinaryOperator *E);
238 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000239 Value *VisitBinComma (const BinaryOperator *E);
240
241 // Other Operators.
242 Value *VisitConditionalOperator(const ConditionalOperator *CO);
243 Value *VisitChooseExpr(ChooseExpr *CE);
244 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
245 return CGF.EmitObjCStringLiteral(E);
246 }
247};
248} // end anonymous namespace.
249
250//===----------------------------------------------------------------------===//
251// Utilities
252//===----------------------------------------------------------------------===//
253
Chris Lattnerd8d44222007-08-26 16:42:57 +0000254/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000255/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000256Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
257 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
258
259 if (SrcType->isRealFloatingType()) {
260 // Compare against 0.0 for fp scalars.
261 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000262 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
263 }
264
265 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
266 "Unknown scalar type to convert");
267
268 // Because of the type rules of C, we often end up computing a logical value,
269 // then zero extending it to int, then wanting it as a logical value again.
270 // Optimize this common case.
271 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
272 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
273 Value *Result = ZI->getOperand(0);
274 ZI->eraseFromParent();
275 return Result;
276 }
277 }
278
279 // Compare against an integer or pointer null.
280 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
281 return Builder.CreateICmpNE(Src, Zero, "tobool");
282}
283
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000284/// EmitScalarConversion - Emit a conversion from the specified type to the
285/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000286Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
287 QualType DstType) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000288 SrcType = SrcType.getCanonicalType();
289 DstType = DstType.getCanonicalType();
290 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000291
292 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000293
294 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000295 if (DstType->isBooleanType())
296 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000297
298 const llvm::Type *DstTy = ConvertType(DstType);
299
300 // Ignore conversions like int -> uint.
301 if (Src->getType() == DstTy)
302 return Src;
303
304 // Handle pointer conversions next: pointers can only be converted to/from
305 // other pointers and integers.
306 if (isa<PointerType>(DstType)) {
307 // The source value may be an integer, or a pointer.
308 if (isa<llvm::PointerType>(Src->getType()))
309 return Builder.CreateBitCast(Src, DstTy, "conv");
310 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
311 return Builder.CreateIntToPtr(Src, DstTy, "conv");
312 }
313
314 if (isa<PointerType>(SrcType)) {
315 // Must be an ptr to int cast.
316 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
317 return Builder.CreateIntToPtr(Src, DstTy, "conv");
318 }
319
320 // Finally, we have the arithmetic types: real int/float.
321 if (isa<llvm::IntegerType>(Src->getType())) {
322 bool InputSigned = SrcType->isSignedIntegerType();
323 if (isa<llvm::IntegerType>(DstTy))
324 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
325 else if (InputSigned)
326 return Builder.CreateSIToFP(Src, DstTy, "conv");
327 else
328 return Builder.CreateUIToFP(Src, DstTy, "conv");
329 }
330
331 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
332 if (isa<llvm::IntegerType>(DstTy)) {
333 if (DstType->isSignedIntegerType())
334 return Builder.CreateFPToSI(Src, DstTy, "conv");
335 else
336 return Builder.CreateFPToUI(Src, DstTy, "conv");
337 }
338
339 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
340 if (DstTy->getTypeID() < Src->getType()->getTypeID())
341 return Builder.CreateFPTrunc(Src, DstTy, "conv");
342 else
343 return Builder.CreateFPExt(Src, DstTy, "conv");
344}
345
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000346/// EmitComplexToScalarConversion - Emit a conversion from the specified
347/// complex type to the specified destination type, where the destination
348/// type is an LLVM scalar type.
349Value *ScalarExprEmitter::
350EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
351 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000352 // Get the source element type.
353 SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType();
354
355 // Handle conversions to bool first, they are special: comparisons against 0.
356 if (DstTy->isBooleanType()) {
357 // Complex != 0 -> (Real != 0) | (Imag != 0)
358 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
359 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
360 return Builder.CreateOr(Src.first, Src.second, "tobool");
361 }
362
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000363 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
364 // the imaginary part of the complex value is discarded and the value of the
365 // real part is converted according to the conversion rules for the
366 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000367 return EmitScalarConversion(Src.first, SrcTy, DstTy);
368}
369
370
Chris Lattner9fba49a2007-08-24 05:35:26 +0000371//===----------------------------------------------------------------------===//
372// Visitor Methods
373//===----------------------------------------------------------------------===//
374
375Value *ScalarExprEmitter::VisitExpr(Expr *E) {
376 fprintf(stderr, "Unimplemented scalar expr!\n");
Chris Lattner1aef6212007-09-13 01:17:29 +0000377 E->dump(CGF.getContext().SourceMgr);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000378 if (E->getType()->isVoidType())
379 return 0;
380 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
381}
382
383Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
384 // Emit subscript expressions in rvalue context's. For most cases, this just
385 // loads the lvalue formed by the subscript expr. However, we have to be
386 // careful, because the base of a vector subscript is occasionally an rvalue,
387 // so we can't get it as an lvalue.
388 if (!E->getBase()->getType()->isVectorType())
389 return EmitLoadOfLValue(E);
390
391 // Handle the vector case. The base must be a vector, the index must be an
392 // integer value.
393 Value *Base = Visit(E->getBase());
394 Value *Idx = Visit(E->getIdx());
395
396 // FIXME: Convert Idx to i32 type.
397 return Builder.CreateExtractElement(Base, Idx, "vecext");
398}
399
400/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
401/// also handle things like function to pointer-to-function decay, and array to
402/// pointer decay.
403Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
404 const Expr *Op = E->getSubExpr();
405
406 // If this is due to array->pointer conversion, emit the array expression as
407 // an l-value.
408 if (Op->getType()->isArrayType()) {
409 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
410 // will not true when we add support for VLAs.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000411 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000412
413 assert(isa<llvm::PointerType>(V->getType()) &&
414 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
415 ->getElementType()) &&
416 "Doesn't support VLAs yet!");
417 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Ted Kremenek7f6f4a42007-09-04 17:20:08 +0000418
419 llvm::Value *Ops[] = {Idx0, Idx0};
420 return Builder.CreateGEP(V, Ops, Ops+2, "arraydecay");
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000421 } else if (E->getType()->isReferenceType()) {
422 assert(cast<ReferenceType>(E->getType())->getReferenceeType() ==
423 Op->getType() && "Incompatible types!");
424
425 return EmitLValue(Op).getAddress();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000426 }
427
428 return EmitCastExpr(Op, E->getType());
429}
430
431
432// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
433// have to handle a more broad range of conversions than explicit casts, as they
434// handle things like function to ptr-to-function decay etc.
435Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000436 // Handle cases where the source is an non-complex type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000437 if (!E->getType()->isComplexType()) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000438 Value *Src = Visit(const_cast<Expr*>(E));
439
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000440 // Use EmitScalarConversion to perform the conversion.
441 return EmitScalarConversion(Src, E->getType(), DestTy);
442 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000443
Chris Lattner82e10392007-08-26 07:26:12 +0000444 // Handle cases where the source is a complex type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000445 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
446 DestTy);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000447}
448
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000449Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000450 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000451}
452
453
Chris Lattner9fba49a2007-08-24 05:35:26 +0000454//===----------------------------------------------------------------------===//
455// Unary Operators
456//===----------------------------------------------------------------------===//
457
458Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000459 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000460 LValue LV = EmitLValue(E->getSubExpr());
461 // FIXME: Handle volatile!
Chris Lattner0dc11f62007-08-26 05:10:16 +0000462 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000463 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000464
465 int AmountVal = isInc ? 1 : -1;
466
467 Value *NextVal;
Chris Lattner0dc11f62007-08-26 05:10:16 +0000468 if (isa<llvm::PointerType>(InVal->getType())) {
469 // FIXME: This isn't right for VLAs.
470 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
471 NextVal = Builder.CreateGEP(InVal, NextVal);
472 } else {
473 // Add the inc/dec to the real part.
474 if (isa<llvm::IntegerType>(InVal->getType()))
475 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000476 else if (InVal->getType() == llvm::Type::FloatTy)
477 // FIXME: Handle long double.
478 NextVal = llvm::ConstantFP::get(InVal->getType(),
479 llvm::APFloat(static_cast<float>(AmountVal)));
480 else {
481 // FIXME: Handle long double.
482 assert(InVal->getType() == llvm::Type::DoubleTy);
483 NextVal = llvm::ConstantFP::get(InVal->getType(),
484 llvm::APFloat(static_cast<double>(AmountVal)));
485 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000486 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
487 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000488
489 // Store the updated result through the lvalue.
490 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
491 E->getSubExpr()->getType());
492
493 // If this is a postinc, return the value read from memory, otherwise use the
494 // updated value.
495 return isPre ? NextVal : InVal;
496}
497
498
499Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
500 Value *Op = Visit(E->getSubExpr());
501 return Builder.CreateNeg(Op, "neg");
502}
503
504Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
505 Value *Op = Visit(E->getSubExpr());
506 return Builder.CreateNot(Op, "neg");
507}
508
509Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
510 // Compare operand to zero.
511 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
512
513 // Invert value.
514 // TODO: Could dynamically modify easy computations here. For example, if
515 // the operand is an icmp ne, turn into icmp eq.
516 BoolVal = Builder.CreateNot(BoolVal, "lnot");
517
518 // ZExt result to int.
519 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
520}
521
522/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
523/// an integer (RetType).
524Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner01211af2007-08-24 21:20:17 +0000525 QualType RetType,bool isSizeOf){
Chris Lattner9fba49a2007-08-24 05:35:26 +0000526 /// FIXME: This doesn't handle VLAs yet!
527 std::pair<uint64_t, unsigned> Info =
528 CGF.getContext().getTypeInfo(TypeToSize, SourceLocation());
529
530 uint64_t Val = isSizeOf ? Info.first : Info.second;
531 Val /= 8; // Return size in bytes, not bits.
532
533 assert(RetType->isIntegerType() && "Result type must be an integer!");
534
Chris Lattnera96e0d82007-09-04 02:34:27 +0000535 unsigned ResultWidth = static_cast<unsigned>(CGF.getContext().getTypeSize(RetType,SourceLocation()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000536 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
537}
538
Chris Lattner01211af2007-08-24 21:20:17 +0000539Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
540 Expr *Op = E->getSubExpr();
541 if (Op->getType()->isComplexType())
542 return CGF.EmitComplexExpr(Op).first;
543 return Visit(Op);
544}
545Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
546 Expr *Op = E->getSubExpr();
547 if (Op->getType()->isComplexType())
548 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000549
550 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
551 // effects are evaluated.
552 CGF.EmitScalarExpr(Op);
553 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000554}
555
556
Chris Lattner9fba49a2007-08-24 05:35:26 +0000557//===----------------------------------------------------------------------===//
558// Binary Operators
559//===----------------------------------------------------------------------===//
560
561BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
562 BinOpInfo Result;
563 Result.LHS = Visit(E->getLHS());
564 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000565 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000566 Result.E = E;
567 return Result;
568}
569
Chris Lattner0d965302007-08-26 21:41:21 +0000570Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000571 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
572 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
573
574 BinOpInfo OpInfo;
575
576 // Load the LHS and RHS operands.
577 LValue LHSLV = EmitLValue(E->getLHS());
578 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000579
580 // Determine the computation type. If the RHS is complex, then this is one of
581 // the add/sub/mul/div operators. All of these operators can be computed in
582 // with just their real component even though the computation domain really is
583 // complex.
Chris Lattner0d965302007-08-26 21:41:21 +0000584 QualType ComputeType = E->getComputationType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000585
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000586 // If the computation type is complex, then the RHS is complex. Emit the RHS.
587 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
588 ComputeType = CT->getElementType();
589
590 // Emit the RHS, only keeping the real component.
591 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
592 RHSTy = RHSTy->getAsComplexType()->getElementType();
593 } else {
594 // Otherwise the RHS is a simple scalar value.
595 OpInfo.RHS = Visit(E->getRHS());
596 }
597
598 // Convert the LHS/RHS values to the computation type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000599 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000600
601 // Do not merge types for -= where the LHS is a pointer.
Chris Lattner42330c32007-08-25 21:56:20 +0000602 if (E->getOpcode() != BinaryOperator::SubAssign ||
603 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnerb1497062007-08-26 07:08:39 +0000604 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000605 }
606 OpInfo.Ty = ComputeType;
607 OpInfo.E = E;
608
609 // Expand the binary operator.
610 Value *Result = (this->*Func)(OpInfo);
611
612 // Truncate the result back to the LHS type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000613 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000614
615 // Store the result value into the LHS lvalue.
616 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
617
618 return Result;
619}
620
621
Chris Lattner9fba49a2007-08-24 05:35:26 +0000622Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
623 if (Ops.LHS->getType()->isFloatingPoint())
624 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000625 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000626 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
627 else
628 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
629}
630
631Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
632 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000633 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000634 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
635 else
636 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
637}
638
639
640Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000641 if (!Ops.Ty->isPointerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000642 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner660e31d2007-08-24 21:00:35 +0000643
644 // FIXME: What about a pointer to a VLA?
Chris Lattner9fba49a2007-08-24 05:35:26 +0000645 if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int
646 return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr");
647 // int + pointer
648 return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr");
649}
650
651Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
652 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
653 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
654
Chris Lattner660e31d2007-08-24 21:00:35 +0000655 // pointer - int
656 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
657 "ptr-ptr shouldn't get here");
658 // FIXME: The pointer could point to a VLA.
659 Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
660 return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr");
661}
662
663Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
664 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
665 // the compound assignment case it is invalid, so just handle it here.
666 if (!E->getRHS()->getType()->isPointerType())
667 return EmitSub(EmitBinOps(E));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000668
669 // pointer - pointer
Chris Lattner660e31d2007-08-24 21:00:35 +0000670 Value *LHS = Visit(E->getLHS());
671 Value *RHS = Visit(E->getRHS());
672
673 const PointerType *LHSPtrType = E->getLHS()->getType()->getAsPointerType();
674 assert(LHSPtrType == E->getRHS()->getType()->getAsPointerType() &&
675 "Can't subtract different pointer types");
676
Chris Lattner9fba49a2007-08-24 05:35:26 +0000677 QualType LHSElementType = LHSPtrType->getPointeeType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000678 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType,
679 SourceLocation()) / 8;
Chris Lattner660e31d2007-08-24 21:00:35 +0000680
681 const llvm::Type *ResultType = ConvertType(E->getType());
682 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
683 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
684 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000685
686 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
687 // remainder. As such, we handle common power-of-two cases here to generate
688 // better code.
689 if (llvm::isPowerOf2_64(ElementSize)) {
690 Value *ShAmt =
691 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
692 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
693 }
Chris Lattner660e31d2007-08-24 21:00:35 +0000694
Chris Lattner9fba49a2007-08-24 05:35:26 +0000695 // Otherwise, do a full sdiv.
696 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
697 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
698}
699
Chris Lattner660e31d2007-08-24 21:00:35 +0000700
Chris Lattner9fba49a2007-08-24 05:35:26 +0000701Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
702 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
703 // RHS to the same size as the LHS.
704 Value *RHS = Ops.RHS;
705 if (Ops.LHS->getType() != RHS->getType())
706 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
707
708 return Builder.CreateShl(Ops.LHS, RHS, "shl");
709}
710
711Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
712 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
713 // RHS to the same size as the LHS.
714 Value *RHS = Ops.RHS;
715 if (Ops.LHS->getType() != RHS->getType())
716 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
717
Chris Lattner660e31d2007-08-24 21:00:35 +0000718 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000719 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
720 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
721}
722
723Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
724 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000725 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000726 QualType LHSTy = E->getLHS()->getType();
727 if (!LHSTy->isComplexType()) {
728 Value *LHS = Visit(E->getLHS());
729 Value *RHS = Visit(E->getRHS());
730
731 if (LHS->getType()->isFloatingPoint()) {
732 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
733 LHS, RHS, "cmp");
734 } else if (LHSTy->isUnsignedIntegerType()) {
735 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
736 LHS, RHS, "cmp");
737 } else {
738 // Signed integers and pointers.
739 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
740 LHS, RHS, "cmp");
741 }
742 } else {
743 // Complex Comparison: can only be an equality comparison.
744 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
745 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
746
747 QualType CETy =
748 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
749
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000750 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000751 if (CETy->isRealFloatingType()) {
752 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
753 LHS.first, RHS.first, "cmp.r");
754 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
755 LHS.second, RHS.second, "cmp.i");
756 } else {
757 // Complex comparisons can only be equality comparisons. As such, signed
758 // and unsigned opcodes are the same.
759 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
760 LHS.first, RHS.first, "cmp.r");
761 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
762 LHS.second, RHS.second, "cmp.i");
763 }
764
765 if (E->getOpcode() == BinaryOperator::EQ) {
766 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
767 } else {
768 assert(E->getOpcode() == BinaryOperator::NE &&
769 "Complex comparison other than == or != ?");
770 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
771 }
772 }
773
774 // ZExt result to int.
775 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
776}
777
778Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
779 LValue LHS = EmitLValue(E->getLHS());
780 Value *RHS = Visit(E->getRHS());
781
782 // Store the value into the LHS.
783 // FIXME: Volatility!
784 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
785
786 // Return the RHS.
787 return RHS;
788}
789
790Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
791 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
792
793 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
794 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
795
796 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
797 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
798
799 CGF.EmitBlock(RHSBlock);
800 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
801
802 // Reaquire the RHS block, as there may be subblocks inserted.
803 RHSBlock = Builder.GetInsertBlock();
804 CGF.EmitBlock(ContBlock);
805
806 // Create a PHI node. If we just evaluted the LHS condition, the result is
807 // false. If we evaluated both, the result is the RHS condition.
808 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
809 PN->reserveOperandSpace(2);
810 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
811 PN->addIncoming(RHSCond, RHSBlock);
812
813 // ZExt result to int.
814 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
815}
816
817Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
818 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
819
820 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
821 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
822
823 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
824 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
825
826 CGF.EmitBlock(RHSBlock);
827 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
828
829 // Reaquire the RHS block, as there may be subblocks inserted.
830 RHSBlock = Builder.GetInsertBlock();
831 CGF.EmitBlock(ContBlock);
832
833 // Create a PHI node. If we just evaluted the LHS condition, the result is
834 // true. If we evaluated both, the result is the RHS condition.
835 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
836 PN->reserveOperandSpace(2);
837 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
838 PN->addIncoming(RHSCond, RHSBlock);
839
840 // ZExt result to int.
841 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
842}
843
844Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
845 CGF.EmitStmt(E->getLHS());
846 return Visit(E->getRHS());
847}
848
849//===----------------------------------------------------------------------===//
850// Other Operators
851//===----------------------------------------------------------------------===//
852
853Value *ScalarExprEmitter::
854VisitConditionalOperator(const ConditionalOperator *E) {
855 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
856 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
857 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
858
859 Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
860 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
861
862 CGF.EmitBlock(LHSBlock);
863
864 // Handle the GNU extension for missing LHS.
865 Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond;
866 Builder.CreateBr(ContBlock);
867 LHSBlock = Builder.GetInsertBlock();
868
869 CGF.EmitBlock(RHSBlock);
870
871 Value *RHS = Visit(E->getRHS());
872 Builder.CreateBr(ContBlock);
873 RHSBlock = Builder.GetInsertBlock();
874
875 CGF.EmitBlock(ContBlock);
876
877 // Create a PHI node for the real part.
878 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
879 PN->reserveOperandSpace(2);
880 PN->addIncoming(LHS, LHSBlock);
881 PN->addIncoming(RHS, RHSBlock);
882 return PN;
883}
884
885Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
886 llvm::APSInt CondVal(32);
887 bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext());
888 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
889
890 // Emit the LHS or RHS as appropriate.
891 return Visit(CondVal != 0 ? E->getLHS() : E->getRHS());
892}
893
894//===----------------------------------------------------------------------===//
895// Entry Point into this File
896//===----------------------------------------------------------------------===//
897
898/// EmitComplexExpr - Emit the computation of the specified expression of
899/// complex type, ignoring the result.
900Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
901 assert(E && !hasAggregateLLVMType(E->getType()) &&
902 "Invalid scalar expression to emit");
903
904 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
905}
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000906
907/// EmitScalarConversion - Emit a conversion from the specified type to the
908/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000909Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
910 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000911 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
912 "Invalid scalar expression to emit");
913 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
914}
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000915
916/// EmitComplexToScalarConversion - Emit a conversion from the specified
917/// complex type to the specified destination type, where the destination
918/// type is an LLVM scalar type.
919Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
920 QualType SrcTy,
921 QualType DstTy) {
922 assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) &&
923 "Invalid complex -> scalar conversion");
924 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
925 DstTy);
926}