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